com.ccg.swing
Class ColorUtils

java.lang.Object
  extended by com.ccg.swing.ColorUtils

public class ColorUtils
extends Object

Helpful utilities for working with colors.

This class serves as a repository for standard utility methods that can be used for working with colors. You will typically use the static getIntance method to gain access to the methods provided.

Here is a code fragment of how one might use this:


 public void foo(JButton jb) {
   ColorUtils cu = ColorUtils.getInstance();
   Color c = jb.getBackground();
                                // set text color to contrast with button
   jb.setForeground(cu.getBlackOrWhite(c));
                                // set text label to hex value of color
   jb.setText(cu.getHexString(c));
 }
 

Since:
1.0
Version:
$Revision: 1.2 $
Author:
$Author: pkb $

Constructor Summary
protected ColorUtils()
          Standard constructor.
 
Method Summary
 void appendHexString(StringBuffer sb, Color c)
          Convert a color to a hexadecimal representation.
 Color getBlackOrWhite(Color c)
          Get a contrasting color.
 String getHexString(Color c)
          Convert a color to a hexadecimal representation.
static ColorUtils getInstance()
          Get access to the global ColorUtils object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorUtils

protected ColorUtils()
Standard constructor.

Since:
1.0
Method Detail

getHexString

public String getHexString(Color c)
Convert a color to a hexadecimal representation.

This method takes a color object and creates a hex string representation of the RGB values of the color contained (and optional alpha channel values). The output form will be something like:

 0xAARRGGBB
 

The two hex "AA" digits will range from [0,FF] and correspond to the alpha channel. The "RR" digits span [0,FF] for the red channel. The "GG" digits span [0,FF] for the green channel. The "BB" digits span [0,FF] for the blue channel.

Note, leading zeros are omitted when possible. For example, if the alpha channel was 0, the red channel was zero and the green channel was 1 (0x1), and the blue channel was 35 (0x23), the resulting string would be "0x123".

Parameters:
c - Color to convert - if you pass null, we'll use Color.black.
Returns:
A hex string representation in the form "0xAARRGGBB"
Since:
1.0
See Also:
Convert.toColor(java.lang.String, java.awt.Color)

appendHexString

public void appendHexString(StringBuffer sb,
                            Color c)
Convert a color to a hexadecimal representation.

This method takes a color object and appends the hex string representation of the RGB values of the color contained (and optional alpha channel values). The results which are appended will be something like:

 0xAARRGGBB
 

The two hex "AA" digits will range from [0,FF] and correspond to the alpha channel. The "RR" digits span [0,FF] for the red channel. The "GG" digits span [0,FF] for the green channel. The "BB" digits span [0,FF] for the blue channel.

Note, leading zeros are omitted when possible. For example, if the alpha channel was 0, the red channel was zero and the green channel was 1 (0x1), and the blue channel was 35 (0x23), the resulting string would be "0x123".

Parameters:
sb - Where to append results to - must not be null.
c - Color to convert - if you pass null, we'll use Color.black.
Since:
1.0
See Also:
Convert.toColor(java.lang.String, java.awt.Color)

getBlackOrWhite

public Color getBlackOrWhite(Color c)
Get a contrasting color.

Sometimes you will want to pick a color which contrasts greatly with another color. This method will look at a color that you pass and then determine whether white or black will contrast more with your color. Whichever contrasts more will be returned.

Parameters:
c - What color you want to contrast with. If you pass null - you get Color.black back.
Returns:
Either Color.black or Color.white depending upon which contrasts more.
Since:
1.0

getInstance

public static ColorUtils getInstance()
Get access to the global ColorUtils object.

Returns:
The global ColorUtils object used to access the member functions.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.