HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

Using Color Technology in Java
Heighten the visual impact of your application

Brighten up your dreary application by learning how to use Java to create and manage color. Using a color picker makes it easy to define exact colors with alpha transparency.

For a developer there are two situations in which color comes up: when you are creating the colors for your interface and when a user can define colors or manipulate images as part of your application's functionality. To work with color effectively, you should know the basics of color management and color definition.

Color Management
Color technology comes in three main types: input devices (like scanners), display devices, and output devices (printers). The possible color range, called the "gamut," differs from device to device. Because of this the colors you see in an on-screen image will be different from those of a scanned or printed image. Controlling the color translation between different environments is what color management is all about.

You can do this translation using the Java 2D API's ColorSpace and ICC_Profile classes. First load the profiles for the two different environments. For the display you can use the built-in RGB profile that is the default. For a device like a scanner or printer, you'll usually need to load a color profile from a file from the manufacturer. For each of the profiles generate a ColorSpace object. ColorSpace has two methods: toCIEXYZ and fromCIEXYZ. The CIEXYZ color space is sort of a master color model that serves as the medium for translation. You can translate any color or image from one color space to another by translating it first to CIEXYZ, then to the target color space.

If you are programming for display only, that's about all you need to know about color management, but if you have color input/output in your application, you may want to learn more (see the resources section at the end of the article).

Picking Color
When users define a color, they need to have a dialog called a color picker. Unfortunately, the color picker that comes in the Swing package is somewhat inadequate. For one thing it has no support for alpha transparency. Also, it's hard to compare colors and see exact hues. Figure 1 shows a color picker that you can download from below. This color picker allows your users (and you) to zero in on colors more easily and includes alpha transparency. Notice that the picker has two boxes: selected color versus compare color, which allow the user to compare two colors against each other. When the selection area is clicked, the compare and selection colors swap places. This is an efficient system for exploring and creating color.

Figure 1

An important consideration for color picking is that you should stick to the HSB (Hue, Saturation, Brightness) system rather than the monitor-oriented RGB encoding. It's much easier to define colors by starting with a hue and then modifying its brightness (shading) and saturation (tinting) than by trying to do an RGB mix. Table 1 shows the basic HSB combinations.

Table 1

GUI Color Schemes
One way to increase the vitality of your application's interface is to create a logical color scheme. The problem with the typical battleship gray is low contrast, so in many cases there will be elements that are difficult for the user to read. Also it simply does not look that good; it's boring and monotonous. Many developers are leery of using colors because of the difficulty in finding a good matching set. You can overcome this problem by using the same combination books graphic designers use. I've listed such a swatch book in the resources section.

Even without using a swatch book there are some basic rules you can apply when creating a scheme. In general, it's best to use a two- or three-color scheme plus highlights. In a two-color scheme, it's a good idea to pick two complementary colors. In a three-color scheme start off with a triad (three hues spaced equally). To create highlights decrease the saturation of your base colors.

Other things to be aware of are the distance effect and contrast. When you look from afar, darker colors such as blue fade to black faster; red and blue are inherently darker than yellow and green. Maximizing contrast is a good policy but having black on white can be harsh. Using a pale yellow background may provide better readability. If you need large text to be read from a distance, for example, in a kiosk display, add a red one-pixel outline to the letters to increase contrast.

Resources

  • Fraser, B., Murphy, C., and Bunting, F. (2003). Real World Color Management. Peachpit Press.
  • Chijiiwa, H. (1987). Color Harmony. Rockport Publishers.

    About the Author
    John Chamberlain, a developer in the Boston area, works at OPeNDAP.org. He holds an MS in computer science, is a frequent contributor to technical journals, and has spoken at JavaOne. Stop by his Web site at http://johnchamberlain.com. [email protected]

    "Using Color Technology in Java"
    Vol. 9, Issue 2, p. 56

    Source Code for this Article java file ~27.6 KB

    All Rights Reserved
    Copyright ©  2004 SYS-CON Media, Inc.
      E-mail: [email protected]

    Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.