|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.swing.ColorUtils
public class ColorUtils
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));
}
| 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 |
|---|
protected ColorUtils()
| Method Detail |
|---|
public String getHexString(Color c)
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".
c - Color to convert - if you pass null, we'll use
Color.black.
Convert.toColor(java.lang.String, java.awt.Color)
public void appendHexString(StringBuffer sb,
Color c)
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".
sb - Where to append results to - must not be null.c - Color to convert - if you pass null, we'll use
Color.black.Convert.toColor(java.lang.String, java.awt.Color)public Color getBlackOrWhite(Color c)
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.
c - What color you want to contrast with. If you
pass null - you get Color.black back.
Color.black or Color.white depending upon which contrasts more.public static ColorUtils getInstance()
ColorUtils object.
ColorUtils object used to access
the member functions.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||