Concepts / Java Color Class

Java Color Class

RGB describes a color with three numbers giving the levels of red, green, and blue, and Java creates such a color with the constructor new Color(r,g,b).

  • Programming

Giving Java a Shade to Draw

Suppose you are drawing shapes in a Java program and want one particular shade. Java needs some way to receive that shade as data. The answer is that a color is itself an object, and you create that object by giving it numbers. This article answers two questions: what those numbers mean, and how two different systems can describe the same kind of thing with different sets of three numbers.

Before reading the code, notice three roles that will matter later. One part is the type name, which is Color. One part is the variable that holds the object. The last part supplies the numbers that define the shade. When an unexpected color appears, you will trace back through exactly these three roles to find the cause.

Describing a Color with RGB Levels

Definition

RGB describes a color with three numbers giving the levels of red, green, and blue. Java creates such a color with the constructor new Color(r, g, b).

RGB works by component. Each of the three numbers answers a separate question: how much red is in this color, how much green, and how much blue. The three answers together identify one color. Changing any one of the numbers changes the color, because that number is one of the levels the final color depends on. The first argument is the red level, the second is the green level, and the third is the blue level. Java is designed to work with this system, so when you want to build a color directly from its red, green, and blue components, you pass those components to the Color constructor.

newcreates a new objectColorthe class being used255first argument: red level128second argument: greenlevel0third argument: blue levelargument 1argument 2argument 3
Which argument controls the red, green, and blue channels in the RGB constructor?

The diagram above shows the constructor call as a strip of tokens. Reading left to right, the keyword new asks Java to create an object, Color names the class, and the three arguments that follow are read in a fixed order. Swapping any two of them changes which channel receives which level, so the order is part of the meaning, not a formatting choice.

java
Notes on the code
  • The Color type is imported from java.awt.
  • The variable orange holds the object created by the constructor.
  • The three numbers supply red, green, and blue in that order.

Describing a Color with Hue, Saturation, and Brightness

HSB is an alternative way to describe a color. Again three numbers are used, but they are not levels of red, green, and blue. They are called hue, saturation, and brightness. Hue selects the basic color, saturation describes how pure that hue is, and brightness describes how bright it is. The same color can therefore be reached through two different sets of inputs, which is why the two systems are worth comparing carefully.

HSB colorone color from threenumbersHuewhich basic color is chosenSaturationhow pure the hue isBrightnesshow bright the result isargument 1argument 2argument 3
How do hue, saturation, and brightness together pick out a color?

The HSB system is not created with a constructor. Instead, the Color class provides a static method named getHSBColor. You call it on the class itself, not on an object, and it returns a color object built from the hue, saturation, and brightness you supply. The arguments follow the order hue, saturation, brightness, so the first value you pass controls the hue.

java
Notes on the code
  • Color.getHSBColor is called on the class, so no new keyword is used.
  • hue, saturation, and brightness are declared as float variables.
  • The variable orange holds the color returned by the method.

RGB and HSB Side by Side

Both systems answer the same question: how do you specify a color with three numbers? They organize those numbers differently. RGB asks how much of each light component is present. HSB asks which basic hue is chosen, how pure it is, and how bright it is.

Red levelamount of red lightHuewhich basic hueGreen levelamount of green lightSaturationhow pure the hue isBlue levelamount of blue lightBrightnesshow bright it is
Which numbers belong to RGB, and which belong to HSB?
AspectRGBHSB
Three numbersRed, green, blue levelsHue, saturation, brightness
Java creationnew Color(r, g, b)Color.getHSBColor(h, s, b)
Creation styleConstructorStatic method on the class
Value type shown in sourceComponent levelsfloat values from 0.0F to 1.0F

Two systems that describe the same kind of thing with three numbers

Notice that no single RGB number maps onto a single HSB number. The two systems are two different coordinate descriptions of the same space of colors. Moving from one to the other is a change of description, not a simple renaming of parts. So when you switch systems in code, you must rethink the numbers rather than copy them across.

Tracing a Program Line by Line

Here is a small program that creates one color with each system. Read it once to identify each variable and each argument, then answer the tracing questions below. Trace it by hand before you run anything.

Code walkthrough
import java.awt.Color;

