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).
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
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.
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.
- 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.
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.
- 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.
| Aspect | RGB | HSB |
|---|---|---|
| Three numbers | Red, green, blue levels | Hue, saturation, brightness |
| Java creation | new Color(r, g, b) | Color.getHSBColor(h, s, b) |
| Creation style | Constructor | Static method on the class |
| Value type shown in source | Component levels | float 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.
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);
}
}- 1Line 1 imports the Color class so the program can use it.
- 2Line 5 uses the constructor with the new keyword; it reads three integer levels in red, green, blue order and stores the object in
fromRgb. - 3Lines 6 to 8 declare three float variables that will supply the HSB values.
- 4Line 9 calls the static method getHSBColor on the Color class, passing hue first, saturation second, and brightness third, and stores the result in
fromHsb.
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?
- 1Identify the argument order
The method expects hue first, saturation second, and brightness third, as stated in the source.
- 2Locate the swapped values
After the swap, the second argument receives the brightness value and the third argument receives the saturation value.
- 3Trace 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.
- 4Check the other two arguments
The hue argument is still first, so the basic hue choice itself is not changed by this swap.
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?
Reveal 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 likenew Color(0.08F, 1.0F, 1.0F)Why it failsHSB colors are not created with a constructor. The source says the HSB system is created with the static method getHSBColor.
Do this insteadColor.getHSBColor(0.08F, 1.0F, 1.0F)Common mistake Assuming all three arguments mean the same kind of thing
What it looks likePassing an HSB hue value where a red level is expected
Why it failsRGB 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 insteadDecide the system first, then supply numbers that match that system's meaning.
Common mistake Swapping the order of HSB arguments
What it looks likeColor.getHSBColor(saturation, hue, brightness)Why it failsThe argument order is hue, saturation, brightness. Swapping the first two puts the saturation value into the hue position.
Do this insteadColor.getHSBColor(hue, saturation, brightness)Common mistake Leaving an HSB value outside the stated range
Why it failsHSB 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
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.