edu.kzoo.util
Class NamedColor

java.lang.Object
  |
  +--java.awt.Color
        |
        +--edu.kzoo.util.NamedColor
All Implemented Interfaces:
java.awt.Paint, java.io.Serializable, java.awt.Transparency

public class NamedColor
extends java.awt.Color

Kalamazoo College Utility Classes:
The edu.kzoo.util.NamedColor class extends the java.awt.Color class to associate names with colors and to provide static methods that generate random colors. The random color generation methods can generate random opaque colors, random named colors, and random colors with variable levels of transparency. Name associations are provided (in English) for many common colors, and the toString method returns the color name whenever possible. Internally all names are stored in upper-case, but the methods that take names as parameters will accept either upper- or lower-case, converting them to upper-case as needed. Names are provided for all of the color constants found in java.awt.Color, as well as a few additional colors. New named colors can be added with the addColor methods. A set of static methods support getting color names and looking up colors by name.

Version:
September 1, 2004
Author:
Joel Booth
See Also:
Serialized Form

Nested Class Summary
protected static class NamedColor.ColorMap
          ColorMap is a protected static class that represents a mapping of colors to names and names to colors.
 
Field Summary
static NamedColor BLACK
          NamedColor version of java.awt.Color constant
static NamedColor BLUE
          NamedColor version of java.awt.Color constant
static NamedColor BROWN
           
static NamedColor CINNAMON
           
static NamedColor CYAN
          NamedColor version of java.awt.Color constant
static NamedColor DARK_BLUE
           
static NamedColor DARK_GRAY
          NamedColor version of java.awt.Color constant
static NamedColor DARK_GREEN
           
static NamedColor FUSCHIA
           
static NamedColor GRAY
          NamedColor version of java.awt.Color constant
static NamedColor GREEN
          NamedColor version of java.awt.Color constant
static NamedColor INDIGO
           
static NamedColor LIGHT_GRAY
          NamedColor version of java.awt.Color constant
static NamedColor LILAC
           
static NamedColor MAGENTA
          NamedColor version of java.awt.Color constant
static NamedColor MEDIUM_GREEN
           
static NamedColor MIDNIGHT
          Midnight blue
static NamedColor ORANGE
          NamedColor version of java.awt.Color constant
static NamedColor PINK
          NamedColor version of java.awt.Color constant
static int PRINT_NAME
          Flag to specify behavior for the toString(int) method
static int PRINT_RGB
          Flag to specify behavior for the toString(int) method
static NamedColor PUMPKIN
          Slightly less yellow than ORANGE
static NamedColor PURPLE
           
static NamedColor RED
          NamedColor version of java.awt.Color constant
static NamedColor ROSE
          Less salmon-colored than PINK
static NamedColor VIOLET
           
static NamedColor WHITE
          NamedColor version of java.awt.Color constant
static NamedColor YELLOW
          NamedColor version of java.awt.Color constant
 
Fields inherited from class java.awt.Color
black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow
 
Fields inherited from interface java.awt.Transparency
BITMASK, OPAQUE, TRANSLUCENT
 
Constructor Summary
NamedColor(java.awt.Color c)
          Creates a NamedColor version of the given Color object.
NamedColor(java.awt.Color c, java.lang.String name)
          Creates a NamedColor version of the given Color object with the given name.
NamedColor(int r, int g, int b)
          Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255).
NamedColor(int r, int g, int b, int a)
          Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).
NamedColor(int r, int g, int b, int a, java.lang.String name)
          Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255) and associated with the given name.
NamedColor(int r, int g, int b, java.lang.String name)
          Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255) and associated with the specified name.
 
Method Summary
 void changeName(java.lang.String name)
          Changes the name associated with this color to the specified name.
static void changeNameOrColor(java.lang.String name, java.awt.Color c)
          Changes a name => color association to the new name and the new color.
static java.util.Set getAllColorNames()
          Gets the names of all the colors with name => color associations.
static java.util.Set getAllNamedColors()
          Gets all the colors with name => color associations.
 java.lang.String getName()
          Gets the name associated with this color.
static NamedColor getNamedColor(java.lang.String name)
          Gets the color associated with the specified name, if there is one.
static java.lang.String getNameFor(java.awt.Color c)
          Gets the name associated with the specified color, if there is one.
static NamedColor getRandomAlphaColor()
          Generates a pseudorandom named NamedColor value.
static NamedColor getRandomColor()
          Generates a pseudorandom opaque NamedColor value.
static NamedColor getRandomColor(int alpha)
          Generates a pseudorandom NamedColor value with the specified transparency level.
static NamedColor getRandomNamedColor()
          Generates a pseudorandom opaque namedNamedColor value.
static NamedColor getRandomNamedColor(int alpha)
          Generates a pseudorandom named NamedColor value with the specified transparency level.
 java.lang.String getRGBRepresentation()
          Gets the component representation of the color (the value returned by the java.util.Color.toString method).
