KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > util > ColorUtil


1 /* ************************************************************************** *
2  * Copyright (C) 2004 NightLabs GmbH, Marco Schulze *
3  * All rights reserved. *
4  * http://www.NightLabs.de *
5  * *
6  * This program and the accompanying materials are free software; you can re- *
7  * distribute it and/or modify it under the terms of the GNU General Public *
8  * License as published by the Free Software Foundation; either ver 2 of the *
9  * License, or any later version. *
10  * *
11  * This module is distributed in the hope that it will be useful, but WITHOUT *
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FIT- *
13  * NESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more *
14  * details. *
15  * *
16  * You should have received a copy of the GNU General Public License along *
17  * with this module; if not, write to the Free Software Foundation, Inc.: *
18  * 59 Temple Place, Suite 330 *
19  * Boston MA 02111-1307 *
20  * USA *
21  * *
22  * Or get it online: *
23  * http://www.opensource.org/licenses/gpl-license.php *
24  * *
25  * In case, you want to use this module or parts of it in a proprietary pro- *
26  * ject, you can purchase it under the NightLabs Commercial License. Please *
27  * contact NightLabs GmbH under info AT nightlabs DOT com for more infos or *
28  * visit http://www.NightLabs.com *
29  * ************************************************************************** */

30
31 /*
32  * Created on Jan 7, 2005
33  */

34 package com.nightlabs.util;
35
36 import java.awt.Color JavaDoc;
37 import java.util.regex.Pattern JavaDoc;
38
39 /**
40  * @author Marco Schulze - marco at nightlabs dot de
41  */

42 public class ColorUtil
43 {
44
45     protected ColorUtil()
46     {
47     }
48     
49     protected static class RGBA {
50         public int r, g, b, a;
51     }
52
53     protected static Pattern JavaDoc SPLIT_RGBA_PATTERN = Pattern.compile(
54             "^\\p{Blank}*|\\p{Blank}*\\{\\p{Blank}*|\\p{Blank}*\\}\\p{Blank}*$|,\\p{Blank}*");
55
56     protected static RGBA parseRGBA(String JavaDoc color)
57     {
58         RGBA rgba = new RGBA();
59         String JavaDoc[] parts = SPLIT_RGBA_PATTERN.split(color);
60         if (!"RGBA".equals(parts[1]) && !"RGB".equals(parts[1]))
61             throw new IllegalArgumentException JavaDoc("color is neither RGB nor RGBA encoded! It must start with 'RGB' or 'RGBA'!");
62
63         if (parts.length < 5)
64             throw new IllegalArgumentException JavaDoc("color is incomplete! It must at least contain three decimal numbers specifying red, green and blue! It should specify alpha as forth value.");
65
66         rgba.r = Integer.parseInt(parts[2]);
67         rgba.g = Integer.parseInt(parts[3]);
68         rgba.b = Integer.parseInt(parts[4]);
69
70         if (parts.length >= 6)
71             rgba.a = Integer.parseInt(parts[5]);
72         else
73             rgba.a = 255;
74
75         if (rgba.r < 0 || rgba.g < 0 || rgba.b < 0 || rgba.a < 0)
76             throw new IllegalArgumentException JavaDoc("color is invalid! One of the values for red, gree, blue or alpha is less than 0!");
77
78         if (rgba.r > 255 || rgba.g > 255 || rgba.b > 255 || rgba.a > 255)
79             throw new IllegalArgumentException JavaDoc("color is invalid! One of the values for red, gree, blue or alpha is greater than 255!");
80
81         return rgba;
82     }
83
84     public static String JavaDoc colorToString(Color JavaDoc color)
85     {
86         return "RGBA{"
87             + color.getRed() + ','
88             + color.getGreen() + ','
89             + color.getBlue() + ','
90             + color.getAlpha() + '}';
91     }
92
93     /**
94      * @param red
95      * @param green
96      * @param blue
97      * @param alpha
98      * @return Returns a String in the form RGBA{255,0,128,255}
99      */

100     public static String JavaDoc colorToString(int red, int green, int blue, int alpha)
101     {
102         return "RGBA{"
103             + red + ','
104             + green + ','
105             + blue + ','
106             + alpha + '}';
107     }
108
109     public static Color JavaDoc stringToColor(String JavaDoc color)
110     {
111         RGBA rgba = parseRGBA(color);
112         return new Color JavaDoc(
113                 rgba.r,
114                 rgba.g,
115                 rgba.b,
116                 rgba.a);
117     }
118
119 // public static void main(String[] args)
120
// {
121
// String[] parts = SPLIT_RGBA_PATTERN.split(" RGBA { 155, 234, 12, 255 }");
122
// System.out.println("---------");
123
// for (int i = 0; i < parts.length; ++i)
124
// System.out.println("" + i +": " + parts[i]);
125
// System.out.println("---------");
126
//
127
// System.out.println(colorToString(stringToColor(" RGBA { 155, 234, 12, 252 }")));
128
// }
129

130 }
131
Popular Tags