In OpenGL, colors are used to define the appearance of points, lines, polygons, and other primitives. OpenGL supports multiple ways to specify colors, primarily:
Definition: The RGB mode specifies color directly using red, green, and blue components. This is also called true color.
Components:
Red (R), Green (G), Blue (B)
Each component can be represented as:
How to Set Color in OpenGL:
// Using floating-point values
glColor3f(1.0, 0.0, 0.0); // Red
glColor3f(0.0, 1.0, 0.0); // Green
glColor3f(0.0, 0.0, 1.0); // Blue
// Using integer values
glColor3ub(255, 0, 0); // Red
Advantages:
Applications:
Definition: In indexed color mode, colors are not specified directly. Instead, each pixel stores an index to a color map (palette). The color map contains the actual RGB values.
How it Works:
Advantages:
Disadvantages:
Example in OpenGL:
GLubyte colormap[256][3]; // 256 colors
colormap[0][0] = 255; // Red
colormap[0][1] = 0;
colormap[0][2] = 0;
// Pixel index 0 will render as red
| Feature | RGB Mode | Indexed Color Mode |
|---|---|---|
| Color specification | Direct (R, G, B) | Indirect (index to palette) |
| Number of colors | Millions (True color) | Limited (depends on palette size) |
| Memory usage | More | Less (stores only index) |
| Flexibility | High | Low (palette-dependent) |
| Use case | Modern graphics, realistic rendering | Legacy systems, limited hardware |
Open this section to load past papers