us.zuercher.gpx2map.plotter
Class Color

java.lang.Object
  extended by us.zuercher.gpx2map.plotter.Color

public class Color
extends Object

Color represents an RGB color. The static getColor(String) and getColors() methods obtain the default set of colors from a copy of rgb.txt taken from Netpbm 10.29.

The rgb.txt file may contain multiple names for a given RGB triplet. For example, "Black", "Gray0", and "Grey0" all map to (0, 0, 0). Also, rgb.txt sometimes contains multiple RGB triplets for a single color name. Examples include "Black" which appears twice as (0, 0, 0) and "Navy" which appears first as (0, 0, 142) and again as (0, 0, 128).

The static internal mapping of color name to Color object maintained by this class uses the first appearance of each color name, so requests for the color "Navy" will return a Color with an RGB value of (0, 0, 142). However, Color also maintains a static internal mapping of RGB value to Color objects. Both versions of "Navy" appear in this map.

Author:
Stephan Zuercher

Field Summary
private  int blue
          Blue value.
private  String colorName
          Color's name.
private static Map<String,Color> colors
          Map of color names to Colors.
private static Map<Integer,Color> colorsByValue
          Map of color values to Colors.
private  int green
          Green value.
private  int red
          Red value.
 
Constructor Summary
private Color(String colorName, int red, int green, int blue)
          Constructs a new Color object.
 
Method Summary
 boolean equalByColorValue(Color other)
          Tests to see if this Color has the same color value as another Color.
 boolean equals(Object other)
          Tests to see if this Color is the same as another Color by both name and RGB value.
 int getBlue()
          Returns the color's blue value.
static Color getColor(String colorName)
          Returns the Color associated with the given name.
 String getColorName()
          Returns the Color's name.
static Collection<Color> getColors()
          Returns an unordered collection of all known Color objects.
 int getGreen()
          Returns the color's green value.
 int getRed()
          Returns the color's red value.
 int hashCode()
          Computes the hashcode of the color's name.
private static void loadColors()
          Load the standard color name/value to Color mappings.
private static int makeColorValueKey(int red, int green, int blue)
          Computes a "color value key" for a given RGB value.
static void resetColors()
          Resets the color name/value to Color mappings to their startup defaults.
 String toString()
          Returns the color's name.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

colors

private static Map<String,Color> colors
Map of color names to Colors.


colorsByValue

private static Map<Integer,Color> colorsByValue
Map of color values to Colors.


colorName

private final String colorName
Color's name.


red

private final int red
Red value.


green

private final int green
Green value.


blue

private final int blue
Blue value.

Constructor Detail

Color

private Color(String colorName,
              int red,
              int green,
              int blue)
Constructs a new Color object. See the static initializer block.

Parameters:
colorName - color's name
red - red value
green - green value
blue - blue value
Method Detail

getColors

public static Collection<Color> getColors()
                                   throws ColorDataError
Returns an unordered collection of all known Color objects.

Returns:
an unordered collection of all known Color objects.
Throws:
ColorDataError - if there's an error loading color data from rgb.txt (only on first call or after resetColors())

getColor

public static Color getColor(String colorName)
                      throws ColorDataError
Returns the Color associated with the given name. The color's name is not case sensitive. If the color's name is in the form "#rrggbb", where rr, gg, and bb are hexadecimal values, the normal search by name occurs. If it fails, a search of existing Colors by RGB value is performed. If an existing Color is found with the provided RGB values, it is returned. Finally, if no existing Color is found by RGB value, a new Color object is constructed with the given name ("#rrggbb") and parsed RGB values.

Example results, assuming that getColor(String) is called repeatedly with each name. Note the unexpected behavior with respect to "Navy" in the last three examples (see class description for an explanation).

Name Result
Red rgb.txt Color("Red", 255, 0, 0)
red rgb.txt Color("Red", 255, 0, 0)
#ff0000 rgb.txt Color("Red", 255, 0, 0)
#FF0000 rgb.txt Color("Red", 255, 0, 0)
#00008D new Color ("#00008D", 0, 0, 141)
#00008d Color("#00008D", 0, 0, 141)
Navy rgb.txt Color("Navy", 0, 0, 142)
#00008e rgb.txt Color("Navy", 0, 0, 142)
#000080 rgb.txt Color("Navy", 0, 0, 128)

Parameters:
colorName - case-insensitive color name
Returns:
the associated Color or null if not found
Throws:
ColorDataError - if there's an error loading color data from rgb.txt (only on first call or after resetColors())

getColorName

public String getColorName()
Returns the Color's name.

Returns:
the Color's name.

getRed

public int getRed()
Returns the color's red value.

Returns:
the color's red value.

getGreen

public int getGreen()
Returns the color's green value.

Returns:
the color's green value.

getBlue

public int getBlue()
Returns the color's blue value.

Returns:
the color's blue value.

toString

public String toString()
Returns the color's name.

Overrides:
toString in class Object
Returns:
the color's name.

hashCode

public int hashCode()
Computes the hashcode of the color's name.

Overrides:
hashCode in class Object
Returns:
a hashcode of the color's name.

equals

public boolean equals(Object other)
Tests to see if this Color is the same as another Color by both name and RGB value. Note that two Colors with the same RGB values, but different names are not considered equal.

Overrides:
equals in class Object
Parameters:
other - another Color object
Returns:
true if the objects are equal (see above)
Throws:
ClassCastException - if other is not a Color
See Also:
equalByColorValue(Color)

equalByColorValue

public boolean equalByColorValue(Color other)
Tests to see if this Color has the same color value as another Color. The Colors' names are irrelevant for this comparison. This method returns true if the RGB values of this are identical to those of the other color.

Parameters:
other - another Color
Returns:
true if the RGB values are the same

resetColors

public static void resetColors()
Resets the color name/value to Color mappings to their startup defaults. In practice, the color mappings are not reloaded until getColor(String) or getColors() is called.


makeColorValueKey

private static int makeColorValueKey(int red,
                                     int green,
                                     int blue)
Computes a "color value key" for a given RGB value. Color value keys are used as a key into the map of Colors by color value.

Parameters:
red - red value
green - green value
blue - blue value
Returns:
the color value key for the given RGB triplet.

loadColors

private static void loadColors()
                        throws ColorDataError
Load the standard color name/value to Color mappings.

Throws:
ColorDataError - if there's an error loading Color data.