forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxHtmlColor.java
More file actions
324 lines (299 loc) · 11.8 KB
/
mxHtmlColor.java
File metadata and controls
324 lines (299 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/**
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.util;
import java.awt.Color;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
/**
* Contains various helper methods for use with mxGraph.
*/
public class mxHtmlColor
{
private static final Logger log = Logger.getLogger(mxHtmlColor.class.getName());
/**
* HTML color lookup table. Supports the 147 CSS color names.
*/
protected static HashMap<String, Color> htmlColors = new HashMap<String, Color>();
protected static final Pattern rgbRegex = Pattern.compile(
"rgba?\\([^)]*\\)", Pattern.CASE_INSENSITIVE);
/**
*
*/
public static String hexString(Color color)
{
int r = color.getRed();
int g = color.getGreen();
int b = color.getBlue();
return String.format("#%02X%02X%02X", r, g, b);
}
/**
* Returns a hex representation for the given color.
*
* @param color
* Color to return the hex string for.
* @return Returns a hex string for the given color.
*/
public static String getHexColorString(Color color)
{
return Integer.toHexString((color.getRGB() & 0x00FFFFFF)
| (color.getAlpha() << 24));
}
/**
* Shortcut for parseColor with no transparency.
*/
public static Color parseColor(String str) throws NumberFormatException
{
return parseColor(str, 1);
};
/**
* Convert a string representing a 24/32bit hex color value into a Color
* object. All 147 CSS color names and none are also supported. None returns
* null.
* Examples of possible hex color values are: #C3D9FF, #6482B9 and #774400,
* but note that you do not include the "#" in the string passed in
*
* @param str
* the 24/32bit hex string value (ARGB)
* @return java.awt.Color (24bit RGB on JDK 1.1, 24/32bit ARGB on JDK1.2)
* @exception NumberFormatException
* if the specified string cannot be interpreted as a
* hexidecimal integer
*/
public static Color parseColor(String str, double alpha) throws NumberFormatException
{
if (str == null || str.equals(mxConstants.NONE))
{
return null;
}
else if (rgbRegex.matcher(str).matches())
{
return parseRgb(str);
}
else if (!str.startsWith("#"))
{
Color result = htmlColors.get(str);
// LATER: Return the result even if it's null to avoid invalid color codes
if (result != null)
{
return result;
}
}
else if (str.length() == 4)
{
// Adds support for special short notation of hex colors, eg. #abc=#aabbcc
str = new String(
new char[] { '#', str.charAt(1), str.charAt(1),
str.charAt(2), str.charAt(2), str.charAt(3),
str.charAt(3) });
}
int value = 0;
try
{
String tmp = str;
if (tmp.startsWith("#"))
{
tmp = tmp.substring(1);
}
value = (int) (Long.parseLong(tmp, 16) | (((int) (alpha * 255)) << 24));
}
catch (NumberFormatException nfe)
{
try
{
value = Long.decode(str).intValue();
}
catch (NumberFormatException e)
{
// ignores exception and returns black
log.log(Level.SEVERE, "Failed to parse color value", e);
}
}
return (alpha < 1) ? new Color(value, true) : new Color(value);
}
protected static Color parseRgb(String rgbString)
{
String[] values = rgbString.split("[,()]");
String red = values[1].trim();
String green = values[2].trim();
String blue = values[3].trim();
String alpha = "1.0";
if (values.length >= 5)
{
alpha = values[4].trim();
}
return new Color(parseValue(red, 255), parseValue(green, 255),
parseValue(blue, 255), parseAlpha(alpha));
}
protected static float parseValue(String val, int max)
{
if (val.endsWith("%"))
{
return (float) (parsePercent(val) * max / max);
}
return (float) (Integer.parseInt(val) / max);
}
protected static double parsePercent(String perc)
{
return Integer.parseInt(perc.substring(0, perc.length() - 1)) / 100.0;
}
protected static float parseAlpha(String alpha)
{
return Float.parseFloat(alpha);
}
/**
* Initializes HTML color table.
*/
static
{
htmlColors.put("aliceblue", parseColor("#F0F8FF"));
htmlColors.put("antiquewhite", parseColor("#FAEBD7"));
htmlColors.put("aqua", parseColor("#00FFFF"));
htmlColors.put("aquamarine", parseColor("#7FFFD4"));
htmlColors.put("azure", parseColor("#F0FFFF"));
htmlColors.put("beige", parseColor("#F5F5DC"));
htmlColors.put("bisque", parseColor("#FFE4C4"));
htmlColors.put("black", parseColor("#000000"));
htmlColors.put("blanchedalmond", parseColor("#FFEBCD"));
htmlColors.put("blue", parseColor("#0000FF"));
htmlColors.put("blueviolet", parseColor("#8A2BE2"));
htmlColors.put("brown", parseColor("#A52A2A"));
htmlColors.put("burlywood", parseColor("#DEB887"));
htmlColors.put("cadetblue", parseColor("#5F9EA0"));
htmlColors.put("chartreuse", parseColor("#7FFF00"));
htmlColors.put("chocolate", parseColor("#D2691E"));
htmlColors.put("coral", parseColor("#FF7F50"));
htmlColors.put("cornflowerblue", parseColor("#6495ED"));
htmlColors.put("cornsilk", parseColor("#FFF8DC"));
htmlColors.put("crimson", parseColor("#DC143C"));
htmlColors.put("cyan", parseColor("#00FFFF"));
htmlColors.put("darkblue", parseColor("#00008B"));
htmlColors.put("darkcyan", parseColor("#008B8B"));
htmlColors.put("darkgoldenrod", parseColor("#B8860B"));
htmlColors.put("darkgray", parseColor("#A9A9A9"));
htmlColors.put("darkgrey", parseColor("#A9A9A9"));
htmlColors.put("darkgreen", parseColor("#006400"));
htmlColors.put("darkkhaki", parseColor("#BDB76B"));
htmlColors.put("darkmagenta", parseColor("#8B008B"));
htmlColors.put("darkolivegreen", parseColor("#556B2F"));
htmlColors.put("darkorange", parseColor("#FF8C00"));
htmlColors.put("darkorchid", parseColor("#9932CC"));
htmlColors.put("darkred", parseColor("#8B0000"));
htmlColors.put("darksalmon", parseColor("#E9967A"));
htmlColors.put("darkseagreen", parseColor("#8FBC8F"));
htmlColors.put("darkslateblue", parseColor("#483D8B"));
htmlColors.put("darkslategray", parseColor("#2F4F4F"));
htmlColors.put("darkslategrey", parseColor("#2F4F4F"));
htmlColors.put("darkturquoise", parseColor("#00CED1"));
htmlColors.put("darkviolet", parseColor("#9400D3"));
htmlColors.put("deeppink", parseColor("#FF1493"));
htmlColors.put("deepskyblue", parseColor("#00BFFF"));
htmlColors.put("dimgray", parseColor("#696969"));
htmlColors.put("dimgrey", parseColor("#696969"));
htmlColors.put("dodgerblue", parseColor("#1E90FF"));
htmlColors.put("firebrick", parseColor("#B22222"));
htmlColors.put("floralwhite", parseColor("#FFFAF0"));
htmlColors.put("forestgreen", parseColor("#228B22"));
htmlColors.put("fuchsia", parseColor("#FF00FF"));
htmlColors.put("gainsboro", parseColor("#DCDCDC"));
htmlColors.put("ghostwhite", parseColor("#F8F8FF"));
htmlColors.put("gold", parseColor("#FFD700"));
htmlColors.put("goldenrod", parseColor("#DAA520"));
htmlColors.put("gray", parseColor("#808080"));
htmlColors.put("grey", parseColor("#808080"));
htmlColors.put("green", parseColor("#008000"));
htmlColors.put("greenyellow", parseColor("#ADFF2F"));
htmlColors.put("honeydew", parseColor("#F0FFF0"));
htmlColors.put("hotpink", parseColor("#FF69B4"));
htmlColors.put("indianred ", parseColor("#CD5C5C"));
htmlColors.put("indigo ", parseColor("#4B0082"));
htmlColors.put("ivory", parseColor("#FFFFF0"));
htmlColors.put("khaki", parseColor("#F0E68C"));
htmlColors.put("lavender", parseColor("#E6E6FA"));
htmlColors.put("lavenderblush", parseColor("#FFF0F5"));
htmlColors.put("lawngreen", parseColor("#7CFC00"));
htmlColors.put("lemonchiffon", parseColor("#FFFACD"));
htmlColors.put("lightblue", parseColor("#ADD8E6"));
htmlColors.put("lightcoral", parseColor("#F08080"));
htmlColors.put("lightcyan", parseColor("#E0FFFF"));
htmlColors.put("lightgoldenrodyellow", parseColor("#FAFAD2"));
htmlColors.put("lightgray", parseColor("#D3D3D3"));
htmlColors.put("lightgrey", parseColor("#D3D3D3"));
htmlColors.put("lightgreen", parseColor("#90EE90"));
htmlColors.put("lightpink", parseColor("#FFB6C1"));
htmlColors.put("lightsalmon", parseColor("#FFA07A"));
htmlColors.put("lightseagreen", parseColor("#20B2AA"));
htmlColors.put("lightskyblue", parseColor("#87CEFA"));
htmlColors.put("lightslategray", parseColor("#778899"));
htmlColors.put("lightslategrey", parseColor("#778899"));
htmlColors.put("lightsteelblue", parseColor("#B0C4DE"));
htmlColors.put("lightyellow", parseColor("#FFFFE0"));
htmlColors.put("lime", parseColor("#00FF00"));
htmlColors.put("limegreen", parseColor("#32CD32"));
htmlColors.put("linen", parseColor("#FAF0E6"));
htmlColors.put("magenta", parseColor("#FF00FF"));
htmlColors.put("maroon", parseColor("#800000"));
htmlColors.put("mediumaquamarine", parseColor("#66CDAA"));
htmlColors.put("mediumblue", parseColor("#0000CD"));
htmlColors.put("mediumorchid", parseColor("#BA55D3"));
htmlColors.put("mediumpurple", parseColor("#9370DB"));
htmlColors.put("mediumseagreen", parseColor("#3CB371"));
htmlColors.put("mediumslateblue", parseColor("#7B68EE"));
htmlColors.put("mediumspringgreen", parseColor("#00FA9A"));
htmlColors.put("mediumturquoise", parseColor("#48D1CC"));
htmlColors.put("mediumvioletred", parseColor("#C71585"));
htmlColors.put("midnightblue", parseColor("#191970"));
htmlColors.put("mintcream", parseColor("#F5FFFA"));
htmlColors.put("mistyrose", parseColor("#FFE4E1"));
htmlColors.put("moccasin", parseColor("#FFE4B5"));
htmlColors.put("navajowhite", parseColor("#FFDEAD"));
htmlColors.put("navy", parseColor("#000080"));
htmlColors.put("oldlace", parseColor("#FDF5E6"));
htmlColors.put("olive", parseColor("#808000"));
htmlColors.put("olivedrab", parseColor("#6B8E23"));
htmlColors.put("orange", parseColor("#FFA500"));
htmlColors.put("orangered", parseColor("#FF4500"));
htmlColors.put("orchid", parseColor("#DA70D6"));
htmlColors.put("palegoldenrod", parseColor("#EEE8AA"));
htmlColors.put("palegreen", parseColor("#98FB98"));
htmlColors.put("paleturquoise", parseColor("#AFEEEE"));
htmlColors.put("palevioletred", parseColor("#DB7093"));
htmlColors.put("papayawhip", parseColor("#FFEFD5"));
htmlColors.put("peachpuff", parseColor("#FFDAB9"));
htmlColors.put("peru", parseColor("#CD853F"));
htmlColors.put("pink", parseColor("#FFC0CB"));
htmlColors.put("plum", parseColor("#DDA0DD"));
htmlColors.put("powderblue", parseColor("#B0E0E6"));
htmlColors.put("purple", parseColor("#800080"));
htmlColors.put("red", parseColor("#FF0000"));
htmlColors.put("rosybrown", parseColor("#BC8F8F"));
htmlColors.put("royalblue", parseColor("#4169E1"));
htmlColors.put("saddlebrown", parseColor("#8B4513"));
htmlColors.put("salmon", parseColor("#FA8072"));
htmlColors.put("sandybrown", parseColor("#F4A460"));
htmlColors.put("seagreen", parseColor("#2E8B57"));
htmlColors.put("seashell", parseColor("#FFF5EE"));
htmlColors.put("sienna", parseColor("#A0522D"));
htmlColors.put("silver", parseColor("#C0C0C0"));
htmlColors.put("skyblue", parseColor("#87CEEB"));
htmlColors.put("slateblue", parseColor("#6A5ACD"));
htmlColors.put("slategray", parseColor("#708090"));
htmlColors.put("slategrey", parseColor("#708090"));
htmlColors.put("snow", parseColor("#FFFAFA"));
htmlColors.put("springgreen", parseColor("#00FF7F"));
htmlColors.put("steelblue", parseColor("#4682B4"));
htmlColors.put("tan", parseColor("#D2B48C"));
htmlColors.put("teal", parseColor("#008080"));
htmlColors.put("thistle", parseColor("#D8BFD8"));
htmlColors.put("tomato", parseColor("#FF6347"));
htmlColors.put("turquoise", parseColor("#40E0D0"));
htmlColors.put("violet", parseColor("#EE82EE"));
htmlColors.put("wheat", parseColor("#F5DEB3"));
htmlColors.put("white", parseColor("#FFFFFF"));
htmlColors.put("whitesmoke", parseColor("#F5F5F5"));
htmlColors.put("yellow", parseColor("#FFFF00"));
htmlColors.put("yellowgreen", parseColor("#9ACD32"));
}
}