2.2.2 Color

Many of the GDI functions that create pens and brushes require that the calling application specify a color in the form of a COLORREF value. A COLORREF value specifies color in one of three ways:

As an explicit RGB value

As an index to a logical-palette entry

As a palette-relative RGB value

The second and third methods require the application to create a logical palette. Section 2.3, “Color Palette Functions,” describes Windows color palettes and the functions used by an application to exploit their capabilities.

An explicit RGB COLORREF value is a long integer that contains a red, a green, and a blue color field. The first (low-order) byte contains the red field, the second byte contains the green field, and the third byte contains the blue field; the fourth (high-order) byte must be zero. Each field specifies the intensity of the color; zero indicates the lowest intensity and 255 indicates the highest. For example, 0x00FF0000 specifies pure blue, and 0x0000FF00 specifies pure green. The RGB macro accepts values for the relative intensities of the three colors and returns an explicit RGB COLORREF value. When GDI receives the RGB value as a function parameter, it passes the RGB color value directly to the output device driver, which selects the closest available color on the device. The GetNearestColor function returns the closest logical color to a specified logical color that a given device can represent.

If the device is a plotter, the driver converts the RGB value to a single color that matches one of the pens on the device.

If the device uses color raster technology and the RGB value specifies a color for a pen, the driver will select a solid color. If the device uses color raster technology and the RGB value specifies a color for a brush, the driver will select from a variety of available color combinations. Since many color devices can display only a few colors, the actual color is simulated by “dithering,” that is, mixing pixels of the colors which the display can actually render.

If the device is monochrome (black-and-white), the driver will select black, white, or a shade of gray, depending on the RGB value. If the sum of the RGB values is zero, the driver selects a black brush. If the sum of the RGB values is 765, the driver selects a white brush. If the sum of the RGB values is between zero and 765, the driver selects one of the gray patterns available.

The GetRValue, GetGValue, and GetBValue functions extract the values for red, green, and blue from an explicit RGB COLORREF value.