Java Color Primer

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

GitHub repository: Java Color Primer

A Primer on Colors in Java

The First Three Bytes

In Java (and many common computer graphics systems) color on a monitor is a mixture of varying intensities of red, green and blue, know 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." Using this scheme, teal would be represented 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 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 save 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

Note also that if we write an integer out in hexadecimal every two hex digits constitutes a byte. Java also has a Color constructor that takes a single integer which encodes all three color values:

white      new Color( 255, 255, 255 )    new Color( 0xFFFFFF )
red        new Color( 255, 0, 0 )        new Color( 0xFF0000 )
cyan       new Color( 0, 255, 255 )      new Color( 0x00FFFF )
coral      new Color( 255, 127, 80 )     new Color( 0xFF8050 )
black      new Color( 0, 0, 0 )          new Color( 0x000000 )

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, for example, HTML Color Picker from W3Schools, you can find the hexadecimal value for pretty much any color you can think of.

The Fourth Byte

In the previous model 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) 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 recognizes the sRGB model, in which the fourth byte encodes the alpha value. The alpha value encodes the transparency of a color, between not transparent at all (1.0f, or 255) and 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 )

(if hasalpha is true, the fourth byte is interpreted as the alpha value; it it's false, using this constructor is equivalent to using Color(int,int,int)) 

Below is the code to overlay a sequence of 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;
    }
}




No comments:

Post a Comment