package com.antilia.web.roundpane;
import java.awt.Color;
import java.util.TreeMap;
/**
*
*/
public class ColorMapper {
public static TreeMap<String, String> colors;
static {
colors = new TreeMap<String, String>();
colors.put("aliceblue", "f0f8ff");
colors.put("antiquewhite", "faebd7");
colors.put("aquamarine", "7fffd4");
colors.put("azure", "f0ffff");
colors.put("aqua", "00ffff");
colors.put("brown", "a52a2a");
colors.put("blueviolet", "8a2be2");
colors.put("blue", "0000ff");
colors.put("blanchedalmond", "ffebcd");
colors.put("black", "000000");
colors.put("bisque", "ffe4c4");
colors.put("beige", "f5f5dc");
colors.put("burlywood", "deb887");
colors.put("cyan", "00ffff");
colors.put("crimson", "dc143c");
colors.put("cornsilk", "fff8dc");
colors.put("cornflowerblue", "6495ed");
colors.put("coral", "ff7f50");
colors.put("chocolate", "d2691e");
colors.put("chartreuse", "7fff00");
colors.put("cadetblue", "5f9ea0");
colors.put("darkviolet", "9400d3");
colors.put("darkturquoise", "00ced1");
colors.put("darkslategray", "2f4f4f");
colors.put("darkslateblue", "483d8b");
colors.put("darkseagreen", "8fbc8f");
colors.put("darksalmon", "e9967a");
colors.put("darkred", "8b0000");
colors.put("darkorchid", "9932cc");
colors.put("darkorange", "ff8c00");
colors.put("darkolivegreen", "556b2f");
colors.put("darkmagenta", "8b008b");
colors.put("darkkhaki", "bdb76b");
colors.put("darkgreen", "006400");
colors.put("darkgray", "a9a9a9");
colors.put("darkgoldenrod", "b8860b");
colors.put("darkcyan", "008b8b");
colors.put("darkblue", "00008b");
colors.put("dodgerblue", "1e90ff");
colors.put("dimgray", "696969");
colors.put("deepskyblue", "00bfff");
colors.put("deeppink", "ff1493");
colors.put("fuchsia", "ff00ff");
colors.put("forestgreen", "228b22");
colors.put("floralwhite", "fffaf0");
colors.put("firebrick", "b22222");
colors.put("greenyellow", "adff2f");
colors.put("green", "008000");
colors.put("gray", "808080");
colors.put("goldenrod", "daa520");
colors.put("gold", "ffd700");
colors.put("ghostwhite", "f8f8ff");
colors.put("gainsboro", "dcdcdc");
colors.put("hotpink"," ff69b4");
colors.put("honeydew", "f0fff0");
colors.put("ivory", "fffff0");
colors.put("indigo", "4b0082");
colors.put("indianred", "cd5c5c");
colors.put("khaki", "f0e68c");
colors.put("linen", "faf0e6");
colors.put("limegreen", "32cd32");
colors.put("lime", "00ff00");
colors.put("lightyellow", "ffffe0");
colors.put("lightsteelblue", "b0c4de");
colors.put("lightslategray", "778899");
colors.put("lightskyblue", "87cefa");
colors.put("lightseagreen", "20b2aa");
colors.put("lightsalmon", "ffa07a");
colors.put("lightpink", "ffb6c1");
colors.put("lightgray", "d3d3d3");
colors.put("lightgreen", "90ee90");
colors.put("lightgoldenrodyellow", "fafad2");
colors.put("lightcyan", "e0ffff");
colors.put("lightcoral", "f08080");
colors.put("lightblue", "add8e6");
colors.put("lemonchiffon", "fffacd");
colors.put("lawngreen", "7cfc00");
colors.put("lavenderblush", "fff0f5");
colors.put("lavender"," e6e6fa");
colors.put("midnightblue", "191970");
colors.put("mediumvioletred", "c71585");
colors.put("mediumturquoise", "48d1cc");
colors.put("mediumspringgreen", "00fa9a");
colors.put("mediumslateblue", "7b68ee");
colors.put("mediumseagreen", "3cb371");
colors.put("mediumpurple", "9370db");
colors.put("mediumorchid", "ba55d3");
colors.put("mediumblue", "0000cd");
colors.put("mediumaquamarine", "66cdaa");
colors.put("maroon", "800000");
colors.put("magenta", "ff00ff");
colors.put("moccasin", "ffe4b5");
colors.put("mistyrose", "ffe4e1");
colors.put("mintcream", "f5fffa");
colors.put("navy", "000080");
colors.put("navajowhite", "ffdead");
colors.put("orchid", "da70d6");
colors.put("orangered", "ff4500");
colors.put("orange", "ffa500");
colors.put("olivedrab", "6b8e23");
colors.put("olive", "808000");
colors.put("oldlace", "fdf5e6");
colors.put("purple", "800080");
colors.put("powderblue", "b0e0e6");
colors.put("plum", "dda0dd");
colors.put("pink", "ffc0cb");
colors.put("peru", "cd853f");
colors.put("peachpuff", "ffdab9");
colors.put("papayawhip", "ffefd5");
colors.put("palevioletred", "db7093");
colors.put("paleturquoise", "afeeee");
colors.put("palegreen", "98fb98");
colors.put("palegoldenrod", "eee8aa");
colors.put("royalblue", "4169e1");
colors.put("rosybrown", "bc8f8f");
colors.put("red", "ff0000");
colors.put("springgreen", "00ff7f");
colors.put("snow", "fffafa");
colors.put("slategray", "708090");
colors.put("slateblue", "6a5acd");
colors.put("skyblue", "87ceeb");
colors.put("silver", "c0c0c0");
colors.put("sienna", "a0522d");
colors.put("seashell", "fff5ee");
colors.put("seagreen", "2e8b57");
colors.put("sandybrown", "f4a460");
colors.put("salmon", "fa8072");
colors.put("saddlebrown", "8b4513");
colors.put("steelblue", "4682b4");
colors.put("turquoise", "40e0d0");
colors.put("tomato", "ff6347");
colors.put("thistle", "d8bfd8");
colors.put("teal", "008080");
colors.put("tan", "d2b48c");
colors.put("whitesmoke", "f5f5f5");
colors.put("white", "ffffff");
colors.put("wheat", "f5deb3");
colors.put("violet", "ee82ee");
colors.put("yellowgreen", "9acd32");
colors.put("yellow", "ffff00");
}
/**
* The constructor.
*/
public ColorMapper() {
super();
}
/**
* Maps a string to a Color. It accepts either the name of the color
* like (red, white, yellow, etc) or a string of the form RGB(int,int,int)
* (or rgb(int,int,int) or just a string representing an integer in
* hexagecimal format (e.g. #ffffff). If no color can be constructed then
* null is returned.
* @param name The name of the color (or a rgb value).
* @return The corresponding color (or null if none is found).
*/
public static Color mapColor(String name) {
return mapColor(name, null);
}
/**
* See @see #mapColor(String). Instead of returning null it returns defaultColor.
*
* @param name
* @param defaultColor
* @return
*/
public static Color mapColor(String name, Color defaultColor) {
if (name==null || name.trim().length()==0)
return defaultColor;
String toTest = name.trim();
if (toTest.startsWith("RGB(") || toTest.startsWith("rgb(")) {
Color color = parseRGB(toTest.trim());
if(color != null)
return color;
return defaultColor;
} else if (toTest.startsWith("#")) {
String integer = toTest.substring(1);
try {
// Parses the hex number and returns a color.
Color color = new Color(Integer.parseInt(integer, 16));
if(color != null)
return color;
return defaultColor;
} catch ( NumberFormatException e) {
return defaultColor;
}
}
// If we arrive here we try to resolve the color by name.
Color color = getColorFromName(toTest);
if(color != null)
return color;
return defaultColor;
}
/**
* Parses a string of the form RGB(int,int,int) (or rgb(int,int,int) )
* into a Color. Returns null if the parsing fails.
* @param rgb
* @return
*/
public static Color parseRGB(String rgb) {
if ( rgb == null)
return null;
if (rgb.startsWith("RGB(") || rgb.startsWith("rgb(")) {
int index = rgb.indexOf('(');
if (index < 0 || index >= rgb.length())
return null;
String temp = rgb.substring(index+1);
index = temp.indexOf(',');
if ( index < 0 || index >= temp.length())
return null;
String r = temp.substring(0, index);
temp = temp.substring(index+1);
index = temp.indexOf(',');
if (index < 0 || index >= temp.length())
return null;
String g = temp.substring(0, index);
temp = temp.substring(index+1);
index = temp.indexOf(')');
if (index < 0)
return null;
String b = temp.substring(0, index);
try {
int ir = Integer.parseInt(r);
int ig = Integer.parseInt(g);
int ib = Integer.parseInt(b);
return new Color(ir,ig,ib);
} catch (NumberFormatException e) {
return null;
}
}
return null;
}
/**
* Tries to return a named color (if the name does not corresponds to
* a color it returns null).
* @param name The name of the color,
* @return
*/
public static Color getColorFromName(String name) {
String colorValue = (String)colors.get(name.toLowerCase());
if ( colorValue == null )
return null;
try {
return new Color(Integer.parseInt(colorValue,16));
} catch (NumberFormatException e) {
return null;
}
}
/**
* Returns a random color.
*
* @return
*/
public static Color randomColor() {
int red = (int)((255)*Math.random())%255;
int green = (int)((255)*Math.random())%255;
int blue = (int)((255)*Math.random())%255;
return new Color(red,green,blue);
}
}
|