1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qglobal.h"
43 
44 #if defined(Q_CC_BOR)
45 // needed for qsort() because of a std namespace problem on Borland
46 #include "qplatformdefs.h"
47 #endif
48 
49 #include "qrgb.h"
50 #include "qstringlist.h"
51 
52 QT_BEGIN_NAMESPACE
53 
h2i(char hex)54 static inline int h2i(char hex)
55 {
56     if (hex >= '0' && hex <= '9')
57         return hex - '0';
58     if (hex >= 'a' && hex <= 'f')
59         return hex - 'a' + 10;
60     if (hex >= 'A' && hex <= 'F')
61         return hex - 'A' + 10;
62     return -1;
63 }
64 
hex2int(const char * s)65 static inline int hex2int(const char *s)
66 {
67     return (h2i(s[0]) << 4) | h2i(s[1]);
68 }
69 
hex2int(char s)70 static inline int hex2int(char s)
71 {
72     int h = h2i(s);
73     return (h << 4) | h;
74 }
75 
qt_get_hex_rgb(const char * name,QRgb * rgb)76 bool qt_get_hex_rgb(const char *name, QRgb *rgb)
77 {
78     if(name[0] != '#')
79         return false;
80     name++;
81     int len = qstrlen(name);
82     int r, g, b;
83     if (len == 12) {
84         r = hex2int(name);
85         g = hex2int(name + 4);
86         b = hex2int(name + 8);
87     } else if (len == 9) {
88         r = hex2int(name);
89         g = hex2int(name + 3);
90         b = hex2int(name + 6);
91     } else if (len == 6) {
92         r = hex2int(name);
93         g = hex2int(name + 2);
94         b = hex2int(name + 4);
95     } else if (len == 3) {
96         r = hex2int(name[0]);
97         g = hex2int(name[1]);
98         b = hex2int(name[2]);
99     } else {
100         r = g = b = -1;
101     }
102     if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255) {
103         *rgb = 0;
104         return false;
105     }
106     *rgb = qRgb(r, g ,b);
107     return true;
108 }
109 
qt_get_hex_rgb(const QChar * str,int len,QRgb * rgb)110 bool qt_get_hex_rgb(const QChar *str, int len, QRgb *rgb)
111 {
112     if (len > 13)
113         return false;
114     char tmp[16];
115     for(int i = 0; i < len; ++i)
116         tmp[i] = str[i].toLatin1();
117     tmp[len] = 0;
118     return qt_get_hex_rgb(tmp, rgb);
119 }
120 
121 #ifndef QT_NO_COLORNAMES
122 
123 /*
124   CSS color names = SVG 1.0 color names + transparent (rgba(0,0,0,0))
125 */
126 
127 #ifdef rgb
128 #  undef rgb
129 #endif
130 #define rgb(r,g,b) (0xff000000 | (r << 16) |  (g << 8) | b)
131 
132 static const struct RGBData {
133     const char *name;
134     uint  value;
135 } rgbTbl[] = {
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) }
284 };
285 
286 static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
287 
288 #undef rgb
289 
operator <(const char * name,const RGBData & data)290 inline bool operator<(const char *name, const RGBData &data)
291 { return qstrcmp(name, data.name) < 0; }
operator <(const RGBData & data,const char * name)292 inline bool operator<(const RGBData &data, const char *name)
293 { return qstrcmp(data.name, name) < 0; }
294 
get_named_rgb(const char * name_no_space,QRgb * rgb)295 static bool get_named_rgb(const char *name_no_space, QRgb *rgb)
296 {
297     QByteArray name = QByteArray(name_no_space).toLower();
298     const RGBData *r = qBinaryFind(rgbTbl, rgbTbl + rgbTblSize, name.constData());
299     if (r != rgbTbl + rgbTblSize) {
300         *rgb = r->value;
301         return true;
302     }
303     return false;
304 }
305 
qt_get_named_rgb(const char * name,QRgb * rgb)306 bool qt_get_named_rgb(const char *name, QRgb* rgb)
307 {
308     int len = int(strlen(name));
309     if(len > 255)
310         return false;
311     char name_no_space[256];
312     int pos = 0;
313     for(int i = 0; i < len; i++) {
314         if(name[i] != '\t' && name[i] != ' ')
315             name_no_space[pos++] = name[i];
316     }
317     name_no_space[pos] = 0;
318 
319     return get_named_rgb(name_no_space, rgb);
320 }
321 
qt_get_named_rgb(const QChar * name,int len,QRgb * rgb)322 bool qt_get_named_rgb(const QChar *name, int len, QRgb *rgb)
323 {
324     if(len > 255)
325         return false;
326     char name_no_space[256];
327     int pos = 0;
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();
331     }
332     name_no_space[pos] = 0;
333     return get_named_rgb(name_no_space, rgb);
334 }
335 
336 
qt_get_rgb_val(const char * name)337 uint qt_get_rgb_val(const char *name)
338 {
339     QRgb r = 0;
340     qt_get_named_rgb(name,&r);
341     return r;
342 }
343 
qt_get_colornames()344 QStringList qt_get_colornames()
345 {
346     int i = 0;
347     QStringList lst;
348     for (i = 0; i < rgbTblSize; i++)
349         lst << QLatin1String(rgbTbl[i].name);
350     return lst;
351 }
352 
353 #else
354 
qt_get_named_rgb(const char *,QRgb *)355 bool qt_get_named_rgb(const char *, QRgb*)
356 {
357     return false;
358 }
359 
qt_get_rgb_val(const char *)360 uint qt_get_rgb_val(const char *)
361 {
362     return 0;
363 }
qt_get_colornames()364 QStringList qt_get_colornames()
365 {
366     return QStringList();
367 }
368 #endif // QT_NO_COLORNAMES
369 
370 QT_END_NAMESPACE
371