Finding Out About Color

During the discussion of bitmaps in Chapter 8, I noted the two ways in which memory in a video display adapter can be organized for color. In some video adapters, memory is organized into a number of color planes. Within a plane, each bit corresponds to a single pixel and represents a particular primary color (such as red, green, or blue). Other video adapters have a single color plane, in which a number of adjacent bits represent the color of each pixel.

GetDeviceCaps lets you determine the organization of memory in the video adapter and the number of colors it can represent. This call returns the number of color planes:

nPlanes = GetDeviceCaps (hdc, PLANES) ;

This call returns the number of color bits per pixel:

nBitsPixel = GetDeviceCaps (hdc, BITSPIXEL) ;

Most graphics display devices that are capable of color use either multiple color planes or multiple color bits per pixel, but not both; in other words, one of these calls will return a value of 1. The number of colors that can be rendered on the video adapter can be calculated by the formula:

nColors = 1 << (nPlanes * nBitsPixel) ;

This value may or may not be the same as the number of colors obtainable with the NUMCOLORS parameter:

nColors = GetDeviceCaps (hdc, NUMCOLORS) ;

These two numbers will be different for most plotters. For a plotter, both the PLANES and BITSPIXEL values will equal 1, but the NUMCOLORS value will reflect the number of colored pens that the plotter has. For monochrome devices, GetDeviceCaps returns a 2 for the NUMCOLORS parameter.

The two values can also be different for video adapters that support loadable color palettes under Windows 3 (such as the IBM 8514/A adapter). The 8514/A has 1 plane and 8 bits per pixel, which means that 256 colors are possible. GetDeviceCaps with the NUMCOLORS parameter returns the number of colors reserved by Windows (20 in the case of the 8514/A). The remaining 236 colors can be set by a Windows program.

The number of colors returned from GetDeviceCaps is the number of pure colors that the device can display. Windows can use dithering (which involves a pixel pattern that combines pixels of different colors) to represent colors in addition to the pure colors.

A color is usually represented by an unsigned long integer with 3 bytes, one each for the intensity of red, green, and blue. (Chapters 5 and 6 discussed this subject in greater detail.) You can determine the closest pure color of a particular color value by calling GetNearestColor:

rgbPureColor = GetNearestColor (hdc, rgbColor) ;