11.2. Color Models

A color model knows how to translate between a color and a set of pixel samples. In the 2D API, this concept is encapsulated in the java.awt.image.ColorModel class. This class has a dizzying number of poorly named methods. But don't worry — most of these methods fall into two categories. The first group of methods converts from a pixel's samples to a color. The second group performs the opposite conversion, from a color to pixel samples.

A color model uses an underlying color space to describe colors using one or more components. An RGB color, for example, has three components: red, green, and blue. A color model based on an RGB color space converts from pixel samples to red, green, and blue components (and vice versa). A color model based on a grayscale color space would convert between pixel samples and a single gray color component. The default color space is sRGB. (For more about color spaces, see Chapter 8.)

Don't be misled by ColorModel's method names. Many of these names begin with get, which implies that they retrieve properties of the ColorModel. Most of the time, however, this is not the case. Instead, these methods perform a translation. For example, one of the methods that translates from sample values to color components is called getComponents().

An instance of ColorModel doesn't really do anything useful, but ColorModel has several subclasses that represent common color models. These are described following the general descriptions of ColorModel ...

Get Java 2D Graphics now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.