Display devices that are capable of displaying 256 or more simultaneous colors using a palette need to provide support for color palettes. A display driver specifies that it has palette support by setting the RC_PALETTE value in the dpRaster member in the GDIINFO structure. The display driver also must set the dpPalColors, dpPalReserved, and dpPalResolution members.
The number of reserved colors on the palette is always 16, corresponding to the VGA colors. Half of the reserved palette colors are placed at the beginning and half at the end of the palette.
If a display driver supports color palettes, it must export the SetPalette, GetPalette, SetPalTrans, and GetPalTrans functions.
A display driver does not need to initialize the hardware palette when the driver initializes the rest of the display hardware. GDI initializes the palette.
The driver has to maintain a palette-translation table to translate the logical color indexes passed to it by GDI into the actual physical color indexes. The translation has to occur before any raster operation (ROP) is performed. ROPs are always applied to physical colors.
Whenever a display driver function receives a DRAWMODE, LPEN, PBRUSH, or PBITMAP structure, the driver may need to translate the logical colors in these structures to physical colors before using the colors.
An application has to perform color translation only when the physical device is involved. In other words, if a line is drawn into a memory bitmap or a bitmap is block transferred into another memory device, no color translation is required. On the other hand, if a bitmap is transferred to or from the screen into a memory bitmap or a line is drawn directly onto the screen, color translation is required. In the case of a block transfer from the screen to the screen (where the physical device is both the source and destination of the block transfer), color translation is not needed, since all the color indexes are already translated into physical indexes.
Color specifications are passed to display drivers as either color indexes or RGB values. A color index is a 32-bit value in which the high 16 bits is set to 0xFF00 and the low 16 bits is the actual index. An RGB value is a 32-bit value as specified by the RBGQUAD structure. If using the DIB engine, when an RGB value is specified, the driver should call DIB_ColorInfo to match the color as closely as possible among the 16 reserved colors.
A palette-translation table is an array of 16-bit indexes, each mapping a logical color index to a physical color index. A display driver uses the translation table to translate color indexes in physical pens and brushes and in the DRAWMODE structure to the actual color indexes used by the hardware palette.
GDI calls the GetPalTrans and SetPalTrans functions to get and set the translation table. The translation table has the number of elements specified by the dpPalColors member. Because color mapping is complex, drivers using the DIB engine should forward these calls to the DIB engine functions DIB_GetPaletteTranslateExt and DIB_SetPaletteTranslateExt. The function DIB_SetPaletteTranslateExt sets or clears the PALETTE_XLAT bit in the deFlags field of the DIBENGINE structure. When a non-identity palette translate is being set, this function sets the bit. This function clears the PALETTE_XLAT bit when an identity translate is set. Drivers and the DIB engine may use this bit to decide whether palette translation is needed or not. For BitBlt, bypassing color translation results in substantial performance improvements.
If a display driver supports color palettes, it must include the UpdateColors function. GDI calls this function to direct the driver to redraw a region on the screen using the translation table passed to the function. For each pixel in the region, the function retrieves the pixel's color index, translates the index, and writes the translated index back to the given pixel.
Display drivers that support color palettes must make sure that the index for the palette entry that corresponds to black must be the one's complement of the index for the palette entry for white. Black and white must be static palette entries, meaning the driver sets the indexes for these colors during initialization and does not change the indexes.