What happens when you try to draw a pixel of a particular color using the RGB values we have discussed? Internally, Windows defines a color using 8 bits each for the red, green, and blue components using the RGB macro, and you can use glColor3ub to duplicate this functionality within OpenGL.
If the PC graphics card is in 24-bit color mode, then each pixel is displayed precisely in the color specified by the 24-bit value (three 8-bit intensities). In the 15- and 16-bit color modes, Windows passes the 24-bit color value to the display driver, which converts the color to a 15- or 16-bit color value before displaying it. In 24-bit color mode, the RGB color cube measured 255 (or 8 bits) per side. In 15- or 16-bit color mode, the color cube measures 32 (5 bits) or 64 (6 bits) on a side. The device driver then matches the 24-bit color value to the nearest color match in the 15 or 16-bit color cube.
Figure 8-13 shows how an 8-bit red value might be mapped to a 5-bit red value.
At the low end of the scale, 4-bit color mode can only display 16 colors. These colors are fixed and cannot be modified. Internally, Windows still represents each color with a 24-bit RGB value. When you specify a color to use for drawing operations using the RGB macro or glColor3ub, Windows uses the nearest color of the 16 available to fulfill the request. If the color is being used for fill operations, the color is approximated by dithering the available colors.
Having only 16 colors to work with makes the 4-bit color modes poorly suited for graphics. One thing the Windows GDI will do to help is to perform dithering on solid shapes and objects in this mode. Dithering is a means of placing different colors close together to produce the illusion of another composite color. For example, if you place yellow and blue squares together in a checkerboard pattern, the pattern will take on a greenish appearance. Without actually mixing the colors, the green would have a grainy appearance. By changing the proportion of yellow to green squares, you are effectively changing the intensities of yellow and green.
Windows uses dithering to produce colors not available in the current palette. In 16-color mode, image quality is typically very poor for more complex scenes. Figure 8-12 is a vivid demonstration of Windows dithering; we attempted to produce the RGB triangle on a system with only 16 colors. Generally, Windows does not perform dithering for OpenGL.
OpenGL can also do its own dithering, providing the command
This can sometimes improve image quality substantially in 8- and 15-bit color modes. You can see dithering in action in the example program DITHER from this chapterís subdirectory on the CD. This program draws a cube with sides of various colors and allows dithering to be enabled or disabled from the menu. When run in 8-bit color mode or better, dithering has little effect, but in the 4-bit, 16-color mode the dithered scene is remarkably different.
The 8-bit color modes can display 256 colors, and this results in a remarkable improvement for color graphics. When Windows is running in a color mode that supports 256 colors, it would make sense if those colors were evenly distributed across RGB color space. Then all applications would have a relatively wide choice of colors, and when a color was selected, the nearest available color would be used. Unfortunately, this is not very practical in the real world.
Since the 256 colors in the palette for the device can be selected from over 16 million different colors, an application can substantially improve the quality of its graphics by carefully selecting those colorsóand many do. For example, to produce a seascape, additional shades of blue will be needed. CAD and modeling applications modify the palette to produce smooth shading of a surface of a particular single color. For example, the scene may require as many as 200 shades of gray to accurately render the image of a pipeís cross section. Thus, applications for the PC typically change this palette to meet their needs, resulting in near-photographic quality for many images and scenes. For 256 color bitmaps, the Windows .bmp format even has an array thatís 256 entries long, containing 24-bit RGB values specifying the palette for the stored image.
An application can create a palette with the CreatePalette function, identifying the palette by a handle of type HPALETTE. This function takes a logical palette structure (LOGPALETTE) that contains 256 entries, each specifying 8-bit values for red, green, and blue components. But before we examine palette creation, letís take a look at how multitasked applications can share the single system palette in 8-bit color mode.