ColorUtil

From EggeWiki
Jump to: navigation, search
/**
 * allows access to named colors, in addition to parsing color tuples.  
 * Minimizes memory usage by reusing color objects.
 * 
 * <a href="http://www.oreilly.com/catalog/wdnut/excerpt/color_names.html">View colors and their names</a>
 * 
 * <p>This class was generated from 'http://www.colorschemer.com/blog/2007/07/24/140-named-colors/' and
 * 'gawk -F\   '{ print "colors.put(\""$1"\", new Color("$3"));" }' colors.txt'.</p>
 * 
 * @author brianegge
 */
public class ColorUtil {
 
	private static final Map SYSTEM_COLORS = new HashMap();
	private static final Map userColors = Collections.synchronizedMap(new WeakHashMap());
	static {
		SYSTEM_COLORS.put("lightcoral", new Color(240, 128, 128));
		SYSTEM_COLORS.put("rosybrown", new Color(188, 143, 143));
		SYSTEM_COLORS.put("indianred", new Color(205, 92, 92));
		SYSTEM_COLORS.put("red", new Color(255, 0, 0));
		SYSTEM_COLORS.put("firebrick", new Color(178, 34, 34));
		SYSTEM_COLORS.put("brown", new Color(165, 42, 42));
		SYSTEM_COLORS.put("darkred", new Color(139, 0, 0));
		SYSTEM_COLORS.put("maroon", new Color(128, 0, 0));
		SYSTEM_COLORS.put("mistyrose", new Color(255, 228, 225));
		SYSTEM_COLORS.put("salmon", new Color(250, 128, 114));
		SYSTEM_COLORS.put("tomato", new Color(255, 99, 71));
		SYSTEM_COLORS.put("darksalmon", new Color(233, 150, 122));
		SYSTEM_COLORS.put("coral", new Color(255, 127, 80));
		SYSTEM_COLORS.put("orangered", new Color(255, 69, 0));
		SYSTEM_COLORS.put("lightsalmon", new Color(255, 160, 122));
		SYSTEM_COLORS.put("sienna", new Color(160, 82, 45));
		SYSTEM_COLORS.put("seashell", new Color(255, 245, 238));
		SYSTEM_COLORS.put("chocolate", new Color(210, 105, 30));
		SYSTEM_COLORS.put("saddlebrown", new Color(139, 69, 19));
		SYSTEM_COLORS.put("sandybrown", new Color(244, 164, 96));
		SYSTEM_COLORS.put("peachpuff", new Color(255, 218, 185));
		SYSTEM_COLORS.put("peru", new Color(205, 133, 63));
		SYSTEM_COLORS.put("linen", new Color(250, 240, 230));
		SYSTEM_COLORS.put("bisque", new Color(255, 228, 196));
		SYSTEM_COLORS.put("darkorange", new Color(255, 140, 0));
		SYSTEM_COLORS.put("burlywood", new Color(222, 184, 135));
		SYSTEM_COLORS.put("antiquewhite", new Color(250, 235, 215));
		SYSTEM_COLORS.put("tan", new Color(210, 180, 140));
		SYSTEM_COLORS.put("navajowhite", new Color(255, 222, 173));
		SYSTEM_COLORS.put("blanchedalmond", new Color(255, 235, 205));
		SYSTEM_COLORS.put("papayawhip", new Color(255, 239, 213));
		SYSTEM_COLORS.put("moccasin", new Color(255, 228, 181));
		SYSTEM_COLORS.put("orange", new Color(255, 165, 0));
		SYSTEM_COLORS.put("wheat", new Color(245, 222, 179));
		SYSTEM_COLORS.put("oldlace", new Color(253, 245, 230));
		SYSTEM_COLORS.put("floralwhite", new Color(255, 250, 240));
		SYSTEM_COLORS.put("darkgoldenrod", new Color(184, 134, 11));
		SYSTEM_COLORS.put("goldenrod", new Color(218, 165, 32));
		SYSTEM_COLORS.put("cornsilk", new Color(255, 248, 220));
		SYSTEM_COLORS.put("gold", new Color(255, 215, 0));
		SYSTEM_COLORS.put("lemonchiffon", new Color(255, 250, 205));
		SYSTEM_COLORS.put("khaki", new Color(240, 230, 140));
		SYSTEM_COLORS.put("palegoldenrod", new Color(238, 232, 170));
		SYSTEM_COLORS.put("darkkhaki", new Color(189, 183, 107));
		SYSTEM_COLORS.put("ivory", new Color(255, 255, 240));
		SYSTEM_COLORS.put("lightyellow", new Color(255, 255, 224));
		SYSTEM_COLORS.put("beige", new Color(245, 245, 220));
		SYSTEM_COLORS.put("lightgoldenrodyellow", new Color(250, 250, 210));
		SYSTEM_COLORS.put("yellow", new Color(255, 255, 0));
		SYSTEM_COLORS.put("olive", new Color(128, 128, 0));
		SYSTEM_COLORS.put("olivedrab", new Color(107, 142, 35));
		SYSTEM_COLORS.put("yellowgreen", new Color(154, 205, 50));
		SYSTEM_COLORS.put("darkolivegreen", new Color(85, 107, 47));
		SYSTEM_COLORS.put("greenyellow", new Color(173, 255, 47));
		SYSTEM_COLORS.put("chartreuse", new Color(127, 255, 0));
		SYSTEM_COLORS.put("lawngreen", new Color(124, 252, 0));
		SYSTEM_COLORS.put("darkseagreen", new Color(143, 188, 139));
		SYSTEM_COLORS.put("honeydew", new Color(240, 255, 240));
		SYSTEM_COLORS.put("palegreen", new Color(152, 251, 152));
		SYSTEM_COLORS.put("lightgreen", new Color(144, 238, 144));
		SYSTEM_COLORS.put("lime", new Color(0, 255, 0));
		SYSTEM_COLORS.put("limegreen", new Color(50, 205, 50));
		SYSTEM_COLORS.put("forestgreen", new Color(34, 139, 34));
		SYSTEM_COLORS.put("green", new Color(0, 128, 0));
		SYSTEM_COLORS.put("darkgreen", new Color(0, 100, 0));
		SYSTEM_COLORS.put("seagreen", new Color(46, 139, 87));
		SYSTEM_COLORS.put("mediumseagreen", new Color(60, 179, 113));
		SYSTEM_COLORS.put("springgreen", new Color(0, 255, 127));
		SYSTEM_COLORS.put("mintcream", new Color(245, 255, 250));
		SYSTEM_COLORS.put("mediumspringgreen", new Color(0, 250, 154));
		SYSTEM_COLORS.put("mediumaquamarine", new Color(102, 205, 170));
		SYSTEM_COLORS.put("aquamarine", new Color(127, 255, 212));
		SYSTEM_COLORS.put("turquoise", new Color(64, 224, 208));
		SYSTEM_COLORS.put("lightseagreen", new Color(32, 178, 170));
		SYSTEM_COLORS.put("mediumturquoise", new Color(72, 209, 204));
		SYSTEM_COLORS.put("azure", new Color(240, 255, 255));
		SYSTEM_COLORS.put("lightcyan", new Color(224, 255, 255));
		SYSTEM_COLORS.put("paleturquoise", new Color(175, 238, 238));
		SYSTEM_COLORS.put("aqua", new Color(0, 255, 255));
		SYSTEM_COLORS.put("cyan", new Color(0, 255, 255));
		SYSTEM_COLORS.put("darkcyan", new Color(0, 139, 139));
		SYSTEM_COLORS.put("teal", new Color(0, 128, 128));
		SYSTEM_COLORS.put("darkslategray", new Color(47, 79, 79));
		SYSTEM_COLORS.put("darkturquoise", new Color(0, 206, 209));
		SYSTEM_COLORS.put("cadetblue", new Color(95, 158, 160));
		SYSTEM_COLORS.put("powderblue", new Color(176, 224, 230));
		SYSTEM_COLORS.put("lightblue", new Color(173, 216, 230));
		SYSTEM_COLORS.put("deepskyblue", new Color(0, 191, 255));
		SYSTEM_COLORS.put("skyblue", new Color(135, 206, 235));
		SYSTEM_COLORS.put("lightskyblue", new Color(135, 206, 250));
		SYSTEM_COLORS.put("steelblue", new Color(70, 130, 180));
		SYSTEM_COLORS.put("aliceblue", new Color(240, 248, 255));
		SYSTEM_COLORS.put("dodgerblue", new Color(30, 144, 255));
		SYSTEM_COLORS.put("lightslategray", new Color(119, 136, 153));
		SYSTEM_COLORS.put("slategray", new Color(112, 128, 144));
		SYSTEM_COLORS.put("lightsteelblue", new Color(176, 196, 222));
		SYSTEM_COLORS.put("cornflowerblue", new Color(100, 149, 237));
		SYSTEM_COLORS.put("royalblue", new Color(65, 105, 225));
		SYSTEM_COLORS.put("ghostwhite", new Color(248, 248, 255));
		SYSTEM_COLORS.put("lavender", new Color(230, 230, 250));
		SYSTEM_COLORS.put("blue", new Color(0, 0, 255));
		SYSTEM_COLORS.put("mediumblue", new Color(0, 0, 205));
		SYSTEM_COLORS.put("darkblue", new Color(0, 0, 139));
		SYSTEM_COLORS.put("midnightblue", new Color(25, 25, 112));
		SYSTEM_COLORS.put("navy", new Color(0, 0, 128));
		SYSTEM_COLORS.put("slateblue", new Color(106, 90, 205));
		SYSTEM_COLORS.put("darkslateblue", new Color(72, 61, 139));
		SYSTEM_COLORS.put("mediumslateblue", new Color(123, 104, 238));
		SYSTEM_COLORS.put("mediumpurple", new Color(147, 112, 219));
		SYSTEM_COLORS.put("blueviolet", new Color(138, 43, 226));
		SYSTEM_COLORS.put("indigo", new Color(75, 0, 130));
		SYSTEM_COLORS.put("darkorchid", new Color(153, 50, 204));
		SYSTEM_COLORS.put("darkviolet", new Color(148, 0, 211));
		SYSTEM_COLORS.put("mediumorchid", new Color(186, 85, 211));
		SYSTEM_COLORS.put("thistle", new Color(216, 191, 216));
		SYSTEM_COLORS.put("plum", new Color(221, 160, 221));
		SYSTEM_COLORS.put("violet", new Color(238, 130, 238));
		SYSTEM_COLORS.put("fuchsia", new Color(255, 0, 255));
		SYSTEM_COLORS.put("magenta", new Color(255, 0, 255));
		SYSTEM_COLORS.put("darkmagenta", new Color(139, 0, 139));
		SYSTEM_COLORS.put("purple", new Color(128, 0, 128));
		SYSTEM_COLORS.put("orchid", new Color(218, 112, 214));
		SYSTEM_COLORS.put("mediumvioletred", new Color(199, 21, 133));
		SYSTEM_COLORS.put("deeppink", new Color(255, 20, 147));
		SYSTEM_COLORS.put("hotpink", new Color(255, 105, 180));
		SYSTEM_COLORS.put("lavenderblush", new Color(255, 240, 245));
		SYSTEM_COLORS.put("palevioletred", new Color(219, 112, 147));
		SYSTEM_COLORS.put("crimson", new Color(220, 20, 60));
		SYSTEM_COLORS.put("pink", new Color(255, 192, 203));
		SYSTEM_COLORS.put("lightpink", new Color(255, 182, 193));
		SYSTEM_COLORS.put("white", new Color(255, 255, 255));
		SYSTEM_COLORS.put("snow", new Color(255, 250, 250));
		SYSTEM_COLORS.put("whitesmoke", new Color(245, 245, 245));
		SYSTEM_COLORS.put("gainsboro", new Color(220, 220, 220));
		SYSTEM_COLORS.put("lightgray", new Color(211, 211, 211));
		SYSTEM_COLORS.put("silver", new Color(192, 192, 192));
		SYSTEM_COLORS.put("darkgray", new Color(169, 169, 169));
		SYSTEM_COLORS.put("gray", new Color(128, 128, 128));
		SYSTEM_COLORS.put("dimgray", new Color(105, 105, 105));
		SYSTEM_COLORS.put("black", new Color(0, 0, 0));
	}
 
	public static Color getColor(String color) {
		if (color == null)
			return null;
		Color c = (Color) SYSTEM_COLORS.get(color.toLowerCase());
		if (c != null)
			return c;
		c = (Color) userColors.get(color);
		if (c != null)
			return c;
		String[] rgb = color.split(",");
		if (rgb.length != 3)
			throw new IllegalArgumentException("Invalid color " + color);
		c = new Color(Integer.parseInt(rgb[0]), Integer.parseInt(rgb[1]), Integer.parseInt(rgb[2]));
		userColors.put(color, c);
		return c;
	}
}
Personal tools