001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/chat/tags/sakai_2-4-1/chat-tool/tool/src/java/org/sakaiproject/chat/tool/ColorMapper.java $
003: * $Id: ColorMapper.java 8206 2006-04-24 19:40:15Z ggolden@umich.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.chat.tool;
021:
022: import java.lang.reflect.Array;
023: import java.util.Hashtable;
024: import java.util.Map;
025:
026: /**
027: * <p>ColorMapper is a wrapper for a Hashtable that maps user names (or any set of Strings) to colors.</p>
028: * <p>The colors are standard names for HTML colors.</p>
029: */
030: public class ColorMapper {
031: // The index of the next color in the COLORS array that will be assigned to a name
032: protected int m_next = 0;
033:
034: // A mapping of names to colors
035: protected Map m_map;
036:
037: // An array of Strings representing standard HTML colors.
038: protected static final String[] COLORS = { "red", "blue", "green",
039: "orange", "firebrick", "teal", "goldenrod", "darkgreen",
040: "darkviolet", "lightslategray", "peru", "deeppink",
041: "dodgerblue", "limegreen", "rosybrown", "cornflowerblue",
042: "crimson", "turquoise", "darkorange", "blueviolet",
043: "royalblue", "brown", "magenta", "olive", "saddlebrown",
044: "purple", "coral", "mediumslateblue", "sienna",
045: "mediumturquoise", "hotpink", "lawngreen",
046: "mediumvioletred", "slateblue", "indianred", "slategray",
047: "indigo", "darkcyan", "springgreen", "darkgoldenrod",
048: "steelblue", "darkgray", "orchid", "darksalmon", "lime",
049: "gold", "darkturquoise", "navy", "orangered", "darkkhaki",
050: "darkmagenta", "darkolivegreen", "tomato", "aqua",
051: "darkred", "olivedrab" };
052:
053: // the size of the COLORS array
054: protected static final int NumColors = Array.getLength(COLORS);
055:
056: /**
057: * Construct the ColorMapper.
058: */
059: public ColorMapper() {
060: m_map = new Hashtable();
061:
062: } // ColorMapper
063:
064: /**
065: * get the color associated with a name. if name not already associated with a color,
066: * make a new association and determine the next color that will be used.
067: */
068: public String getColor(String name) {
069: String color;
070: if (m_map.containsKey(name)) {
071: color = (String) m_map.get(name);
072: } else {
073: color = COLORS[m_next++];
074: m_map.put(name, color);
075: if (m_next >= NumColors) {
076: m_next = 0;
077: }
078: }
079:
080: return color;
081:
082: } // getColor
083:
084: /**
085: * Returns the mapping of names to colors.
086: */
087: public Map getMapping() {
088: return m_map;
089:
090: } // getColors
091:
092: /**
093: * Returns the index of the next color in the COLORS array that will be assigned to a name.
094: */
095: public int getNext() {
096: return m_next;
097:
098: } // getNext
099:
100: /**
101: * Returns the entire array of color names.
102: */
103: public String[] getColors() {
104: return COLORS;
105:
106: } // getColors
107:
108: /**
109: * Returns the size of the array of color names.
110: */
111: public int getNum_colors() {
112: return NumColors;
113:
114: } // getNum_colors
115:
116: } // ColorMapper
|