[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Realizing palettes

Posted on 1998-07-08
2
Medium Priority
?
491 Views
Last Modified: 2010-04-04
How to realize a palette in Delphi application (I mean, images to be properly shown in 256-color mode)?.
0
Comment
Question by:Stern
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:Stern
ID: 1357317
Edited text of question
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 150 total points
ID: 1357318
Hi Stern,

1. Delphi Informant edition of July has an isue of palettes in Delphi. Maybe interesting to read.

2. Here is some code I found on the net a while ago :

Creating and selecting palettes


How do you create and use a Palette in Delphi. According to the help fn I
think you should use the CreatePalette and SetPaletteEntries and RealizePalette
but how?
A:
Below are functions that help to create a palette (an identity
palette, BTW) from an array of RGBQuads (such as you would find in
the palette section of a .BMP file). I stole this from the WinG
documentation, and converted it to Delphi. First call
ClearSystemPalette, then you can get an identity palette by calling
CreateIdentityPalette.

 If you plan to try palette animation, work in a 256-color mode, and
change all the PC_NOCOLLAPSE entries below to PC_RESERVED.

Besides creating the palette, the other pieces to the puzzle are

1. Override the form's GetPalette method, so that it returns the
new palette.

2. Select and realize the new palette just before you paint.

  OldPal := SelectPalette(Canvas.Handle, NewPalette, False);
  RealizePalette(Canvas.Handle);  
  { Do your painting here }
  SelectPalette(Canvas.Handle, OldPal, False);

3. Remember to release the palette when you are done using
DeleteObject

4. If you are used to using the RGB function to get color values, use
the PaletteRGB function in its place.

function CreateIdentityPalette(const aRGB; nColors : Integer) : HPALETTE;
type
  QA = Array[0..255] of TRGBQUAD;
var
  Palette : PLOGPALETTE;
  PalSize : Word;
  ScreenDC : HDC;
  I : Integer;
  nStaticColors : Integer;
  nUsableColors : Integer;
begin
  PalSize := SizeOf(TLOGPALETTE) + SizeOf(TPALETTEENTRY) * 256;
  GetMem(Palette, PalSize);
  try
    with Palette^ do
      begin
        palVersion := $0300;
        palNumEntries := 256;
        ScreenDC := GetDC(0);
        try
          { For SYSPAL_NOSTATIC, just copy the color table into a PALETTEENTRY
            array and replace the first and last entries with black and white }
          if (GetSystemPaletteUse(ScreenDC) = SYSPAL_NOSTATIC)
            then
              begin
              { Fill in the palette with the given values, marking each
                  with PalFlag }
                {$R-}
                for i := 0 to (nColors-1) do
                with palPalEntry[i], QA(aRGB)[I] do
                  begin
                    peRed := rgbRed;
                    peGreen := rgbGreen;
                    peBlue := rgbBlue;
                    peFlags := PC_NOCOLLAPSE;
                  end;

                { Mark any unused entries with PalFlag }
                for i := nColors to 255 do
                  palPalEntry[i].peFlags := PC_NOCOLLAPSE;

                { Make sure the last entry is white --
                  This may replace an entry in the array!}
                I := 255;
                with palPalEntry[i] do
                  begin
                    peRed := 255;
                    peGreen := 255;
                    peBlue := 255;
                    peFlags := 0;
                  end;

                { And the first is black --
                  This may replace an entry in the array!}
                with palPalEntry[0] do
                  begin
                    peRed := 0;
                    peGreen := 0;
                    peBlue := 0;
                    peFlags := 0;
                  end;
                {$R+}
              end
            else
              begin
              { For SYSPAL_STATIC, get the twenty static colors into the
                  array, then fill in the empty spaces with the given color
                  table }

                { Get the static colors from the system palette }
                nStaticColors := GetDeviceCaps(ScreenDC, NUMRESERVED);
                GetSystemPaletteEntries(ScreenDC, 0, 256, palPalEntry);

                {$R-}
                { Set the peFlags of the lower static colors to zero }
                nStaticColors := nStaticColors shr 1;
                for i:= 0 to (nStaticColors-1) do
                  palPalEntry[i].peFlags := 0;

                { Fill in the entries from the given color table}
                nUsableColors := nColors - nStaticColors;
                for I := nStaticColors to (nUsableColors-1) do
                  with palPalEntry[i], QA(aRGB)[i] do
                    begin
                      peRed := rgbRed;
                      peGreen := rgbGreen;
                      peBlue := rgbBlue;
                      peFlags := PC_NOCOLLAPSE;
                    end;

                { Mark any empty entries as PC_NOCOLLAPSE }
                for i := nUsableColors to (255-nStaticColors) do
                  palPalEntry[i].peFlags := PC_NOCOLLAPSE;

                { Set the peFlags of the upper static colors to zero }
                for i := (256 - nStaticColors) to 255 do
                  palPalEntry[i].peFlags := 0;
              end;
        finally
          ReleaseDC(0, ScreenDC);
        end;
      end;
    { Return the palette }
    Result := CreatePalette(Palette^);
  finally
    FreeMem(Palette, PalSize);
  end;
end;


procedure ClearSystemPalette;
var
  Palette : PLOGPALETTE;
  PalSize : Word;
  ScreenDC : HDC;
  I : Word;
const
  ScreenPal : HPALETTE = 0;
begin
  PalSize := SizeOf(TLOGPALETTE) + SizeOf(TPALETTEENTRY) * 255; {256th = [0] }
  GetMem(Palette, PalSize);
  try
    FillChar(Palette^, PalSize, 0);
    Palette^.palVersion := $0300;
    Palette^.palNumEntries := 256;
{$R-}
    For I := 0 to 255 do
      With Palette^.palPalEntry[I] do
        peFlags := PC_NOCOLLAPSE;
{$R+}
    { Create, select, realize, deselect, and delete the palette }
    ScreenDC := GetDC(0);
    try
      ScreenPal := CreatePalette(Palette^);
      if ScreenPal <> 0
        then
          begin
            ScreenPal := SelectPalette(ScreenDC,ScreenPal,FALSE);
            RealizePalette(ScreenDC);
            ScreenPal := SelectPalette(ScreenDC,ScreenPal,FALSE);
            DeleteObject(ScreenPal);
          end;
    finally
      ReleaseDC(0, ScreenDC);
    end;
  finally
    FreeMem(Palette, PalSize);
  end;
end;

-----

Loading a bitmap from .res without losing palette


procedure loadgraphic(name:string);
var
  { I've moved these in here, so they exist only during the lifetime of the
    procedure. }
  HResInfo: THandle;
  BMF: TBitmapFileHeader;
  MemHandle: THandle;
  Stream: TMemoryStream;
  ResPtr: PByte;
  ResSize: Longint;
  null:array [0..8] of char;
 
begin
  { In this first part, you are retrieving the bitmap from the resource.
    The bitmap that you retrieve is almost, but not quite, the same as a
    .BMP file (complete with palette information). }

  strpcopy (null, name);
  HResInfo := FindResource(HInstance, null, RT_Bitmap);
  ResSize := SizeofResource(HInstance, HResInfo);
  MemHandle := LoadResource(HInstance, HResInfo);
  ResPtr := LockResource(MemHandle);

  { Think of a MemoryStream almost as a "File" that exists in memory.
    With a Stream, you can treat either the same way! }

  Stream := TMemoryStream.Create;

  try
    Stream.SetSize(ResSize + SizeOf(BMF));

    { Next, you effectively create a .BMP file in memory by first writing
      the header (missing from the resource, so you add it)... }
    BMF.bfType := $4D42;
    Stream.Write(BMF, SizeOf(BMF));

    { Then the data from the resource. Now the stream contains a .BMP file }
    Stream.Write(ResPtr^, ResSize);

    { So you point to the beginning of the stream... }
    Stream.Seek(0, 0);

    { ...and let Delphi's TBitmap load it in }
    Bitmap:=tbitmap.create;
    Bitmap.LoadFromStream(Stream);

    { At this point, you are done with the stream and the resource. }
  finally
    Stream.Free;
  end;
  FreeResource(MemHandle);
end;

Author: htrsoft@midwest.net  (Eric Nielsen)


ZiF.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question