Colors


Computer screens, like television screens, consist of rows and columns of tiny dots. It's difficult to see the dots with your unaided eye, but they are easy to see through a magnifying glass. The screen's electronics control each dot's color, under the direction of the computer's software or the TV's signal.

If you look closely at a pixel, you'll see that it consists of a red region, a green region, and a blue region. These are the pixel's primary colors.

This might contradict your childhood experiences. When you first started drawing or painting, you probably noticed that certain colors combine to make other colors. Mix red and blue to make purple. Red and yellow make orange, and blue and yellow make green. Other combinations aren't as pleasing: Red and green make an especially unpleasant brown. No doubt, someone explained to you that red, yellow, and blue are the primary colors that can be combined to make all other colors.

That isn't exactly true (there's no way to make white), but it pretty much explains the world of color. That is, until you stare too closely at a screen or start learning about computer colors. Then it seems that the primary colors are not red, yellow, and blue, but rather red, green, and blue.

Which trio is the real set of primary colors? It depends on your situation. When you mix paints, the pigments in the paints absorb certain colors from the ambient light. The remaining colors get reflected back into your eyes. Red, yellow, and blue (the primary colors of painting) are called subtractive primary colors because they are primary when light reflects off the absorbing pigment. Red, green, and blue (the primary colors of screens) are called additive primary colors because they are primary when different colors of light combine without pigment to absorb any hues. With additive primary colors, red plus green makes yellow, and red plus green plus blue makes white. You might have seen additive primaries in theaters or other venues that use colored spotlights. Where a red and green spotlight overlap, the light is yellow. Where red, green, and blue spotlights overlap, the light is white.

So when you control colors in a Java program, you have to think in terms of additive, not subtractive, primary colors. Table 14.1 summarizes additive color mixing.

Table 14.1: Combining Additive Primary Colors

Primary Colors

Result

Red + green

Yellow

Red + blue

Magenta

Green + blue

Cyan

Red + green + blue

White

In Java, colors are represented by the java.awt.Color class. The constructor for this class has three arguments, which represent the amount of red, green, and blue that make up the color. The arguments range from a minimum of 0 through a maximum of 255. If all three arguments are 0, the color is black. If all three are 255, the color is white. As you can see from Table 14.1, if red and blue are 255 while green is 0, the color is magenta. If red is 200, blue is 255, and green is 0, the color is a somewhat bluer magenta (because it contains less red).

The Color class has 13 predefined colors. These are public final static variables of type Color. (It may seem convoluted for a class to contain data of the same type as the class. That's just how it is.) The names of these variables are

  • Color.BLACK

  • Color.WHITE

  • Color.RED

  • Color.GREEN

  • Color.BLUE

  • Color.YELLOW

  • Color.CYAN

  • Color.MAGENTA

  • Color.ORANGE

  • Color.PINK

  • Color.LIGHT_GRAY

  • Color.GRAY

  • Color.DARK_GRAY

If you want to use one of these colors, you don't have to create a new instance. For example, to set a frame's background color to orange, you can call setBackground(Color.orange). If the 13 predefined colors don't give you what you want, you need to construct your own. You might find that Color.orange isn't intense enough. Its green level is 200, which tends to wash out the brilliance of the red. A nice intense orange is created by calling new Color(255, 200, 0). You can make this the background color of a frame by calling setBackground(new Color(255, 200, 0)).

Looking at colors is better than reading about them. The Color Lab program lets you practice mixing primaries, and it also shows you all 13 predefined colors. To run the program, type java visual.ColorLab. Figure 14.2 shows the initial display.

click to expand
Figure 14.2: Color Lab

The control panel contains three scrollbars, with a text field showing the value of each scrollbar, as well as a pull-down menu. When you choose Custom from the pull-down menu, as shown in Figure 14.2, the scrollbars are enabled. You can set them to any value from 0 through 255, and the display area to the right of the control panel will show the color you've specified. You can also type numbers into the text fields. Press Enter to make your entry take effect.

In addition to Custom, the pull-down menu lets you choose any of the 13 predefined colors of the Color class. When one of these is selected, the scrollbars and text fields are disabled. They display the red/green/blue levels of the selected color, but you can't use them for input. Figure 14.3 shows Color Lab displaying a predefined color.

click to expand
Figure 14.3: Color Lab with a predefined color

Notice that the sliders have no bubbles, and the numbers in the text fields are gray, indicating that those components are not enabled to receive user input.

Set the Color Lab inputs to display yellow. You can do this by selecting YELLOW, or by selecting Custom and manipulating the scrollbars. Now look at the yellow area of the screen through a magnifying glass. Observe the separate red and green areas of the individual pixels. Have a friend hold the magnifying glass steady, and move slowly backwards until the red and green seem to coalesce into yellow. How far from the screen are you when this begins to happen? You are invited to e-mail the distance to us at groundupjava@sgsware.com. We will compile the statistics and publish them on our website.

Now you know how to use Java's predefined colors, and how to construct a custom color when you need one. Now let's move on to using colors to draw shapes inside a frame.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net