gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / coerce / CoerceToColor.java @ 1746
History | View | Annotate | Download (14.9 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.tools.swing.impl.coerce; |
25 |
|
26 |
import java.awt.Color; |
27 |
import java.util.HashMap; |
28 |
import java.util.Map; |
29 |
import org.apache.commons.lang3.StringUtils; |
30 |
import org.apache.commons.lang3.math.NumberUtils; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dataTypes.CoercionException; |
33 |
import org.gvsig.tools.dataTypes.DataTypesManager; |
34 |
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion; |
35 |
import org.gvsig.tools.swing.api.DataTypes; |
36 |
|
37 |
public class CoerceToColor implements Coercion { |
38 |
|
39 |
|
40 |
private static Map<String,Color> colorTable = null; |
41 |
|
42 |
@Override
|
43 |
public Object coerce(Object value) throws CoercionException { |
44 |
try {
|
45 |
if (value == null || value instanceof Color) { |
46 |
return value;
|
47 |
} |
48 |
if( value instanceof Number ) { |
49 |
int rgb = ((Number)value).intValue(); |
50 |
Color color = new Color(rgb); // Alpha?? |
51 |
return color;
|
52 |
} |
53 |
String s = value.toString().trim();
|
54 |
if( s.startsWith("rgb(") ) { |
55 |
s = s.replace(")", ""); |
56 |
String[] x = StringUtils.split(s, ","); |
57 |
if( x.length<3 || x.length>4 ) { |
58 |
throw new CoercionException("Can't convert '"+value.toString()+"' to color."); |
59 |
} |
60 |
try {
|
61 |
Color color;
|
62 |
int r = NumberUtils.createInteger(x[0]); |
63 |
int g = NumberUtils.createInteger(x[1]); |
64 |
int b = NumberUtils.createInteger(x[2]); |
65 |
if( x.length==3 ) { |
66 |
color = new Color(r, g, b); |
67 |
} else {
|
68 |
int a = NumberUtils.createInteger(x[3]); |
69 |
color = new Color(r, g, b, a); |
70 |
} |
71 |
return color;
|
72 |
} catch(NumberFormatException ex) { |
73 |
throw new CoercionException("Can't convert '"+value.toString()+"' to color."); |
74 |
} |
75 |
|
76 |
} |
77 |
if( s.startsWith("#") ) { |
78 |
s = "0x" + s.substring(1); |
79 |
} |
80 |
try {
|
81 |
int rgb = NumberUtils.createInteger(s);
|
82 |
Color color = new Color(rgb); |
83 |
return color;
|
84 |
} catch(NumberFormatException ex) { |
85 |
// Pass, do nothing
|
86 |
} |
87 |
Color color = this.getColor(s); |
88 |
if( color == null ) { |
89 |
throw new CoercionException("Can't convert '"+value.toString()+"' to color."); |
90 |
} |
91 |
return color;
|
92 |
} catch (Exception e) { |
93 |
throw new CoercionException(e); |
94 |
} |
95 |
|
96 |
} |
97 |
|
98 |
private Color getColor(String name) { |
99 |
if( colorTable==null ) { |
100 |
//
|
101 |
// HTML Color Names
|
102 |
// from https://www.w3schools.com/colors/colors_names.asp
|
103 |
//
|
104 |
colorTable = new HashMap<>(); |
105 |
colorTable.put("aliceblue",new Color(0xF0,0xF8,0xFF)); |
106 |
colorTable.put("antiquewhite",new Color(0xFA,0xEB,0xD7)); |
107 |
colorTable.put("aqua",new Color(0x00,0xFF,0xFF)); |
108 |
colorTable.put("aquamarine",new Color(0x7F,0xFF,0xD4)); |
109 |
colorTable.put("azure",new Color(0xF0,0xFF,0xFF)); |
110 |
colorTable.put("beige",new Color(0xF5,0xF5,0xDC)); |
111 |
colorTable.put("bisque",new Color(0xFF,0xE4,0xC4)); |
112 |
colorTable.put("black",new Color(0x00,0x00,0x00)); |
113 |
colorTable.put("blanchedalmond",new Color(0xFF,0xEB,0xCD)); |
114 |
colorTable.put("blue",new Color(0x00,0x00,0xFF)); |
115 |
colorTable.put("blueviolet",new Color(0x8A,0x2B,0xE2)); |
116 |
colorTable.put("brown",new Color(0xA5,0x2A,0x2A)); |
117 |
colorTable.put("burlywood",new Color(0xDE,0xB8,0x87)); |
118 |
colorTable.put("cadetblue",new Color(0x5F,0x9E,0xA0)); |
119 |
colorTable.put("chartreuse",new Color(0x7F,0xFF,0x00)); |
120 |
colorTable.put("chocolate",new Color(0xD2,0x69,0x1E)); |
121 |
colorTable.put("coral",new Color(0xFF,0x7F,0x50)); |
122 |
colorTable.put("cornflowerblue",new Color(0x64,0x95,0xED)); |
123 |
colorTable.put("cornsilk",new Color(0xFF,0xF8,0xDC)); |
124 |
colorTable.put("crimson",new Color(0xDC,0x14,0x3C)); |
125 |
colorTable.put("cyan",new Color(0x00,0xFF,0xFF)); |
126 |
colorTable.put("darkblue",new Color(0x00,0x00,0x8B)); |
127 |
colorTable.put("darkcyan",new Color(0x00,0x8B,0x8B)); |
128 |
colorTable.put("darkgoldenRod",new Color(0xB8,0x86,0x0B)); |
129 |
colorTable.put("darkgray",new Color(0xA9,0xA9,0xA9)); |
130 |
colorTable.put("darkgrey",new Color(0xA9,0xA9,0xA9)); |
131 |
colorTable.put("darkgreen",new Color(0x00,0x64,0x00)); |
132 |
colorTable.put("darkkhaki",new Color(0xBD,0xB7,0x6B)); |
133 |
colorTable.put("darkmagenta",new Color(0x8B,0x00,0x8B)); |
134 |
colorTable.put("darkolivegreen",new Color(0x55,0x6B,0x2F)); |
135 |
colorTable.put("darkorange",new Color(0xFF,0x8C,0x00)); |
136 |
colorTable.put("darkorchid",new Color(0x99,0x32,0xCC)); |
137 |
colorTable.put("darkred",new Color(0x8B,0x00,0x00)); |
138 |
colorTable.put("darksalmon",new Color(0xE9,0x96,0x7A)); |
139 |
colorTable.put("darkseagreen",new Color(0x8F,0xBC,0x8F)); |
140 |
colorTable.put("darkslateblue",new Color(0x48,0x3D,0x8B)); |
141 |
colorTable.put("darkslategray",new Color(0x2F,0x4F,0x4F)); |
142 |
colorTable.put("darkslategrey",new Color(0x2F,0x4F,0x4F)); |
143 |
colorTable.put("darkturquoise",new Color(0x00,0xCE,0xD1)); |
144 |
colorTable.put("darkviolet",new Color(0x94,0x00,0xD3)); |
145 |
colorTable.put("deeppink",new Color(0xFF,0x14,0x93)); |
146 |
colorTable.put("deepskyblue",new Color(0x00,0xBF,0xFF)); |
147 |
colorTable.put("dimgray",new Color(0x69,0x69,0x69)); |
148 |
colorTable.put("dimgrey",new Color(0x69,0x69,0x69)); |
149 |
colorTable.put("dodgerblue",new Color(0x1E,0x90,0xFF)); |
150 |
colorTable.put("firebrick",new Color(0xB2,0x22,0x22)); |
151 |
colorTable.put("floralwhite",new Color(0xFF,0xFA,0xF0)); |
152 |
colorTable.put("forestgreen",new Color(0x22,0x8B,0x22)); |
153 |
colorTable.put("fuchsia",new Color(0xFF,0x00,0xFF)); |
154 |
colorTable.put("gainsboro",new Color(0xDC,0xDC,0xDC)); |
155 |
colorTable.put("ghostwhite",new Color(0xF8,0xF8,0xFF)); |
156 |
colorTable.put("gold",new Color(0xFF,0xD7,0x00)); |
157 |
colorTable.put("goldenrod",new Color(0xDA,0xA5,0x20)); |
158 |
colorTable.put("gray",new Color(0x80,0x80,0x80)); |
159 |
colorTable.put("grey",new Color(0x80,0x80,0x80)); |
160 |
colorTable.put("green",new Color(0x00,0x80,0x00)); |
161 |
colorTable.put("greenyellow",new Color(0xAD,0xFF,0x2F)); |
162 |
colorTable.put("honeydew",new Color(0xF0,0xFF,0xF0)); |
163 |
colorTable.put("hotpink",new Color(0xFF,0x69,0xB4)); |
164 |
colorTable.put("indianred",new Color(0xCD,0x5C,0x5C)); |
165 |
colorTable.put("indigo",new Color(0x4B,0x00,0x82)); |
166 |
colorTable.put("ivory",new Color(0xFF,0xFF,0xF0)); |
167 |
colorTable.put("khaki",new Color(0xF0,0xE6,0x8C)); |
168 |
colorTable.put("lavender",new Color(0xE6,0xE6,0xFA)); |
169 |
colorTable.put("lavenderblush",new Color(0xFF,0xF0,0xF5)); |
170 |
colorTable.put("lawnGreen",new Color(0x7C,0xFC,0x00)); |
171 |
colorTable.put("lemonchiffon",new Color(0xFF,0xFA,0xCD)); |
172 |
colorTable.put("lightblue",new Color(0xAD,0xD8,0xE6)); |
173 |
colorTable.put("lightcoral",new Color(0xF0,0x80,0x80)); |
174 |
colorTable.put("lightcyan",new Color(0xE0,0xFF,0xFF)); |
175 |
colorTable.put("lightgoldenRodYellow",new Color(0xFA,0xFA,0xD2)); |
176 |
colorTable.put("lightgray",new Color(0xD3,0xD3,0xD3)); |
177 |
colorTable.put("lightgrey",new Color(0xD3,0xD3,0xD3)); |
178 |
colorTable.put("lightgreen",new Color(0x90,0xEE,0x90)); |
179 |
colorTable.put("lightpink",new Color(0xFF,0xB6,0xC1)); |
180 |
colorTable.put("lightsalmon",new Color(0xFF,0xA0,0x7A)); |
181 |
colorTable.put("lightseagreen",new Color(0x20,0xB2,0xAA)); |
182 |
colorTable.put("lightskyblue",new Color(0x87,0xCE,0xFA)); |
183 |
colorTable.put("lightslategray",new Color(0x77,0x88,0x99)); |
184 |
colorTable.put("lightslategrey",new Color(0x77,0x88,0x99)); |
185 |
colorTable.put("lightsteelblue",new Color(0xB0,0xC4,0xDE)); |
186 |
colorTable.put("lightyellow",new Color(0xFF,0xFF,0xE0)); |
187 |
colorTable.put("lime",new Color(0x00,0xFF,0x00)); |
188 |
colorTable.put("limegreen",new Color(0x32,0xCD,0x32)); |
189 |
colorTable.put("linen",new Color(0xFA,0xF0,0xE6)); |
190 |
colorTable.put("magenta",new Color(0xFF,0x00,0xFF)); |
191 |
colorTable.put("maroon",new Color(0x80,0x00,0x00)); |
192 |
colorTable.put("mediumaquamarine",new Color(0x66,0xCD,0xAA)); |
193 |
colorTable.put("mediumblue",new Color(0x00,0x00,0xCD)); |
194 |
colorTable.put("mediumorchid",new Color(0xBA,0x55,0xD3)); |
195 |
colorTable.put("mediumpurple",new Color(0x93,0x70,0xDB)); |
196 |
colorTable.put("mediumseagreen",new Color(0x3C,0xB3,0x71)); |
197 |
colorTable.put("mediumslateblue",new Color(0x7B,0x68,0xEE)); |
198 |
colorTable.put("mediumspringgreen",new Color(0x00,0xFA,0x9A)); |
199 |
colorTable.put("mediumturquoise",new Color(0x48,0xD1,0xCC)); |
200 |
colorTable.put("mediumvioletred",new Color(0xC7,0x15,0x85)); |
201 |
colorTable.put("midnightblue",new Color(0x19,0x19,0x70)); |
202 |
colorTable.put("mintcream",new Color(0xF5,0xFF,0xFA)); |
203 |
colorTable.put("mistyrose",new Color(0xFF,0xE4,0xE1)); |
204 |
colorTable.put("moccasin",new Color(0xFF,0xE4,0xB5)); |
205 |
colorTable.put("navajowhite",new Color(0xFF,0xDE,0xAD)); |
206 |
colorTable.put("navy",new Color(0x00,0x00,0x80)); |
207 |
colorTable.put("oldlace",new Color(0xFD,0xF5,0xE6)); |
208 |
colorTable.put("olive",new Color(0x80,0x80,0x00)); |
209 |
colorTable.put("olivedrab",new Color(0x6B,0x8E,0x23)); |
210 |
colorTable.put("orange",new Color(0xFF,0xA5,0x00)); |
211 |
colorTable.put("orangered",new Color(0xFF,0x45,0x00)); |
212 |
colorTable.put("orchid",new Color(0xDA,0x70,0xD6)); |
213 |
colorTable.put("palegoldenrod",new Color(0xEE,0xE8,0xAA)); |
214 |
colorTable.put("palegreen",new Color(0x98,0xFB,0x98)); |
215 |
colorTable.put("paleturquoise",new Color(0xAF,0xEE,0xEE)); |
216 |
colorTable.put("palevioletred",new Color(0xDB,0x70,0x93)); |
217 |
colorTable.put("papayawhip",new Color(0xFF,0xEF,0xD5)); |
218 |
colorTable.put("peachpuff",new Color(0xFF,0xDA,0xB9)); |
219 |
colorTable.put("peru",new Color(0xCD,0x85,0x3F)); |
220 |
colorTable.put("pink",new Color(0xFF,0xC0,0xCB)); |
221 |
colorTable.put("plum",new Color(0xDD,0xA0,0xDD)); |
222 |
colorTable.put("powderblue",new Color(0xB0,0xE0,0xE6)); |
223 |
colorTable.put("purple",new Color(0x80,0x00,0x80)); |
224 |
colorTable.put("rebeccapurple",new Color(0x66,0x33,0x99)); |
225 |
colorTable.put("red",new Color(0xFF,0x00,0x00)); |
226 |
colorTable.put("rosybrown",new Color(0xBC,0x8F,0x8F)); |
227 |
colorTable.put("royalblue",new Color(0x41,0x69,0xE1)); |
228 |
colorTable.put("saddlebrown",new Color(0x8B,0x45,0x13)); |
229 |
colorTable.put("salmon",new Color(0xFA,0x80,0x72)); |
230 |
colorTable.put("sandybrown",new Color(0xF4,0xA4,0x60)); |
231 |
colorTable.put("seagreen",new Color(0x2E,0x8B,0x57)); |
232 |
colorTable.put("seashell",new Color(0xFF,0xF5,0xEE)); |
233 |
colorTable.put("sienna",new Color(0xA0,0x52,0x2D)); |
234 |
colorTable.put("silver",new Color(0xC0,0xC0,0xC0)); |
235 |
colorTable.put("skyblue",new Color(0x87,0xCE,0xEB)); |
236 |
colorTable.put("slateblue",new Color(0x6A,0x5A,0xCD)); |
237 |
colorTable.put("Slategray",new Color(0x70,0x80,0x90)); |
238 |
colorTable.put("slategrey",new Color(0x70,0x80,0x90)); |
239 |
colorTable.put("snow",new Color(0xFF,0xFA,0xFA)); |
240 |
colorTable.put("springgreen",new Color(0x00,0xFF,0x7F)); |
241 |
colorTable.put("steelblue",new Color(0x46,0x82,0xB4)); |
242 |
colorTable.put("tan",new Color(0xD2,0xB4,0x8C)); |
243 |
colorTable.put("teal",new Color(0x00,0x80,0x80)); |
244 |
colorTable.put("thistle",new Color(0xD8,0xBF,0xD8)); |
245 |
colorTable.put("tomato",new Color(0xFF,0x63,0x47)); |
246 |
colorTable.put("turquoise",new Color(0x40,0xE0,0xD0)); |
247 |
colorTable.put("violet",new Color(0xEE,0x82,0xEE)); |
248 |
colorTable.put("wheat",new Color(0xF5,0xDE,0xB3)); |
249 |
colorTable.put("white",new Color(0xFF,0xFF,0xFF)); |
250 |
colorTable.put("whitesmoke",new Color(0xF5,0xF5,0xF5)); |
251 |
colorTable.put("yellow",new Color(0xFF,0xFF,0x00)); |
252 |
colorTable.put("yellowgreen",new Color(0x9A,0xCD,0x32)); |
253 |
} |
254 |
return colorTable.get(name.toLowerCase());
|
255 |
} |
256 |
|
257 |
public static class CoerceColorToString implements Coercion { |
258 |
|
259 |
@Override
|
260 |
public Object coerce(Object value) throws CoercionException { |
261 |
if( value == null ) { |
262 |
return null; |
263 |
} |
264 |
if( value instanceof Color ) { |
265 |
int rgb = ((Color) value).getRGB(); |
266 |
int r = ((Color) value).getRed(); |
267 |
int g = ((Color) value).getGreen(); |
268 |
int b = ((Color) value).getBlue(); |
269 |
int a = ((Color) value).getAlpha(); |
270 |
for (Map.Entry<String, Color> entry : colorTable.entrySet()) { |
271 |
String name = entry.getKey();
|
272 |
Color color = entry.getValue();
|
273 |
if( rgb==color.getRGB() && color.getAlpha()==a ) {
|
274 |
return name;
|
275 |
} |
276 |
} |
277 |
return String.format("rgb(0x%02x,0x%02x,0x%02x,0x%02x)", r,g,b,a); |
278 |
} |
279 |
throw new CoercionException("Can't convert '"+value.getClass().getName()+"' to string"); |
280 |
} |
281 |
|
282 |
} |
283 |
|
284 |
public static void selfRegister() { |
285 |
DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager(); |
286 |
dataTypesManager.addtype(DataTypes.COLOR, null, "Color", Color.class, new CoerceToColor()); |
287 |
dataTypesManager.addCoercion(DataTypes.STRING, new CoerceColorToString());
|
288 |
} |
289 |
} |