Home | Articles

New Color Classes: A Programmer's Palette of Color

Starry Night by
Vincent Van Gogh Light flowing into the eyes is perceived by the mind as an interplay of shapes with color, size, dimension, perspective, and meaning. Color is integral to human perception. Without color, shapes lose clarity and meaning because the mind perceives shape by noticing subtle changes in color.

One reason the paintings of Vincent van Gogh are so popular is that he makes magical use of color to form shapes and images that swirl and blend and come alive on the canvas.

Color is an important communication tool when used carefully on web pages and in programs. Most work with color is device-dependent meaning that colors created on one hardware platform do not look the same when shown on another hardware platform.

Even a good reproduction of van Gogh's The Starry Night does not really capture the color, texture, and magic of the original. Keeping the look and feel designed into an application or electronic art with color is also a challenge in today's open systems of heterogeneous platforms.

Device-Independent Color

The java.awt.Color class uses a standard red, green, and blue (sRGB) color space as the default color space. The sRGB color space is a proposed standard for a simple and robust color definition for use in operating systems, device drivers, and the Internet to help ensure a color is correctly mapped from one platform to another without a significant loss of color information.

Organization
of colors in a 3D space. While the Impressionist painters of yesterday used palettes and easels to create colors, color scientists in the computer age use color spaces and number values to represent colors in 2D and 3D spaces. Just as there are many types of paints -- acrylics, oil, water -- there are many types of color spaces.

The RGB color spaces organize red, green, and blue as a set of 3D axes so any color can be visualized as a unique point within the cube (or space) formed by the axes. Other color spaces use different models to pinpoint colors.

The RGB color spaces do not help you identify colors that share related attributes such as hue, saturation, or luminance, or colors for accurate reproduction on many printers or digital video platforms. Color spaces using different models are available for these applications. A list of the supported types of color spaces is at the end of this article.

Color Spaces and Color Matching

The java.awt.color package gives you access to a wide range of color spaces and provides color matching capabilities. Color matching is the process by which a device-dependent color is made to appear the same on all devices.

Color matching might be necessary when the color gamut (range of recognized colors) of the source device is different from the color gamut of the destination device. For example, some colors can be displayed on a monitor, but cannot be displayed on a printer and vice versa.

Color translation from
RGB to CMYK When a color is created in a device-dependent color space, a color profile can be used to provide the data necessary to map the color to an equivalent color in one of the device-independent conversion color spaces, either sRGB or CIEXYZ. These color spaces are described in the list of supported color spaces at the end of this article.

The translated color is passed to the destination color space, and the color profile for the destination provides the information necessary for mapping the color from sRGB or CIEXYZ to an equivalent color in the device-dependent destination color space. Images can be translated using the same process on a pixel by pixel basis. The java.awt.image.ColorConvertOp class provides optimized methods for converting complete images.

About the New Classes

The ICC_ColorSpace class is an implementation of the abstract ColorSpace class based on International Color Consortium (ICC) specifications for device-dependent and device-independent color space profiles.

An ICC_ColorSpace object is constructed from an ICC_Profile, which can be constructed from a file or byte stream. The file or byte stream contains data describing the color transformation or color conversion process for a particular color space; for example, a particular hardware device or image.

ICC_ProfileGray for monochrome monitors and ICC_ProfileRGB for color monitors subclass ICC_Profile adding methods for getting transformation or conversion information specific to those color space types.

Examples

This code segment instantiates a source RGB profile, creates a device-dependent RGB color space, and transforms an RGB color to the device-independent CIEXYZ color space.

ICC profiles (for example, RGBsrc.pf) are created by persons expert enough in color science to represent the color space of a device (monitor, printer, or scanner, for example), or a particular color conversion process.

import java.awt.color.*;
float[] ciecolor, rgbcolor;

ICC_ProfileRGB RGBpf = 
  ICC_ProfileRGB.getInstance("RGBsrc.pf");

ICC_ColorSpace rgbSpace = new ICC_ColorSpace(RGBpf);
ciecolor = rgbSpace.toCIEXYZ(rgbcolor);

This code segment creates an RGB color in the device-dependent RGB color space from the three floating point values specified in the color array where the value in color[0] is red, the value in color[1] is green, and the value in color[2] is blue.

float alpha = (float)0.5;
float color[] = new float[3];
color[0] = (float)0.2;
color[1] = (float)0.5;
color[2] = (float)1.0;

Color rgbColor = new Color(rgbSpace, color, alpha);

Supported Color Spaces

The ICC_ColorSpace class supports two specific color spaces, and a number of color space types. The specific color spaces are CIEXYZ and sRGB:

Space What it is Description
CIEXYZ Commission Internationale de l'Eclairage color system. X, Y, and Z roughly correspond to red, green, and blue. Device-independent conversion space that represents all visible colors.
sRGB Standard red, green, and blue color space. An RGB color space and proposed standard default color space for the Internet.

The color space types are the following:

Type Description
CMY Cyan, magenta, and yellow color space for color printers.
CMYK Cyan, magenta, yellow, and black color space for color printers.
GRAY Gray scale color space for monochrome.
HLS Hue, light, and saturation color space.
HSB Hue, saturation, and brightness color space.
HSV Hue, saturation, and value color space.
Lab Color and brightness color space.
Luv Color and intensity color space.
RGB Red, green, and blue color space.
XYZ X, Y, and Z roughly correspond to red, green, and blue.
YCbCr Luminance and chromaticity for digital video
Yxy Chromaticity and luminance color space related to XYZ color space.

© 1994-2005 Sun Microsystems, Inc.