1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qcolor.h"
41 #include "qcolor_p.h"
42 #include "qdrawhelper_p.h"
43 #include "qfloat16.h"
44 #include "qnamespace.h"
45 #include "qdatastream.h"
46 #include "qvariant.h"
47 #include "qdebug.h"
48 #include "private/qtools_p.h"
49 
50 #include <algorithm>
51 
52 #include <stdio.h>
53 #include <limits.h>
54 
55 QT_BEGIN_NAMESPACE
56 
57 /*!
58     \internal
59     If s[0..n] is a valid hex number, returns its integer value,
60     otherwise returns -1.
61  */
hex2int(const char * s,int n)62 static inline int hex2int(const char *s, int n)
63 {
64     if (n < 0)
65         return -1;
66     int result = 0;
67     for (; n > 0; --n) {
68         result = result * 16;
69         const int h = QtMiscUtils::fromHex(*s++);
70         if (h < 0)
71             return -1;
72         result += h;
73     }
74     return result;
75 }
76 
get_hex_rgb(const char * name,size_t len,QRgba64 * rgb)77 static bool get_hex_rgb(const char *name, size_t len, QRgba64 *rgb)
78 {
79     if (name[0] != '#')
80         return false;
81     name++;
82     --len;
83     int a, r, g, b;
84     a = 65535;
85     if (len == 12) {
86         r = hex2int(name + 0, 4);
87         g = hex2int(name + 4, 4);
88         b = hex2int(name + 8, 4);
89     } else if (len == 9) {
90         r = hex2int(name + 0, 3);
91         g = hex2int(name + 3, 3);
92         b = hex2int(name + 6, 3);
93         if (r == -1 || g == -1 || b == -1)
94             return false;
95         r = (r << 4) | (r >> 8);
96         g = (g << 4) | (g >> 8);
97         b = (b << 4) | (b >> 8);
98     } else if (len == 8) {
99         a = hex2int(name + 0, 2) * 0x101;
100         r = hex2int(name + 2, 2) * 0x101;
101         g = hex2int(name + 4, 2) * 0x101;
102         b = hex2int(name + 6, 2) * 0x101;
103     } else if (len == 6) {
104         r = hex2int(name + 0, 2) * 0x101;
105         g = hex2int(name + 2, 2) * 0x101;
106         b = hex2int(name + 4, 2) * 0x101;
107     } else if (len == 3) {
108         r = hex2int(name + 0, 1) * 0x1111;
109         g = hex2int(name + 1, 1) * 0x1111;
110         b = hex2int(name + 2, 1) * 0x1111;
111     } else {
112         r = g = b = -1;
113     }
114     if ((uint)r > 65535 || (uint)g > 65535 || (uint)b > 65535 || (uint)a > 65535) {
115         *rgb = 0;
116         return false;
117     }
118     *rgb = qRgba64(r, g ,b, a);
119     return true;
120 }
121 
qt_get_hex_rgb(const char * name,QRgb * rgb)122 bool qt_get_hex_rgb(const char *name, QRgb *rgb)
123 {
124     QRgba64 rgba64;
125     if (!get_hex_rgb(name, qstrlen(name), &rgba64))
126         return false;
127     *rgb = rgba64.toArgb32();
128     return true;
129 }
130 
get_hex_rgb(const QChar * str,size_t len,QRgba64 * rgb)131 static bool get_hex_rgb(const QChar *str, size_t len, QRgba64 *rgb)
132 {
133     if (len > 13)
134         return false;
135     char tmp[16];
136     for (size_t i = 0; i < len; ++i)
137         tmp[i] = str[i].toLatin1();
138     tmp[len] = 0;
139     return get_hex_rgb(tmp, len, rgb);
140 }
141 
142 #ifndef QT_NO_COLORNAMES
143 
144 /*
145   CSS color names = SVG 1.0 color names + transparent (rgba(0,0,0,0))
146 */
147 
148 #ifdef rgb
149 #  undef rgb
150 #endif
151 #define rgb(r,g,b) (0xff000000 | (r << 16) |  (g << 8) | b)
152 
153 // keep this is in sync with QColorConstants
154 static const struct RGBData {
155     const char name[21];
156     uint  value;
157 } rgbTbl[] = {
158     { "aliceblue", rgb(240, 248, 255) },
159     { "antiquewhite", rgb(250, 235, 215) },
160     { "aqua", rgb( 0, 255, 255) },
161     { "aquamarine", rgb(127, 255, 212) },
162     { "azure", rgb(240, 255, 255) },
163     { "beige", rgb(245, 245, 220) },
164     { "bisque", rgb(255, 228, 196) },
165     { "black", rgb( 0, 0, 0) },
166     { "blanchedalmond", rgb(255, 235, 205) },
167     { "blue", rgb( 0, 0, 255) },
168     { "blueviolet", rgb(138, 43, 226) },
169     { "brown", rgb(165, 42, 42) },
170     { "burlywood", rgb(222, 184, 135) },
171     { "cadetblue", rgb( 95, 158, 160) },
172     { "chartreuse", rgb(127, 255, 0) },
173     { "chocolate", rgb(210, 105, 30) },
174     { "coral", rgb(255, 127, 80) },
175     { "cornflowerblue", rgb(100, 149, 237) },
176     { "cornsilk", rgb(255, 248, 220) },
177     { "crimson", rgb(220, 20, 60) },
178     { "cyan", rgb( 0, 255, 255) },
179     { "darkblue", rgb( 0, 0, 139) },
180     { "darkcyan", rgb( 0, 139, 139) },
181     { "darkgoldenrod", rgb(184, 134, 11) },
182     { "darkgray", rgb(169, 169, 169) },
183     { "darkgreen", rgb( 0, 100, 0) },
184     { "darkgrey", rgb(169, 169, 169) },
185     { "darkkhaki", rgb(189, 183, 107) },
186     { "darkmagenta", rgb(139, 0, 139) },
187     { "darkolivegreen", rgb( 85, 107, 47) },
188     { "darkorange", rgb(255, 140, 0) },
189     { "darkorchid", rgb(153, 50, 204) },
190     { "darkred", rgb(139, 0, 0) },
191     { "darksalmon", rgb(233, 150, 122) },
192     { "darkseagreen", rgb(143, 188, 143) },
193     { "darkslateblue", rgb( 72, 61, 139) },
194     { "darkslategray", rgb( 47, 79, 79) },
195     { "darkslategrey", rgb( 47, 79, 79) },
196     { "darkturquoise", rgb( 0, 206, 209) },
197     { "darkviolet", rgb(148, 0, 211) },
198     { "deeppink", rgb(255, 20, 147) },
199     { "deepskyblue", rgb( 0, 191, 255) },
200     { "dimgray", rgb(105, 105, 105) },
201     { "dimgrey", rgb(105, 105, 105) },
202     { "dodgerblue", rgb( 30, 144, 255) },
203     { "firebrick", rgb(178, 34, 34) },
204     { "floralwhite", rgb(255, 250, 240) },
205     { "forestgreen", rgb( 34, 139, 34) },
206     { "fuchsia", rgb(255, 0, 255) },
207     { "gainsboro", rgb(220, 220, 220) },
208     { "ghostwhite", rgb(248, 248, 255) },
209     { "gold", rgb(255, 215, 0) },
210     { "goldenrod", rgb(218, 165, 32) },
211     { "gray", rgb(128, 128, 128) },
212     { "green", rgb( 0, 128, 0) },
213     { "greenyellow", rgb(173, 255, 47) },
214     { "grey", rgb(128, 128, 128) },
215     { "honeydew", rgb(240, 255, 240) },
216     { "hotpink", rgb(255, 105, 180) },
217     { "indianred", rgb(205, 92, 92) },
218     { "indigo", rgb( 75, 0, 130) },
219     { "ivory", rgb(255, 255, 240) },
220     { "khaki", rgb(240, 230, 140) },
221     { "lavender", rgb(230, 230, 250) },
222     { "lavenderblush", rgb(255, 240, 245) },
223     { "lawngreen", rgb(124, 252, 0) },
224     { "lemonchiffon", rgb(255, 250, 205) },
225     { "lightblue", rgb(173, 216, 230) },
226     { "lightcoral", rgb(240, 128, 128) },
227     { "lightcyan", rgb(224, 255, 255) },
228     { "lightgoldenrodyellow", rgb(250, 250, 210) },
229     { "lightgray", rgb(211, 211, 211) },
230     { "lightgreen", rgb(144, 238, 144) },
231     { "lightgrey", rgb(211, 211, 211) },
232     { "lightpink", rgb(255, 182, 193) },
233     { "lightsalmon", rgb(255, 160, 122) },
234     { "lightseagreen", rgb( 32, 178, 170) },
235     { "lightskyblue", rgb(135, 206, 250) },
236     { "lightslategray", rgb(119, 136, 153) },
237     { "lightslategrey", rgb(119, 136, 153) },
238     { "lightsteelblue", rgb(176, 196, 222) },
239     { "lightyellow", rgb(255, 255, 224) },
240     { "lime", rgb( 0, 255, 0) },
241     { "limegreen", rgb( 50, 205, 50) },
242     { "linen", rgb(250, 240, 230) },
243     { "magenta", rgb(255, 0, 255) },
244     { "maroon", rgb(128, 0, 0) },
245     { "mediumaquamarine", rgb(102, 205, 170) },
246     { "mediumblue", rgb( 0, 0, 205) },
247     { "mediumorchid", rgb(186, 85, 211) },
248     { "mediumpurple", rgb(147, 112, 219) },
249     { "mediumseagreen", rgb( 60, 179, 113) },
250     { "mediumslateblue", rgb(123, 104, 238) },
251     { "mediumspringgreen", rgb( 0, 250, 154) },
252     { "mediumturquoise", rgb( 72, 209, 204) },
253     { "mediumvioletred", rgb(199, 21, 133) },
254     { "midnightblue", rgb( 25, 25, 112) },
255     { "mintcream", rgb(245, 255, 250) },
256     { "mistyrose", rgb(255, 228, 225) },
257     { "moccasin", rgb(255, 228, 181) },
258     { "navajowhite", rgb(255, 222, 173) },
259     { "navy", rgb( 0, 0, 128) },
260     { "oldlace", rgb(253, 245, 230) },
261     { "olive", rgb(128, 128, 0) },
262     { "olivedrab", rgb(107, 142, 35) },
263     { "orange", rgb(255, 165, 0) },
264     { "orangered", rgb(255, 69, 0) },
265     { "orchid", rgb(218, 112, 214) },
266     { "palegoldenrod", rgb(238, 232, 170) },
267     { "palegreen", rgb(152, 251, 152) },
268     { "paleturquoise", rgb(175, 238, 238) },
269     { "palevioletred", rgb(219, 112, 147) },
270     { "papayawhip", rgb(255, 239, 213) },
271     { "peachpuff", rgb(255, 218, 185) },
272     { "peru", rgb(205, 133, 63) },
273     { "pink", rgb(255, 192, 203) },
274     { "plum", rgb(221, 160, 221) },
275     { "powderblue", rgb(176, 224, 230) },
276     { "purple", rgb(128, 0, 128) },
277     { "red", rgb(255, 0, 0) },
278     { "rosybrown", rgb(188, 143, 143) },
279     { "royalblue", rgb( 65, 105, 225) },
280     { "saddlebrown", rgb(139, 69, 19) },
281     { "salmon", rgb(250, 128, 114) },
282     { "sandybrown", rgb(244, 164, 96) },
283     { "seagreen", rgb( 46, 139, 87) },
284     { "seashell", rgb(255, 245, 238) },
285     { "sienna", rgb(160, 82, 45) },
286     { "silver", rgb(192, 192, 192) },
287     { "skyblue", rgb(135, 206, 235) },
288     { "slateblue", rgb(106, 90, 205) },
289     { "slategray", rgb(112, 128, 144) },
290     { "slategrey", rgb(112, 128, 144) },
291     { "snow", rgb(255, 250, 250) },
292     { "springgreen", rgb( 0, 255, 127) },
293     { "steelblue", rgb( 70, 130, 180) },
294     { "tan", rgb(210, 180, 140) },
295     { "teal", rgb( 0, 128, 128) },
296     { "thistle", rgb(216, 191, 216) },
297     { "tomato", rgb(255, 99, 71) },
298     { "transparent", 0 },
299     { "turquoise", rgb( 64, 224, 208) },
300     { "violet", rgb(238, 130, 238) },
301     { "wheat", rgb(245, 222, 179) },
302     { "white", rgb(255, 255, 255) },
303     { "whitesmoke", rgb(245, 245, 245) },
304     { "yellow", rgb(255, 255, 0) },
305     { "yellowgreen", rgb(154, 205, 50) }
306 };
307 
308 static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
309 
310 #undef rgb
311 
operator <(const char * name,const RGBData & data)312 inline bool operator<(const char *name, const RGBData &data)
313 { return qstrcmp(name, data.name) < 0; }
operator <(const RGBData & data,const char * name)314 inline bool operator<(const RGBData &data, const char *name)
315 { return qstrcmp(data.name, name) < 0; }
316 
get_named_rgb_no_space(const char * name_no_space,QRgb * rgb)317 static bool get_named_rgb_no_space(const char *name_no_space, QRgb *rgb)
318 {
319     const RGBData *r = std::lower_bound(rgbTbl, rgbTbl + rgbTblSize, name_no_space);
320     if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r)) {
321         *rgb = r->value;
322         return true;
323     }
324     return false;
325 }
326 
get_named_rgb(const char * name,int len,QRgb * rgb)327 static bool get_named_rgb(const char *name, int len, QRgb* rgb)
328 {
329     if (len > 255)
330         return false;
331     char name_no_space[256];
332     int pos = 0;
333     for (int i = 0; i < len; i++) {
334         if (name[i] != '\t' && name[i] != ' ')
335             name_no_space[pos++] = QChar::toLower(name[i]);
336     }
337     name_no_space[pos] = 0;
338 
339     return get_named_rgb_no_space(name_no_space, rgb);
340 }
341 
get_named_rgb(const QChar * name,int len,QRgb * rgb)342 static bool get_named_rgb(const QChar *name, int len, QRgb *rgb)
343 {
344     if (len > 255)
345         return false;
346     char name_no_space[256];
347     int pos = 0;
348     for (int i = 0; i < len; i++) {
349         if (name[i] != QLatin1Char('\t') && name[i] != QLatin1Char(' '))
350             name_no_space[pos++] = name[i].toLower().toLatin1();
351     }
352     name_no_space[pos] = 0;
353     return get_named_rgb_no_space(name_no_space, rgb);
354 }
355 
356 #endif // QT_NO_COLORNAMES
357 
get_colornames()358 static QStringList get_colornames()
359 {
360     QStringList lst;
361 #ifndef QT_NO_COLORNAMES
362     lst.reserve(rgbTblSize);
363     for (int i = 0; i < rgbTblSize; i++)
364         lst << QLatin1String(rgbTbl[i].name);
365 #endif
366     return lst;
367 }
368 
369 /*!
370     \class QColor
371     \brief The QColor class provides colors based on RGB, HSV or CMYK values.
372 
373     \ingroup painting
374     \ingroup appearance
375     \inmodule QtGui
376 
377 
378     A color is normally specified in terms of RGB (red, green, and
379     blue) components, but it is also possible to specify it in terms
380     of HSV (hue, saturation, and value) and CMYK (cyan, magenta,
381     yellow and black) components. In addition a color can be specified
382     using a color name. The color name can be any of the SVG 1.0 color
383     names.
384 
385     \table
386     \header
387     \li RGB \li HSV \li CMYK
388     \row
389     \li \inlineimage qcolor-rgb.png
390     \li \inlineimage qcolor-hsv.png
391     \li \inlineimage qcolor-cmyk.png
392     \endtable
393 
394     The QColor constructor creates the color based on RGB values.  To
395     create a QColor based on either HSV or CMYK values, use the
396     toHsv() and toCmyk() functions respectively. These functions
397     return a copy of the color using the desired format. In addition
398     the static fromRgb(), fromHsv() and fromCmyk() functions create
399     colors from the specified values. Alternatively, a color can be
400     converted to any of the three formats using the convertTo()
401     function (returning a copy of the color in the desired format), or
402     any of the setRgb(), setHsv() and setCmyk() functions altering \e
403     this color's format. The spec() function tells how the color was
404     specified.
405 
406     A color can be set by passing an RGB string (such as "#112233"),
407     or an ARGB string (such as "#ff112233") or a color name (such as "blue"),
408     to the setNamedColor() function.
409     The color names are taken from the SVG 1.0 color names. The name()
410     function returns the name of the color in the format
411     "#RRGGBB". Colors can also be set using setRgb(), setHsv() and
412     setCmyk(). To get a lighter or darker color use the lighter() and
413     darker() functions respectively.
414 
415     The isValid() function indicates whether a QColor is legal at
416     all. For example, a RGB color with RGB values out of range is
417     illegal. For performance reasons, QColor mostly disregards illegal
418     colors, and for that reason, the result of using an invalid color
419     is undefined.
420 
421     The color components can be retrieved individually, e.g with
422     red(), hue() and cyan(). The values of the color components can
423     also be retrieved in one go using the getRgb(), getHsv() and
424     getCmyk() functions. Using the RGB color model, the color
425     components can in addition be accessed with rgb().
426 
427     There are several related non-members: QRgb is a typdef for an
428     unsigned int representing the RGB value triplet (r, g, b). Note
429     that it also can hold a value for the alpha-channel (for more
430     information, see the \l {QColor#Alpha-Blended
431     Drawing}{Alpha-Blended Drawing} section). The qRed(), qBlue() and
432     qGreen() functions return the respective component of the given
433     QRgb value, while the qRgb() and qRgba() functions create and
434     return the QRgb triplet based on the given component
435     values. Finally, the qAlpha() function returns the alpha component
436     of the provided QRgb, and the qGray() function calculates and
437     return a gray value based on the given value.
438 
439     QColor is platform and device independent. The QColormap class
440     maps the color to the hardware.
441 
442     For more information about painting in general, see the \l{Paint
443     System} documentation.
444 
445     \tableofcontents
446 
447     \section1 Integer vs. Floating Point Precision
448 
449     QColor supports floating point precision and provides floating
450     point versions of all the color components functions,
451     e.g. getRgbF(), hueF() and fromCmykF(). Note that since the
452     components are stored using 16-bit integers, there might be minor
453     deviations between the values set using, for example, setRgbF()
454     and the values returned by the getRgbF() function due to rounding.
455 
456     While the integer based functions take values in the range 0-255
457     (except hue() which must have values within the range 0-359),
458     the floating point functions accept values in the range 0.0 - 1.0.
459 
460     \section1 Alpha-Blended Drawing
461 
462     QColor also support alpha-blended outlining and filling. The
463     alpha channel of a color specifies the transparency effect, 0
464     represents a fully transparent color, while 255 represents a fully
465     opaque color. For example:
466 
467     \snippet code/src_gui_painting_qcolor.cpp 0
468 
469     The code above produces the following output:
470 
471     \image alphafill.png
472 
473     The alpha channel of a color can be retrieved and set using the
474     alpha() and setAlpha() functions if its value is an integer, and
475     alphaF() and setAlphaF() if its value is qreal (double). By
476     default, the alpha-channel is set to 255 (opaque). To retrieve and
477     set \e all the RGB color components (including the alpha-channel)
478     in one go, use the rgba() and setRgba() functions.
479 
480     \section1 Predefined Colors
481 
482     There are 20 predefined QColor objects in the \c{QColorConstants}
483     namespace, including black, white, primary and secondary colors,
484     darker versions of these colors, and three shades of gray.
485     Furthermore, the \c{QColorConstants::Svg} namespace defines QColor
486     objects for the standard \l{https://www.w3.org/TR/SVG11/types.html#ColorKeywords}{SVG color keyword names}.
487 
488     \image qt-colors.png Qt Colors
489 
490     The \c{QColorConstants::Color0}, \c{QColorConstants::Color1} and
491     \c{QColorConstants::Transparent} colors are used for special
492     purposes.
493 
494     \c{QColorConstants::Color0} (zero pixel value) and
495     \c{QColorConstants::Color1} (non-zero pixel value) are special
496     colors for drawing in QBitmaps. Painting with
497     \c{QColorConstants::Color0} sets the bitmap bits to 0 (transparent;
498     i.e., background), and painting with c{QColorConstants::Color1}
499     sets the bits to 1 (opaque; i.e., foreground).
500 
501     \c{QColorConstants::Transparent} is used to indicate a transparent
502     pixel. When painting with this value, a pixel value will be used
503     that is appropriate for the underlying pixel format in use.
504 
505     For historical reasons, the 20 predefined colors are also available
506     in the Qt::GlobalColor enumeration.
507 
508     Finally, QColor recognizes a variety of color names (as strings);
509     the static colorNames() function returns a QStringList color names
510     that QColor knows about.
511 
512     \section1 The Extended RGB Color Model
513 
514     The extended RGB color model, also known as the scRGB color space,
515     is the same the RGB color model except it allows values under 0.0,
516     and over 1.0. This makes it possible to represent colors that would
517     otherwise be outside the range of the RGB colorspace but still use
518     the same values for colors inside the RGB colorspace.
519 
520     \section1 The HSV Color Model
521 
522     The RGB model is hardware-oriented. Its representation is close to
523     what most monitors show. In contrast, HSV represents color in a way
524     more suited to the human perception of color. For example, the
525     relationships "stronger than", "darker than", and "the opposite of"
526     are easily expressed in HSV but are much harder to express in RGB.
527 
528     HSV, like RGB, has three components:
529 
530     \list
531     \li H, for hue, is in the range 0 to 359 if the color is chromatic (not
532     gray), or meaningless if it is gray. It represents degrees on the
533     color wheel familiar to most people. Red is 0 (degrees), green is
534     120, and blue is 240.
535 
536     \inlineimage qcolor-hue.png
537 
538     \li S, for saturation, is in the range 0 to 255, and the bigger it is,
539     the stronger the color is. Grayish colors have saturation near 0; very
540     strong colors have saturation near 255.
541 
542     \inlineimage qcolor-saturation.png
543 
544     \li V, for value, is in the range 0 to 255 and represents lightness or
545     brightness of the color. 0 is black; 255 is as far from black as
546     possible.
547 
548     \inlineimage qcolor-value.png
549     \endlist
550 
551     Here are some examples: pure red is H=0, S=255, V=255; a dark red,
552     moving slightly towards the magenta, could be H=350 (equivalent to
553     -10), S=255, V=180; a grayish light red could have H about 0 (say
554     350-359 or 0-10), S about 50-100, and S=255.
555 
556     Qt returns a hue value of -1 for achromatic colors. If you pass a
557     hue value that is too large, Qt forces it into range. Hue 360 or 720 is
558     treated as 0; hue 540 is treated as 180.
559 
560     In addition to the standard HSV model, Qt provides an
561     alpha-channel to feature \l {QColor#Alpha-Blended
562     Drawing}{alpha-blended drawing}.
563 
564     \section1 The HSL Color Model
565 
566     HSL is similar to HSV, however instead of the Value parameter, HSL
567     specifies a Lightness parameter which maps somewhat differently to the
568     brightness of the color.
569 
570     Similarly, the HSL saturation value is not in general the same as the HSV
571     saturation value for the same color. hslSaturation() provides the color's
572     HSL saturation value, while saturation() and hsvSaturation() provides the
573     HSV saturation value.
574 
575     The hue value is defined to be the same in HSL and HSV.
576 
577     \section1 The CMYK Color Model
578 
579     While the RGB and HSV color models are used for display on
580     computer monitors, the CMYK model is used in the four-color
581     printing process of printing presses and some hard-copy
582     devices.
583 
584     CMYK has four components, all in the range 0-255: cyan (C),
585     magenta (M), yellow (Y) and black (K).  Cyan, magenta and yellow
586     are called subtractive colors; the CMYK color model creates color
587     by starting with a white surface and then subtracting color by
588     applying the appropriate components. While combining cyan, magenta
589     and yellow gives the color black, subtracting one or more will
590     yield any other color. When combined in various percentages, these
591     three colors can create the entire spectrum of colors.
592 
593     Mixing 100 percent of cyan, magenta and yellow \e does produce
594     black, but the result is unsatisfactory since it wastes ink,
595     increases drying time, and gives a muddy colour when printing. For
596     that reason, black is added in professional printing to provide a
597     solid black tone; hence the term 'four color process'.
598 
599     In addition to the standard CMYK model, Qt provides an
600     alpha-channel to feature \l {QColor#Alpha-Blended
601     Drawing}{alpha-blended drawing}.
602 
603     \sa QPalette, QBrush, QColorConstants
604 */
605 
606 #define QCOLOR_INT_RANGE_CHECK(fn, var) \
607     do { \
608         if (var < 0 || var > 255) { \
609             qWarning(#fn": invalid value %d", var); \
610             var = qMax(0, qMin(var, 255)); \
611         } \
612     } while (0)
613 
614 #define QCOLOR_REAL_RANGE_CHECK(fn, var) \
615     do { \
616         if (var < qreal(0.0) || var > qreal(1.0)) { \
617             qWarning(#fn": invalid value %g", var); \
618             var = qMax(qreal(0.0), qMin(var, qreal(1.0)));      \
619         } \
620     } while (0)
621 
622 /*****************************************************************************
623   QColor member functions
624  *****************************************************************************/
625 
626 /*!
627     \enum QColor::Spec
628 
629     The type of color specified, either RGB, extended RGB, HSV, CMYK or HSL.
630 
631     \value Rgb
632     \value Hsv
633     \value Cmyk
634     \value Hsl
635     \value ExtendedRgb
636     \value Invalid
637 
638     \sa spec(), convertTo()
639 */
640 
641 /*!
642     \enum QColor::NameFormat
643 
644     How to format the output of the name() function
645 
646     \value HexRgb #RRGGBB A "#" character followed by three two-digit hexadecimal numbers (i.e. \c{#RRGGBB}).
647     \value HexArgb #AARRGGBB A "#" character followed by four two-digit hexadecimal numbers (i.e. \c{#AARRGGBB}).
648 
649     \sa name()
650 */
651 
652 /*!
653     \fn Spec QColor::spec() const
654 
655     Returns how the color was specified.
656 
657     \sa Spec, convertTo()
658 */
659 
660 
661 /*!
662     \fn QColor::QColor()
663 
664     Constructs an invalid color with the RGB value (0, 0, 0). An
665     invalid color is a color that is not properly set up for the
666     underlying window system.
667 
668     The alpha value of an invalid color is unspecified.
669 
670     \sa isValid()
671 */
672 
673 /*!
674     \overload
675 
676     Constructs a new color with a color value of \a color.
677 
678     \sa isValid(), {QColor#Predefined Colors}{Predefined Colors}
679  */
QColor(Qt::GlobalColor color)680 QColor::QColor(Qt::GlobalColor color) noexcept
681 {
682 #define QRGB(r, g, b) \
683     QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
684 #define QRGBA(r, g, b, a) \
685     QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
686 
687     static const QRgb global_colors[] = {
688         QRGB(255, 255, 255), // Qt::color0
689         QRGB(  0,   0,   0), // Qt::color1
690         QRGB(  0,   0,   0), // black
691         QRGB(255, 255, 255), // white
692         /*
693          * From the "The Palette Manager: How and Why" by Ron Gery,
694          * March 23, 1992, archived on MSDN:
695          *
696          *     The Windows system palette is broken up into two
697          *     sections, one with fixed colors and one with colors
698          *     that can be changed by applications. The system palette
699          *     predefines 20 entries; these colors are known as the
700          *     static or reserved colors and consist of the 16 colors
701          *     found in the Windows version 3.0 VGA driver and 4
702          *     additional colors chosen for their visual appeal.  The
703          *     DEFAULT_PALETTE stock object is, as the name implies,
704          *     the default palette selected into a device context (DC)
705          *     and consists of these static colors. Applications can
706          *     set the remaining 236 colors using the Palette Manager.
707          *
708          * The 20 reserved entries have indices in [0,9] and
709          * [246,255]. We reuse 17 of them.
710          */
711         QRGB(128, 128, 128), // index 248   medium gray
712         QRGB(160, 160, 164), // index 247   light gray
713         QRGB(192, 192, 192), // index 7     light gray
714         QRGB(255,   0,   0), // index 249   red
715         QRGB(  0, 255,   0), // index 250   green
716         QRGB(  0,   0, 255), // index 252   blue
717         QRGB(  0, 255, 255), // index 254   cyan
718         QRGB(255,   0, 255), // index 253   magenta
719         QRGB(255, 255,   0), // index 251   yellow
720         QRGB(128,   0,   0), // index 1     dark red
721         QRGB(  0, 128,   0), // index 2     dark green
722         QRGB(  0,   0, 128), // index 4     dark blue
723         QRGB(  0, 128, 128), // index 6     dark cyan
724         QRGB(128,   0, 128), // index 5     dark magenta
725         QRGB(128, 128,   0), // index 3     dark yellow
726         QRGBA(0, 0, 0, 0)    //             transparent
727     };
728 #undef QRGB
729 #undef QRGBA
730 
731     setRgb(qRed(global_colors[color]),
732            qGreen(global_colors[color]),
733            qBlue(global_colors[color]),
734            qAlpha(global_colors[color]));
735 }
736 
737 /*!
738     \fn QColor::QColor(int r, int g, int b, int a = 255)
739 
740     Constructs a color with the RGB value \a r, \a g, \a b, and the
741     alpha-channel (transparency) value of \a a.
742 
743     The color is left invalid if any of the arguments are invalid.
744 
745     \sa setRgba(), isValid()
746 */
747 
748 /*!
749     Constructs a color with the value \a color. The alpha component is
750     ignored and set to solid.
751 
752     \sa fromRgb(), isValid()
753 */
754 
QColor(QRgb color)755 QColor::QColor(QRgb color) noexcept
756 {
757     cspec = Rgb;
758     ct.argb.alpha = 0xffff;
759     ct.argb.red   = qRed(color)   * 0x101;
760     ct.argb.green = qGreen(color) * 0x101;
761     ct.argb.blue  = qBlue(color)  * 0x101;
762     ct.argb.pad   = 0;
763 }
764 
765 /*!
766     \since 5.6
767 
768     Constructs a color with the value \a rgba64.
769 
770     \sa fromRgba64()
771 */
772 
QColor(QRgba64 rgba64)773 QColor::QColor(QRgba64 rgba64) noexcept
774 {
775     setRgba64(rgba64);
776 }
777 
778 /*!
779     \internal
780 
781     Constructs a color with the given \a spec.
782 
783     This function is primarly present to avoid that QColor::Invalid
784     becomes a valid color by accident.
785 */
786 
QColor(Spec spec)787 QColor::QColor(Spec spec) noexcept
788 {
789     switch (spec) {
790     case Invalid:
791         invalidate();
792         break;
793     case Rgb:
794         setRgb(0, 0, 0);
795         break;
796     case Hsv:
797         setHsv(0, 0, 0);
798         break;
799     case Cmyk:
800         setCmyk(0, 0, 0, 0);
801         break;
802     case Hsl:
803         setHsl(0, 0, 0, 0);
804         break;
805     case ExtendedRgb:
806         cspec = spec;
807         setRgbF(0, 0, 0, 0);
808         break;
809     }
810 }
811 
812 /*!
813     \fn QColor::QColor(const QString &name)
814 
815     Constructs a named color in the same way as setNamedColor() using
816     the given \a name.
817 
818     The color is left invalid if the \a name cannot be parsed.
819 
820     \sa setNamedColor(), name(), isValid()
821 */
822 
823 /*!
824     \fn QColor::QColor(const char *name)
825 
826     Constructs a named color in the same way as setNamedColor() using
827     the given \a name.
828 
829     \overload
830     \sa setNamedColor(), name(), isValid()
831 */
832 
833 /*!
834     \fn QColor::QColor(QLatin1String name)
835 
836     Constructs a named color in the same way as setNamedColor() using
837     the given \a name.
838 
839     \overload
840     \since 5.8
841     \sa setNamedColor(), name(), isValid()
842 */
843 
844 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
845 /*!
846     \fn QColor::QColor(const QColor &color)
847 
848     Constructs a color that is a copy of \a color.
849 
850     \sa isValid()
851 */
852 #endif
853 
854 /*!
855     \fn bool QColor::isValid() const
856 
857     Returns \c true if the color is valid; otherwise returns \c false.
858 */
859 
860 /*!
861     Returns the name of the color in the format "#RRGGBB"; i.e. a "#"
862     character followed by three two-digit hexadecimal numbers.
863 
864     \sa setNamedColor()
865 */
866 
name() const867 QString QColor::name() const
868 {
869     return name(HexRgb);
870 }
871 
872 /*!
873     \since 5.2
874 
875     Returns the name of the color in the specified \a format.
876 
877     \sa setNamedColor(), NameFormat
878 */
879 
name(NameFormat format) const880 QString QColor::name(NameFormat format) const
881 {
882     switch (format) {
883     case HexRgb:
884         return QLatin1Char('#') + QString::number(rgba() | 0x1000000, 16).rightRef(6);
885     case HexArgb:
886         // it's called rgba() but it does return AARRGGBB
887         return QLatin1Char('#') + QString::number(rgba() | Q_INT64_C(0x100000000), 16).rightRef(8);
888     }
889     return QString();
890 }
891 
892 #if QT_STRINGVIEW_LEVEL < 2
893 /*!
894     Sets the RGB value of this QColor to \a name, which may be in one
895     of these formats:
896 
897     \list
898     \li #RGB (each of R, G, and B is a single hex digit)
899     \li #RRGGBB
900     \li #AARRGGBB (Since 5.2)
901     \li #RRRGGGBBB
902     \li #RRRRGGGGBBBB
903     \li A name from the list of colors defined in the list of
904        \l{https://www.w3.org/TR/SVG11/types.html#ColorKeywords}{SVG color keyword names}
905        provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
906        These color names work on all platforms. Note that these color names are \e not the
907        same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
908        refer to the same color.
909     \li \c transparent - representing the absence of a color.
910     \endlist
911 
912     The color is invalid if \a name cannot be parsed.
913 
914     \sa QColor(), name(), isValid()
915 */
916 
setNamedColor(const QString & name)917 void QColor::setNamedColor(const QString &name)
918 {
919     setColorFromString(qToStringViewIgnoringNull(name));
920 }
921 #endif
922 
923 /*!
924     \overload
925     \since 5.10
926 */
927 
setNamedColor(QStringView name)928 void QColor::setNamedColor(QStringView name)
929 {
930     setColorFromString(name);
931 }
932 
933 /*!
934     \overload
935     \since 5.8
936 */
937 
setNamedColor(QLatin1String name)938 void QColor::setNamedColor(QLatin1String name)
939 {
940     setColorFromString(name);
941 }
942 
943 #if QT_STRINGVIEW_LEVEL < 2
944 /*!
945    \since 4.7
946 
947    Returns \c true if the \a name is a valid color name and can
948    be used to construct a valid QColor object, otherwise returns
949    false.
950 
951    It uses the same algorithm used in setNamedColor().
952 
953    \sa setNamedColor()
954 */
isValidColor(const QString & name)955 bool QColor::isValidColor(const QString &name)
956 {
957     return isValidColor(qToStringViewIgnoringNull(name));
958 }
959 #endif
960 
961 /*!
962     \overload
963     \since 5.10
964 */
isValidColor(QStringView name)965 bool QColor::isValidColor(QStringView name) noexcept
966 {
967     return name.size() && QColor().setColorFromString(name);
968 }
969 
970 /*!
971     \overload
972     \since 5.8
973 */
isValidColor(QLatin1String name)974 bool QColor::isValidColor(QLatin1String name) noexcept
975 {
976     return name.size() && QColor().setColorFromString(name);
977 }
978 
979 template <typename String>
setColorFromString(String name)980 bool QColor::setColorFromString(String name)
981 {
982     if (!name.size()) {
983         invalidate();
984         return true;
985     }
986 
987     if (name[0] == QLatin1Char('#')) {
988         QRgba64 rgba;
989         if (get_hex_rgb(name.data(), name.size(), &rgba)) {
990             setRgba64(rgba);
991             return true;
992         } else {
993             invalidate();
994             return false;
995         }
996     }
997 
998 #ifndef QT_NO_COLORNAMES
999     QRgb rgb;
1000     if (get_named_rgb(name.data(), name.size(), &rgb)) {
1001         setRgba(rgb);
1002         return true;
1003     } else
1004 #endif
1005     {
1006         invalidate();
1007         return false;
1008     }
1009 }
1010 
1011 /*!
1012     Returns a QStringList containing the color names Qt knows about.
1013 
1014     \sa {QColor#Predefined Colors}{Predefined Colors}
1015 */
colorNames()1016 QStringList QColor::colorNames()
1017 {
1018     return get_colornames();
1019 }
1020 
1021 /*!
1022     Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
1023     saturation, value, and alpha-channel (transparency) components of the
1024     color's HSV value.
1025 
1026     These components can be retrieved individually using the hueF(),
1027     saturationF(), valueF() and alphaF() functions.
1028 
1029     \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1030 */
getHsvF(qreal * h,qreal * s,qreal * v,qreal * a) const1031 void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
1032 {
1033         if (!h || !s || !v)
1034         return;
1035 
1036     if (cspec != Invalid && cspec != Hsv) {
1037         toHsv().getHsvF(h, s, v, a);
1038         return;
1039     }
1040 
1041     *h = ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
1042     *s = ct.ahsv.saturation / qreal(USHRT_MAX);
1043     *v = ct.ahsv.value / qreal(USHRT_MAX);
1044 
1045     if (a)
1046         *a = ct.ahsv.alpha / qreal(USHRT_MAX);
1047 }
1048 
1049 /*!
1050     Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
1051     saturation, value, and alpha-channel (transparency) components of the
1052     color's HSV value.
1053 
1054     These components can be retrieved individually using the hue(),
1055     saturation(), value() and alpha() functions.
1056 
1057     \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1058 */
getHsv(int * h,int * s,int * v,int * a) const1059 void QColor::getHsv(int *h, int *s, int *v, int *a) const
1060 {
1061     if (!h || !s || !v)
1062         return;
1063 
1064     if (cspec != Invalid && cspec != Hsv) {
1065         toHsv().getHsv(h, s, v, a);
1066         return;
1067     }
1068 
1069     *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1070     *s = qt_div_257(ct.ahsv.saturation);
1071     *v = qt_div_257(ct.ahsv.value);
1072 
1073     if (a)
1074         *a = qt_div_257(ct.ahsv.alpha);
1075 }
1076 
1077 /*!
1078     Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
1079     the value and \a a is the alpha component of the HSV color.
1080 
1081     All the values must be in the range 0.0-1.0.
1082 
1083     \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1084 */
setHsvF(qreal h,qreal s,qreal v,qreal a)1085 void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a)
1086 {
1087     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
1088         || (s < qreal(0.0) || s > qreal(1.0))
1089         || (v < qreal(0.0) || v > qreal(1.0))
1090         || (a < qreal(0.0) || a > qreal(1.0))) {
1091         qWarning("QColor::setHsvF: HSV parameters out of range");
1092         return;
1093     }
1094 
1095     cspec = Hsv;
1096     ct.ahsv.alpha      = qRound(a * USHRT_MAX);
1097     ct.ahsv.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
1098     ct.ahsv.saturation = qRound(s * USHRT_MAX);
1099     ct.ahsv.value      = qRound(v * USHRT_MAX);
1100     ct.ahsv.pad        = 0;
1101 }
1102 
1103 /*!
1104     Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
1105     the value and \a a is the alpha component of the HSV color.
1106 
1107     The saturation, value and alpha-channel values must be in the range 0-255,
1108     and the hue value must be greater than -1.
1109 
1110     \sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1111 */
setHsv(int h,int s,int v,int a)1112 void QColor::setHsv(int h, int s, int v, int a)
1113 {
1114     if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
1115         qWarning("QColor::setHsv: HSV parameters out of range");
1116         invalidate();
1117         return;
1118     }
1119 
1120     cspec = Hsv;
1121     ct.ahsv.alpha      = a * 0x101;
1122     ct.ahsv.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
1123     ct.ahsv.saturation = s * 0x101;
1124     ct.ahsv.value      = v * 0x101;
1125     ct.ahsv.pad        = 0;
1126 }
1127 
1128 /*!
1129     \since 4.6
1130 
1131     Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
1132     saturation, lightness, and alpha-channel (transparency) components of the
1133     color's HSL value.
1134 
1135     These components can be retrieved individually using the hslHueF(),
1136     hslSaturationF(), lightnessF() and alphaF() functions.
1137 
1138     \sa getHsl(), setHslF(), {QColor#The HSL Color Model}{The HSL Color Model}
1139 */
getHslF(qreal * h,qreal * s,qreal * l,qreal * a) const1140 void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
1141 {
1142         if (!h || !s || !l)
1143         return;
1144 
1145     if (cspec != Invalid && cspec != Hsl) {
1146         toHsl().getHslF(h, s, l, a);
1147         return;
1148     }
1149 
1150     *h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
1151     *s = ct.ahsl.saturation / qreal(USHRT_MAX);
1152     *l = ct.ahsl.lightness / qreal(USHRT_MAX);
1153 
1154     if (a)
1155         *a = ct.ahsl.alpha / qreal(USHRT_MAX);
1156 }
1157 
1158 /*!
1159     \since 4.6
1160 
1161     Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
1162     saturation, lightness, and alpha-channel (transparency) components of the
1163     color's HSL value.
1164 
1165     These components can be retrieved individually using the hslHue(),
1166     hslSaturation(), lightness() and alpha() functions.
1167 
1168     \sa getHslF(), setHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
1169 */
getHsl(int * h,int * s,int * l,int * a) const1170 void QColor::getHsl(int *h, int *s, int *l, int *a) const
1171 {
1172     if (!h || !s || !l)
1173         return;
1174 
1175     if (cspec != Invalid && cspec != Hsl) {
1176         toHsl().getHsl(h, s, l, a);
1177         return;
1178     }
1179 
1180     *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1181     *s = qt_div_257(ct.ahsl.saturation);
1182     *l = qt_div_257(ct.ahsl.lightness);
1183 
1184     if (a)
1185         *a = qt_div_257(ct.ahsl.alpha);
1186 }
1187 
1188 /*!
1189     \since 4.6
1190 
1191     Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
1192     the lightness and \a a is the alpha component of the HSL color.
1193 
1194     All the values must be in the range 0.0-1.0.
1195 
1196     \sa getHslF(), setHsl()
1197 */
setHslF(qreal h,qreal s,qreal l,qreal a)1198 void QColor::setHslF(qreal h, qreal s, qreal l, qreal a)
1199 {
1200     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
1201         || (s < qreal(0.0) || s > qreal(1.0))
1202         || (l < qreal(0.0) || l > qreal(1.0))
1203         || (a < qreal(0.0) || a > qreal(1.0))) {
1204         qWarning("QColor::setHsvF: HSV parameters out of range");
1205         return;
1206     }
1207 
1208     cspec = Hsl;
1209     ct.ahsl.alpha      = qRound(a * USHRT_MAX);
1210     ct.ahsl.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
1211     ct.ahsl.saturation = qRound(s * USHRT_MAX);
1212     ct.ahsl.lightness  = qRound(l * USHRT_MAX);
1213     ct.ahsl.pad        = 0;
1214 }
1215 
1216 /*!
1217     \since 4.6
1218 
1219     Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
1220     the lightness and \a a is the alpha component of the HSL color.
1221 
1222     The saturation, value and alpha-channel values must be in the range 0-255,
1223     and the hue value must be greater than -1.
1224 
1225     \sa getHsl(), setHslF()
1226 */
setHsl(int h,int s,int l,int a)1227 void QColor::setHsl(int h, int s, int l, int a)
1228 {
1229     if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
1230         qWarning("QColor::setHsv: HSV parameters out of range");
1231         invalidate();
1232         return;
1233     }
1234 
1235     cspec = Hsl;
1236     ct.ahsl.alpha      = a * 0x101;
1237     ct.ahsl.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
1238     ct.ahsl.saturation = s * 0x101;
1239     ct.ahsl.lightness  = l * 0x101;
1240     ct.ahsl.pad        = 0;
1241 }
1242 
castF16(quint16 & v)1243 static inline qfloat16 &castF16(quint16 &v)
1244 {
1245     // this works because qfloat16 internally is a quint16
1246     return *reinterpret_cast<qfloat16 *>(&v);
1247 }
1248 
castF16(const quint16 & v)1249 static inline const qfloat16 &castF16(const quint16 &v)
1250 {
1251     return *reinterpret_cast<const qfloat16 *>(&v);
1252 }
1253 
1254 /*!
1255     Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
1256     green, blue, and alpha-channel (transparency) components of the color's
1257     RGB value.
1258 
1259     These components can be retrieved individually using the redF(), greenF(),
1260     blueF() and alphaF() functions.
1261 
1262     \sa rgb(), setRgb()
1263 */
getRgbF(qreal * r,qreal * g,qreal * b,qreal * a) const1264 void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
1265 {
1266     if (!r || !g || !b)
1267         return;
1268 
1269     if (cspec == Invalid)
1270         return;
1271 
1272     if (cspec != Rgb && cspec != ExtendedRgb) {
1273         toRgb().getRgbF(r, g, b, a);
1274         return;
1275     }
1276 
1277     if (cspec == Rgb) {
1278         *r = ct.argb.red   / qreal(USHRT_MAX);
1279         *g = ct.argb.green / qreal(USHRT_MAX);
1280         *b = ct.argb.blue  / qreal(USHRT_MAX);
1281         if (a)
1282             *a = ct.argb.alpha / qreal(USHRT_MAX);
1283     }  else {
1284         *r = castF16(ct.argbExtended.redF16);
1285         *g = castF16(ct.argbExtended.greenF16);
1286         *b = castF16(ct.argbExtended.blueF16);
1287         if (a)
1288             *a = castF16(ct.argbExtended.alphaF16);
1289     }
1290 }
1291 
1292 /*!
1293     Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
1294     green, blue, and alpha-channel (transparency) components of the color's
1295     RGB value.
1296 
1297     These components can be retrieved individually using the red(), green(),
1298     blue() and alpha() functions.
1299 
1300     \sa rgb(), setRgb()
1301 */
getRgb(int * r,int * g,int * b,int * a) const1302 void QColor::getRgb(int *r, int *g, int *b, int *a) const
1303 {
1304     if (!r || !g || !b)
1305         return;
1306 
1307     if (cspec != Invalid && cspec != Rgb) {
1308         toRgb().getRgb(r, g, b, a);
1309         return;
1310     }
1311 
1312     *r = qt_div_257(ct.argb.red);
1313     *g = qt_div_257(ct.argb.green);
1314     *b = qt_div_257(ct.argb.blue);
1315 
1316     if (a)
1317         *a = qt_div_257(ct.argb.alpha);
1318 }
1319 
1320 /*!
1321     \fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
1322 
1323     Sets the color channels of this color to \a r (red), \a g (green),
1324     \a b (blue) and \a a (alpha, transparency).
1325 
1326     The alpha value must be in the range 0.0-1.0.
1327     If any of the other values are outside the range of 0.0-1.0 the
1328     color model will be set as \c ExtendedRgb.
1329 
1330     \sa rgb(), getRgbF(), setRgb()
1331 */
setRgbF(qreal r,qreal g,qreal b,qreal a)1332 void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
1333 {
1334     if (a < qreal(0.0) || a > qreal(1.0)) {
1335         qWarning("QColor::setRgbF: Alpha parameter is out of range");
1336         invalidate();
1337         return;
1338     }
1339     if (r < qreal(0.0) || r > qreal(1.0) ||
1340         g < qreal(0.0) || g > qreal(1.0) ||
1341         b < qreal(0.0) || b > qreal(1.0) || cspec == ExtendedRgb) {
1342         cspec = ExtendedRgb;
1343         castF16(ct.argbExtended.redF16)   = qfloat16(r);
1344         castF16(ct.argbExtended.greenF16) = qfloat16(g);
1345         castF16(ct.argbExtended.blueF16)  = qfloat16(b);
1346         castF16(ct.argbExtended.alphaF16) = qfloat16(a);
1347         ct.argbExtended.pad   = 0;
1348         return;
1349     }
1350     cspec = Rgb;
1351     ct.argb.red   = qRound(r * USHRT_MAX);
1352     ct.argb.green = qRound(g * USHRT_MAX);
1353     ct.argb.blue  = qRound(b * USHRT_MAX);
1354     ct.argb.alpha = qRound(a * USHRT_MAX);
1355     ct.argb.pad   = 0;
1356 }
1357 
1358 /*!
1359     Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
1360 
1361     All the values must be in the range 0-255.
1362 
1363     \sa rgb(), getRgb(), setRgbF()
1364 */
setRgb(int r,int g,int b,int a)1365 void QColor::setRgb(int r, int g, int b, int a)
1366 {
1367     if (!isRgbaValid(r, g, b, a)) {
1368         qWarning("QColor::setRgb: RGB parameters out of range");
1369         invalidate();
1370         return;
1371     }
1372 
1373     cspec = Rgb;
1374     ct.argb.alpha = a * 0x101;
1375     ct.argb.red   = r * 0x101;
1376     ct.argb.green = g * 0x101;
1377     ct.argb.blue  = b * 0x101;
1378     ct.argb.pad   = 0;
1379 }
1380 
1381 /*!
1382     \fn QRgb QColor::rgba() const
1383 
1384     Returns the RGB value of the color, including its alpha.
1385 
1386     For an invalid color, the alpha value of the returned color is unspecified.
1387 
1388     \sa setRgba(), rgb(), rgba64()
1389 */
1390 
rgba() const1391 QRgb QColor::rgba() const noexcept
1392 {
1393     if (cspec != Invalid && cspec != Rgb)
1394         return toRgb().rgba();
1395     return qRgba(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue), qt_div_257(ct.argb.alpha));
1396 }
1397 
1398 /*!
1399     Sets the RGB value to \a rgba, including its alpha.
1400 
1401     \sa rgba(), rgb(), setRgba64()
1402 */
setRgba(QRgb rgba)1403 void QColor::setRgba(QRgb rgba) noexcept
1404 {
1405     cspec = Rgb;
1406     ct.argb.alpha = qAlpha(rgba) * 0x101;
1407     ct.argb.red   = qRed(rgba)   * 0x101;
1408     ct.argb.green = qGreen(rgba) * 0x101;
1409     ct.argb.blue  = qBlue(rgba)  * 0x101;
1410     ct.argb.pad   = 0;
1411 }
1412 
1413 /*!
1414     \since 5.6
1415 
1416     Returns the RGB64 value of the color, including its alpha.
1417 
1418     For an invalid color, the alpha value of the returned color is unspecified.
1419 
1420     \sa setRgba64(), rgba(), rgb()
1421 */
1422 
rgba64() const1423 QRgba64 QColor::rgba64() const noexcept
1424 {
1425     if (cspec != Invalid && cspec != Rgb)
1426         return toRgb().rgba64();
1427     return qRgba64(ct.argb.red, ct.argb.green, ct.argb.blue, ct.argb.alpha);
1428 }
1429 
1430 /*!
1431     \since 5.6
1432 
1433     Sets the RGB64 value to \a rgba, including its alpha.
1434 
1435     \sa setRgba(), rgba64()
1436 */
setRgba64(QRgba64 rgba)1437 void QColor::setRgba64(QRgba64 rgba) noexcept
1438 {
1439     cspec = Rgb;
1440     ct.argb.alpha = rgba.alpha();
1441     ct.argb.red   = rgba.red();
1442     ct.argb.green = rgba.green();
1443     ct.argb.blue  = rgba.blue();
1444     ct.argb.pad   = 0;
1445 }
1446 
1447 /*!
1448     \fn QRgb QColor::rgb() const
1449 
1450     Returns the RGB value of the color. The alpha value is opaque.
1451 
1452     \sa getRgb(), rgba()
1453 */
rgb() const1454 QRgb QColor::rgb() const noexcept
1455 {
1456     if (cspec != Invalid && cspec != Rgb)
1457         return toRgb().rgb();
1458     return qRgb(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue));
1459 }
1460 
1461 /*!
1462     \overload
1463 
1464     Sets the RGB value to \a rgb. The alpha value is set to opaque.
1465 */
setRgb(QRgb rgb)1466 void QColor::setRgb(QRgb rgb) noexcept
1467 {
1468     cspec = Rgb;
1469     ct.argb.alpha = 0xffff;
1470     ct.argb.red   = qRed(rgb)   * 0x101;
1471     ct.argb.green = qGreen(rgb) * 0x101;
1472     ct.argb.blue  = qBlue(rgb)  * 0x101;
1473     ct.argb.pad   = 0;
1474 }
1475 
1476 /*!
1477     Returns the alpha color component of this color.
1478 
1479     \sa setAlpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1480 */
alpha() const1481 int QColor::alpha() const noexcept
1482 {
1483     if (cspec == ExtendedRgb)
1484         return qRound(qreal(castF16(ct.argbExtended.alphaF16)) * 255);
1485     return qt_div_257(ct.argb.alpha);
1486 }
1487 
1488 
1489 /*!
1490     Sets the alpha of this color to \a alpha. Integer alpha is specified in the
1491     range 0-255.
1492 
1493     \sa alpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1494 */
1495 
setAlpha(int alpha)1496 void QColor::setAlpha(int alpha)
1497 {
1498     QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
1499     if (cspec == ExtendedRgb) {
1500         constexpr qreal f = qreal(1.0) / 255;
1501         castF16(ct.argbExtended.alphaF16) = alpha * f;
1502         return;
1503     }
1504     ct.argb.alpha = alpha * 0x101;
1505 }
1506 
1507 /*!
1508     Returns the alpha color component of this color.
1509 
1510     \sa setAlphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1511 */
alphaF() const1512 qreal QColor::alphaF() const noexcept
1513 {
1514     if (cspec == ExtendedRgb)
1515         return castF16(ct.argbExtended.alphaF16);
1516     return ct.argb.alpha / qreal(USHRT_MAX);
1517 }
1518 
1519 /*!
1520     Sets the alpha of this color to \a alpha. qreal alpha is specified in the
1521     range 0.0-1.0.
1522 
1523     \sa alphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1524 
1525 */
setAlphaF(qreal alpha)1526 void QColor::setAlphaF(qreal alpha)
1527 {
1528     QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
1529     if (cspec == ExtendedRgb) {
1530         castF16(ct.argbExtended.alphaF16) = alpha;
1531         return;
1532     }
1533     qreal tmp = alpha * USHRT_MAX;
1534     ct.argb.alpha = qRound(tmp);
1535 }
1536 
1537 
1538 /*!
1539     Returns the red color component of this color.
1540 
1541     \sa setRed(), redF(), getRgb()
1542 */
red() const1543 int QColor::red() const noexcept
1544 {
1545     if (cspec != Invalid && cspec != Rgb)
1546         return toRgb().red();
1547     return qt_div_257(ct.argb.red);
1548 }
1549 
1550 /*!
1551     Sets the red color component of this color to \a red. Integer components
1552     are specified in the range 0-255.
1553 
1554     \sa red(), redF(), setRgb()
1555 */
setRed(int red)1556 void QColor::setRed(int red)
1557 {
1558     QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
1559     if (cspec != Rgb)
1560         setRgb(red, green(), blue(), alpha());
1561     else
1562         ct.argb.red = red * 0x101;
1563 }
1564 
1565 /*!
1566     Returns the green color component of this color.
1567 
1568     \sa setGreen(), greenF(), getRgb()
1569 */
green() const1570 int QColor::green() const noexcept
1571 {
1572     if (cspec != Invalid && cspec != Rgb)
1573         return toRgb().green();
1574     return qt_div_257(ct.argb.green);
1575 }
1576 
1577 /*!
1578     Sets the green color component of this color to \a green. Integer
1579     components are specified in the range 0-255.
1580 
1581     \sa green(), greenF(), setRgb()
1582 */
setGreen(int green)1583 void QColor::setGreen(int green)
1584 {
1585     QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
1586     if (cspec != Rgb)
1587         setRgb(red(), green, blue(), alpha());
1588     else
1589         ct.argb.green = green * 0x101;
1590 }
1591 
1592 
1593 /*!
1594     Returns the blue color component of this color.
1595 
1596     \sa setBlue(), blueF(), getRgb()
1597 */
blue() const1598 int QColor::blue() const noexcept
1599 {
1600     if (cspec != Invalid && cspec != Rgb)
1601         return toRgb().blue();
1602     return qt_div_257(ct.argb.blue);
1603 }
1604 
1605 
1606 /*!
1607     Sets the blue color component of this color to \a blue. Integer components
1608     are specified in the range 0-255.
1609 
1610     \sa blue(), blueF(), setRgb()
1611 */
setBlue(int blue)1612 void QColor::setBlue(int blue)
1613 {
1614     QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
1615     if (cspec != Rgb)
1616         setRgb(red(), green(), blue, alpha());
1617     else
1618         ct.argb.blue = blue * 0x101;
1619 }
1620 
1621 /*!
1622     Returns the red color component of this color.
1623 
1624     \sa setRedF(), red(), getRgbF()
1625 */
redF() const1626 qreal QColor::redF() const noexcept
1627 {
1628     if (cspec == Rgb || cspec == Invalid)
1629         return ct.argb.red / qreal(USHRT_MAX);
1630     if (cspec == ExtendedRgb)
1631         return castF16(ct.argbExtended.redF16);
1632 
1633     return toRgb().redF();
1634 }
1635 
1636 
1637 /*!
1638     Sets the red color component of this color to \a red. If \a red lies outside
1639     the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1640 
1641     \sa redF(), red(), setRgbF()
1642 */
setRedF(qreal red)1643 void QColor::setRedF(qreal red)
1644 {
1645     if (cspec == Rgb && red >= qreal(0.0) && red <= qreal(1.0))
1646         ct.argb.red = qRound(red * USHRT_MAX);
1647     else if (cspec == ExtendedRgb)
1648         castF16(ct.argbExtended.redF16) = red;
1649     else
1650         setRgbF(red, greenF(), blueF(), alphaF());
1651 }
1652 
1653 /*!
1654     Returns the green color component of this color.
1655 
1656     \sa setGreenF(), green(), getRgbF()
1657 */
greenF() const1658 qreal QColor::greenF() const noexcept
1659 {
1660     if (cspec == Rgb || cspec == Invalid)
1661         return ct.argb.green / qreal(USHRT_MAX);
1662     if (cspec == ExtendedRgb)
1663         return castF16(ct.argbExtended.greenF16);
1664 
1665     return toRgb().greenF();
1666 }
1667 
1668 
1669 /*!
1670     Sets the green color component of this color to \a green. If \a green lies outside
1671     the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1672 
1673     \sa greenF(), green(), setRgbF()
1674 */
setGreenF(qreal green)1675 void QColor::setGreenF(qreal green)
1676 {
1677     if (cspec == Rgb && green >= qreal(0.0) && green <= qreal(1.0))
1678         ct.argb.green = qRound(green * USHRT_MAX);
1679     else if (cspec == ExtendedRgb)
1680         castF16(ct.argbExtended.greenF16) = green;
1681     else
1682         setRgbF(redF(), green, blueF(), alphaF());
1683 }
1684 
1685 /*!
1686     Returns the blue color component of this color.
1687 
1688      \sa setBlueF(), blue(), getRgbF()
1689 */
blueF() const1690 qreal QColor::blueF() const noexcept
1691 {
1692     if (cspec == Rgb || cspec == Invalid)
1693         return ct.argb.blue / qreal(USHRT_MAX);
1694     if (cspec == ExtendedRgb)
1695         return castF16(ct.argbExtended.blueF16);
1696 
1697     return toRgb().blueF();
1698 }
1699 
1700 /*!
1701     Sets the blue color component of this color to \a blue. If \a blue lies outside
1702     the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1703     \sa blueF(), blue(), setRgbF()
1704 */
setBlueF(qreal blue)1705 void QColor::setBlueF(qreal blue)
1706 {
1707     if (cspec == Rgb && blue >= qreal(0.0) && blue <= qreal(1.0))
1708         ct.argb.blue = qRound(blue * USHRT_MAX);
1709     else if (cspec == ExtendedRgb)
1710         castF16(ct.argbExtended.blueF16) = blue;
1711     else
1712         setRgbF(redF(), greenF(), blue, alphaF());
1713 }
1714 
1715 /*!
1716     Returns the HSV hue color component of this color.
1717 
1718     The color is implicitly converted to HSV.
1719 
1720     \sa hsvHue(), hslHue(), hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1721 */
1722 
hue() const1723 int QColor::hue() const noexcept
1724 {
1725     return hsvHue();
1726 }
1727 
1728 /*!
1729     Returns the HSV hue color component of this color.
1730 
1731     \sa hueF(), hslHue(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1732 */
hsvHue() const1733 int QColor::hsvHue() const noexcept
1734 {
1735     if (cspec != Invalid && cspec != Hsv)
1736         return toHsv().hue();
1737     return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1738 }
1739 
1740 /*!
1741     Returns the HSV saturation color component of this color.
1742 
1743     The color is implicitly converted to HSV.
1744 
1745     \sa hsvSaturation(), hslSaturation(), saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1746     Model}
1747 */
1748 
saturation() const1749 int QColor::saturation() const noexcept
1750 {
1751     return hsvSaturation();
1752 }
1753 
1754 /*!
1755     Returns the HSV saturation color component of this color.
1756 
1757     \sa saturationF(), hslSaturation(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1758 */
hsvSaturation() const1759 int QColor::hsvSaturation() const noexcept
1760 {
1761     if (cspec != Invalid && cspec != Hsv)
1762         return toHsv().saturation();
1763     return qt_div_257(ct.ahsv.saturation);
1764 }
1765 
1766 /*!
1767     Returns the value color component of this color.
1768 
1769     \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1770 */
value() const1771 int QColor::value() const noexcept
1772 {
1773     if (cspec != Invalid && cspec != Hsv)
1774         return toHsv().value();
1775     return qt_div_257(ct.ahsv.value);
1776 }
1777 
1778 /*!
1779     Returns the HSV hue color component of this color.
1780 
1781     The color is implicitly converted to HSV.
1782 
1783     \sa hsvHueF(), hslHueF(), hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1784 */
hueF() const1785 qreal QColor::hueF() const noexcept
1786 {
1787     return hsvHueF();
1788 }
1789 
1790 /*!
1791     Returns the hue color component of this color.
1792 
1793     \sa hue(), hslHueF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1794     Model}
1795 */
hsvHueF() const1796 qreal QColor::hsvHueF() const noexcept
1797 {
1798     if (cspec != Invalid && cspec != Hsv)
1799         return toHsv().hueF();
1800     return ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
1801 }
1802 
1803 /*!
1804     Returns the HSV saturation color component of this color.
1805 
1806      The color is implicitly converted to HSV.
1807 
1808     \sa hsvSaturationF(), hslSaturationF(), saturation(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1809     Model}
1810 */
saturationF() const1811 qreal QColor::saturationF() const noexcept
1812 {
1813     return hsvSaturationF();
1814 }
1815 
1816 /*!
1817     Returns the HSV saturation color component of this color.
1818 
1819     \sa saturation(), hslSaturationF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1820 */
hsvSaturationF() const1821 qreal QColor::hsvSaturationF() const noexcept
1822 {
1823     if (cspec != Invalid && cspec != Hsv)
1824         return toHsv().saturationF();
1825     return ct.ahsv.saturation / qreal(USHRT_MAX);
1826 }
1827 
1828 /*!
1829     Returns the value color component of this color.
1830 
1831     \sa value(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1832 */
valueF() const1833 qreal QColor::valueF() const noexcept
1834 {
1835     if (cspec != Invalid && cspec != Hsv)
1836         return toHsv().valueF();
1837     return ct.ahsv.value / qreal(USHRT_MAX);
1838 }
1839 
1840 /*!
1841     \since 4.6
1842 
1843     Returns the HSL hue color component of this color.
1844 
1845     \sa hslHueF(), hsvHue(), getHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
1846 */
hslHue() const1847 int QColor::hslHue() const noexcept
1848 {
1849     if (cspec != Invalid && cspec != Hsl)
1850         return toHsl().hslHue();
1851     return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1852 }
1853 
1854 /*!
1855     \since 4.6
1856 
1857     Returns the HSL saturation color component of this color.
1858 
1859     \sa hslSaturationF(), hsvSaturation(), getHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
1860 */
hslSaturation() const1861 int QColor::hslSaturation() const noexcept
1862 {
1863     if (cspec != Invalid && cspec != Hsl)
1864         return toHsl().hslSaturation();
1865     return qt_div_257(ct.ahsl.saturation);
1866 }
1867 
1868 /*!
1869     \since 4.6
1870 
1871     Returns the lightness color component of this color.
1872 
1873     \sa lightnessF(), getHsl()
1874 */
lightness() const1875 int QColor::lightness() const noexcept
1876 {
1877     if (cspec != Invalid && cspec != Hsl)
1878         return toHsl().lightness();
1879     return qt_div_257(ct.ahsl.lightness);
1880 }
1881 
1882 /*!
1883     \since 4.6
1884 
1885     Returns the HSL hue color component of this color.
1886 
1887     \sa hslHue(), hsvHueF(), getHslF()
1888 */
hslHueF() const1889 qreal QColor::hslHueF() const noexcept
1890 {
1891     if (cspec != Invalid && cspec != Hsl)
1892         return toHsl().hslHueF();
1893     return ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
1894 }
1895 
1896 /*!
1897     \since 4.6
1898 
1899     Returns the HSL saturation color component of this color.
1900 
1901     \sa hslSaturation(), hsvSaturationF(), getHslF(), {QColor#The HSL Color Model}{The HSL Color Model}
1902 */
hslSaturationF() const1903 qreal QColor::hslSaturationF() const noexcept
1904 {
1905     if (cspec != Invalid && cspec != Hsl)
1906         return toHsl().hslSaturationF();
1907     return ct.ahsl.saturation / qreal(USHRT_MAX);
1908 }
1909 
1910 /*!
1911     \since 4.6
1912 
1913     Returns the lightness color component of this color.
1914 
1915     \sa value(), getHslF()
1916 */
lightnessF() const1917 qreal QColor::lightnessF() const noexcept
1918 {
1919     if (cspec != Invalid && cspec != Hsl)
1920         return toHsl().lightnessF();
1921     return ct.ahsl.lightness / qreal(USHRT_MAX);
1922 }
1923 
1924 /*!
1925     Returns the cyan color component of this color.
1926 
1927     \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1928 */
cyan() const1929 int QColor::cyan() const noexcept
1930 {
1931     if (cspec != Invalid && cspec != Cmyk)
1932         return toCmyk().cyan();
1933     return qt_div_257(ct.acmyk.cyan);
1934 }
1935 
1936 /*!
1937     Returns the magenta color component of this color.
1938 
1939     \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1940 */
magenta() const1941 int QColor::magenta() const noexcept
1942 {
1943     if (cspec != Invalid && cspec != Cmyk)
1944         return toCmyk().magenta();
1945     return qt_div_257(ct.acmyk.magenta);
1946 }
1947 
1948 /*!
1949     Returns the yellow color component of this color.
1950 
1951     \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1952 */
yellow() const1953 int QColor::yellow() const noexcept
1954 {
1955     if (cspec != Invalid && cspec != Cmyk)
1956         return toCmyk().yellow();
1957     return qt_div_257(ct.acmyk.yellow);
1958 }
1959 
1960 /*!
1961     Returns the black color component of this color.
1962 
1963     \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1964 
1965 */
black() const1966 int QColor::black() const noexcept
1967 {
1968     if (cspec != Invalid && cspec != Cmyk)
1969         return toCmyk().black();
1970     return qt_div_257(ct.acmyk.black);
1971 }
1972 
1973 /*!
1974     Returns the cyan color component of this color.
1975 
1976     \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1977 */
cyanF() const1978 qreal QColor::cyanF() const noexcept
1979 {
1980     if (cspec != Invalid && cspec != Cmyk)
1981         return toCmyk().cyanF();
1982     return ct.acmyk.cyan / qreal(USHRT_MAX);
1983 }
1984 
1985 /*!
1986     Returns the magenta color component of this color.
1987 
1988     \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1989 */
magentaF() const1990 qreal QColor::magentaF() const noexcept
1991 {
1992     if (cspec != Invalid && cspec != Cmyk)
1993         return toCmyk().magentaF();
1994     return ct.acmyk.magenta / qreal(USHRT_MAX);
1995 }
1996 
1997 /*!
1998     Returns the yellow color component of this color.
1999 
2000      \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2001 */
yellowF() const2002 qreal QColor::yellowF() const noexcept
2003 {
2004     if (cspec != Invalid && cspec != Cmyk)
2005         return toCmyk().yellowF();
2006     return ct.acmyk.yellow / qreal(USHRT_MAX);
2007 }
2008 
2009 /*!
2010     Returns the black color component of this color.
2011 
2012     \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2013 */
blackF() const2014 qreal QColor::blackF() const noexcept
2015 {
2016     if (cspec != Invalid && cspec != Cmyk)
2017         return toCmyk().blackF();
2018     return ct.acmyk.black / qreal(USHRT_MAX);
2019 }
2020 
2021 /*!
2022     Create and returns an extended RGB QColor based on this color.
2023     \since 5.14
2024 
2025     \sa toRgb, convertTo()
2026 */
toExtendedRgb() const2027 QColor QColor::toExtendedRgb() const noexcept
2028 {
2029     if (!isValid() || cspec == ExtendedRgb)
2030         return *this;
2031     if (cspec != Rgb)
2032         return toRgb().toExtendedRgb();
2033 
2034     constexpr qreal f = qreal(1.0) / USHRT_MAX;
2035     QColor color;
2036     color.cspec = ExtendedRgb;
2037     castF16(color.ct.argbExtended.alphaF16) = qfloat16(ct.argb.alpha * f);
2038     castF16(color.ct.argbExtended.redF16)   = qfloat16(ct.argb.red   * f);
2039     castF16(color.ct.argbExtended.greenF16) = qfloat16(ct.argb.green * f);
2040     castF16(color.ct.argbExtended.blueF16)  = qfloat16(ct.argb.blue  * f);
2041     color.ct.argbExtended.pad = 0;
2042     return color;
2043 }
2044 
2045 /*!
2046     Create and returns an RGB QColor based on this color.
2047 
2048     \sa fromRgb(), convertTo(), isValid()
2049 */
toRgb() const2050 QColor QColor::toRgb() const noexcept
2051 {
2052     if (!isValid() || cspec == Rgb)
2053         return *this;
2054 
2055     QColor color;
2056     color.cspec = Rgb;
2057     if (cspec != ExtendedRgb)
2058         color.ct.argb.alpha = ct.argb.alpha;
2059     color.ct.argb.pad = 0;
2060 
2061     switch (cspec) {
2062     case Hsv:
2063         {
2064             if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
2065                 // achromatic case
2066                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
2067                 break;
2068             }
2069 
2070             // chromatic case
2071             const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / 6000.;
2072             const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX);
2073             const qreal v = ct.ahsv.value / qreal(USHRT_MAX);
2074             const int i = int(h);
2075             const qreal f = h - i;
2076             const qreal p = v * (qreal(1.0) - s);
2077 
2078             if (i & 1) {
2079                 const qreal q = v * (qreal(1.0) - (s * f));
2080 
2081                 switch (i) {
2082                 case 1:
2083                     color.ct.argb.red   = qRound(q * USHRT_MAX);
2084                     color.ct.argb.green = qRound(v * USHRT_MAX);
2085                     color.ct.argb.blue  = qRound(p * USHRT_MAX);
2086                     break;
2087                 case 3:
2088                     color.ct.argb.red   = qRound(p * USHRT_MAX);
2089                     color.ct.argb.green = qRound(q * USHRT_MAX);
2090                     color.ct.argb.blue  = qRound(v * USHRT_MAX);
2091                     break;
2092                 case 5:
2093                     color.ct.argb.red   = qRound(v * USHRT_MAX);
2094                     color.ct.argb.green = qRound(p * USHRT_MAX);
2095                     color.ct.argb.blue  = qRound(q * USHRT_MAX);
2096                     break;
2097                 }
2098             } else {
2099                 const qreal t = v * (qreal(1.0) - (s * (qreal(1.0) - f)));
2100 
2101                 switch (i) {
2102                 case 0:
2103                     color.ct.argb.red   = qRound(v * USHRT_MAX);
2104                     color.ct.argb.green = qRound(t * USHRT_MAX);
2105                     color.ct.argb.blue  = qRound(p * USHRT_MAX);
2106                     break;
2107                 case 2:
2108                     color.ct.argb.red   = qRound(p * USHRT_MAX);
2109                     color.ct.argb.green = qRound(v * USHRT_MAX);
2110                     color.ct.argb.blue  = qRound(t * USHRT_MAX);
2111                     break;
2112                 case 4:
2113                     color.ct.argb.red   = qRound(t * USHRT_MAX);
2114                     color.ct.argb.green = qRound(p * USHRT_MAX);
2115                     color.ct.argb.blue  = qRound(v * USHRT_MAX);
2116                     break;
2117                 }
2118             }
2119             break;
2120         }
2121     case Hsl:
2122         {
2123             if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
2124                 // achromatic case
2125                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
2126             } else if (ct.ahsl.lightness == 0) {
2127                 // lightness 0
2128                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
2129             } else {
2130                 // chromatic case
2131                 const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / 36000.;
2132                 const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX);
2133                 const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX);
2134 
2135                 qreal temp2;
2136                 if (l < qreal(0.5))
2137                     temp2 = l * (qreal(1.0) + s);
2138                 else
2139                     temp2 = l + s - (l * s);
2140 
2141                 const qreal temp1 = (qreal(2.0) * l) - temp2;
2142                 qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)),
2143                                    h,
2144                                    h - (qreal(1.0) / qreal(3.0)) };
2145 
2146                 for (int i = 0; i != 3; ++i) {
2147                     if (temp3[i] < qreal(0.0))
2148                         temp3[i] += qreal(1.0);
2149                     else if (temp3[i] > qreal(1.0))
2150                         temp3[i] -= qreal(1.0);
2151 
2152                     const qreal sixtemp3 = temp3[i] * qreal(6.0);
2153                     if (sixtemp3 < qreal(1.0))
2154                         color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
2155                     else if ((temp3[i] * qreal(2.0)) < qreal(1.0))
2156                         color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
2157                     else if ((temp3[i] * qreal(3.0)) < qreal(2.0))
2158                         color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX);
2159                     else
2160                         color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
2161                 }
2162                 color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
2163                 color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
2164                 color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
2165             }
2166             break;
2167         }
2168     case Cmyk:
2169         {
2170             const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX);
2171             const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX);
2172             const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX);
2173             const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
2174 
2175             color.ct.argb.red   = qRound((qreal(1.0) - (c * (qreal(1.0) - k) + k)) * USHRT_MAX);
2176             color.ct.argb.green = qRound((qreal(1.0) - (m * (qreal(1.0) - k) + k)) * USHRT_MAX);
2177             color.ct.argb.blue  = qRound((qreal(1.0) - (y * (qreal(1.0) - k) + k)) * USHRT_MAX);
2178             break;
2179         }
2180     case ExtendedRgb:
2181         color.ct.argb.alpha = qRound(USHRT_MAX * qreal(castF16(ct.argbExtended.alphaF16)));
2182         color.ct.argb.red   = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.redF16)),   qreal(1.0)));
2183         color.ct.argb.green = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.greenF16)), qreal(1.0)));
2184         color.ct.argb.blue  = qRound(USHRT_MAX * qBound(qreal(0.0), qreal(castF16(ct.argbExtended.blueF16)),  qreal(1.0)));
2185         break;
2186     default:
2187         break;
2188     }
2189 
2190     return color;
2191 }
2192 
2193 
2194 #define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
2195 #define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
2196 
2197 
2198 /*!
2199     Creates and returns an HSV QColor based on this color.
2200 
2201     \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2202 */
toHsv() const2203 QColor QColor::toHsv() const noexcept
2204 {
2205     if (!isValid() || cspec == Hsv)
2206         return *this;
2207 
2208     if (cspec != Rgb)
2209         return toRgb().toHsv();
2210 
2211     QColor color;
2212     color.cspec = Hsv;
2213     color.ct.ahsv.alpha = ct.argb.alpha;
2214     color.ct.ahsv.pad = 0;
2215 
2216     const qreal r = ct.argb.red   / qreal(USHRT_MAX);
2217     const qreal g = ct.argb.green / qreal(USHRT_MAX);
2218     const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
2219     const qreal max = Q_MAX_3(r, g, b);
2220     const qreal min = Q_MIN_3(r, g, b);
2221     const qreal delta = max - min;
2222     color.ct.ahsv.value = qRound(max * USHRT_MAX);
2223     if (qFuzzyIsNull(delta)) {
2224         // achromatic case, hue is undefined
2225         color.ct.ahsv.hue = USHRT_MAX;
2226         color.ct.ahsv.saturation = 0;
2227     } else {
2228         // chromatic case
2229         qreal hue = 0;
2230         color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
2231         if (qFuzzyCompare(r, max)) {
2232             hue = ((g - b) /delta);
2233         } else if (qFuzzyCompare(g, max)) {
2234             hue = (qreal(2.0) + (b - r) / delta);
2235         } else if (qFuzzyCompare(b, max)) {
2236             hue = (qreal(4.0) + (r - g) / delta);
2237         } else {
2238             Q_ASSERT_X(false, "QColor::toHsv", "internal error");
2239         }
2240         hue *= qreal(60.0);
2241         if (hue < qreal(0.0))
2242             hue += qreal(360.0);
2243         color.ct.ahsv.hue = qRound(hue * 100);
2244     }
2245 
2246     return color;
2247 }
2248 
2249 /*!
2250     Creates and returns an HSL QColor based on this color.
2251 
2252     \sa fromHsl(), convertTo(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2253 */
toHsl() const2254 QColor QColor::toHsl() const noexcept
2255 {
2256     if (!isValid() || cspec == Hsl)
2257         return *this;
2258 
2259     if (cspec != Rgb)
2260         return toRgb().toHsl();
2261 
2262     QColor color;
2263     color.cspec = Hsl;
2264     color.ct.ahsl.alpha = ct.argb.alpha;
2265     color.ct.ahsl.pad = 0;
2266 
2267     const qreal r = ct.argb.red   / qreal(USHRT_MAX);
2268     const qreal g = ct.argb.green / qreal(USHRT_MAX);
2269     const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
2270     const qreal max = Q_MAX_3(r, g, b);
2271     const qreal min = Q_MIN_3(r, g, b);
2272     const qreal delta = max - min;
2273     const qreal delta2 = max + min;
2274     const qreal lightness = qreal(0.5) * delta2;
2275     color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
2276     if (qFuzzyIsNull(delta)) {
2277         // achromatic case, hue is undefined
2278         color.ct.ahsl.hue = USHRT_MAX;
2279         color.ct.ahsl.saturation = 0;
2280     } else {
2281         // chromatic case
2282         qreal hue = 0;
2283         if (lightness < qreal(0.5))
2284             color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
2285         else
2286             color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX);
2287         if (qFuzzyCompare(r, max)) {
2288             hue = ((g - b) /delta);
2289         } else if (qFuzzyCompare(g, max)) {
2290             hue = (qreal(2.0) + (b - r) / delta);
2291         } else if (qFuzzyCompare(b, max)) {
2292             hue = (qreal(4.0) + (r - g) / delta);
2293         } else {
2294             Q_ASSERT_X(false, "QColor::toHsv", "internal error");
2295         }
2296         hue *= qreal(60.0);
2297         if (hue < qreal(0.0))
2298             hue += qreal(360.0);
2299         color.ct.ahsl.hue = qRound(hue * 100);
2300     }
2301 
2302     return color;
2303 }
2304 
2305 /*!
2306     Creates and returns a CMYK QColor based on this color.
2307 
2308     \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2309 */
toCmyk() const2310 QColor QColor::toCmyk() const noexcept
2311 {
2312     if (!isValid() || cspec == Cmyk)
2313         return *this;
2314     if (cspec != Rgb)
2315         return toRgb().toCmyk();
2316 
2317     QColor color;
2318     color.cspec = Cmyk;
2319     color.ct.acmyk.alpha = ct.argb.alpha;
2320 
2321     if (!ct.argb.red && !ct.argb.green && !ct.argb.blue) {
2322         // Avoid div-by-0 below
2323         color.ct.acmyk.cyan    = 0;
2324         color.ct.acmyk.magenta = 0;
2325         color.ct.acmyk.yellow  = 0;
2326         color.ct.acmyk.black   = USHRT_MAX;
2327     } else {
2328         // rgb -> cmy
2329         const qreal r = ct.argb.red   / qreal(USHRT_MAX);
2330         const qreal g = ct.argb.green / qreal(USHRT_MAX);
2331         const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
2332         qreal c = qreal(1.0) - r;
2333         qreal m = qreal(1.0) - g;
2334         qreal y = qreal(1.0) - b;
2335 
2336         // cmy -> cmyk
2337         const qreal k = qMin(c, qMin(m, y));
2338         c = (c - k) / (qreal(1.0) - k);
2339         m = (m - k) / (qreal(1.0) - k);
2340         y = (y - k) / (qreal(1.0) - k);
2341 
2342         color.ct.acmyk.cyan    = qRound(c * USHRT_MAX);
2343         color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2344         color.ct.acmyk.yellow  = qRound(y * USHRT_MAX);
2345         color.ct.acmyk.black   = qRound(k * USHRT_MAX);
2346     }
2347 
2348     return color;
2349 }
2350 
convertTo(QColor::Spec colorSpec) const2351 QColor QColor::convertTo(QColor::Spec colorSpec) const noexcept
2352 {
2353     if (colorSpec == cspec)
2354         return *this;
2355     switch (colorSpec) {
2356     case Rgb:
2357         return toRgb();
2358     case ExtendedRgb:
2359         return toExtendedRgb();
2360     case Hsv:
2361         return toHsv();
2362     case Cmyk:
2363         return toCmyk();
2364     case Hsl:
2365         return toHsl();
2366     case Invalid:
2367         break;
2368     }
2369     return QColor(); // must be invalid
2370 }
2371 
2372 
2373 /*!
2374     Static convenience function that returns a QColor constructed from the
2375     given QRgb value \a rgb.
2376 
2377     The alpha component of \a rgb is ignored (i.e. it is automatically set to
2378     255), use the fromRgba() function to include the alpha-channel specified by
2379     the given QRgb value.
2380 
2381     \sa fromRgba(), fromRgbF(), toRgb(), isValid()
2382 */
2383 
fromRgb(QRgb rgb)2384 QColor QColor::fromRgb(QRgb rgb) noexcept
2385 {
2386     return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
2387 }
2388 
2389 
2390 /*!
2391     Static convenience function that returns a QColor constructed from the
2392     given QRgb value \a rgba.
2393 
2394     Unlike the fromRgb() function, the alpha-channel specified by the given
2395     QRgb value is included.
2396 
2397     \sa fromRgb(), fromRgba64(), isValid()
2398 */
2399 
fromRgba(QRgb rgba)2400 QColor QColor::fromRgba(QRgb rgba) noexcept
2401 {
2402     return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
2403 }
2404 
2405 /*!
2406     Static convenience function that returns a QColor constructed from the RGB
2407     color values, \a r (red), \a g (green), \a b (blue), and \a a
2408     (alpha-channel, i.e. transparency).
2409 
2410     All the values must be in the range 0-255.
2411 
2412     \sa toRgb(), fromRgba64(), fromRgbF(), isValid()
2413 */
fromRgb(int r,int g,int b,int a)2414 QColor QColor::fromRgb(int r, int g, int b, int a)
2415 {
2416     if (!isRgbaValid(r, g, b, a)) {
2417         qWarning("QColor::fromRgb: RGB parameters out of range");
2418         return QColor();
2419     }
2420 
2421     QColor color;
2422     color.cspec = Rgb;
2423     color.ct.argb.alpha = a * 0x101;
2424     color.ct.argb.red   = r * 0x101;
2425     color.ct.argb.green = g * 0x101;
2426     color.ct.argb.blue  = b * 0x101;
2427     color.ct.argb.pad   = 0;
2428     return color;
2429 }
2430 
2431 /*!
2432     Static convenience function that returns a QColor constructed from the RGB
2433     color values, \a r (red), \a g (green), \a b (blue), and \a a
2434     (alpha-channel, i.e. transparency).
2435 
2436     The alpha value must be in the range 0.0-1.0.
2437     If any of the other values are outside the range of 0.0-1.0 the
2438     color model will be set as \c ExtendedRgb.
2439 
2440     \sa fromRgb(), fromRgba64(), toRgb(), isValid()
2441 */
fromRgbF(qreal r,qreal g,qreal b,qreal a)2442 QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a)
2443 {
2444     if (a < qreal(0.0) || a > qreal(1.0)) {
2445         qWarning("QColor::fromRgbF: Alpha parameter out of range");
2446         return QColor();
2447     }
2448 
2449     if (r < qreal(0.0) || r > qreal(1.0)
2450             || g < qreal(0.0) || g > qreal(1.0)
2451             || b < qreal(0.0) || b > qreal(1.0)) {
2452         QColor color;
2453         color.cspec = ExtendedRgb;
2454         castF16(color.ct.argbExtended.alphaF16) = qfloat16(a);
2455         castF16(color.ct.argbExtended.redF16)   = qfloat16(r);
2456         castF16(color.ct.argbExtended.greenF16) = qfloat16(g);
2457         castF16(color.ct.argbExtended.blueF16)  = qfloat16(b);
2458         color.ct.argbExtended.pad   = 0;
2459         return color;
2460     }
2461 
2462     QColor color;
2463     color.cspec = Rgb;
2464     color.ct.argb.alpha = qRound(a * USHRT_MAX);
2465     color.ct.argb.red   = qRound(r * USHRT_MAX);
2466     color.ct.argb.green = qRound(g * USHRT_MAX);
2467     color.ct.argb.blue  = qRound(b * USHRT_MAX);
2468     color.ct.argb.pad   = 0;
2469     return color;
2470 }
2471 
2472 
2473 /*!
2474     \since 5.6
2475 
2476     Static convenience function that returns a QColor constructed from the RGBA64
2477     color values, \a r (red), \a g (green), \a b (blue), and \a a
2478     (alpha-channel, i.e. transparency).
2479 
2480     \sa fromRgb(), fromRgbF(), toRgb(), isValid()
2481 */
fromRgba64(ushort r,ushort g,ushort b,ushort a)2482 QColor QColor::fromRgba64(ushort r, ushort g, ushort b, ushort a) noexcept
2483 {
2484     QColor color;
2485     color.setRgba64(qRgba64(r, g, b, a));
2486     return color;
2487 }
2488 
2489 /*!
2490     \since 5.6
2491 
2492     Static convenience function that returns a QColor constructed from the
2493     given QRgba64 value \a rgba64.
2494 
2495     \sa fromRgb(), fromRgbF(), toRgb(), isValid()
2496 */
fromRgba64(QRgba64 rgba64)2497 QColor QColor::fromRgba64(QRgba64 rgba64) noexcept
2498 {
2499     QColor color;
2500     color.setRgba64(rgba64);
2501     return color;
2502 }
2503 
2504 /*!
2505     Static convenience function that returns a QColor constructed from the HSV
2506     color values, \a h (hue), \a s (saturation), \a v (value), and \a a
2507     (alpha-channel, i.e. transparency).
2508 
2509     The value of \a s, \a v, and \a a must all be in the range 0-255; the value
2510     of \a h must be in the range 0-359.
2511 
2512     \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2513 */
fromHsv(int h,int s,int v,int a)2514 QColor QColor::fromHsv(int h, int s, int v, int a)
2515 {
2516     if (((h < 0 || h >= 360) && h != -1)
2517         || s < 0 || s > 255
2518         || v < 0 || v > 255
2519         || a < 0 || a > 255) {
2520         qWarning("QColor::fromHsv: HSV parameters out of range");
2521         return QColor();
2522     }
2523 
2524     QColor color;
2525     color.cspec = Hsv;
2526     color.ct.ahsv.alpha      = a * 0x101;
2527     color.ct.ahsv.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
2528     color.ct.ahsv.saturation = s * 0x101;
2529     color.ct.ahsv.value      = v * 0x101;
2530     color.ct.ahsv.pad        = 0;
2531     return color;
2532 }
2533 
2534 /*!
2535     \overload
2536 
2537     Static convenience function that returns a QColor constructed from the HSV
2538     color values, \a h (hue), \a s (saturation), \a v (value), and \a a
2539     (alpha-channel, i.e. transparency).
2540 
2541     All the values must be in the range 0.0-1.0.
2542 
2543     \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2544 */
fromHsvF(qreal h,qreal s,qreal v,qreal a)2545 QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a)
2546 {
2547     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
2548         || (s < qreal(0.0) || s > qreal(1.0))
2549         || (v < qreal(0.0) || v > qreal(1.0))
2550         || (a < qreal(0.0) || a > qreal(1.0))) {
2551         qWarning("QColor::fromHsvF: HSV parameters out of range");
2552         return QColor();
2553     }
2554 
2555     QColor color;
2556     color.cspec = Hsv;
2557     color.ct.ahsv.alpha      = qRound(a * USHRT_MAX);
2558     color.ct.ahsv.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
2559     color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
2560     color.ct.ahsv.value      = qRound(v * USHRT_MAX);
2561     color.ct.ahsv.pad        = 0;
2562     return color;
2563 }
2564 
2565 /*!
2566     \since 4.6
2567 
2568     Static convenience function that returns a QColor constructed from the HSV
2569     color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2570     (alpha-channel, i.e. transparency).
2571 
2572     The value of \a s, \a l, and \a a must all be in the range 0-255; the value
2573     of \a h must be in the range 0-359.
2574 
2575     \sa toHsl(), fromHslF(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2576 */
fromHsl(int h,int s,int l,int a)2577 QColor QColor::fromHsl(int h, int s, int l, int a)
2578 {
2579     if (((h < 0 || h >= 360) && h != -1)
2580         || s < 0 || s > 255
2581         || l < 0 || l > 255
2582         || a < 0 || a > 255) {
2583         qWarning("QColor::fromHsl: HSL parameters out of range");
2584         return QColor();
2585     }
2586 
2587     QColor color;
2588     color.cspec = Hsl;
2589     color.ct.ahsl.alpha      = a * 0x101;
2590     color.ct.ahsl.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
2591     color.ct.ahsl.saturation = s * 0x101;
2592     color.ct.ahsl.lightness  = l * 0x101;
2593     color.ct.ahsl.pad        = 0;
2594     return color;
2595 }
2596 
2597 /*!
2598     \overload
2599     \since 4.6
2600 
2601     Static convenience function that returns a QColor constructed from the HSV
2602     color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2603     (alpha-channel, i.e. transparency).
2604 
2605     All the values must be in the range 0.0-1.0.
2606 
2607     \sa toHsl(), fromHsl(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2608 */
fromHslF(qreal h,qreal s,qreal l,qreal a)2609 QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a)
2610 {
2611     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
2612         || (s < qreal(0.0) || s > qreal(1.0))
2613         || (l < qreal(0.0) || l > qreal(1.0))
2614         || (a < qreal(0.0) || a > qreal(1.0))) {
2615         qWarning("QColor::fromHslF: HSL parameters out of range");
2616         return QColor();
2617     }
2618 
2619     QColor color;
2620     color.cspec = Hsl;
2621     color.ct.ahsl.alpha      = qRound(a * USHRT_MAX);
2622     color.ct.ahsl.hue        = (h == qreal(-1.0)) ? USHRT_MAX : qRound(h * 36000);
2623     if (color.ct.ahsl.hue == 36000)
2624         color.ct.ahsl.hue = 0;
2625     color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2626     color.ct.ahsl.lightness  = qRound(l * USHRT_MAX);
2627     color.ct.ahsl.pad        = 0;
2628     return color;
2629 }
2630 
2631 /*!
2632    \obsolete
2633 
2634    Use the \c const overload instead.
2635 */
getCmyk(int * c,int * m,int * y,int * k,int * a)2636 void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a)
2637 {
2638     const_cast<const QColor *>(this)->getCmyk(c, m, y, k, a);
2639 }
2640 
2641 /*!
2642     Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2643     cyan, magenta, yellow, black, and alpha-channel (transparency) components
2644     of the color's CMYK value.
2645 
2646     These components can be retrieved individually using the cyan(), magenta(),
2647     yellow(), black() and alpha() functions.
2648 
2649     \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2650 */
getCmyk(int * c,int * m,int * y,int * k,int * a) const2651 void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a) const
2652 {
2653     if (!c || !m || !y || !k)
2654         return;
2655 
2656     if (cspec != Invalid && cspec != Cmyk) {
2657         toCmyk().getCmyk(c, m, y, k, a);
2658         return;
2659     }
2660 
2661     *c = qt_div_257(ct.acmyk.cyan);
2662     *m = qt_div_257(ct.acmyk.magenta);
2663     *y = qt_div_257(ct.acmyk.yellow);
2664     *k = qt_div_257(ct.acmyk.black);
2665 
2666     if (a)
2667         *a = qt_div_257(ct.acmyk.alpha);
2668 }
2669 
2670 /*!
2671    \obsolete
2672 
2673    Use the \c const overload instead.
2674 */
getCmykF(qreal * c,qreal * m,qreal * y,qreal * k,qreal * a)2675 void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a)
2676 {
2677     const_cast<const QColor *>(this)->getCmykF(c, m, y, k, a);
2678 }
2679 
2680 /*!
2681     Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2682     cyan, magenta, yellow, black, and alpha-channel (transparency) components
2683     of the color's CMYK value.
2684 
2685     These components can be retrieved individually using the cyanF(),
2686     magentaF(), yellowF(), blackF() and alphaF() functions.
2687 
2688     \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2689 */
getCmykF(qreal * c,qreal * m,qreal * y,qreal * k,qreal * a) const2690 void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a) const
2691 {
2692     if (!c || !m || !y || !k)
2693         return;
2694 
2695     if (cspec != Invalid && cspec != Cmyk) {
2696         toCmyk().getCmykF(c, m, y, k, a);
2697         return;
2698     }
2699 
2700     *c = ct.acmyk.cyan    / qreal(USHRT_MAX);
2701     *m = ct.acmyk.magenta / qreal(USHRT_MAX);
2702     *y = ct.acmyk.yellow  / qreal(USHRT_MAX);
2703     *k = ct.acmyk.black   / qreal(USHRT_MAX);
2704 
2705     if (a)
2706         *a = ct.acmyk.alpha / qreal(USHRT_MAX);
2707 }
2708 
2709 /*!
2710     Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2711     \a k (black), and \a a (alpha-channel, i.e. transparency).
2712 
2713     All the values must be in the range 0-255.
2714 
2715     \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2716 */
setCmyk(int c,int m,int y,int k,int a)2717 void QColor::setCmyk(int c, int m, int y, int k, int a)
2718 {
2719     if (c < 0 || c > 255
2720         || m < 0 || m > 255
2721         || y < 0 || y > 255
2722         || k < 0 || k > 255
2723         || a < 0 || a > 255) {
2724         qWarning("QColor::setCmyk: CMYK parameters out of range");
2725         return;
2726     }
2727 
2728     cspec = Cmyk;
2729     ct.acmyk.alpha   = a * 0x101;
2730     ct.acmyk.cyan    = c * 0x101;
2731     ct.acmyk.magenta = m * 0x101;
2732     ct.acmyk.yellow  = y * 0x101;
2733     ct.acmyk.black   = k * 0x101;
2734 }
2735 
2736 /*!
2737     \overload
2738 
2739     Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2740     \a k (black), and \a a (alpha-channel, i.e. transparency).
2741 
2742     All the values must be in the range 0.0-1.0.
2743 
2744     \sa getCmykF(), setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2745 */
setCmykF(qreal c,qreal m,qreal y,qreal k,qreal a)2746 void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2747 {
2748     if (c < qreal(0.0) || c > qreal(1.0)
2749         || m < qreal(0.0) || m > qreal(1.0)
2750         || y < qreal(0.0) || y > qreal(1.0)
2751         || k < qreal(0.0) || k > qreal(1.0)
2752         || a < qreal(0.0) || a > qreal(1.0)) {
2753         qWarning("QColor::setCmykF: CMYK parameters out of range");
2754         return;
2755     }
2756 
2757     cspec = Cmyk;
2758     ct.acmyk.alpha   = qRound(a * USHRT_MAX);
2759     ct.acmyk.cyan    = qRound(c * USHRT_MAX);
2760     ct.acmyk.magenta = qRound(m * USHRT_MAX);
2761     ct.acmyk.yellow  = qRound(y * USHRT_MAX);
2762     ct.acmyk.black   = qRound(k * USHRT_MAX);
2763 }
2764 
2765 /*!
2766     Static convenience function that returns a QColor constructed from the
2767     given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2768     (black), and \a a (alpha-channel, i.e. transparency).
2769 
2770     All the values must be in the range 0-255.
2771 
2772     \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2773 */
fromCmyk(int c,int m,int y,int k,int a)2774 QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2775 {
2776     if (c < 0 || c > 255
2777         || m < 0 || m > 255
2778         || y < 0 || y > 255
2779         || k < 0 || k > 255
2780         || a < 0 || a > 255) {
2781         qWarning("QColor::fromCmyk: CMYK parameters out of range");
2782         return QColor();
2783     }
2784 
2785     QColor color;
2786     color.cspec = Cmyk;
2787     color.ct.acmyk.alpha   = a * 0x101;
2788     color.ct.acmyk.cyan    = c * 0x101;
2789     color.ct.acmyk.magenta = m * 0x101;
2790     color.ct.acmyk.yellow  = y * 0x101;
2791     color.ct.acmyk.black   = k * 0x101;
2792     return color;
2793 }
2794 
2795 /*!
2796     \overload
2797 
2798     Static convenience function that returns a QColor constructed from the
2799     given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2800     (black), and \a a (alpha-channel, i.e. transparency).
2801 
2802     All the values must be in the range 0.0-1.0.
2803 
2804     \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2805 */
fromCmykF(qreal c,qreal m,qreal y,qreal k,qreal a)2806 QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2807 {
2808     if (c < qreal(0.0) || c > qreal(1.0)
2809         || m < qreal(0.0) || m > qreal(1.0)
2810         || y < qreal(0.0) || y > qreal(1.0)
2811         || k < qreal(0.0) || k > qreal(1.0)
2812         || a < qreal(0.0) || a > qreal(1.0)) {
2813         qWarning("QColor::fromCmykF: CMYK parameters out of range");
2814         return QColor();
2815     }
2816 
2817     QColor color;
2818     color.cspec = Cmyk;
2819     color.ct.acmyk.alpha   = qRound(a * USHRT_MAX);
2820     color.ct.acmyk.cyan    = qRound(c * USHRT_MAX);
2821     color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2822     color.ct.acmyk.yellow  = qRound(y * USHRT_MAX);
2823     color.ct.acmyk.black   = qRound(k * USHRT_MAX);
2824     return color;
2825 }
2826 
2827 /*!
2828     \fn QColor QColor::lighter(int factor) const
2829     \since 4.3
2830 
2831     Returns a lighter (or darker) color, but does not change this object.
2832 
2833     If the \a factor is greater than 100, this functions returns a lighter
2834     color. Setting \a factor to 150 returns a color that is 50% brighter. If
2835     the \a factor is less than 100, the return color is darker, but we
2836     recommend using the darker() function for this purpose. If the \a factor
2837     is 0 or negative, the return value is unspecified.
2838 
2839     The function converts the current color to HSV, multiplies the value
2840     (V) component by \a factor and converts the color back to it's original
2841     color spec.
2842 
2843     \sa darker(), isValid()
2844 */
lighter(int factor) const2845 QColor QColor::lighter(int factor) const noexcept
2846 {
2847     if (factor <= 0)                                // invalid lightness factor
2848         return *this;
2849     else if (factor < 100)                        // makes color darker
2850         return darker(10000 / factor);
2851 
2852     QColor hsv = toHsv();
2853     int s = hsv.ct.ahsv.saturation;
2854     uint v = hsv.ct.ahsv.value;
2855 
2856     v = (factor*v)/100;
2857     if (v > USHRT_MAX) {
2858         // overflow... adjust saturation
2859         s -= v - USHRT_MAX;
2860         if (s < 0)
2861             s = 0;
2862         v = USHRT_MAX;
2863     }
2864 
2865     hsv.ct.ahsv.saturation = s;
2866     hsv.ct.ahsv.value = v;
2867 
2868     // convert back to same color spec as original color
2869     return hsv.convertTo(cspec);
2870 }
2871 
2872 /*!
2873     \fn QColor QColor::darker(int factor) const
2874     \since 4.3
2875 
2876     Returns a darker (or lighter) color, but does not change this object.
2877 
2878     If the \a factor is greater than 100, this functions returns a darker
2879     color. Setting \a factor to 300 returns a color that has one-third the
2880     brightness. If the \a factor is less than 100, the return color is lighter,
2881     but we recommend using the lighter() function for this purpose. If the
2882     \a factor is 0 or negative, the return value is unspecified.
2883 
2884     The function converts the current color to HSV, divides the value (V)
2885     component by \a factor and converts the color back to it's original
2886     color spec.
2887 
2888     \sa lighter(), isValid()
2889 */
darker(int factor) const2890 QColor QColor::darker(int factor) const noexcept
2891 {
2892     if (factor <= 0)                                // invalid darkness factor
2893         return *this;
2894     else if (factor < 100)                        // makes color lighter
2895         return lighter(10000 / factor);
2896 
2897     QColor hsv = toHsv();
2898     hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2899 
2900     // convert back to same color spec as original color
2901     return hsv.convertTo(cspec);
2902 }
2903 
2904 #if QT_DEPRECATED_SINCE(5, 13)
2905 /*!
2906     \obsolete
2907 
2908     Use lighter(\a factor) instead.
2909 */
light(int factor) const2910 QColor QColor::light(int factor) const noexcept
2911 {
2912     return lighter(factor);
2913 }
2914 
2915 /*!
2916     \obsolete
2917 
2918     Use darker(\a factor) instead.
2919 */
dark(int factor) const2920 QColor QColor::dark(int factor) const noexcept
2921 {
2922     return darker(factor);
2923 }
2924 #endif
2925 
2926 #if QT_VERSION < QT_VERSION_CHECK(6,0,0)
2927 /*!
2928     Assigns a copy of \a color to this color, and returns a reference to it.
2929 */
operator =(const QColor & color)2930 QColor &QColor::operator=(const QColor &color) noexcept
2931 {
2932     cspec = color.cspec;
2933     ct.argb = color.ct.argb;
2934     return *this;
2935 }
2936 #endif
2937 
2938 /*! \overload
2939     Assigns a copy of \a color and returns a reference to this color.
2940  */
operator =(Qt::GlobalColor color)2941 QColor &QColor::operator=(Qt::GlobalColor color) noexcept
2942 {
2943     return operator=(QColor(color));
2944 }
2945 
2946 /*!
2947     Returns \c true if this color has the same color specification and component values as \a color;
2948     otherwise returns \c false.
2949 
2950     ExtendedRgb and Rgb specifications are considered matching in this context.
2951 
2952     \sa spec()
2953 */
operator ==(const QColor & color) const2954 bool QColor::operator==(const QColor &color) const noexcept
2955 {
2956     if (cspec == Hsl && cspec == color.cspec) {
2957         return (ct.argb.alpha == color.ct.argb.alpha
2958                 && ct.ahsl.hue % 36000 == color.ct.ahsl.hue % 36000
2959                 && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50
2960                     || ct.ahsl.lightness == 0
2961                     || color.ct.ahsl.lightness == 0
2962                     || ct.ahsl.lightness == USHRT_MAX
2963                     || color.ct.ahsl.lightness == USHRT_MAX)
2964                 && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
2965     } else if ((cspec == ExtendedRgb || color.cspec == ExtendedRgb) &&
2966                (cspec == color.cspec || cspec == Rgb || color.cspec == Rgb))  {
2967         return qFuzzyCompare(alphaF(), color.alphaF())
2968             && qFuzzyCompare(redF(), color.redF())
2969             && qFuzzyCompare(greenF(), color.greenF())
2970             && qFuzzyCompare(blueF(), color.blueF());
2971     } else {
2972         return (cspec == color.cspec
2973                 && ct.argb.alpha == color.ct.argb.alpha
2974                 && (((cspec == QColor::Hsv)
2975                      && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
2976                     || (ct.ahsv.hue == color.ct.ahsv.hue))
2977                 && ct.argb.green == color.ct.argb.green
2978                 && ct.argb.blue  == color.ct.argb.blue
2979                 && ct.argb.pad   == color.ct.argb.pad);
2980     }
2981 }
2982 
2983 /*!
2984     Returns \c true if this color has different color specification or component values from
2985     \a color; otherwise returns \c false.
2986 
2987     ExtendedRgb and Rgb specifications are considered matching in this context.
2988 
2989     \sa spec()
2990 */
operator !=(const QColor & color) const2991 bool QColor::operator!=(const QColor &color) const noexcept
2992 { return !operator==(color); }
2993 
2994 
2995 /*!
2996     Returns the color as a QVariant
2997 */
operator QVariant() const2998 QColor::operator QVariant() const
2999 {
3000     return QVariant(QMetaType::QColor, this);
3001 }
3002 
3003 /*! \internal
3004 
3005     Marks the color as invalid and sets all components to zero (alpha is set
3006     to fully opaque for compatibility with Qt 3).
3007 */
invalidate()3008 void QColor::invalidate() noexcept
3009 {
3010     cspec = Invalid;
3011     ct.argb.alpha = USHRT_MAX;
3012     ct.argb.red = 0;
3013     ct.argb.green = 0;
3014     ct.argb.blue = 0;
3015     ct.argb.pad = 0;
3016 }
3017 
3018 /*****************************************************************************
3019   QColor stream functions
3020  *****************************************************************************/
3021 
3022 #ifndef QT_NO_DEBUG_STREAM
operator <<(QDebug dbg,const QColor & c)3023 QDebug operator<<(QDebug dbg, const QColor &c)
3024 {
3025     QDebugStateSaver saver(dbg);
3026     if (!c.isValid())
3027         dbg.nospace() << "QColor(Invalid)";
3028     else if (c.spec() == QColor::Rgb)
3029         dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
3030     else if (c.spec() == QColor::ExtendedRgb)
3031         dbg.nospace() << "QColor(Ext. ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
3032     else if (c.spec() == QColor::Hsv)
3033         dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
3034     else if (c.spec() == QColor::Cmyk)
3035         dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
3036                       << c.blackF()<< ')';
3037     else if (c.spec() == QColor::Hsl)
3038         dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
3039 
3040     return dbg;
3041 }
3042 #endif
3043 
3044 #ifndef QT_NO_DATASTREAM
3045 /*!
3046     \fn QDataStream &operator<<(QDataStream &stream, const QColor &color)
3047     \relates QColor
3048 
3049     Writes the \a color to the \a stream.
3050 
3051     \sa {Serializing Qt Data Types}
3052 */
operator <<(QDataStream & stream,const QColor & color)3053 QDataStream &operator<<(QDataStream &stream, const QColor &color)
3054 {
3055     if (stream.version() < 7) {
3056         if (!color.isValid())
3057             return stream << quint32(0x49000000);
3058         quint32 p = (quint32)color.rgb();
3059         if (stream.version() == 1) // Swap red and blue
3060             p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
3061         return stream << p;
3062     }
3063 
3064     qint8   s = color.cspec;
3065     quint16 a = color.ct.argb.alpha;
3066     quint16 r = color.ct.argb.red;
3067     quint16 g = color.ct.argb.green;
3068     quint16 b = color.ct.argb.blue;
3069     quint16 p = color.ct.argb.pad;
3070 
3071     stream << s;
3072     stream << a;
3073     stream << r;
3074     stream << g;
3075     stream << b;
3076     stream << p;
3077 
3078     return stream;
3079 }
3080 
3081 /*!
3082     \fn QDataStream &operator>>(QDataStream &stream, QColor &color)
3083     \relates QColor
3084 
3085     Reads the \a color from the \a stream.
3086 
3087     \sa {Serializing Qt Data Types}
3088 */
operator >>(QDataStream & stream,QColor & color)3089 QDataStream &operator>>(QDataStream &stream, QColor &color)
3090 {
3091     if (stream.version() < 7) {
3092         quint32 p;
3093         stream >> p;
3094         if (p == 0x49000000) {
3095             color.invalidate();
3096             return stream;
3097         }
3098         if (stream.version() == 1) // Swap red and blue
3099             p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
3100         color.setRgb(p);
3101         return stream;
3102     }
3103 
3104     qint8 s;
3105     quint16 a, r, g, b, p;
3106     stream >> s;
3107     stream >> a;
3108     stream >> r;
3109     stream >> g;
3110     stream >> b;
3111     stream >> p;
3112 
3113     color.cspec = QColor::Spec(s);
3114     color.ct.argb.alpha = a;
3115     color.ct.argb.red   = r;
3116     color.ct.argb.green = g;
3117     color.ct.argb.blue  = b;
3118     color.ct.argb.pad   = p;
3119 
3120     return stream;
3121 }
3122 #endif // QT_NO_DATASTREAM
3123 
3124 // A table of precalculated results of 0x00ff00ff/alpha use by qUnpremultiply:
3125 const uint qt_inv_premul_factor[256] = {
3126     0, 16711935, 8355967, 5570645, 4177983, 3342387, 2785322, 2387419,
3127     2088991, 1856881, 1671193, 1519266, 1392661, 1285533, 1193709, 1114129,
3128     1044495, 983055, 928440, 879575, 835596, 795806, 759633, 726605,
3129     696330, 668477, 642766, 618960, 596854, 576273, 557064, 539094,
3130     522247, 506422, 491527, 477483, 464220, 451673, 439787, 428511,
3131     417798, 407608, 397903, 388649, 379816, 371376, 363302, 355573,
3132     348165, 341059, 334238, 327685, 321383, 315319, 309480, 303853,
3133     298427, 293191, 288136, 283253, 278532, 273966, 269547, 265268,
3134     261123, 257106, 253211, 249431, 245763, 242201, 238741, 235379,
3135     232110, 228930, 225836, 222825, 219893, 217038, 214255, 211543,
3136     208899, 206320, 203804, 201348, 198951, 196611, 194324, 192091,
3137     189908, 187774, 185688, 183647, 181651, 179698, 177786, 175915,
3138     174082, 172287, 170529, 168807, 167119, 165464, 163842, 162251,
3139     160691, 159161, 157659, 156186, 154740, 153320, 151926, 150557,
3140     149213, 147893, 146595, 145321, 144068, 142837, 141626, 140436,
3141     139266, 138115, 136983, 135869, 134773, 133695, 132634, 131590,
3142     130561, 129549, 128553, 127572, 126605, 125653, 124715, 123792,
3143     122881, 121984, 121100, 120229, 119370, 118524, 117689, 116866,
3144     116055, 115254, 114465, 113686, 112918, 112160, 111412, 110675,
3145     109946, 109228, 108519, 107818, 107127, 106445, 105771, 105106,
3146     104449, 103800, 103160, 102527, 101902, 101284, 100674, 100071,
3147     99475, 98887, 98305, 97730, 97162, 96600, 96045, 95496,
3148     94954, 94417, 93887, 93362, 92844, 92331, 91823, 91322,
3149     90825, 90334, 89849, 89368, 88893, 88422, 87957, 87497,
3150     87041, 86590, 86143, 85702, 85264, 84832, 84403, 83979,
3151     83559, 83143, 82732, 82324, 81921, 81521, 81125, 80733,
3152     80345, 79961, 79580, 79203, 78829, 78459, 78093, 77729,
3153     77370, 77013, 76660, 76310, 75963, 75619, 75278, 74941,
3154     74606, 74275, 73946, 73620, 73297, 72977, 72660, 72346,
3155     72034, 71725, 71418, 71114, 70813, 70514, 70218, 69924,
3156     69633, 69344, 69057, 68773, 68491, 68211, 67934, 67659,
3157     67386, 67116, 66847, 66581, 66317, 66055, 65795, 65537
3158 };
3159 
3160 /*****************************************************************************
3161   QColor global functions (documentation only)
3162  *****************************************************************************/
3163 
3164 /*!
3165     \fn int qRed(QRgb rgb)
3166     \relates QColor
3167 
3168     Returns the red component of the ARGB quadruplet \a rgb.
3169 
3170     \sa qRgb(), QColor::red()
3171 */
3172 
3173 /*!
3174     \fn int qGreen(QRgb rgb)
3175     \relates QColor
3176 
3177     Returns the green component of the ARGB quadruplet \a rgb.
3178 
3179     \sa qRgb(), QColor::green()
3180 */
3181 
3182 /*!
3183     \fn int qBlue(QRgb rgb)
3184     \relates QColor
3185 
3186     Returns the blue component of the ARGB quadruplet \a rgb.
3187 
3188     \sa qRgb(), QColor::blue()
3189 */
3190 
3191 /*!
3192     \fn int qAlpha(QRgb rgba)
3193     \relates QColor
3194 
3195     Returns the alpha component of the ARGB quadruplet \a rgba.
3196 
3197     \sa qRgb(), QColor::alpha()
3198 */
3199 
3200 /*!
3201     \fn QRgb qRgb(int r, int g, int b)
3202     \relates QColor
3203 
3204     Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}).
3205 
3206     \sa qRgba(), qRed(), qGreen(), qBlue(), qAlpha()
3207 */
3208 
3209 /*!
3210     \fn QRgb qRgba(int r, int g, int b, int a)
3211     \relates QColor
3212 
3213     Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}).
3214 
3215     \sa qRgb(), qRed(), qGreen(), qBlue(), qAlpha()
3216 */
3217 
3218 /*!
3219     \fn int qGray(int r, int g, int b)
3220     \relates QColor
3221 
3222     Returns a gray value (0 to 255) from the (\a r, \a g, \a b)
3223     triplet.
3224 
3225     The gray value is calculated using the formula (\a r * 11 + \a g * 16 +
3226     \a b * 5)/32.
3227 */
3228 
3229 /*!
3230     \fn int qGray(QRgb rgb)
3231     \overload
3232     \relates QColor
3233 
3234     Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb.
3235 
3236     The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32;
3237     the alpha-channel is ignored.
3238 */
3239 
3240 /*!
3241     \fn QRgb qPremultiply(QRgb rgb)
3242     \since 5.3
3243     \relates QColor
3244 
3245     Converts an unpremultiplied ARGB quadruplet \a rgb into a premultiplied ARGB quadruplet.
3246 
3247     \sa qUnpremultiply()
3248 */
3249 
3250 /*!
3251     \fn QRgb qUnpremultiply(QRgb rgb)
3252     \since 5.3
3253     \relates QColor
3254 
3255     Converts a premultiplied ARGB quadruplet \a rgb into an unpremultiplied ARGB quadruplet.
3256 
3257     \sa qPremultiply()
3258 */
3259 
3260 /*!
3261     \fn QColor QColor::convertTo(Spec colorSpec) const
3262 
3263     Creates a copy of \e this color in the format specified by \a colorSpec.
3264 
3265     \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()
3266 */
3267 
3268 /*!
3269     \typedef QRgb
3270     \relates QColor
3271 
3272     An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
3273 
3274     The type also holds a value for the alpha-channel. The default alpha
3275     channel is \c ff, i.e opaque. For more information, see the
3276     \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section.
3277 
3278     \sa QColor::rgb(), QColor::rgba()
3279 */
3280 
3281 /*!
3282     \namespace QColorConstants
3283     \inmodule QtGui
3284     \since 5.14
3285 
3286     \brief The QColorConstants namespace contains QColor predefined constants.
3287 
3288     These constants are usable everywhere a QColor object is expected:
3289 
3290     \code
3291     painter.setBrush(QColorConstants::Svg::lightblue);
3292     \endcode
3293 
3294     Their usage is much cheaper than e.g. passing a string to QColor's constructor,
3295     as they don't require any parsing of the string, and always result in a valid
3296     QColor object:
3297 
3298     \badcode
3299     object.setColor(QColor("lightblue")); // expensive
3300     \endcode
3301 
3302     \section1 Qt Colors
3303 
3304     The following colors are defined in the \c{QColorConstants} namespace:
3305 
3306     \include qt-colors.qdocinc
3307 
3308     \section1 SVG Colors
3309 
3310     The following table lists the available
3311     \l {http://www.w3.org/TR/SVG/types.html#ColorKeywords}{SVG colors}.
3312     They are available in the \c{QColorConstants::Svg} inner namespace.
3313 
3314     \include svg-colors.qdocinc
3315 
3316     \sa QColor, Qt::GlobalColor
3317 */
3318 
3319 QT_END_NAMESPACE
3320