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

source code of qtbase/src/gui/painting/qcolor.cpp