| By John Chamberlain | Article Rating: |
|
| February 5, 2004 12:00 AM EST | Reads: |
17,374 |
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
Published February 5, 2004 Reads 17,374
Copyright © 2004 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
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
![]() |
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 |
||||
- Patterns for Building High Performance Applications
- It's the Java vs. C++ Shootout Revisited!
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Cross-Platform Mobile Website Development – a Tool Comparison
- Three Buzzwords That Every CIO Hears but One They Should Listen To
- Write Once Run Anywhere or Cross Platform Mobile Development Tools
- Immersing into JavaScript Frameworks
- Workday Reportedly Prepping to Go Public
- Cloud Expo New York: The Java EE 7 Platform - Developing for the Cloud
- Book Review: Sams Teach Yourself Java in 24 Hours
- OpenOffice.com Lives
- Book Excerpt: Introducing HTML5
- Adobe Sends Flex to the Apache Foundation
- Five Years Waiting for JRE 7: Is It Justified? (Part 1)
- Book Excerpt: Java Application Profiling Tips and Tricks
- i-Technology in 2012: Five Industry Predictions
- Patterns for Building High Performance Applications
- It's the Java vs. C++ Shootout Revisited!
- OpenXava 4.3: Rapid Java Web Development
- The Next Web Architecture
- Asynchronous Logging Using Spring
- Java for Programmers (2nd Edition)
- Is Write Once Run Anywhere Ever Going to Be a Reality?
- A Cup of AJAX? Nay, Just Regular Java Please
- Java Developer's Journal Exclusive: 2006 "JDJ Editors' Choice" Awards
- JavaServer Faces (JSF) vs Struts
- The i-Technology Right Stuff
- Rich Internet Applications with Adobe Flex 2 and Java
- Java vs C++ "Shootout" Revisited
- Bean-Managed Persistence Using a Proxy List
- Reporting Made Easy with JasperReports and Hibernate
- Creating a Pet Store Application with JavaServer Faces, Spring, and Hibernate
- Why Do 'Cool Kids' Choose Ruby or PHP to Build Websites Instead of Java?
- What's New in Eclipse?
- i-Technology Predictions for 2007: Where's It All Headed?





















