colour : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

chameleon  colour
This entry is about colour in general, not just the java.awt.Col or 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 select palette Alphabetically (140) select palette BHS: by Brightness, Hue, Saturation select palette HBS: by Hue, Brightness, Saturation select palette SBH: by Saturation, Brightness, Hue select palette Java AWT Colours (16,777,216)
select palette RGB: Numerically (140) select palette BSH: by Brightness, Saturation, Hue select palette HSB: by Hue, Saturation, Brightness select palette SHB: by Saturation, Hue, Brightness select palette Java Swing Colours (16,777,216)
Numbered Colours select palette HTML 3.2 (16) select palette Websafe (216) select palette Rainbow (4096) select palette Spectrum (401) select palette X11 (657)
Selected Colours select palette Pale (256) select palette Dark (2022) select palette Simple (105) select palette Greys (256) select palette Colour Schemes
The above colour chart shows Netscape’s 133 standard colours and HTML (Hypertext Markup Language) 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 (Red Green Blue) an HSB (Hue Saturation Brightness) values both as HTML and raw ASCII (American Standard Code for Information Interchange) 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 transparency 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. (Note Java uses American spelling Color.GRAY not Colour.GREY).

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 coordinates 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 (Java Development Kit) 1.3-, the colours had lower case names such as Color. white. Since these are static final constants, in Java version 1.4 or later, 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

Oracle’s Javadoc on Color class : available:
Oracle’s Javadoc on SystemColor to control the overall AWT colour scheme : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/colour.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\colour.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.139.87.113]
You are visitor number