The information in this article applies to:
SUMMARYThe palette is a mechanism to manage the colors used on the Windows display. This article discusses the steps necessary for an application to create and use a palette. This article is meant to supplement the information provided in the Windows SDK documentation. CWheel.exe is a file in the Software Library that demonstrates how to manage the palette by drawing a color wheel on the screen. The following is an excerpt of the code in that file. MORE INFORMATION
Q: What is the difference between the system palette and a logical palette?
The variable iNumColors should have a value of 2, 16, or 256, depending on the video card installed. This number can change in the future, depending on the advances in hardware technology.
If RC_PALETTE is not set, the device is not capable of palette manipulation. When this is the case, however, an application can still create a palette, select it into a display context, and use the colors from the palette. GDI will create a dithered pattern (the disadvantages of which are discussed below) to simulate the color in the palette. After the number of colors has been determined, a block of memory must be allocated to hold the palette entries. A palette entry is a structure that contains the red, green, and blue values to specify the color, plus a flag. The flag is used to denote special colors for use in animation and other special cases. Consult the SDK documentation for a more complete description. For this article, the default flag value of NULL will be used; which means this color has no special attributes. The following code will allocate a block of memory large enough for the palette in local memory:
Now that the memory structure has been created, it is the responsibility of the application to put the necessary values into the structure to create colors. There are two different means of doing this: use the default system color, or create a custom color. The following code examples demonstrate these two options. Assume that the variable PaletteIndex is valid in the range from 0 (zero)to (iNumColors-1).
Use the default system color:
Use a custom color:
Once the structure has been filled with the appropriate values, it is time to use the GDI call CreatePalette() to make the logical palette:
During the processing of one of the application termination message (WM_QUIT or WM_CLOSE), use DeleteObject(hPal) to free the system resources used by the palette.
Q: How are logical palettes used? A: In Windows applications, colors are generated by three means: by specifying the explicit red, green, and blue color values [using the RGB() macro], by specifying the index into a palette [using the PALETTEINDEX() macro], or by specifying red, green, and blue color values and asking Windows to return the closest entry into the palette. Therefore, if a particular set of red, green, and blue values has been specified for palette entry 25, the following three lines of code will seem to be identical:
However, this is not the case. Even though the palette entry #25 may have red, green, and blue (RGB) values of 122, 15, and 200, respectively, the results will be different. The first example produces a brush that is a dithered pattern of the eight main system colors. The second and third examples use the pure color defined in the palette, which produces more pleasing results.
There are advantages and disadvantages to using RGB dithers or to using palette colors:
Additional query words:
Keywords : kbfile kbsample kb16bitonly kbGDI kbPalettes |
Last Reviewed: July 7, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |