HomeMultimedia Page 2 - Working with Colors in OpenGL for Game Programming with SDL
RGB and Indexed Mode - Multimedia
Colors magically bring a scene to life. No matter how interesting the plot is, how engrossing the special effects are, if the color combinations are not accurate or the colors are tepid, the liveliness of the game suffers. In short, it is the combination of and correct usage of colors that decide what the scene and eventually the whole game will be like.
RGB Mode is also known as the RGBA mode when the Alpha component is used. RGB stands for red, green, and blue. The A in RGBA is the Alpha component. It defines transparency for red (R), blue (B), and green (G) components. In this mode, the hardware sets aside a certain number of bit-planes for each red, blue, and green component. The R, G, B values are stored as integer values rather than floating point numbers. The values of these components can vary from 0 to 255. The range can be more. The range of the acceptable value for a given component is based on the number of bits available for that component. Typically, on most systems the number of available bits is 8. Hence, the number 255 is used for specifying a particular intensity of a color component.
Now, the number of bits available directly corresponds to the bit-planes of a component. In this case, since 8 bits are there, there are 28=256 bit-planes in a system supporting 8 bits for each of the red, blue and green components. The integer values of a component can be converted into their floating point by dividing the integer value of a component with the number of bit-planes available for that component. For example, in an 8-bit system there are 256 bit-planes available for the red color component. The value in each plane is from 0-255. Thus 0 in the bit-plane would correspond to 0/255=0.0 in floating point value of R. The values range from 0.0 to 1.0 for all the components. Pictorially, the RGBA would be represented as follows:
2. Indexed Mode:
Indexed Mode, or color index mode, derives its name from the fact that it uses a lookup table to mix the colors. It is metaphorically similar to a painter who uses a numbered palette to create different colors. To take the metaphor further, as a painter's palette has spaces to mix the colors and these spaces act as an index for new colors, in the same way a color map or lookup table indexes where the components can be mixed. This map is stored in the bit-planes. Then those bit-plane values reference the color map, and the screen is painted with the corresponding red, green, and blue values from the color map. Pictorially a color map would look like:
OpenGL manages both of these very easily without letting the developer know the low-level system calls and device-dependent API mappings. The steps involved in invoking the OpenGL commands that perform these functions are the focus of the next section.