1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 // needed for qsort() because of a std namespace problem on Borland
46 #include "qplatformdefs.h"
50 #include "qstringlist.h"
54 static inline int h2i(char hex)
56 if (hex >= '0' && hex <= '9')
58 if (hex >= 'a' && hex <= 'f')
59 return hex - 'a' + 10;
60 if (hex >= 'A' && hex <= 'F')
61 return hex - 'A' + 10;
65 static inline int hex2int(const char *s)
67 return (h2i(s[0]) << 4) | h2i(s[1]);
70 static inline int hex2int(char s)
76 bool qt_get_hex_rgb(const char *name, QRgb *rgb)
81 int len = qstrlen(name);
85 g = hex2int(name + 4);
86 b = hex2int(name + 8);
87 } else if (len == 9) {
89 g = hex2int(name + 3);
90 b = hex2int(name + 6);
91 } else if (len == 6) {
93 g = hex2int(name + 2);
94 b = hex2int(name + 4);
95 } else if (len == 3) {
102 if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255) {
106 *rgb = qRgb(r, g ,b);
110 bool qt_get_hex_rgb(const QChar *str, int len, QRgb *rgb)
115 for(int i = 0; i < len; ++i)
116 tmp[i] = str[i].toLatin1();
118 return qt_get_hex_rgb(tmp, rgb);
121 #ifndef QT_NO_COLORNAMES
124 CSS color names = SVG 1.0 color names + transparent (rgba(0,0,0,0))
130 #define rgb(r,g,b) (0xff000000 | (r << 16) | (g << 8) | b)
132 static const struct RGBData {
136 { "aliceblue", rgb(240, 248, 255) },
137 { "antiquewhite", rgb(250, 235, 215) },
138 { "aqua", rgb( 0, 255, 255) },
139 { "aquamarine", rgb(127, 255, 212) },
140 { "azure", rgb(240, 255, 255) },
141 { "beige", rgb(245, 245, 220) },
142 { "bisque", rgb(255, 228, 196) },
143 { "black", rgb( 0, 0, 0) },
144 { "blanchedalmond", rgb(255, 235, 205) },
145 { "blue", rgb( 0, 0, 255) },
146 { "blueviolet", rgb(138, 43, 226) },
147 { "brown", rgb(165, 42, 42) },
148 { "burlywood", rgb(222, 184, 135) },
149 { "cadetblue", rgb( 95, 158, 160) },
150 { "chartreuse", rgb(127, 255, 0) },
151 { "chocolate", rgb(210, 105, 30) },
152 { "coral", rgb(255, 127, 80) },
153 { "cornflowerblue", rgb(100, 149, 237) },
154 { "cornsilk", rgb(255, 248, 220) },
155 { "crimson", rgb(220, 20, 60) },
156 { "cyan", rgb( 0, 255, 255) },
157 { "darkblue", rgb( 0, 0, 139) },
158 { "darkcyan", rgb( 0, 139, 139) },
159 { "darkgoldenrod", rgb(184, 134, 11) },
160 { "darkgray", rgb(169, 169, 169) },
161 { "darkgreen", rgb( 0, 100, 0) },
162 { "darkgrey", rgb(169, 169, 169) },
163 { "darkkhaki", rgb(189, 183, 107) },
164 { "darkmagenta", rgb(139, 0, 139) },
165 { "darkolivegreen", rgb( 85, 107, 47) },
166 { "darkorange", rgb(255, 140, 0) },
167 { "darkorchid", rgb(153, 50, 204) },
168 { "darkred", rgb(139, 0, 0) },
169 { "darksalmon", rgb(233, 150, 122) },
170 { "darkseagreen", rgb(143, 188, 143) },
171 { "darkslateblue", rgb( 72, 61, 139) },
172 { "darkslategray", rgb( 47, 79, 79) },
173 { "darkslategrey", rgb( 47, 79, 79) },
174 { "darkturquoise", rgb( 0, 206, 209) },
175 { "darkviolet", rgb(148, 0, 211) },
176 { "deeppink", rgb(255, 20, 147) },
177 { "deepskyblue", rgb( 0, 191, 255) },
178 { "dimgray", rgb(105, 105, 105) },
179 { "dimgrey", rgb(105, 105, 105) },
180 { "dodgerblue", rgb( 30, 144, 255) },
181 { "firebrick", rgb(178, 34, 34) },
182 { "floralwhite", rgb(255, 250, 240) },
183 { "forestgreen", rgb( 34, 139, 34) },
184 { "fuchsia", rgb(255, 0, 255) },
185 { "gainsboro", rgb(220, 220, 220) },
186 { "ghostwhite", rgb(248, 248, 255) },
187 { "gold", rgb(255, 215, 0) },
188 { "goldenrod", rgb(218, 165, 32) },
189 { "gray", rgb(128, 128, 128) },
190 { "green", rgb( 0, 128, 0) },
191 { "greenyellow", rgb(173, 255, 47) },
192 { "grey", rgb(128, 128, 128) },
193 { "honeydew", rgb(240, 255, 240) },
194 { "hotpink", rgb(255, 105, 180) },
195 { "indianred", rgb(205, 92, 92) },
196 { "indigo", rgb( 75, 0, 130) },
197 { "ivory", rgb(255, 255, 240) },
198 { "khaki", rgb(240, 230, 140) },
199 { "lavender", rgb(230, 230, 250) },
200 { "lavenderblush", rgb(255, 240, 245) },
201 { "lawngreen", rgb(124, 252, 0) },
202 { "lemonchiffon", rgb(255, 250, 205) },
203 { "lightblue", rgb(173, 216, 230) },
204 { "lightcoral", rgb(240, 128, 128) },
205 { "lightcyan", rgb(224, 255, 255) },
206 { "lightgoldenrodyellow", rgb(250, 250, 210) },
207 { "lightgray", rgb(211, 211, 211) },
208 { "lightgreen", rgb(144, 238, 144) },
209 { "lightgrey", rgb(211, 211, 211) },
210 { "lightpink", rgb(255, 182, 193) },
211 { "lightsalmon", rgb(255, 160, 122) },
212 { "lightseagreen", rgb( 32, 178, 170) },
213 { "lightskyblue", rgb(135, 206, 250) },
214 { "lightslategray", rgb(119, 136, 153) },
215 { "lightslategrey", rgb(119, 136, 153) },
216 { "lightsteelblue", rgb(176, 196, 222) },
217 { "lightyellow", rgb(255, 255, 224) },
218 { "lime", rgb( 0, 255, 0) },
219 { "limegreen", rgb( 50, 205, 50) },
220 { "linen", rgb(250, 240, 230) },
221 { "magenta", rgb(255, 0, 255) },
222 { "maroon", rgb(128, 0, 0) },
223 { "mediumaquamarine", rgb(102, 205, 170) },
224 { "mediumblue", rgb( 0, 0, 205) },
225 { "mediumorchid", rgb(186, 85, 211) },
226 { "mediumpurple", rgb(147, 112, 219) },
227 { "mediumseagreen", rgb( 60, 179, 113) },
228 { "mediumslateblue", rgb(123, 104, 238) },
229 { "mediumspringgreen", rgb( 0, 250, 154) },
230 { "mediumturquoise", rgb( 72, 209, 204) },
231 { "mediumvioletred", rgb(199, 21, 133) },
232 { "midnightblue", rgb( 25, 25, 112) },
233 { "mintcream", rgb(245, 255, 250) },
234 { "mistyrose", rgb(255, 228, 225) },
235 { "moccasin", rgb(255, 228, 181) },
236 { "navajowhite", rgb(255, 222, 173) },
237 { "navy", rgb( 0, 0, 128) },
238 { "oldlace", rgb(253, 245, 230) },
239 { "olive", rgb(128, 128, 0) },
240 { "olivedrab", rgb(107, 142, 35) },
241 { "orange", rgb(255, 165, 0) },
242 { "orangered", rgb(255, 69, 0) },
243 { "orchid", rgb(218, 112, 214) },
244 { "palegoldenrod", rgb(238, 232, 170) },
245 { "palegreen", rgb(152, 251, 152) },
246 { "paleturquoise", rgb(175, 238, 238) },
247 { "palevioletred", rgb(219, 112, 147) },
248 { "papayawhip", rgb(255, 239, 213) },
249 { "peachpuff", rgb(255, 218, 185) },
250 { "peru", rgb(205, 133, 63) },
251 { "pink", rgb(255, 192, 203) },
252 { "plum", rgb(221, 160, 221) },
253 { "powderblue", rgb(176, 224, 230) },
254 { "purple", rgb(128, 0, 128) },
255 { "red", rgb(255, 0, 0) },
256 { "rosybrown", rgb(188, 143, 143) },
257 { "royalblue", rgb( 65, 105, 225) },
258 { "saddlebrown", rgb(139, 69, 19) },
259 { "salmon", rgb(250, 128, 114) },
260 { "sandybrown", rgb(244, 164, 96) },
261 { "seagreen", rgb( 46, 139, 87) },
262 { "seashell", rgb(255, 245, 238) },
263 { "sienna", rgb(160, 82, 45) },
264 { "silver", rgb(192, 192, 192) },
265 { "skyblue", rgb(135, 206, 235) },
266 { "slateblue", rgb(106, 90, 205) },
267 { "slategray", rgb(112, 128, 144) },
268 { "slategrey", rgb(112, 128, 144) },
269 { "snow", rgb(255, 250, 250) },
270 { "springgreen", rgb( 0, 255, 127) },
271 { "steelblue", rgb( 70, 130, 180) },
272 { "tan", rgb(210, 180, 140) },
273 { "teal", rgb( 0, 128, 128) },
274 { "thistle", rgb(216, 191, 216) },
275 { "tomato", rgb(255, 99, 71) },
276 { "transparent", 0 },
277 { "turquoise", rgb( 64, 224, 208) },
278 { "violet", rgb(238, 130, 238) },
279 { "wheat", rgb(245, 222, 179) },
280 { "white", rgb(255, 255, 255) },
281 { "whitesmoke", rgb(245, 245, 245) },
282 { "yellow", rgb(255, 255, 0) },
283 { "yellowgreen", rgb(154, 205, 50) }
286 static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
290 inline bool operator<(const char *name, const RGBData &data)
291 { return qstrcmp(name, data.name) < 0; }
292 inline bool operator<(const RGBData &data, const char *name)
293 { return qstrcmp(data.name, name) < 0; }
295 static bool get_named_rgb(const char *name_no_space, QRgb *rgb)
297 QByteArray name = QByteArray(name_no_space).toLower();
298 const RGBData *r = qBinaryFind(rgbTbl, rgbTbl + rgbTblSize, name.constData());
299 if (r != rgbTbl + rgbTblSize) {
306 bool qt_get_named_rgb(const char *name, QRgb* rgb)
308 int len = int(strlen(name));
311 char name_no_space[256];
313 for(int i = 0; i < len; i++) {
314 if(name[i] != '\t' && name[i] != ' ')
315 name_no_space[pos++] = name[i];
317 name_no_space[pos] = 0;
319 return get_named_rgb(name_no_space, rgb);
322 bool qt_get_named_rgb(const QChar *name, int len, QRgb *rgb)
326 char name_no_space[256];
328 for(int i = 0; i < len; i++) {
329 if(name[i] != QLatin1Char('\t') && name[i] != QLatin1Char(' '))
330 name_no_space[pos++] = name[i].toLatin1();
332 name_no_space[pos] = 0;
333 return get_named_rgb(name_no_space, rgb);
337 uint qt_get_rgb_val(const char *name)
340 qt_get_named_rgb(name,&r);
344 QStringList qt_get_colornames()
348 for (i = 0; i < rgbTblSize; i++)
349 lst << QLatin1String(rgbTbl[i].name);
355 bool qt_get_named_rgb(const char *, QRgb*)
360 uint qt_get_rgb_val(const char *)
364 QStringList qt_get_colornames()
366 return QStringList();
368 #endif // QT_NO_COLORNAMES