Using a Custom Logical Palette

You can create a logical palette containing colors of your choice with fg_logpal(). The fg_logpal() function requires three parameters: the starting color number, the number of colors in the logical palette, and a byte array containing that many sets of RGB color components. Like fg_defpal(), fg_logpal() returns a logical palette handle of type HPALETTE, or NULL (zero) if it could not create the logical palette. Note that fg_logpal() creates a new logical palette each time you call it. If you want to modify the colors in an existing logical palette, use fg_setdacs() or fg_setrgb().

The starting color number specifies the first color to define in the new logical palette, between 0 and 255. The number of colors parameter can be any value between 0 and 256 as long as the sum of the starting color number and the number of colors does not exceed 256. The first three bytes of the RGB array contain the red, green, and blue components for the starting color, the next three bytes contain the components for the next color, and so forth. Each color component is a value between 0 and 255, and the RGB array must be large enough to hold the specified number of RGB triples. For example, we could use the code shown here to create a logical palette containing our own colors as the non-system colors:

C/C++:

char RGBvalues[236*3] = {...};
hPal = fg_logpal(10,236,RGBvalues);
fg_realize(hPal);

Delphi:

var
  RGBvalues : array [1..236*3] of byte;
begin
  hPal := fg_logpal(10,236,RGBvalues);
  fg_realize(hPal);

Visual Basic:

Dim RGBvalues(236 * 3) As Byte
Call fg_logpal(10, 236, RGBvalues(0))
Call fg_realize(hPal)

Although fg_logpal() lets you define fewer than 256 colors, it always creates a 256-color logical palette. In addition, it will set up the logical palette with the system colors (colors 0-9 and 246-255) set to their default values, unless you explicitly change them (which we don't recommend). Any non-system colors not defined through the fg_logpal() parameters will remain undefined. Hence, if the number of colors parameter is zero, fg_logpal() will create a logical palette containing the Windows system colors as its first and last ten entries, with the remaining 236 colors undefined.

Logical palettes created with fg_logpal() are treated the same as those created with fg_defpal(). That is, you must activate them with fg_realize() in a program's WM_SETFOCUS handler (and typically in WM_CREATE as well), and you must delete them with DeleteObject() before the program exits.

<< Prev

Next >>

Contents
Fastgraph Home Page

 

copyright 2001 Ted Gruber Software, Inc.