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