KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > gui > ColorFactory


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Apr 17, 2005
23  */

24 package org.lobobrowser.util.gui;
25 import java.util.*;
26 import java.util.logging.*;
27 import java.awt.*;
28
29 /**
30  * @author J. H. S.
31  */

32 public class ColorFactory {
33     private static final Logger logger = Logger.getLogger(ColorFactory.class.getName());
34     public static final Color TRANSPARENT = new Color(0, 0, 0, 0);
35     private static ColorFactory instance;
36     private final Map colorMap = new HashMap();
37     
38     private ColorFactory() {
39         Map colorMap = this.colorMap;
40         synchronized(this) {
41             //Standard
42
colorMap.put("transparent", new Color(0, 0, 0, 0));
43             colorMap.put("black", Color.black);
44             colorMap.put("blue", Color.blue);
45             colorMap.put("cyan", Color.cyan);
46             colorMap.put("darkGray", Color.darkGray);
47             colorMap.put("gray", Color.gray);
48             colorMap.put("green", new Color(0, 100, 0));
49             colorMap.put("magenta", Color.magenta);
50             colorMap.put("orange", Color.orange);
51             colorMap.put("pink", Color.pink);
52             colorMap.put("red", Color.red);
53             colorMap.put("white", Color.white);
54             colorMap.put("yellow", Color.yellow);
55             //Other colors
56
colorMap.put("grey", new Color(50, 100, 0));
57             colorMap.put("brown", new Color(100, 50, 0));
58         }
59     }
60     public static final ColorFactory getInstance() {
61         if(instance == null) {
62             synchronized(ColorFactory.class) {
63                 if(instance == null) {
64                     instance = new ColorFactory();
65                 }
66             }
67         }
68         return instance;
69     }
70     
71     private static final String JavaDoc RGB_START = "rgb(";
72     
73     public boolean isColor(String JavaDoc colorSpec) {
74         if(colorSpec.startsWith("#")) {
75             return true;
76         }
77         String JavaDoc normalSpec = colorSpec.toLowerCase();
78         if(normalSpec.startsWith(RGB_START)) {
79             return true;
80         }
81         synchronized(this) {
82             return colorMap.containsKey(normalSpec);
83         }
84     }
85     
86     public Color getColor(String JavaDoc colorSpec) {
87         String JavaDoc normalSpec = colorSpec.toLowerCase();
88         synchronized(this) {
89             Color color = (Color) colorMap.get(normalSpec);
90             if(color == null) {
91                 if(normalSpec.startsWith(RGB_START)) {
92                     int endIdx = normalSpec.lastIndexOf(')');
93                     String JavaDoc commaValues = endIdx == -1 ? normalSpec.substring(RGB_START.length()) : normalSpec.substring(RGB_START.length(), endIdx);
94                     StringTokenizer tok = new StringTokenizer(commaValues, ",");
95                     int r = 0, g = 0, b = 0;
96                     if(tok.hasMoreTokens()) {
97                         String JavaDoc rstr = tok.nextToken().trim();
98                         try {
99                             r = Integer.parseInt(rstr);
100                         } catch(NumberFormatException JavaDoc nfe) {
101                             // ignore
102
}
103                         if(tok.hasMoreTokens()) {
104                             String JavaDoc gstr = tok.nextToken().trim();
105                             try {
106                                 g = Integer.parseInt(gstr);
107                             } catch(NumberFormatException JavaDoc nfe) {
108                                 // ignore
109
}
110                             if(tok.hasMoreTokens()) {
111                                 String JavaDoc bstr = tok.nextToken().trim();
112                                 try {
113                                     b = Integer.parseInt(bstr);
114                                 } catch(NumberFormatException JavaDoc nfe) {
115                                     // ignore
116
}
117                             }
118                         }
119                     }
120                     color = new Color(r, g, b);
121                 }
122                 else if(normalSpec.startsWith("#")) {
123                     int len = normalSpec.length();
124                     int[] rgba = new int[4];
125                     rgba[3] = 255;
126                     for(int i = 0; i < rgba.length; i++)
127                     {
128                         int idx = 2 * i + 1;
129                         if(idx < len)
130                         {
131                             String JavaDoc hexText = normalSpec.substring(idx, idx + Math.min(2, len - idx));
132                             try {
133                                 rgba[i] = Integer.parseInt(hexText, 16);
134                             } catch(NumberFormatException JavaDoc nfe) {
135                                 // Ignore
136
}
137                         }
138                     }
139                     color = new Color(rgba[0], rgba[1], rgba[2], rgba[3]);
140                 }
141                 else {
142                     if(logger.isLoggable(Level.INFO)) {
143                         logger.warning("getColor(): Color spec [" + normalSpec + "] unknown.");
144                     }
145                     return Color.RED;
146                 }
147                 colorMap.put(normalSpec, color);
148             }
149             return color;
150         }
151     }
152     
153     
154 }
155
Popular Tags