colour : Java Glossary
home C words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish by Roedy Green ©1996-2008 Canadian Mind Products
Go to : punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all)
colour
This entry is about colour in general, not just the java.awt.Color class. Note the American spelling of colour missing the u. Often your colours will be mangled when you run them on some other platform or browser than the one you developed with. Some platforms only support 256 colours. Colours with the following magic values as the R G or B component are most likely to survive unmolested: 0, 51, 102, 153, 204, 255 or 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff.

Click any ball to view the corresponding colour palette.

Named Colours button Alphabetically (113) button BHS: by Brightness, Hue, Saturation button HBS: by Hue, Brightness, Saturation button SBH: by Saturation, Brightness, Hue button Java AWT Colours (16,777,216)
button RGB: Numerically (113) button BSH: by Brightness, Saturation, Hue button HSB: by Hue, Saturation, Brightness button SHB: by Saturation, Hue, Brightness button Java Swing Colours (16,777,216)
Numbered Colours button HTML 3.2 (16) button Websafe (216) button Rainbow (4096) button Spectrum (401) button X11 (657)
Selected Colours button Pale (256) button Dark (2022) button Simple (105) button Greys (256) button Colour Schemes
The above colour chart shows Netscape’s 133 standard colours, and HTML 3.2’s 16 standard colours. It shows the colours displayed eight ways, (colour on white, colour on black, black on colour, white on colour) both using alpha names and hex names. You can check out your browser for Netscape colour compatibility. It shows the Standard Netscape 8.0 alpha names such as "aliceblue" and also the hex, RGB an HSB values both as HTML and raw ASCII text.

Under the Hood

The java.awt.Color class internally creates a 32 bit descriptor of a colour composed of 4 8-bit unsigned bytes. A R G B.

A = alpha channel that measures opacity. 0 = 100% transparent, 255 = 100% opaque.
R = red channel that measures redness. 0 = black, 255 = intense red.
G = green channel that measures greenness. 0 = black, 255 = intense green.
B = blue channel that measures blueness. 0 = black, 255 = intense blue.

So 0.0.0.0 would be a totally transparent black colour, and 255.255.255.255 would be a totally opaque white. I have discovered by experiment that if you want transparent backgrounds that you can save variable tranparency png files if you use this magic combination:

Colour Intuition

It is fairly easy to tweak colours once you understand how the colour numbering system works. 0,0,0 is black. To increase the redness you increase the first number, the greenness the second, the blueness the third. ff,ff,ff is white, a full intensity mixture of red, green and blue. 80,80,80 is mid gray.

It is bit like mixing poster paint in grade two art class, except that you are mixing light not pigments, so red + green = yellow (surprise); green + blue = cyan (turquoise); and blue + red = violet.

To make a colour more yellowy you increase the red and green simultaneously and reduce the blue. Do make it darker decrease all three numbers.

The color mixing rules for pigments and light have little to do with physics and a lot to with anatomy and the variable sensitivity of different cells on your retina to different frequencies of light. Other species presumably would have different rules.

You can experiment with colours by looking at the various color charts above or using the Java FontShower which shows Java fonts in various foreground and background colours.

I find the HSB system is easier to work with than RGB. Normally I want a darker shade of the same colour, or a not so circus-bright intensity version of the same colour, or I want the saturation and brightness the same, but a slightly different hue. The FontShower for Swing Applet will let you select in HSB co-ordinates and convert to RGB for use in Java or HTML. The “right” colour is subjective. You have to try a colour and live with it in context a while so see if it is right.

Unfortunately, the colour balance on my faithful Sony monitor is shot and I don’t have the money for a new one. This effectively makes me colour blind. That may explain some of the unusual colour choices on this website.

Controlling Colour in Java

Note that you use Component.setForeground() and Component.setBackground() to control the colours of a component. However, in a paint or paintComponent method, you use Graphics.setColor() to paint both the background and foreground, e.g.
// paint background
g.setColor( getBackground() );
g.fillRect( 0, 0, width, height );

// paint foreground
g.setColor( getForeground() );
g.drawString( "hello", x, y );

To make your app blend with other applications, you can use the SystemColor class to select your colours. That way your app will adapt to the Look & Feel and possibly the desktop theme colours. In JDK 1.3-, the colours had lower case names such as Color. white. Since these are static final constants, in JDK 1.4+, Sun gave them proper upper case names such as Color.WHITE. However, beware of using the upper case names it you want to target JDK 1.3- versions. Your programs will explode in a glory of exceptions from the missing support. The lower case names will work in any JDK version.

Learning More

Sun’s Javadoc on the Color class : available:
Sun’s Javadoc on the SystemColor to control the overall AWT colour scheme class : available:
channel
ColorSaver
Colour blind colour chart
colour conversion formulas
Colour Vision Test
Colour Vision Test as movie
CYMK
FontShower Amanuensis: shows fonts in a variety of families, styles, sizes and colours
frequency
gamma correction
HSB
IndexColorModel
Japanese Colour Names: (requires shift_jis support to view)
Lynda Weinman’s essay: browser-safe colours
Netscape
palette
pseudograys: Rich Franzen’s essay and code on smoother gray scales
RGB
RichInStyle.com colorizer
wavelength
Website Tips

CMP_homejump to top
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.17] The information on this page is for non-military use only.
You are visitor number 28,076. Military use includes use by defence contractors.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/colour.html J:\mindprod\jgloss\colour.html