public class NamedColor
extends java.awt.Color
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 named colors,
random opaque 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 NamedColor
constructors
that take a name as a parameter and with the setName
methods.
A set of static methods support getting color names and looking up colors
by name.Modifier and Type | Class and Description |
---|---|
protected static class |
NamedColor.ColorMap
ColorMap is a protected static class that represents a
mapping of colors to names and names to colors. |
Modifier and Type | Field and Description |
---|---|
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 |
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
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<java.lang.String> |
getAllColorNames()
Gets the names of all the colors with name => color associations.
|
static java.util.Set<NamedColor> |
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 named
NamedColor 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.
|
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
public static final NamedColor WHITE
java.awt.Color
constantpublic static final NamedColor LIGHT_GRAY
java.awt.Color
constantpublic static final NamedColor GRAY
java.awt.Color
constantpublic static final NamedColor DARK_GRAY
java.awt.Color
constantpublic static final NamedColor BLACK
java.awt.Color
constantpublic static final NamedColor RED
java.awt.Color
constantpublic static final NamedColor PINK
java.awt.Color
constantpublic static final NamedColor ORANGE
java.awt.Color
constantpublic static final NamedColor YELLOW
java.awt.Color
constantpublic static final NamedColor GREEN
java.awt.Color
constantpublic static final NamedColor MAGENTA
java.awt.Color
constantpublic static final NamedColor CYAN
java.awt.Color
constantpublic static final NamedColor BLUE
java.awt.Color
constantpublic static final NamedColor CINNAMON
public static final NamedColor ROSE
PINK
public static final NamedColor FUSCHIA
public static final NamedColor PUMPKIN
ORANGE
public static final NamedColor MEDIUM_GREEN
public static final NamedColor DARK_GREEN
public static final NamedColor DARK_BLUE
public static final NamedColor MIDNIGHT
public static final NamedColor INDIGO
public static final NamedColor PURPLE
public static final NamedColor VIOLET
public static final NamedColor LILAC
public static final NamedColor BROWN
public static final int PRINT_RGB
toString(int)
methodpublic static final int PRINT_NAME
toString(int)
methodpublic NamedColor(java.awt.Color c)
NamedColor
version of the given
Color
object.c
- the Color
object to convert to a
NamedColor
objectpublic NamedColor(java.awt.Color c, java.lang.String name)
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.)c
- the Color
object to convert to a
NamedColor
objectname
- the name to associate with the given colorjava.lang.IllegalArgumentException
- if there is already
a name => color association involving either c
or name
public NamedColor(int r, int g, int b)
r
- the red componentg
- the green componentb
- the blue componentColor.Color(int, int, int)
public NamedColor(int r, int g, int b, java.lang.String name)
r
- the red componentg
- the green componentb
- the blue componentname
- the name to associate with the given colorjava.lang.IllegalArgumentException
- if there is already
a name => color association involving either c
or name
public NamedColor(int r, int g, int b, int a)
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha componentColor.Color(int, int, int, int)
public NamedColor(int r, int g, int b, int a, java.lang.String name)
r
- the red componentg
- the green componentb
- the blue componenta
- the alpha componentname
- the name to associate with the given colorjava.lang.IllegalArgumentException
- if there is already
a name => color association involving either c
or name
public static void setNameFor(java.awt.Color c, java.lang.String name)
name
- the name to associate with the color c
c
- the colorjava.lang.IllegalArgumentException
- if there is already
a name => color association involving either c
or name
public static void changeNameOrColor(java.lang.String name, java.awt.Color c)
name
or
c
no longer exists.name
- the name of the colorc
- the colorpublic static boolean hasNameFor(java.awt.Color c)
c
- the colortrue
if there is an associated name,
otherwise false
public static boolean hasColorFor(java.lang.String name)
name
- the name of the colortrue
if there is an associated color,
otherwise false
public static java.lang.String getNameFor(java.awt.Color c)
c
- a colorcolor
;
null if there is no such associationpublic static NamedColor getNamedColor(java.lang.String name)
name
- a color namename
;
null if there is no such associationpublic static java.util.Set<java.lang.String> getAllColorNames()
public static java.util.Set<NamedColor> getAllNamedColors()
public static NamedColor getRandomColor()
NamedColor
value.public static NamedColor getRandomColor(int alpha)
NamedColor
value with the
specified transparency level.alpha
- the transparency level to use, in the range
of 0 to 255 where 255 is completely opaquepublic static NamedColor getRandomAlphaColor()
NamedColor
value. Colors
returned by this method have a random alpha (transparency) component,
as well as random red, green, and blue components.public static NamedColor getRandomNamedColor()
NamedColor
value.public static NamedColor getRandomNamedColor(int alpha)
NamedColor
value with the
specified transparency level.alpha
- the transparency level to use, in the range
of 0 to 255 where 255 is completely opaquepublic void setName(java.lang.String name)
name
- the name to associate with this colorjava.lang.IllegalArgumentException
- if there is already
a name => color association involving either c
or name
public void changeName(java.lang.String name)
name
was previously associated with any other color,
that association no longer exists.name
- the name of this colorpublic java.lang.String getName()
NamedColor
object;
null if there is no such associationpublic java.lang.String getRGBRepresentation()
java.util.Color.toString
method).public java.lang.String toString()
java.awt.Color.toString
.toString
in class java.awt.Color
String
representation of the colorpublic java.lang.String toString(int printMode)
java.awt.Color
representation of
the color componentsNamedColor
(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 colorprintMode
- the type of String
to generateString
representation of the color