2.3.1 How Color Palettes Work

Color palettes provide a device-independent method for accessing the color capabilities of a display device by managing the device's physical (or system) palette, if one is available. Typically, devices that can display at least 256 colors use a physical palette.

An application employs the system palette by creating and using one or more logical palettes. Each entry in the palette contains a specific color. Then, instead of specifying an explicit value for a color when performing graphics operations, the application indicates which color is to be displayed by supplying an index into its logical palette.

Since more than one application can use logical palettes, it is possible that the total number of colors requested for display can exceed the capacity of the display device. Windows acts as a mediator among these applications.

When a window requests that its logical palette be given its requested colors (a process known as realizing its palette), Windows first exactly matches entries in the logical palette to current entries in the system palette.

If an exact match for a given logical-palette entry is not possible, 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 takes these logical palette entries that do not exactly match and matches them as closely as possible to entries already in the system palette. To further aid this color matching, Windows sets aside 20 static colors (called the “default palette”) in the system palette to which it can match entries in a background palette.

Windows always satisfies the color requests of the foreground window first; this ensures that the active window will have the best color display possible. For the remaining windows, Windows satisfies the color requests of the window which most recently received input focus, the window which was active before that one, and so on.

Figure 2.4 illustrates this process. In this figure, a hypothetical display has a

system palette capable of containing 12 colors. The application that created Logical Palette 1 owns the active window and was the first to realize its logical palette, which consists of 8 colors. Logical Palette 2 is owned by a window which 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.

Three of the colors (1, 3, and 5) in Logical Palette 2 are identical to colors in the system palette; to save space in the palette, then, Windows simply matched those colors to the existing system colors when the second application realized its palette. Colors 0, 2, 4, and 6 were not already in the system palette, however, and so Windows mapped those colors into the system palette.

Because the system palette is now full, Windows was not able to map the remaining two colors (which do not exactly match existing colors in the system palette) into the system palette. Instead, it matched them to the closest colors in the system palette.