KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > common > util > ColorUtils


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.common.util;
25
26 import java.awt.Color JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.regex.Matcher JavaDoc;
29 import java.util.regex.Pattern JavaDoc;
30
31 /**
32  * @author Felix Gnass [fgnass at neteye dot de]
33  * @since 6.5
34  */

35 public final class ColorUtils {
36
37     private static final Pattern JavaDoc RGB_PATTERN = Pattern.compile(
38             "rgb\\(\\s?(\\d{1,3})%?\\s?,\\s?(\\d{1,3})%?\\s?," +
39             "\\s?(\\d{1,3})(%?)\\s?\\)");
40
41     private static HashMap JavaDoc namedColors = new HashMap JavaDoc();
42
43     static {
44         namedColors.put("aliceblue", new Color JavaDoc(0xf0, 0xf8, 0xff));
45         namedColors.put("antiquewhite", new Color JavaDoc(0xfa, 0xeb, 0xd7));
46         namedColors.put("aqua", new Color JavaDoc(0x00, 0xff, 0xff));
47         namedColors.put("aquamarine", new Color JavaDoc(0x7f, 0xff, 0xd4));
48         namedColors.put("azure", new Color JavaDoc(0xf0, 0xff, 0xff));
49         namedColors.put("beige", new Color JavaDoc(0xf5, 0xf5, 0xdc));
50         namedColors.put("bisque", new Color JavaDoc(0xff, 0xe4, 0xc4));
51         namedColors.put("black", new Color JavaDoc(0x00, 0x00, 0x00));
52         namedColors.put("blanchedalmond", new Color JavaDoc(0xff, 0xeb, 0xcd));
53         namedColors.put("blue", new Color JavaDoc(0x00, 0x00, 0xff));
54         namedColors.put("blueviolet", new Color JavaDoc(0x8a, 0x2b, 0xe2));
55         namedColors.put("brown", new Color JavaDoc(0xa5, 0x2a, 0x2a));
56         namedColors.put("burlywood", new Color JavaDoc(0xde, 0xb8, 0x87));
57         namedColors.put("cadetblue", new Color JavaDoc(0x5f, 0x9e, 0xa0));
58         namedColors.put("chartreuse", new Color JavaDoc(0x7f, 0xff, 0x00));
59         namedColors.put("chocolate", new Color JavaDoc(0xd2, 0x69, 0x1e));
60         namedColors.put("coral", new Color JavaDoc(0xff, 0x7f, 0x50));
61         namedColors.put("cornflowerblue", new Color JavaDoc(0x64, 0x95, 0xed));
62         namedColors.put("cornsilk", new Color JavaDoc(0xff, 0xf8, 0xdc));
63         namedColors.put("crimson", new Color JavaDoc(0xdc, 0x14, 0x3c));
64         namedColors.put("cyan", new Color JavaDoc(0x00, 0xff, 0xff));
65         namedColors.put("darkblue", new Color JavaDoc(0x00, 0x00, 0x8b));
66         namedColors.put("darkcyan", new Color JavaDoc(0x00, 0x8b, 0x8b));
67         namedColors.put("darkgoldenrod", new Color JavaDoc(0xb8, 0x86, 0x0b));
68         namedColors.put("darkgray", new Color JavaDoc(0xa9, 0xa9, 0xa9));
69         namedColors.put("darkgrey", new Color JavaDoc(0xa9, 0xa9, 0xa9));
70         namedColors.put("darkgreen", new Color JavaDoc(0x00, 0x64, 0x00));
71         namedColors.put("darkkhaki", new Color JavaDoc(0xbd, 0xb7, 0x6b));
72         namedColors.put("darkmagenta", new Color JavaDoc(0x8b, 0x00, 0x8b));
73         namedColors.put("darkolivegreen", new Color JavaDoc(0x55, 0x6b, 0x2f));
74         namedColors.put("darkorange", new Color JavaDoc(0xff, 0x8c, 0x00));
75         namedColors.put("darkorchid", new Color JavaDoc(0x99, 0x32, 0xcc));
76         namedColors.put("darkred", new Color JavaDoc(0x8b, 0x00, 0x00));
77         namedColors.put("darksalmon", new Color JavaDoc(0xe9, 0x96, 0x7a));
78         namedColors.put("darkseagreen", new Color JavaDoc(0x8f, 0xbc, 0x8f));
79         namedColors.put("darkslateblue", new Color JavaDoc(0x48, 0x3d, 0x8b));
80         namedColors.put("darkslategray", new Color JavaDoc(0x2f, 0x4f, 0x4f));
81         namedColors.put("darkslategrey", new Color JavaDoc(0x2f, 0x4f, 0x4f));
82         namedColors.put("darkturquoise", new Color JavaDoc(0x00, 0xce, 0xd1));
83         namedColors.put("darkviolet", new Color JavaDoc(0x94, 0x00, 0xd3));
84         namedColors.put("deeppink", new Color JavaDoc(0xff, 0x14, 0x93));
85         namedColors.put("deepskyblue", new Color JavaDoc(0x00, 0xbf, 0xff));
86         namedColors.put("dimgray", new Color JavaDoc(0x69, 0x69, 0x69));
87         namedColors.put("dimgrey", new Color JavaDoc(0x69, 0x69, 0x69));
88         namedColors.put("dodgerblue", new Color JavaDoc(0x1e, 0x90, 0xff));
89         namedColors.put("firebrick", new Color JavaDoc(0xb2, 0x22, 0x22));
90         namedColors.put("floralwhite", new Color JavaDoc(0xff, 0xfa, 0xf0));
91         namedColors.put("forestgreen", new Color JavaDoc(0x22, 0x8b, 0x22));
92         namedColors.put("fuchsia", new Color JavaDoc(0xff, 0x00, 0xff));
93         namedColors.put("gainsboro", new Color JavaDoc(0xdc, 0xdc, 0xdc));
94         namedColors.put("ghostwhite", new Color JavaDoc(0xf8, 0xf8, 0xff));
95         namedColors.put("gold", new Color JavaDoc(0xff, 0xd7, 0x00));
96         namedColors.put("goldenrod", new Color JavaDoc(0xda, 0xa5, 0x20));
97         namedColors.put("gray", new Color JavaDoc(0x80, 0x80, 0x80));
98         namedColors.put("grey", new Color JavaDoc(0x80, 0x80, 0x80));
99         namedColors.put("green", new Color JavaDoc(0x00, 0x80, 0x00));
100         namedColors.put("greenyellow", new Color JavaDoc(0xad, 0xff, 0x2f));
101         namedColors.put("honeydew", new Color JavaDoc(0xf0, 0xff, 0xf0));
102         namedColors.put("hotpink", new Color JavaDoc(0xff, 0x69, 0xb4));
103         namedColors.put("indianred", new Color JavaDoc(0xcd, 0x5c, 0x5c));
104         namedColors.put("indigo", new Color JavaDoc(0x4b, 0x00, 0x82));
105         namedColors.put("ivory", new Color JavaDoc(0xff, 0xff, 0xf0));
106         namedColors.put("khaki", new Color JavaDoc(0xf0, 0xe6, 0x8c));
107         namedColors.put("lavender", new Color JavaDoc(0xe6, 0xe6, 0xfa));
108         namedColors.put("lavenderblush", new Color JavaDoc(0xff, 0xf0, 0xf5));
109         namedColors.put("lawngreen", new Color JavaDoc(0x7c, 0xfc, 0x00));
110         namedColors.put("lemonchiffon", new Color JavaDoc(0xff, 0xfa, 0xcd));
111         namedColors.put("lightblue", new Color JavaDoc(0xad, 0xd8, 0xe6));
112         namedColors.put("lightcoral", new Color JavaDoc(0xf0, 0x80, 0x80));
113         namedColors.put("lightcyan", new Color JavaDoc(0xe0, 0xff, 0xff));
114         namedColors.put("lightgoldenrodyellow", new Color JavaDoc(0xfa, 0xfa, 0xd2));
115         namedColors.put("lightgray", new Color JavaDoc(0xd3, 0xd3, 0xd3));
116         namedColors.put("lightgrey", new Color JavaDoc(0xd3, 0xd3, 0xd3));
117         namedColors.put("lightgreen", new Color JavaDoc(0x90, 0xee, 0x90));
118         namedColors.put("lightpink", new Color JavaDoc(0xff, 0xb6, 0xc1));
119         namedColors.put("lightsalmon", new Color JavaDoc(0xff, 0xa0, 0x7a));
120         namedColors.put("lightseagreen", new Color JavaDoc(0x20, 0xb2, 0xaa));
121         namedColors.put("lightskyblue", new Color JavaDoc(0x87, 0xce, 0xfa));
122         namedColors.put("lightslategray", new Color JavaDoc(0x77, 0x88, 0x99));
123         namedColors.put("lightslategrey", new Color JavaDoc(0x77, 0x88, 0x99));
124         namedColors.put("lightsteelblue", new Color JavaDoc(0xb0, 0xc4, 0xde));
125         namedColors.put("lightyellow", new Color JavaDoc(0xff, 0xff, 0xe0));
126         namedColors.put("lime", new Color JavaDoc(0x00, 0xff, 0x00));
127         namedColors.put("limegreen", new Color JavaDoc(0x32, 0xcd, 0x32));
128         namedColors.put("linen", new Color JavaDoc(0xfa, 0xf0, 0xe6));
129         namedColors.put("magenta", new Color JavaDoc(0xff, 0x00, 0xff));
130         namedColors.put("maroon", new Color JavaDoc(0x80, 0x00, 0x00));
131         namedColors.put("mediumaquamarine", new Color JavaDoc(0x66, 0xcd, 0xaa));
132         namedColors.put("mediumblue", new Color JavaDoc(0x00, 0x00, 0xcd));
133         namedColors.put("mediumorchid", new Color JavaDoc(0xba, 0x55, 0xd3));
134         namedColors.put("mediumpurple", new Color JavaDoc(0x93, 0x70, 0xd8));
135         namedColors.put("mediumseagreen", new Color JavaDoc(0x3c, 0xb3, 0x71));
136         namedColors.put("mediumslateblue", new Color JavaDoc(0x7b, 0x68, 0xee));
137         namedColors.put("mediumspringgreen", new Color JavaDoc(0x00, 0xfa, 0x9a));
138         namedColors.put("mediumturquoise", new Color JavaDoc(0x48, 0xd1, 0xcc));
139         namedColors.put("mediumvioletred", new Color JavaDoc(0xc7, 0x15, 0x85));
140         namedColors.put("midnightblue", new Color JavaDoc(0x19, 0x19, 0x70));
141         namedColors.put("mintcream", new Color JavaDoc(0xf5, 0xff, 0xfa));
142         namedColors.put("mistyrose", new Color JavaDoc(0xff, 0xe4, 0xe1));
143         namedColors.put("moccasin", new Color JavaDoc(0xff, 0xe4, 0xb5));
144         namedColors.put("navajowhite", new Color JavaDoc(0xff, 0xde, 0xad));
145         namedColors.put("navy", new Color JavaDoc(0x00, 0x00, 0x80));
146         namedColors.put("oldlace", new Color JavaDoc(0xfd, 0xf5, 0xe6));
147         namedColors.put("olive", new Color JavaDoc(0x80, 0x80, 0x00));
148         namedColors.put("olivedrab", new Color JavaDoc(0x6b, 0x8e, 0x23));
149         namedColors.put("orange", new Color JavaDoc(0xff, 0xa5, 0x00));
150         namedColors.put("orangered", new Color JavaDoc(0xff, 0x45, 0x00));
151         namedColors.put("orchid", new Color JavaDoc(0xda, 0x70, 0xd6));
152         namedColors.put("palegoldenrod", new Color JavaDoc(0xee, 0xe8, 0xaa));
153         namedColors.put("palegreen", new Color JavaDoc(0x98, 0xfb, 0x98));
154         namedColors.put("paleturquoise", new Color JavaDoc(0xaf, 0xee, 0xee));
155         namedColors.put("palevioletred", new Color JavaDoc(0xd8, 0x70, 0x93));
156         namedColors.put("papayawhip", new Color JavaDoc(0xff, 0xef, 0xd5));
157         namedColors.put("peachpuff", new Color JavaDoc(0xff, 0xda, 0xb9));
158         namedColors.put("peru", new Color JavaDoc(0xcd, 0x85, 0x3f));
159         namedColors.put("pink", new Color JavaDoc(0xff, 0xc0, 0xcb));
160         namedColors.put("plum", new Color JavaDoc(0xdd, 0xa0, 0xdd));
161         namedColors.put("powderblue", new Color JavaDoc(0xb0, 0xe0, 0xe6));
162         namedColors.put("purple", new Color JavaDoc(0x80, 0x00, 0x80));
163         namedColors.put("red", new Color JavaDoc(0xff, 0x00, 0x00));
164         namedColors.put("rosybrown", new Color JavaDoc(0xbc, 0x8f, 0x8f));
165         namedColors.put("royalblue", new Color JavaDoc(0x41, 0x69, 0xe1));
166         namedColors.put("saddlebrown", new Color JavaDoc(0x8b, 0x45, 0x13));
167         namedColors.put("salmon", new Color JavaDoc(0xfa, 0x80, 0x72));
168         namedColors.put("sandybrown", new Color JavaDoc(0xf4, 0xa4, 0x60));
169         namedColors.put("seagreen", new Color JavaDoc(0x2e, 0x8b, 0x57));
170         namedColors.put("seashell", new Color JavaDoc(0xff, 0xf5, 0xee));
171         namedColors.put("sienna", new Color JavaDoc(0xa0, 0x52, 0x2d));
172         namedColors.put("silver", new Color JavaDoc(0xc0, 0xc0, 0xc0));
173         namedColors.put("skyblue", new Color JavaDoc(0x87, 0xce, 0xeb));
174         namedColors.put("slateblue", new Color JavaDoc(0x6a, 0x5a, 0xcd));
175         namedColors.put("slategray", new Color JavaDoc(0x70, 0x80, 0x90));
176         namedColors.put("slategrey", new Color JavaDoc(0x70, 0x80, 0x90));
177         namedColors.put("snow", new Color JavaDoc(0xff, 0xfa, 0xfa));
178         namedColors.put("springgreen", new Color JavaDoc(0x00, 0xff, 0x7f));
179         namedColors.put("steelblue", new Color JavaDoc(0x46, 0x82, 0xb4));
180         namedColors.put("tan", new Color JavaDoc(0xd2, 0xb4, 0x8c));
181         namedColors.put("teal", new Color JavaDoc(0x00, 0x80, 0x80));
182         namedColors.put("thistle", new Color JavaDoc(0xd8, 0xbf, 0xd8));
183         namedColors.put("tomato", new Color JavaDoc(0xff, 0x63, 0x47));
184         namedColors.put("turquoise", new Color JavaDoc(0x40, 0xe0, 0xd0));
185         namedColors.put("violet", new Color JavaDoc(0xee, 0x82, 0xee));
186         namedColors.put("wheat", new Color JavaDoc(0xf5, 0xde, 0xb3));
187         namedColors.put("white", new Color JavaDoc(0xff, 0xff, 0xff));
188         namedColors.put("whitesmoke", new Color JavaDoc(0xf5, 0xf5, 0xf5));
189         namedColors.put("yellow", new Color JavaDoc(0xff, 0xff, 0x00));
190         namedColors.put("yellowgreen", new Color JavaDoc(0x9a, 0xcd, 0x32));
191     }
192
193     /**
194      * Creates a Color instance from a String. Supported formats are:
195      * <pre>
196      * #fff
197      * #ffffff
198      * rgb(255,155,155)
199      * rgb(100%,100%,100%)
200      * </pre>
201      * You may also use one of the color names listed at
202      * {@linkplain http://www.w3schools.com/css/css_colornames.asp}
203      */

204     public static Color JavaDoc parseColor(String JavaDoc s) throws IllegalArgumentException JavaDoc {
205         int r, g, b;
206         if (s.startsWith("#")) {
207             if (s.length() == 4) {
208                 r = Integer.parseInt(s.substring(1, 2) + s.charAt(1), 16);
209                 g = Integer.parseInt(s.substring(2, 3) + s.charAt(2), 16);
210                 b = Integer.parseInt(s.substring(3) + s.charAt(3), 16);
211                 return new Color JavaDoc(r, g, b);
212             }
213             if (s.length() == 7) {
214                 r = Integer.parseInt(s.substring(1, 3), 16);
215                 g = Integer.parseInt(s.substring(3, 5), 16);
216                 b = Integer.parseInt(s.substring(5), 16);
217                 return new Color JavaDoc(r, g, b);
218             }
219         }
220         else {
221             Matcher JavaDoc m = RGB_PATTERN.matcher(s);
222             if (m.matches()) {
223                 r = Integer.parseInt(m.group(1));
224                 g = Integer.parseInt(m.group(2));
225                 b = Integer.parseInt(m.group(3));
226                 if ("%".equals(m.group(4))) {
227                     r = 255 * (r / 100);
228                     g = 255 * (g / 100);
229                     b = 255 * (b / 100);
230                 }
231                 return new Color JavaDoc(r, g, b);
232             }
233             else {
234                 Color JavaDoc color = (Color JavaDoc) namedColors.get(s.toLowerCase());
235                 if (color != null) {
236                     return color;
237                 }
238             }
239         }
240         throw new IllegalArgumentException JavaDoc("Invalid color format.");
241     }
242     
243     /**
244      * Adjusts the brightness of the given color.
245      * @param c The color to adjust
246      * @param scale The factor (a value from -1 to 1)
247      */

248     public static Color JavaDoc brightness(Color JavaDoc c, float scale) {
249         int r = c.getRed();
250         int g = c.getGreen();
251         int b = c.getBlue();
252         if (r == 0 && g == 0 && b == 0 && scale > 0) {
253             r = g = b = Math.min(255, (int) (255 * scale));
254         }
255         else {
256             r = Math.max(0, (int) (r * scale));
257             g = Math.max(0, (int) (g * scale));
258             b = Math.max(0, (int) (b * scale));
259             if (r > 255 || g > 255 || b > 255) {
260                 return c;
261             }
262         }
263         return new Color JavaDoc(r, g, b);
264     }
265     
266     /**
267      * Adjusts the saturation of the given color.
268      * @param c The color to adjust
269      * @param scale The factor (a value from -1 to 1)
270      */

271     public static Color JavaDoc saturation(Color JavaDoc c, float scale) {
272         int r = c.getRed();
273         int g = c.getGreen();
274         int b = c.getBlue();
275         float[] hsb = Color.RGBtoHSB(r, g, b, null);
276         float s = hsb[1] * scale;
277         if (s < 0) {
278             s = 0;
279         };
280         if (s > 1) {
281             s = 1;
282         };
283         return Color.getHSBColor(hsb[0], s, hsb[2]);
284     }
285
286     public static String JavaDoc toHex(Color JavaDoc color) {
287         String JavaDoc r = Integer.toHexString(color.getRed());
288         String JavaDoc g = Integer.toHexString(color.getGreen());
289         String JavaDoc b = Integer.toHexString(color.getBlue());
290         if (r.length() == 1) {
291           r = "0" + r;
292         }
293         if (g.length() == 1) {
294           g = "0" + g;
295         }
296         if (b.length() == 1) {
297           b = "0" + b;
298         }
299         return ("#" + r + g + b).toUpperCase();
300     }
301 }
302
303     
Popular Tags