public class TwoSystems {
    public static void main(String[] args) {
        Color fromRgb = new Color(255, 128, 0);
        float hue = 0.08F;
        float saturation = 1.0F;
        float brightness = 1.0F;
        Color fromHsb = Color.getHSBColor(hue, saturation, brightness);
    }
}
  1. 1
    Line 1 imports the Color class so the program can use it.
  2. 2
    Line 5 uses the constructor with the new keyword; it reads three integer levels in red, green, blue order and stores the object in fromRgb.
  3. 3
    Lines 6 to 8 declare three float variables that will supply the HSB values.
  4. 4
    Line 9 calls the static method getHSBColor on the Color class, passing hue first, saturation second, and brightness third, and stores the result in fromHsb.
Worked example

Finding Which Argument Controls Which Property

In the HSB call Color.getHSBColor(hue, saturation, brightness), suppose a learner accidentally swaps the saturation and brightness variables, so the call becomes getHSBColor(hue, brightness, saturation). Which property changes, and why?

  1. 1
    Identify the argument order

    The method expects hue first, saturation second, and brightness third, as stated in the source.

  2. 2
    Locate the swapped values

    After the swap, the second argument receives the brightness value and the third argument receives the saturation value.

  3. 3
    Trace each property

    Saturation now controls how pure the hue is using the brightness value, and brightness now controls how bright the color is using the saturation value. Both properties are affected, and the result is a different color even though the hue is unchanged.

  4. 4
    Check the other two arguments

    The hue argument is still first, so the basic hue choice itself is not changed by this swap.

Result

The swap changes saturation and brightness, but not hue. Because the argument order is the mistake, the displayed color differs from the one the learner intended.

Predicting the Effect of Lowering Saturation

What do you think happens?

Keep the same hue and brightness in the HSB call, but lower the saturation value toward 0.0F. What happens to the color you see?

  • The color changes to a different basic hue

  • The color becomes a paler version of the same hue

Reveal answer
Answer:

The color becomes a paler version of the same hue

The source describes saturation as how pure the hue is, while the hue itself is the basic color chosen. Lowering saturation keeps the hue and makes it less pure, so the color looks paler. Hue is the first argument and is not changed by saturation.

Mistakes That Produce Unexpected Colors

When a color is not what you expected, work backward through the program rather than guessing at the drawing code. Ask first which system the color was created in, because that decides what each number means. Next check the creation line: is it the constructor for RGB or the static method for HSB, and are the arguments in the order that method expects. Finally check the variable values feeding that line, including whether an HSB input falls within the stated range. The first step where your expectation and the code's meaning differ is the place where the result diverged.

  • Common mistake

    Using the constructor for HSB values

    What it looks like
    new Color(0.08F, 1.0F, 1.0F)
    Why it fails

    HSB colors are not created with a constructor. The source says the HSB system is created with the static method getHSBColor.

    Do this instead
    Color.getHSBColor(0.08F, 1.0F, 1.0F)
  • Common mistake

    Assuming all three arguments mean the same kind of thing

    What it looks like

    Passing an HSB hue value where a red level is expected

    Why it fails

    RGB arguments are red, green, and blue levels, while HSB arguments are hue, saturation, and brightness. Moving from one system to the other is a change of description.

    Do this instead

    Decide the system first, then supply numbers that match that system's meaning.

  • Common mistake

    Swapping the order of HSB arguments

    What it looks like
    Color.getHSBColor(saturation, hue, brightness)
    Why it fails

    The argument order is hue, saturation, brightness. Swapping the first two puts the saturation value into the hue position.

    Do this instead
    Color.getHSBColor(hue, saturation, brightness)
  • Common mistake

    Leaving an HSB value outside the stated range

    Why it fails

    HSB values are floats from 0.0F to 1.0F, so an out-of-range value is not a valid input for this system.

Checking Your Understanding

MEDIUM

Write two Java lines that create the same color using each system if you can. Start by choosing an RGB color such as red level 255, green level 0, blue level 0. Then, in a separate variable, choose any HSB values you like inside the 0.0F to 1.0F range. Next, for each line, name which argument controls which property. Finally, if you changed the order of the HSB arguments, state which property would move and which would stay the same.

Hints
  • The RGB constructor takes red, green, and blue in that order.
  • The HSB method takes hue, saturation, and brightness in that order.
  • Changing the order of arguments changes which value controls which property, even though the numbers stay the same.

Key Takeaways

  • RGB describes a color with three numbers giving the red, green, and blue levels, and Java creates it with new Color(r, g, b).
  • HSB describes a color with hue, saturation, and brightness, and Java creates it with the static method Color.getHSBColor(h, s, b), using float values from 0.0F to 1.0F.
  • Each argument controls one property, so the argument order is part of the meaning; swapping arguments changes the result.
  • When a color is unexpected, first identify the system, then check the creation line, its argument order, and the input values feeding it.