Working with Colors

Some display devices and printers display only monochrome images; others use hundreds, thousands, or millions of colors. You should design your applications to display properly on devices with a variety of color capabilities.

The color range available to a display device is determined primarily by the pixel depth it supports. Pixel depth is measured in bits per pixel. Each bit can have a value of 1 or 0. A pixel depth of 1 bpp accepts only two values. While these values are usually black and white, you can use any two colors available on your device. A pixel depth of 2 bpp has four possible color values or all possible combinations of 0 and 1 with two bits. In general, the number of possible colors is equal to 2 raised to the power of the pixel depth. Windows CE supports pixel depths of 1, 2, 4, 8, 16, 24, and 32 bpp.

You can call the GetDeviceCaps function, which specifies the NUMCOLORS value, to discover the number of colors a device supports. Usually, this count corresponds to a physical property of the output device, such as the number of inks in the printer or the number of distinct color signals the display adapter can transmit to the monitor. For modes greater than 8 bpp, use the return value of GetDeviceCaps with the BITSPIXEL value. The number of supported colors for that mode is 2 raised to the power of the return value.

Windows and applications use parameters and variables having the COLORREF type to pass and store color values. You can extract the individual values of the red, green, and blue components of a color value by using the GetRValue, GetGValue, and GetBValue macros, respectively. Use the RGB or PALLETERGB macros to create a color value from individual RGB component values.

If you request a color that the display device cannot generate, Windows CE approximates it with a color that the device can generate. For example, if you attempt to create a red pen for a black-and-white printer, you will receive a black pen instead—Windows CE uses black as the approximation for red.

You can discover how Windows CE approximates a specified color by calling the GetNearestColor function. The function takes a color value and returns the color value of the closest matching color the device can generate.

Windows handles colors in bitmaps differently from colors in pens, brushes, and text. Compatible bitmaps, created by calling the CreateBitmap or CreateCompatibleBitmap function, retain color information in a device-dependent format. Specifically, device-dependent bitmaps use the color values of the device on which they were created.

The DIBSection structure representing a DIB retains color information as either color values or color palette indexes. If color values are used, the colors might be approximated as necessary. Although Windows CE does not approximate colors identified by indexes, the colors in the bitmap could change if the palette changes.

Note An offscreen DIB section should have the same color table as the screen; otherwise, GDI will have to perform a time-consuming color translating blit when the DIB section is transferred to the screen. For grayscale devices, the color table should be 0x000000, 0x808080, 0xc0c0c0, and 0xFFFFFF. For color devices, the application should first query the standard palette to determine its color display capabilities, and then build a matching color table.