org.ceryle.ui
Class ColorLookupTable

java.lang.Object
  extended by org.ceryle.util.VectorMap
      extended by org.ceryle.ui.ColorLookupTable
All Implemented Interfaces:
Map

public class ColorLookupTable
extends VectorMap

Extends HashMap, creating a color lookup table that can be loaded from a text or XTM document. The text format is simple: '#' at start of line is a comment, otherwise first token is ID followed by whitespace, followed by a color spec. For XTM files, the order of the <topic> elements in the file is significant, and determines the iteration order of the table (e.g., when used to create menus). As a lookup table, of course, order is insignificant.

The lookup() method is a synonym for HashMap's get() method, and includes a bit of a trick: if the lookup key begins with a "#", the key is interpreted as a hex color specification (e.g., "#df7e82") and processed accordingly. If it contains a comma, it is interpreted as an RGB or RGBA color (e.g., "223,126,130" or "223,126,130,90") with values between 0-255.

This class also includes import and export methods from the XTM syntax. The syntax for the .xtm file is shown in the example below:

   <topicMap id="basic" xmlns="http://www.topicmaps.org/xtm/1.0/">
     <topic id="orange">
       <occurrence><resourceData>#ff6300</resourceData></occurrence>
     </topic>
     <topic id="red">
       <occurrence><resourceData>#de0800</resourceData></occurrence>
     </topic>
     <topic id="fuchsia">
       <occurrence><resourceData>#f70884</resourceData></occurrence>
     </topic>
   </topicMap>   

Noting that both the ColorLookupTable and each color have individual identifiers, provided by the IDs of the <topic> elements.

Since:
JDK1.4
Version:
$Id: ColorLookupTable.java,v 3.9 2007-06-15 12:09:35 altheim Exp $
Author:
Murray Altheim

Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry<K,V>
 
Field Summary
static String DEFAULT_CLUT_NAME
          The default CLUT name.
 
Fields inherited from class org.ceryle.util.VectorMap
m_map, m_vector
 
Constructor Summary
ColorLookupTable(String id)
          Constructor provided an identifier for this ColorLookupTable.
 
Method Summary
 Color get(String name)
          Returns the Color to which this ColorLookupTable maps the specified color name.
 String getColorName(int n)
          Returns the name of the Color as according to the order of the table.
 Color getColorNumber(int n)
          Returns the numbered Color as according to the order of the table.
 Color getFirstColor()
          Returns the first Color (in table order).
 String[] getHexColorSpecifications()
          Returns a String array containing the the hex color specifications in the same order as the originating list.
 String getID()
          Returns the ID of this ColorLookupTable.
 Color getLastColor()
          Returns the last Color (in table order).
 Iterator getNames()
          Returns the color names in the same order as the originating list.
 void load(String filename)
          Imports the text or XTM document at filename as a color lookup table.
 Color lookup(String name, Color failColor)
          Looks up a color (by name) from this ColorLookupTable, returning null if a value cannot be found.
 Object put(String id, Color color)
          Associates the specified ID with the specified Color in this CLUT.
 
Methods inherited from class org.ceryle.util.VectorMap
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keys, keySet, put, putAll, remove, size, values
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_CLUT_NAME

public static final String DEFAULT_CLUT_NAME
The default CLUT name.

See Also:
Constant Field Values
Constructor Detail

ColorLookupTable

public ColorLookupTable(String id)
Constructor provided an identifier for this ColorLookupTable.

Method Detail

getID

public String getID()
Returns the ID of this ColorLookupTable.


put

public Object put(String id,
                  Color color)
Associates the specified ID with the specified Color in this CLUT.

Returns:
the previous Color of the specified ID in this hashtable, or null if it did not have one.

getNames

public Iterator getNames()
Returns the color names in the same order as the originating list.


getHexColorSpecifications

public String[] getHexColorSpecifications()
Returns a String array containing the the hex color specifications in the same order as the originating list. E.g., "#FF0300", "#FFBE56", etc.


get

public Color get(String name)
Returns the Color to which this ColorLookupTable maps the specified color name. This provides the same lookup functionality as lookup(String,Color), with no frills, returning null on failure.


getFirstColor

public Color getFirstColor()
Returns the first Color (in table order).


getLastColor

public Color getLastColor()
Returns the last Color (in table order).


getColorNumber

public Color getColorNumber(int n)
Returns the numbered Color as according to the order of the table. A color number of less than zero returns null, and any value larger than the size of the table returns the last color in the table. As with most things in Java, the numbering starts with zero, not one.


getColorName

public String getColorName(int n)
Returns the name of the Color as according to the order of the table. A color number of less than zero returns "none", and any value larger than the size of the table returns the last color in the table. As with most things in Java, the numbering starts with zero, not one.


lookup

public Color lookup(String name,
                    Color failColor)
Looks up a color (by name) from this ColorLookupTable, returning null if a value cannot be found. If name is null, or the Color failColor is non-null, it will be returned on a lookup failure rather than null. This issues a warning message to the MessageHandler upon a failed lookup rather than throwing an exception (because of the availability of the fail color).


load

public void load(String filename)
Imports the text or XTM document at filename as a color lookup table. This will clear any existing values in the table. Note that if the imported XTM document has an 'id' attribute on the document element, this value will overwrite any existing value for the ColorLookupTable's ID (i.e., the value returned by getID()).



The Ceryle Project. Copyright ©2001-2007 Murray Altheim, All Rights Reserved. See LICENSE included with distribution.