Windows provides a device-independent method for accessing the color capabilities of a display device by managing the device's system palette, if the device has one.
As noted previously, your application employs the system palette by creating and using one or more logical palettes. A logical palette is a graphics device interface (GDI) object that specifies the colors to be drawn in the device context. Each entry in the palette contains a specific color. When performing graphics operations, the application does not indicate which color is to be displayed by supplying an explicit red, green, blue (RGB) value. Instead, the application accesses the palette either directly or indirectly. Using the direct method, it indicates which color to use in your logical palette by specifying an index into the palette entries. Using the indirect method, you specify a palette-relative RGB value similar to an explicit RGB value. For a more complete description of these two methods, see Sections 19.4.1, “Directly Specifying Palette Colors,” and 19.4.2, “Indirectly Specifying Palette Colors.”
When a window requests that the system use the colors in the window's logical palette (a process known as realizing the window's palette), Windows first exactly matches entries in the logical palette to current entries in the system palette.
If it cannot make an exact match for a given logical-palette entry, Windows sets the entry in the logical palette into an unused entry in the system palette.
Finally, when all entries in the system palette have been used, Windows matches logical-palette entries as closely as possible to entries in the system palette. Windows sets aside 20 static colors (called the default palette) in the system palette to aid this color matching.
Windows always satisfies the color requests of the foreground window first; this ensures that the active window will have the best color display. For the remaining windows, Windows satisfies the color requests of the window that most recently received the input focus, and so on. This process is shown in the following figure:
In this figure, a hypothetical screen has a system palette capable of containing 12 colors. The application that created Logical Palette 1, which contains 8 colors, owns the active window and was the first to realize its logical palette. Logical Palette 2, which contains 9 colors, is owned by a window that realized its logical palette while it was inactive. Because the active window was active when it realized its palette, Windows mapped all of the colors in Logical Palette 1 directly to the system palette.
Colors 1, 3, and 5 in Logical Palette 2 are identical to colors in the system palette. When the second application realized its logical palette, Windows simply matched those colors to the existing system colors to save space in the palette. Colors 0, 2, 4, and 6 of Logical Palette 2 were not already in the system palette, however, so Windows mapped those colors into the system palette.
Colors 7 and 8 in Logical Palette 2 do not exactly match colors in the system palette. Because the system palette was full, Windows could not map these 2 colors into the system palette. Instead, it matched them to the closest colors in the system palette.