static boolean hasColorFor(java.lang.String name)
          Checks whether there is a color associated with the given name.
static boolean hasNameFor(java.awt.Color c)
          Checks whether there is a name associated with the given color.
 void setName(java.lang.String name)
          Associates the given name with this color.
static void setNameFor(java.awt.Color c, java.lang.String name)
          Associates the given name with the given color.
 java.lang.String toString()
          Returns a string representation of the color -- the name of the color if known, otherwise the standard representation as defined in java.awt.Color.toString.
 java.lang.String toString(int printMode)
          Returns a string representation of the color.
 
Methods inherited from class java.awt.Color
brighter, createContext, darker, decode, equals, getAlpha, getBlue, getColor, getColor, getColor, getColorComponents, getColorComponents, getColorSpace, getComponents, getComponents, getGreen, getHSBColor, getRed, getRGB, getRGBColorComponents, getRGBComponents, getTransparency, hashCode, HSBtoRGB, RGBtoHSB
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

WHITE

public static final NamedColor WHITE
NamedColor version of java.awt.Color constant


LIGHT_GRAY

public static final NamedColor LIGHT_GRAY
NamedColor version of java.awt.Color constant


GRAY

public static final NamedColor GRAY
NamedColor version of java.awt.Color constant


DARK_GRAY

public static final NamedColor DARK_GRAY
NamedColor version of java.awt.Color constant


BLACK

public static final NamedColor BLACK
NamedColor version of java.awt.Color constant


RED

public static final NamedColor RED
NamedColor version of java.awt.Color constant


PINK

public static final NamedColor PINK
NamedColor version of java.awt.Color constant


ORANGE

public static final NamedColor ORANGE
NamedColor version of java.awt.Color constant


YELLOW

public static final NamedColor YELLOW
NamedColor version of java.awt.Color constant


GREEN

public static final NamedColor GREEN
NamedColor version of java.awt.Color constant


MAGENTA

public static final NamedColor MAGENTA
NamedColor version of java.awt.Color constant


CYAN

public static final NamedColor CYAN
NamedColor version of java.awt.Color constant


BLUE

public static final NamedColor BLUE
NamedColor version of java.awt.Color constant


CINNAMON

public static final NamedColor CINNAMON

ROSE

public static final NamedColor ROSE
Less salmon-colored than PINK


FUSCHIA

public static final NamedColor FUSCHIA

PUMPKIN

public static final NamedColor PUMPKIN
Slightly less yellow than ORANGE


MEDIUM_GREEN

public static final NamedColor MEDIUM_GREEN

DARK_GREEN

public static final NamedColor DARK_GREEN

DARK_BLUE

public static final NamedColor DARK_BLUE

MIDNIGHT

public static final NamedColor MIDNIGHT
Midnight blue


INDIGO

public static final NamedColor INDIGO

PURPLE

public static final NamedColor PURPLE

VIOLET

public static final NamedColor VIOLET

LILAC

public static final NamedColor LILAC

BROWN

public static final NamedColor BROWN

PRINT_RGB

public static final int PRINT_RGB
Flag to specify behavior for the toString(int) method

See Also:
Constant Field Values

PRINT_NAME

public static final int PRINT_NAME
Flag to specify behavior for the toString(int) method

See Also:
Constant Field Values
Constructor Detail

NamedColor

public NamedColor(java.awt.Color c)
Creates a NamedColor version of the given Color object.

Parameters:
c - the Color object to convert to a NamedColor object

NamedColor

public NamedColor(java.awt.Color c,
                  java.lang.String name)
Creates a NamedColor version of the given Color object with the given name. (Precondition: there is not already a name associated with the given color, nor is there already a color associated with the given name.)

Parameters:
c - the Color object to convert to a NamedColor object
name - the name to associate with the given color
Throws:
IllegalArgumentException - if there is already a name => color association involving either c or name

NamedColor

public NamedColor(int r,
                  int g,
                  int b)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255). The actual color used in rendering depends on finding the best match given the color space available for a particular output device. Alpha is defaulted to 255.

Parameters:
r - the red component
g - the green component
b - the blue component
See Also:
Color.Color(int, int, int)

NamedColor

public NamedColor(int r,
                  int g,
                  int b,
                  java.lang.String name)
Creates an opaque sRGB color with the specified red, green, and blue values in the range (0 - 255) and associated with the specified name. The actual color used in rendering depends on finding the best match given the color space available for a particular output device. Alpha (the transparency level) defaults to 255. (Precondition: there is not already a name associated with the given color, nor is there already a color associated with the given name.)

Parameters:
r - the red component
g - the green component
b - the blue component
name - the name to associate with the given color
Throws:
IllegalArgumentException - if there is already a name => color association involving either c or name

NamedColor

public NamedColor(int r,
                  int g,
                  int b,
                  int a)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255). The actual color used in rendering depends on finding the best match given the color space available for a particular output device.

