001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.ui.style;
029:
030: import java.util.*;
031: import java.util.regex.Matcher;
032: import java.util.regex.Pattern;
033:
034: /**
035: * @author Joshua J. Gertzen
036: */
037: public final class Color {
038: private static final Map<String, Color> NAMED_COLORS = new HashMap<String, Color>();
039: private static final Color[] VALUES;
040:
041: //Borrowed from SVG 1.0 Color Set
042: public static final Color ALICEBLUE = new Color("aliceblue", 240,
043: 248, 255);
044: public static final Color ANTIQUEWHITE = new Color("antiquewhite",
045: 250, 235, 215);
046: public static final Color AQUA = new Color("aqua", 0, 255, 255);
047: public static final Color AQUAMARINE = new Color("aquamarine", 127,
048: 255, 212);
049: public static final Color AZURE = new Color("azure", 240, 255, 255);
050: public static final Color BEIGE = new Color("beige", 245, 245, 220);
051: public static final Color BISQUE = new Color("bisque", 255, 228,
052: 196);
053: public static final Color BLACK = new Color("black", 0, 0, 0);
054: public static final Color BLANCHEDALMOND = new Color(
055: "blanchedalmond", 255, 235, 205);
056: public static final Color BLUE = new Color("blue", 0, 0, 255);
057: public static final Color BLUEVIOLET = new Color("blueviolet", 138,
058: 43, 226);
059: public static final Color BROWN = new Color("brown", 165, 42, 42);
060: public static final Color BURLYWOOD = new Color("burlywood", 222,
061: 184, 135);
062: public static final Color CADETBLUE = new Color("cadetblue", 95,
063: 158, 160);
064: public static final Color CHARTREUSE = new Color("chartreuse", 127,
065: 255, 0);
066: public static final Color CHOCOLATE = new Color("chocolate", 210,
067: 105, 30);
068: public static final Color CORAL = new Color("coral", 255, 127, 80);
069: public static final Color CORNFLOWERBLUE = new Color(
070: "cornflowerblue", 100, 149, 237);
071: public static final Color CORNSILK = new Color("cornsilk", 255,
072: 248, 220);
073: public static final Color CRIMSON = new Color("crimson", 220, 20,
074: 60);
075: public static final Color CYAN = new Color("cyan", 0, 255, 255);
076: public static final Color DARKBLUE = new Color("darkblue", 0, 0,
077: 139);
078: public static final Color DARKCYAN = new Color("darkcyan", 0, 139,
079: 139);
080: public static final Color DARKGOLDENROD = new Color(
081: "darkgoldenrod", 184, 134, 11);
082: public static final Color DARKGRAY = new Color("darkgray", 169,
083: 169, 169);
084: public static final Color DARKGREEN = new Color("darkgreen", 0,
085: 100, 0);
086: public static final Color DARKGREY = new Color("darkgrey", 169,
087: 169, 169);
088: public static final Color DARKKHAKI = new Color("darkkhaki", 189,
089: 183, 107);
090: public static final Color DARKMAGENTA = new Color("darkmagenta",
091: 139, 0, 139);
092: public static final Color DARKOLIVEGREEN = new Color(
093: "darkolivegreen", 85, 107, 47);
094: public static final Color DARKORANGE = new Color("darkorange", 255,
095: 140, 0);
096: public static final Color DARKORCHID = new Color("darkorchid", 153,
097: 50, 204);
098: public static final Color DARKRED = new Color("darkred", 139, 0, 0);
099: public static final Color DARKSALMON = new Color("darksalmon", 233,
100: 150, 122);
101: public static final Color DARKSEAGREEN = new Color("darkseagreen",
102: 143, 188, 143);
103: public static final Color DARKSLATEBLUE = new Color(
104: "darkslateblue", 72, 61, 139);
105: public static final Color DARKSLATEGRAY = new Color(
106: "darkslategray", 47, 79, 79);
107: public static final Color DARKSLATEGREY = new Color(
108: "darkslategrey", 47, 79, 79);
109: public static final Color DARKTURQUOISE = new Color(
110: "darkturquoise", 0, 206, 209);
111: public static final Color DARKVIOLET = new Color("darkviolet", 148,
112: 0, 211);
113: public static final Color DEEPPINK = new Color("deeppink", 255, 20,
114: 147);
115: public static final Color DEEPSKYBLUE = new Color("deepskyblue", 0,
116: 191, 255);
117: public static final Color DIMGRAY = new Color("dimgray", 105, 105,
118: 105);
119: public static final Color DIMGREY = new Color("dimgrey", 105, 105,
120: 105);
121: public static final Color DODGERBLUE = new Color("dodgerblue", 30,
122: 144, 255);
123: public static final Color FIREBRICK = new Color("firebrick", 178,
124: 34, 34);
125: public static final Color FLORALWHITE = new Color("floralwhite",
126: 255, 250, 240);
127: public static final Color FORESTGREEN = new Color("forestgreen",
128: 34, 139, 34);
129: public static final Color FUCHSIA = new Color("fuchsia", 255, 0,
130: 255);
131: public static final Color GAINSBORO = new Color("gainsboro", 220,
132: 220, 220);
133: public static final Color GHOSTWHITE = new Color("ghostwhite", 248,
134: 248, 255);
135: public static final Color GOLD = new Color("gold", 255, 215, 0);
136: public static final Color GOLDENROD = new Color("goldenrod", 218,
137: 165, 32);
138: public static final Color GRAY = new Color("gray", 128, 128, 128);
139: public static final Color GREY = new Color("grey", 128, 128, 128);
140: public static final Color GREEN = new Color("green", 0, 128, 0);
141: public static final Color GREENYELLOW = new Color("greenyellow",
142: 173, 255, 47);
143: public static final Color HONEYDEW = new Color("honeydew", 240,
144: 255, 240);
145: public static final Color HOTPINK = new Color("hotpink", 255, 105,
146: 180);
147: public static final Color INDIANRED = new Color("indianred", 205,
148: 92, 92);
149: public static final Color INDIGO = new Color("indigo", 75, 0, 130);
150: public static final Color IVORY = new Color("ivory", 255, 255, 240);
151: public static final Color KHAKI = new Color("khaki", 240, 230, 140);
152: public static final Color LAVENDER = new Color("lavender", 230,
153: 230, 250);
154: public static final Color LAVENDERBLUSH = new Color(
155: "lavenderblush", 255, 240, 245);
156: public static final Color LAWNGREEN = new Color("lawngreen", 124,
157: 252, 0);
158: public static final Color LEMONCHIFFON = new Color("lemonchiffon",
159: 255, 250, 205);
160: public static final Color LIGHTBLUE = new Color("lightblue", 173,
161: 216, 230);
162: public static final Color LIGHTCORAL = new Color("lightcoral", 240,
163: 128, 128);
164: public static final Color LIGHTCYAN = new Color("lightcyan", 224,
165: 255, 255);
166: public static final Color LIGHTGOLDENRODYELLOW = new Color(
167: "lightgoldenrodyellow", 250, 250, 210);
168: public static final Color LIGHTGRAY = new Color("lightgray", 211,
169: 211, 211);
170: public static final Color LIGHTGREEN = new Color("lightgreen", 144,
171: 238, 144);
172: public static final Color LIGHTGREY = new Color("lightgrey", 211,
173: 211, 211);
174: public static final Color LIGHTPINK = new Color("lightpink", 255,
175: 182, 193);
176: public static final Color LIGHTSALMON = new Color("lightsalmon",
177: 255, 160, 122);
178: public static final Color LIGHTSEAGREEN = new Color(
179: "lightseagreen", 32, 178, 170);
180: public static final Color LIGHTSKYBLUE = new Color("lightskyblue",
181: 135, 206, 250);
182: public static final Color LIGHTSLATEGRAY = new Color(
183: "lightslategray", 119, 136, 153);
184: public static final Color LIGHTSLATEGREY = new Color(
185: "lightslategrey", 119, 136, 153);
186: public static final Color LIGHTSTEELBLUE = new Color(
187: "lightsteelblue", 176, 196, 222);
188: public static final Color LIGHTYELLOW = new Color("lightyellow",
189: 255, 255, 224);
190: public static final Color LIME = new Color("lime", 0, 255, 0);
191: public static final Color LIMEGREEN = new Color("limegreen", 50,
192: 205, 50);
193: public static final Color LINEN = new Color("linen", 250, 240, 230);
194: public static final Color MAGENTA = new Color("magenta", 255, 0,
195: 255);
196: public static final Color MAROON = new Color("maroon", 128, 0, 0);
197: public static final Color MEDIUMAQUAMARINE = new Color(
198: "mediumaquamarine", 102, 205, 170);
199: public static final Color MEDIUMBLUE = new Color("mediumblue", 0,
200: 0, 205);
201: public static final Color MEDIUMORCHID = new Color("mediumorchid",
202: 186, 85, 211);
203: public static final Color MEDIUMPURPLE = new Color("mediumpurple",
204: 147, 112, 219);
205: public static final Color MEDIUMSEAGREEN = new Color(
206: "mediumseagreen", 60, 179, 113);
207: public static final Color MEDIUMSLATEBLUE = new Color(
208: "mediumslateblue", 123, 104, 238);
209: public static final Color MEDIUMSPRINGGREEN = new Color(
210: "mediumspringgreen", 0, 250, 154);
211: public static final Color MEDIUMTURQUOISE = new Color(
212: "mediumturquoise", 72, 209, 204);
213: public static final Color MEDIUMVIOLETRED = new Color(
214: "mediumvioletred", 199, 21, 133);
215: public static final Color MIDNIGHTBLUE = new Color("midnightblue",
216: 25, 25, 112);
217: public static final Color MINTCREAM = new Color("mintcream", 245,
218: 255, 250);
219: public static final Color MISTYROSE = new Color("mistyrose", 255,
220: 228, 225);
221: public static final Color MOCCASIN = new Color("moccasin", 255,
222: 228, 181);
223: public static final Color NAVAJOWHITE = new Color("navajowhite",
224: 255, 222, 173);
225: public static final Color NAVY = new Color("navy", 0, 0, 128);
226: public static final Color OLDLACE = new Color("oldlace", 253, 245,
227: 230);
228: public static final Color OLIVE = new Color("olive", 128, 128, 0);
229: public static final Color OLIVEDRAB = new Color("olivedrab", 107,
230: 142, 35);
231: public static final Color ORANGE = new Color("orange", 255, 165, 0);
232: public static final Color ORANGERED = new Color("orangered", 255,
233: 69, 0);
234: public static final Color ORCHID = new Color("orchid", 218, 112,
235: 214);
236: public static final Color PALEGOLDENROD = new Color(
237: "palegoldenrod", 238, 232, 170);
238: public static final Color PALEGREEN = new Color("palegreen", 152,
239: 251, 152);
240: public static final Color PALETURQUOISE = new Color(
241: "paleturquoise", 175, 238, 238);
242: public static final Color PALEVIOLETRED = new Color(
243: "palevioletred", 219, 112, 147);
244: public static final Color PAPAYAWHIP = new Color("papayawhip", 255,
245: 239, 213);
246: public static final Color PEACHPUFF = new Color("peachpuff", 255,
247: 218, 185);
248: public static final Color PERU = new Color("peru", 205, 133, 63);
249: public static final Color PINK = new Color("pink", 255, 192, 203);
250: public static final Color PLUM = new Color("plum", 221, 160, 221);
251: public static final Color POWDERBLUE = new Color("powderblue", 176,
252: 224, 230);
253: public static final Color PURPLE = new Color("purple", 128, 0, 128);
254: public static final Color RED = new Color("red", 255, 0, 0);
255: public static final Color ROSYBROWN = new Color("rosybrown", 188,
256: 143, 143);
257: public static final Color ROYALBLUE = new Color("royalblue", 65,
258: 105, 225);
259: public static final Color SADDLEBROWN = new Color("saddlebrown",
260: 139, 69, 19);
261: public static final Color SALMON = new Color("salmon", 250, 128,
262: 114);
263: public static final Color SANDYBROWN = new Color("sandybrown", 244,
264: 164, 96);
265: public static final Color SEAGREEN = new Color("seagreen", 46, 139,
266: 87);
267: public static final Color SEASHELL = new Color("seashell", 255,
268: 245, 238);
269: public static final Color SIENNA = new Color("sienna", 160, 82, 45);
270: public static final Color SILVER = new Color("silver", 192, 192,
271: 192);
272: public static final Color SKYBLUE = new Color("skyblue", 135, 206,
273: 235);
274: public static final Color SLATEBLUE = new Color("slateblue", 106,
275: 90, 205);
276: public static final Color SLATEGRAY = new Color("slategray", 112,
277: 128, 144);
278: public static final Color SLATEGREY = new Color("slategrey", 112,
279: 128, 144);
280: public static final Color SNOW = new Color("snow", 255, 250, 250);
281: public static final Color SPRINGGREEN = new Color("springgreen", 0,
282: 255, 127);
283: public static final Color STEELBLUE = new Color("steelblue", 70,
284: 130, 180);
285: public static final Color TAN = new Color("tan", 210, 180, 140);
286: public static final Color TEAL = new Color("teal", 0, 128, 128);
287: public static final Color THISTLE = new Color("thistle", 216, 191,
288: 216);
289: public static final Color TOMATO = new Color("tomato", 255, 99, 71);
290: public static final Color TURQUOISE = new Color("turquoise", 64,
291: 224, 208);
292: public static final Color VIOLET = new Color("violet", 238, 130,
293: 238);
294: public static final Color WHEAT = new Color("wheat", 245, 222, 179);
295: public static final Color WHITE = new Color("white", 255, 255, 255);
296: public static final Color WHITESMOKE = new Color("whitesmoke", 245,
297: 245, 245);
298: public static final Color YELLOW = new Color("yellow", 255, 255, 0);
299: public static final Color YELLOWGREEN = new Color("yellowgreen",
300: 154, 205, 50);
301:
302: //Borrowed from CSS 2.0 System Color Set
303: public static final Color ACTIVEBORDER = new Color("activeborder",
304: -1, -1, -1);
305: public static final Color ACTIVECAPTION = new Color(
306: "activecaption", -1, -1, -1);
307: public static final Color APPWORKSPACE = new Color("appworkspace",
308: -1, -1, -1);
309: public static final Color BACKGROUND = new Color("background", -1,
310: -1, -1);
311: public static final Color BUTTONFACE = new Color("buttonface", -1,
312: -1, -1);
313: public static final Color BUTTONHIGHLIGHT = new Color(
314: "buttonhighlight", -1, -1, -1);
315: public static final Color BUTTONSHADOW = new Color("buttonshadow",
316: -1, -1, -1);
317: public static final Color BUTTONTEXT = new Color("buttontext", -1,
318: -1, -1);
319: public static final Color CAPTIONTEXT = new Color("captiontext",
320: -1, -1, -1);
321: public static final Color GRAYTEXT = new Color("graytext", -1, -1,
322: -1);
323: public static final Color HIGHLIGHT = new Color("highlight", -1,
324: -1, -1);
325: public static final Color HIGHLIGHTTEXT = new Color(
326: "highlighttext", -1, -1, -1);
327: public static final Color INACTIVEBORDER = new Color(
328: "inactiveborder", -1, -1, -1);
329: public static final Color INACTIVECAPTION = new Color(
330: "inactivecaption", -1, -1, -1);
331: public static final Color INACTIVECAPTIONTEXT = new Color(
332: "inactivecaptiontext", -1, -1, -1);
333: public static final Color INFOBACKGROUND = new Color(
334: "infobackground", -1, -1, -1);
335: public static final Color INFOTEXT = new Color("infotext", -1, -1,
336: -1);
337: public static final Color MENU = new Color("menu", -1, -1, -1);
338: public static final Color MENUTEXT = new Color("menutext", -1, -1,
339: -1);
340: public static final Color SCROLLBAR = new Color("scrollbar", -1,
341: -1, -1);
342: public static final Color THREEDDARKSHADOW = new Color(
343: "threeddarkshadow", -1, -1, -1);
344: public static final Color THREEDFACE = new Color("threedface", -1,
345: -1, -1);
346: public static final Color THREEDHIGHLIGHT = new Color(
347: "threedhighlight", -1, -1, -1);
348: public static final Color THREEDLIGHTSHADOW = new Color(
349: "threedlightshadow", -1, -1, -1);
350: public static final Color THREEDSHADOW = new Color("threedshadow",
351: -1, -1, -1);
352: public static final Color WINDOW = new Color("window", -1, -1, -1);
353: public static final Color WINDOWFRAME = new Color("windowframe",
354: -1, -1, -1);
355: public static final Color WINDOWTEXT = new Color("windowtext", -1,
356: -1, -1);
357: public static final Color TRANSPARENT = new Color("transparent",
358: -1, -1, -1);
359: static {
360: VALUES = NAMED_COLORS.values().toArray(
361: new Color[NAMED_COLORS.size()]);
362: Arrays.sort(VALUES, new Comparator<Color>() {
363: public int compare(Color c1, Color c2) {
364: int o1 = c1.ordinal();
365: int o2 = c2.ordinal();
366:
367: if (o1 == o2) {
368: return 0;
369: } else if (o1 > o2) {
370: return 1;
371: } else {
372: return -1;
373: }
374: }
375: });
376: }
377:
378: private static final Pattern REGEX_RGB = Pattern
379: .compile("rgb[(]\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*,\\s*(\\d{1,3})\\s*[)]");
380: private static final Pattern REGEX_HEX = Pattern
381: .compile("#([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})");
382: private static int nextOrdinal = 0;
383:
384: public static final Color valueOf(String colorId) {
385: if (colorId.startsWith("rgb(")) {
386: Matcher m = REGEX_RGB.matcher(colorId);
387:
388: if (m.find()) {
389: int red = Integer.parseInt(m.group(1));
390: int green = Integer.parseInt(m.group(2));
391: int blue = Integer.parseInt(m.group(3));
392: if (red < 0 || red > 255 || green < 0 || green > 255
393: || blue < 0 || blue > 255)
394: throw new IllegalArgumentException(
395: "red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255 : "
396: + colorId);
397:
398: for (Color c : VALUES) {
399: if (c.red == red && c.green == green
400: && c.blue == blue)
401: return c;
402: }
403:
404: return new Color(null, red, green, blue);
405: } else {
406: throw new IllegalArgumentException("colorId '"
407: + colorId + "' has an unrecognized rgb format");
408: }
409: } else if (colorId.startsWith("#")) {
410: Matcher m = REGEX_HEX.matcher(colorId);
411:
412: if (m.find()) {
413: int red = Integer.parseInt(m.group(1), 16);
414: int green = Integer.parseInt(m.group(2), 16);
415: int blue = Integer.parseInt(m.group(3), 16);
416: if (red < 0 || red > 255 || green < 0 || green > 255
417: || blue < 0 || blue > 255)
418: throw new IllegalArgumentException(
419: "red < 0 || red > 255 || green < 0 || green > 255 || blue < 0 || blue > 255 : "
420: + colorId);
421:
422: for (Color c : VALUES) {
423: if (c.red == red && c.green == green
424: && c.blue == blue)
425: return c;
426: }
427:
428: return new Color(null, red, green, blue);
429: } else {
430: throw new IllegalArgumentException("colorId '"
431: + colorId + "' has an unrecognized hex format");
432: }
433: } else {
434: Color named = NAMED_COLORS.get(colorId.toLowerCase());
435: if (named == null)
436: throw new IllegalArgumentException(
437: "specified named color is unknown");
438: return named;
439: }
440: }
441:
442: public static final Color[] values() {
443: Color[] values = new Color[VALUES.length];
444: System.arraycopy(VALUES, 0, values, 0, VALUES.length);
445: return values;
446: }
447:
448: private int ordinal;
449: private String name;
450: private String rgbString;
451: private String hexString;
452: private int red;
453: private int green;
454: private int blue;
455:
456: private Color(String name, int red, int green, int blue) {
457: if (name != null && name.length() > 0) {
458: this .ordinal = nextOrdinal++;
459:
460: if (red >= 0 && green >= 0 && blue >= 0) {
461: this .red = red;
462: this .green = green;
463: this .blue = blue;
464: this .name = name;
465: rgbString = "rgb(" + this .red + "," + this .green + ","
466: + this .blue + ")";
467: hexString = "#" + toHexValue(this .red)
468: + toHexValue(this .green)
469: + toHexValue(this .blue);
470: } else {
471: this .red = this .green = this .blue = -1;
472: this .name = rgbString = hexString = name;
473: }
474:
475: NAMED_COLORS.put(name, this );
476: } else if (red >= 0 && green >= 0 && blue >= 0) {
477: this .ordinal = -1;
478: this .name = "";
479: this .red = red;
480: this .green = green;
481: this .blue = blue;
482: rgbString = "rgb(" + this .red + "," + this .green + ","
483: + this .blue + ")";
484: hexString = "#" + toHexValue(this .red)
485: + toHexValue(this .green) + toHexValue(this .blue);
486: } else {
487: throw new IllegalArgumentException(
488: "either a name, RGB pair or Hex number must be specified");
489: }
490: }
491:
492: private String toHexValue(int value) {
493: String s = Integer.toString(value, 16);
494: if (s.length() == 1)
495: s = "0" + s;
496: return s;
497: }
498:
499: public int getRed() {
500: return this .red;
501: }
502:
503: public int getGreen() {
504: return this .green;
505: }
506:
507: public int getBlue() {
508: return this .blue;
509: }
510:
511: public String name() {
512: return name.toUpperCase();
513: }
514:
515: public int ordinal() {
516: return ordinal;
517: }
518:
519: public String toRGBString() {
520: return rgbString;
521: }
522:
523: public String toHexString() {
524: return hexString;
525: }
526:
527: public boolean isSystemColor() {
528: return red == -1 || green == -1 || blue == -1;
529: }
530:
531: public int hashCode() {
532: return rgbString.hashCode();
533: }
534:
535: public boolean equals(Object o) {
536: if (o == null || !(o instanceof Color)) {
537: return false;
538: } else {
539: Color c = (Color) o;
540: return this .red == c.red && this .green == c.green
541: && this .blue == c.blue && this .name.equals(c.name);
542: }
543: }
544:
545: public String toString() {
546: if (name.length() > 0) {
547: return name;
548: } else {
549: return toRGBString();
550: }
551: }
552: }
|