Java Color Primer

This is a brief discussion of how Java uses the RGB and sRGB models to display colors on a monitor. There are other color models and many other color topics; for a survey, see the Color model article on Wikipedia.

GitHub repository: Java Color Primer

A Primer on Color in Java

The First Three Bytes

In Java (and many computer graphics systems), color on a monitor is a combination of red, green, and blue at varying intensities, known as the RGB Color Model. The intensity of one of these three colors can be described as somewhere between all the way off and all the way on. The color teal, for example, can be represented as red = 0 intensity, green = half intensity, and blue = half intensity.

Intensity is represented as either a decimal number or an integer. As a decimal number, 0.0 = “all the way off” and 1.0 = “all the way on.” This scheme would represent teal as red = 0.0, green = 0.5, and blue = 0.5.

As an integer, intensity is measured as a value between 0 and 255 (255 is the largest integer that can be stored in an 8-bit byte), so teal would be red = 0, green = 128, and blue = 128. Java has Color constructors that take three values as either float or int and assemble them into a color: Color( float red, float green, float blue ) and Color( int red, int green, int blue ). Some examples of their use are:

white      new Color( 1.0f, 1.0f, 1.0f )    new Color( 255, 255, 255 )
red        new Color( 1.0f, 0.0f, 0.0f )    new Color( 255, 0, 0 )
cyan       new Color( 0.0f, 1.0f, 1.0f )    new Color( 0, 255, 255 )
coral      new Color( 1.0f, 0.5f, 0.31f )   new Color( 255, 127, 80 )
black      new Color( 0.0f, 0.0f, 0.0f )    new Color( 0, 0, 0 )

By the way, grays are a special case in which all three color components have the same value:

nearly white  new Color( .9f, .9f, .9f )     new Color( 230, 230, 230 )
light gray    new Color( .75f, .75f, .75f )  new Color( 191, 191, 191 )
medium gray   new Color( .5f, .5f, .5f )     new Color( 127, 127, 127 )
dark gray     new Color( .25f, .25f, .25f )  new Color( 64, 64, 64 )
nearly black  new Color( .1f, .1f, .1f )     new Color( 26, 26, 26 );

Note that because each of the red, green, and blue integer values occupies no more than one byte, the three components of the color can be combined into a single integer:

coral
255 127 80 0xFF7F50

byte 3 byte 2 byte 1 byte 0

This is especially convenient because you can find the hexadecimal values of thousands of colors on the Web. You’ve probably seen them written like this: #FF8050. By looking at tables of colors on the Web, such as HTML Color Picker from W3Schools, you can find the hexadecimal value for almost any color you can imagine. For the record, the total number of colors that can be represented in the RGB color model is 224, 16,777,216.

The Fourth Byte

In the previous paragraphs, we discussed the RGB color model (a.k.a. RGB color space, a.k.a. colour space) in which the first three bytes of a 32-bit integer (type int in Java) encode the red, green, and blue components of a given color. In the RGB model, the fourth byte of the integer is unused. Java also supports the sRGB color space, in which the fourth byte encodes the alpha value. The alpha value determines the transparency of a color, ranging from completely opaque (1.0f, or 255) to completely transparent (0.0f, or 0). To encode transparency into a color, use one of the color constructors:

Color​( float red, float green, float blue, float alpha )
Color​( int red, int green, int blue, float alpha )
Color( int rgba, boolean hasAlpha )

In the third constructor, if hasalpha is true, the fourth byte of rgba is interpreted as the alpha value; if it’s false, using this constructor is equivalent to using Color(int,int,int)) .

Below is the code to overlay increasingly opaque transparencies on a figure. The figure it draws follows.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
private void paintTransparentOverlay()
{
    int         divisions       = 50;
    float       rWidth          = currWidth;
    float       rHeight         = currHeight / divisions;
    float       alpha           = 0; // percent
    float       alphaIncr       = 1f / divisions;
    Rectangle2D rect            = new Rectangle2D.Float();
    for ( float yco = 0 ; yco < currHeight - rHeight ; yco += rHeight )
    {
        rect.setRect( 0, yco, rWidth, rHeight );
        Color   color   = new Color( 0f, 0f, 1f, alpha );
        gtx.setColor( color );
        gtx.fill( rect );
        alpha += alphaIncr;
    }
}

For additional discussion of colors, see the documentation for the Color class and the W3Schools Color Tutorial.