Parameters:
r - the red component
g - the green component
b - the blue component
a - the alpha component
See Also:
Color.Color(int, int, int, int)

NamedColor

public NamedColor(int r,
                  int g,
                  int b,
                  int a,
                  java.lang.String name)
Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255) and associated with the given name. The actual color used in rendering depends on finding the best match given the color space available for a particular output device. (Precondition: there is not already a name associated with the given color, nor is there already a color associated with the given name.)

Parameters:
r - the red component
g - the green component
b - the blue component
a - the alpha component
name - the name to associate with the given color
Throws:
IllegalArgumentException - if there is already a name => color association involving either c or name
Method Detail

setNameFor

public static void setNameFor(java.awt.Color c,
                              java.lang.String name)
Associates the given name with the given color. (Precondition: there is not already a name associated with the given color, nor is there already a color associated with the given name.)

Parameters:
name - the name to associate with the color c
c - the color
Throws:
IllegalArgumentException - if there is already a name => color association involving either c or name

changeNameOrColor

public static void changeNameOrColor(java.lang.String name,
                                     java.awt.Color c)
Changes a name => color association to the new name and the new color. Any previous association involving either name or c no longer exists.

Parameters:
name - the name of the color
c - the color

hasNameFor

public static boolean hasNameFor(java.awt.Color c)
Checks whether there is a name associated with the given color.

Parameters:
c - the color
Returns:
true if there is an associated name, otherwise false

hasColorFor

public static boolean hasColorFor(java.lang.String name)
Checks whether there is a color associated with the given name.

Parameters:
name - the name of the color
Returns:
true if there is an associated color, otherwise false

getNameFor

public static java.lang.String getNameFor(java.awt.Color c)
Gets the name associated with the specified color, if there is one. Returns a null object if there is no name => color association for the specified name.

Parameters:
c - a color
Returns:
the name associated with color; null if there is no such association

getNamedColor

public static NamedColor getNamedColor(java.lang.String name)
Gets the color associated with the specified name, if there is one. Returns a null object if there is no name => color association for the specified name.

Parameters:
name - a color name
Returns:
the color associated with name; null if there is no such association

getAllColorNames

public static java.util.Set getAllColorNames()
Gets the names of all the colors with name => color associations.

Returns:
all the known color names

getAllNamedColors

public static java.util.Set getAllNamedColors()
Gets all the colors with name => color associations.

Returns:
all the known colors

getRandomColor

public static NamedColor getRandomColor()
Generates a pseudorandom opaque NamedColor value.

Returns:
the new pseudorandom opaque color

getRandomColor

public static NamedColor getRandomColor(int alpha)
Generates a pseudorandom NamedColor value with the specified transparency level.

Parameters:
alpha - the transparency level to use, in the range of 0 to 255 where 255 is completely opaque
Returns:
the new pseudorandom color with the specified transparency level

getRandomAlphaColor

public static NamedColor getRandomAlphaColor()
Generates a pseudorandom named NamedColor value. Colors returned by this method have a random alpha (transparency) component, as well as random red, green, and blue components.

Returns:
the new pseudorandom color

getRandomNamedColor

public static NamedColor getRandomNamedColor()
Generates a pseudorandom opaque namedNamedColor value.

Returns:
the new pseudorandom named opaque color

getRandomNamedColor

public static NamedColor getRandomNamedColor(int alpha)
Generates a pseudorandom named NamedColor value with the specified transparency level.

Parameters:
alpha - the transparency level to use, in the range of 0 to 255 where 255 is completely opaque
Returns:
the new pseudorandom named color with the specified transparency level

setName

public void setName(java.lang.String name)
Associates the given name with this color. (Precondition: there is not already a name associated with this color.)

Parameters:
name - the name to associate with this color
Throws:
IllegalArgumentException - if there is already a name => color association involving either c or name

changeName

public void changeName(java.lang.String name)
Changes the name associated with this color to the specified name. If name was previously associated with any other color, that association no longer exists.

Parameters:
name - the name of this color

getName

public java.lang.String getName()
Gets the name associated with this color.

Returns:
the name associated with this NamedColor object; null if there is no such association

getRGBRepresentation

public java.lang.String getRGBRepresentation()
Gets the component representation of the color (the value returned by the java.util.Color.toString method).

Returns:
the component representation of the color

toString

public java.lang.String toString()
Returns a string representation of the color -- the name of the color if known, otherwise the standard representation as defined in java.awt.Color.toString.

Overrides:
toString in class java.awt.Color
Returns:
a String representation of the color

toString

public java.lang.String toString(int printMode)
Returns a string representation of the color. Two flags are available:
PRINT_RGB - Return the java.awt.Color representation of the color components
PRINT_NAME - Return the NamedColor (default) representation, which consists of the English name if it is available or the java.awt.Color representation if there is no name available for this color

Parameters:
printMode - the type of String to generate
Returns:
the String representation of the color