Color

In order to use color in a class, you need to import it up at the top of the file:

import objectdraw.*;
import java.awt.Color;

public class ....

Usually, all you want to use Color for is to set the color of some object. There are two ways to do this: you can use the Color constant variables included in the Color class, or you can mix your own Color by doing new Color() with some amount each of red, green, and blue. This page will also talk about some more complicated things you can do with color.

Table of Contents

1. Mixing your own RGB color

Your browser doesn't do Java.
Gradient.java

RGB stands for Red-Green-Blue. All the colors on a computer monitor are made by some combination of these three colors of light, which are the three colors that our eyes see. So, for example, if a pixel on the screen is giving off both red and green light, it stimulates both the red and green cones in your eye, and your brain decides that you must be seeing a color between the two, namely, yellow.

To create a new RGB color, use this constructor:

public Color(int red, int green, int blue);

Each of these numbers can be anywhere in the range from 0 to 255. So, for example, white has a value of 255 for each; magenta has a value of 255 for red and blue but 0 for green. The one color that everyone finds confusing when they first learn about mixing color of light is yellow: 255 each of red and green with no blue.

Here are some examples of colors created with this constructor:
new Color(204, 204, 204)new Color(255, 153, 0)new Color(102, 0, 255)

You can also get the red, green, and blue components of a color, as ints, with the methods getRed(), getGreen(), and getBlue(). So, for example, I can duplicate a color object like this:

Color duplicate = new Color(color.getRed(), color.getGreen(), color.getBlue());

2. Using the predefined colors

There are several predefined Color objects that are public variables in the Color class. You will most often use these when you want a color. So, for example, I don't need to go trying to recreate a black color, because I can just use Color.BLACK. The other available colors are:
Color.WHITEColor.LIGHT_GRAYColor.GRAYColor.DARK_GRAY
Color.REDColor.PINKColor.ORANGEColor.YELLOW
Color.GREENColor.MAGENTAColor.CYANColor.BLUE

3. Rainbow colors

Your browser doesn't do Java.
ColorWheel.java
Suppose that you want colors to vary in a rainbow fashion: red-orange-yellow-green-blue. You could do this with something like listing above, but an easier way is to use a different method of specifying color: the HSB color space.

public static Color getHSBColor(float hue, float saturation, float brightness)

HSB stands for Hue-Saturation-Brightness. This color space is often shown in a computer as a color wheel.

The Hue determines what color of the rainbow you are dealing with: 0. is red, .33 is green, .66 is blue, and the other colors fall somewhere in between.

The Saturation determines how pure that color is: for a hue of 0., a saturation of 1. would be pure red, a saturation of .5 would be pink, and a saturation of .1 would be a very light pink, almost white. Any color with a saturation of 0. is a white or grey.

The Brightness is how bright the color is. A brightness of 1. is as bright as possible; a brightness of 0. is black.

To construct a color in HSB, use the static method:

public static Color Color.getHSBcolor(float hue, float saturation, float brightness)

The float type is something we haven't talked about much: it's just a less precise version of double. You can write a number as, for example, .25f, with an "f" at the end, to get Java to interpret it as a float. If you have a double that you want treated as a float, you can typecast it: for example, (double)x or (double)(i / 255.).

The applet to the right creates the "color wheel" that you see with this code. Notice the typecasting I have to do to get floats for the color:

size = Math.min(canvas.getWidth(), canvas.getHeight() - 20);
center = new Location(canvas.getWidth() / 2, size / 2);

for(int j = STEPS; j >= 1; j--) {
   int arcSize = size * j / STEPS;
   int x = center.getX() - arcSize / 2;
   int y = center.getY() - arcSize / 2;
   
   for(int i = 0; i < SLICES; i++) {
      Color c = Color.getHSBColor((float)i / SLICES, (float)j / STEPS, (float)brightness);
      new FilledArc(x, y, arcSize, arcSize, i * 360. / SLICES, 360. / SLICES + 1, c, canvas);
   }
}

4. Transparent colors

Your browser doesn't do Java.
Transparency.java
Another thing you can do with color on a computer is to determine how transparent it is. Normally, if I place a blue circle on top of a red square, there is no red visible throung the blue. If the blue circle is partially transparent, however, it might look purple instead, since some of the red is showing through. The same circle, placed on a yellow background, would appear green.

To create a color that is partially transparent, just add an argument, called the alpha, to the constructor:

public Color(int red, int green, int blue, int alpha)

With alpha = 255, the color is completely opaque. If alpha = 0, the color is transparent, like invisible ink: nothing shows up when you draw with that color. Something in between will make a color that changes, but does not replace, the color it is drawing on.

So, for example, in the applet to the right, I have created only three objects:

new FilledOval(50, 30, 60, 120, new Color(0, 200, 0, 180), canvas);
new FilledRect(80, 60, 70, 100, new Color(255, 0, 0, 60), canvas);
new FilledRoundedRect(60, 100, 120, 40, 20, 20, new Color(0, 0, 255, 120), canvas);

Notice how the shapes with a higher alpha value are a much more opaque color. Try dragging the shapes around to see what it looks like when they overlap.

5. Full specification for the Color class

You can read the full specification for the Color class here on the java website:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html