Welcome!

Java Authors: App Man, Liz McMillan, Jeremy Geelan, Yakov Fain, Hari Gottipati

Related Topics: Java

Java: Article

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 JDJ's Web site (www.sys-con.com/java/sourcec.cfm). 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.

 

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.

 

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.
  • More Stories By John Chamberlain

    John Chamberlain is a developer in the Boston area who works at OPeNDAP.org. He holds a master's degree in computer science, is a frequent contributor to technical journals and has spoken at JavaOne. He can be reached by email at jdj@johnchamberlain or stop by his web site at http://johnchamberlain.com

    Comments (2) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


    Most Recent Comments
    Malcolm 02/27/04 08:05:02 AM EST

    The code supplied with the article is incorrect. However on http://johnchamberlain.com the author has placed an updated version which does work.

    Paul Pomeroy 02/13/04 07:37:37 PM EST

    John, thanks for the short but good article on color. Regarding using a "swatch book" for finding compatible colors, your readers may want to look at ColorWrite (at www.adaptiveview.com/cw), a well regarded Java app. In addition to displaying more color combinations (all based on color theory) than you''ll ever find in a book, it generates Java (and HTML and CSS) code for using a selected color. It''s free, so help yourself.

    Paul Pomeroy
    ------------
    www.adaptiveview.com