Back to index

moin  1.9.0~rc2
ColorMap.java
Go to the documentation of this file.
00001 /*
00002  * @(#)ColorMap.java 5.1
00003  *
00004  */
00005 
00006 package CH.ifa.draw.util;
00007 
00008 import java.awt.Color;
00009 import java.util.*;
00010 
00011 class ColorEntry {
00012     public String    fName;
00013     public Color     fColor;
00014 
00015     ColorEntry(String name, Color color) {
00016        fColor = color;
00017        fName = name;
00018     }
00019 }
00020 
00027 public  class ColorMap
00028     extends Object {
00029 
00030     static ColorEntry defaultMap[] = {
00031        // there is no support for alpha values so we use a special value
00032        // to represent a transparent color
00033        new ColorEntry("None",          new Color(0xFFC79E)),
00034        new ColorEntry("White",         Color.white),
00035        new ColorEntry("Light Gray",    Color.lightGray),
00036        new ColorEntry("Gray",          Color.gray),
00037        new ColorEntry("Dark Gray",     Color.darkGray),
00038        new ColorEntry("Black",         Color.black),
00039        new ColorEntry("Blue",          Color.blue),
00040        new ColorEntry("Cyan",             Color.cyan),
00041        new ColorEntry("Green",         Color.green),
00042        new ColorEntry("Magenta",       Color.magenta),
00043        new ColorEntry("Orange",        Color.orange),
00044        new ColorEntry("Pink",          Color.pink),
00045        new ColorEntry("Red",           Color.red),
00046        new ColorEntry("Yellow",        Color.yellow),
00047     };
00048 
00049     static ColorMap colorMap = null;
00050 
00051     Vector fMap = null;
00052 
00053     ColorMap() {
00054        fMap = new Vector();
00055        for (int i = 0; i < defaultMap.length; i++)
00056            addColor(defaultMap[i]);
00057     }
00058 
00059     public static void reset() {
00060        colorMap = null;
00061     }
00062 
00063     public static ColorMap getColorMap() {
00064        if (colorMap == null)
00065            colorMap = new ColorMap();
00066        return colorMap;
00067     }
00068 
00069     public int size() {
00070        return fMap.size();
00071     }
00072 
00073     private void addColor(ColorEntry color) {
00074        fMap.addElement(color);
00075     }
00076 
00077     public void addColor(String name, Color colour) {
00078        addColor(new ColorEntry(name, colour));
00079     }
00080 
00081     public Color color(int index) {
00082        if (index < size() && index >= 0)
00083            return ((ColorEntry)fMap.elementAt(index)).fColor;
00084        
00085        throw new ArrayIndexOutOfBoundsException("Color index: " + index);
00086     }
00087 
00088     public Color color(String name) {
00089        Enumeration e = fMap.elements();
00090        while (e.hasMoreElements()) {
00091            ColorEntry ce = (ColorEntry)e.nextElement();
00092             if (ce.fName.equals(name))
00093                 return ce.fColor;
00094        }
00095        return Color.black;
00096     }
00097 
00098     public String name(int index) {
00099        if (index < size() && index >= 0)
00100            return ((ColorEntry)fMap.elementAt(index)).fName;
00101        
00102        throw new ArrayIndexOutOfBoundsException("Color index: " + index);
00103     }
00104 
00105     public int colorIndex(Color color) {
00106        Enumeration e = fMap.elements();
00107        int i = 0;
00108        while (e.hasMoreElements()) {
00109            ColorEntry ce = (ColorEntry)e.nextElement();
00110             if (ce.fColor.equals(color))
00111                 return i;
00112            i++;
00113        }
00114         return 0;
00115     }
00116 
00117     public boolean isTransparent(Color color) {
00118         return color.equals(color("None"));
00119     }
00120 }