2.3.2 Using a Color Palette

Before drawing to the display device using a color palette, an application must first create a logical palette by calling the CreatePalette function and then call SelectPalette to select the palette for the device context (DC) for the output device for which it will be used. An application cannot select a palette into a device context using the SelectObject function.

All functions which accept a color parameter accept an index to an entry in the logical palette. The palette-index specifier is a long integer value with the first bit in its high-order byte set to 1 and the palette index in the two low-order bytes. For example, 0x01000005 would specify the palette entry with an index of 5. The PALETTEINDEX macro accepts an integer value representing the index of a logical-palette entry and returns a palette-index COLORREF value which an application can use as a parameter for GDI functions that require a color.

An application can also specify a palette index indirectly by using a palette-relative RGB COLORREF value. If the target display device supports logical palettes, Windows matches the palette-relative RGB COLORREF value to the closest palette entry; if the target device does not support palettes, then the RGB value is used as though it were an explicit RGB COLORREF value. The palette-relative RGB COLORREF value is identical to an explicit RGB COLORREF value except that the second bit of the high-order byte is set to 1. For example, 0x02FF0000 would specify a palette-relative RGB COLORREF value for pure blue. The PALETTERGB macro accepts values for red, green and blue, and returns a palette-relative RGB COLORREF value which an application can use as a parameter for GDI functions that require a color.

If an application does specify an RGB value instead of a palette entry, Windows will use the closest matching color in the default palette of 20 static colors.

NOTE:

If the source and destination device contexts have selected and realized different palettes, the BitBlt function does not properly move bitmap bits to or from a memory device context. In this case, you must call the GetDIBits with the wUsage parameter set to DIB_RGB_COLORS to retrieve the bitmap bits from the source bitmap in a device-independent format. You then use the SetDIBits function to set the retrieved bits in the destination bitmap. This ensures that Windows will properly match colors between the two device contexts.

BitBlt can successfully move bitmap bits between two screen display contexts, even if they have selected and realized different palettes. The StretchBlt function properly moves bitmap bits between device contexts whether or not they use different palettes.