Color

Not so long ago, color was a luxury; these days, color is a requirement. A program that uses only black and white seems hopelessly old fashioned. AWT's Color class lets you define and work with Color objects. When we discuss the Component class (see Components), you will see how to use these color objects, and our discussion of the SystemColor subclass (new to Java 1.1; discussed later in this chapter) shows you how to control the colors that are painted on the screen.

A few words of warning: while colors give you the opportunity to make visually pleasing applications, they also let you do things that are incredibly ugly. Resist the urge to go overboard with your use of color; it's easy to make something hideous when you are trying to use every color in the palette. Also, realize that colors are fundamentally platform dependent, and in a very messy way. Java lets you use the same Color objects on any platform, but it can't guarantee that every display will treat the color the same way; the result depends on everything from your software to the age of your monitor. What looks pink on one monitor may be red on another. Furthermore, when running in an environment with a limited palette, AWT picks the available color that is closest to what you requested. If you really care about appearance, there is no substitute for testing.

Color Methods

Constants

The Color class has predefined constants (all of type public static final Color) for frequently used colors. These constants, their RGB values, and their HSB values (hue, saturation, brightness) are given in Table 3.1.

Comparison of RGB and HSB Colors
Color Red Green Blue Hue Saturation Brightness
black
blue
cyan
darkGray
gray
green
lightGray
magenta
orange
pink
red
white
yellow

These constants are used like any other class variable: for example, Color.red is a constant Color object representing the color red. Many other color constants are defined in the SystemColor class. Constructors

When you're not using a predefined constant, you create Color objects by specifying the color's red, green, and blue components. Depending on which constructor you use, you can specify the components as integers between 0 and 255 (most intense) or as floating point intensities between 0.0 and 1.0 (most intense). The result is a 24-bit quantity that represents a color. The remaining 8 bits are used to represent transparency: that is, if the color is painted on top of something, does whatever was underneath show through? The Color class doesn't let you work with the transparency bits; all Color objects are opaque. However, you can use transparency when working with images; this topic is covered in Image Processing.

(((red & 0xFF) << 16 ) | ((green & 0xFF) << 8) | ((blue & 0xFF) << 0)) 
Settings

NOTE:

Black does not get any brighter.

Color properties

Color properties are very similar to Font properties. You can use system properties (or resource files) to allow users to select colors for your programs. The settings have the form 0xRRGGBB, where RR is the red component of the color, GG represents the green component, and BB represents the blue component. 0x indicates that the number is in hexadecimal. If you (or your user) are comfortable using decimal values for colors (0x112233 is 1122867 in decimal), you can, but then it is harder to see the values of the different components.

NOTE:

The location of the system properties file depends on the run-time environment and version you are using. Ordinarily, the file will go into a subdirectory of the installation directory or, for environment's where users have home directories, in a subdirectory for the user. Sun's HotJava, JDK, and appletviewer tools use the properties file in the .hotjava directory.

Most browsers do not permit modifying properties, so there is no file.

Java 1.1 adds the idea of "resource files," which are syntactically similar to properties files. Resource files are then placed on the server or within a directory found in the CLASSPATH. Updating the properties file is no longer recommended.

For example, consider a screen that uses four colors: one each for the foreground, the background, inactive components, and highlighted text. In the system properties file, you allow users to select colors by setting the following properties:

myPackage.myClass.foreground
myPackage.myClass.background
myPackage.myClass.inactive
myPackage.myClass.highlight

One particular user set two:

myPackage.myClass.foreground=0xff00ff #magenta myPackage.myClass.background=0xe0e0e0 #light gray 

These lines tell the program to use magenta as the foreground color and light gray for the background. The program will use its default colors for inactive components and highlighted text.

// Java 1.1 only InputStream is = instance.getClass().getResourceAsStream("propfile"); Properties p = new Properties(); try {
 p.load (is); Color c = Color.decode(p.getProperty("myPackage.myClass.foreground"));
}
catch (IOException e) {
 System.out.println ("error loading props...");
}
Hue, saturation, and brightness

So far, the methods we have seen work with a color's red, green, and blue components. There are many other ways to represent colors. This group of methods allows you to work in terms of the HSB (hue, saturation, brightness) model. Hue represents the base color to work with: working through the colors of the rainbow, red is represented by numbers immediately above 0; magenta is represented by numbers below 1; white is 0; and black is 1. Saturation represents the color's purity, ranging from completely unsaturated (either white or black depending upon brightness) to totally saturated ( just the base color present). Brightness is the desired level of luminance, ranging from black (0) to the maximum amount determined by the saturation level.

Miscellaneous methods
java.awt.Color[r=255,g=200,b=0]