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 {Color wheel with RGB values}
563 \li \inlineimage qcolor-hsv.png {Color wheel with HSV values}
564 \li \inlineimage qcolor-cmyk.png {Color wheel with CMYK values}
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 {Color wheel showing hue values from 0 to 300}
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 {Saturation gradient from gray to blue}
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 {Value gradient from black to white}
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 static constexpr QRgb global_colors[] = {
838 qRgb(255, 255, 255), // Qt::color0
839 qRgb( 0, 0, 0), // Qt::color1
840 qRgb( 0, 0, 0), // black
841 qRgb(255, 255, 255), // white
842 /*
843 * From the "The Palette Manager: How and Why" by Ron Gery,
844 * March 23, 1992, archived on MSDN:
845 *
846 * The Windows system palette is broken up into two
847 * sections, one with fixed colors and one with colors
848 * that can be changed by applications. The system palette
849 * predefines 20 entries; these colors are known as the
850 * static or reserved colors and consist of the 16 colors
851 * found in the Windows version 3.0 VGA driver and 4
852 * additional colors chosen for their visual appeal. The
853 * DEFAULT_PALETTE stock object is, as the name implies,
854 * the default palette selected into a device context (DC)
855 * and consists of these static colors. Applications can
856 * set the remaining 236 colors using the Palette Manager.
857 *
858 * The 20 reserved entries have indices in [0,9] and
859 * [246,255]. We reuse 17 of them.
860 */
861 qRgb(128, 128, 128), // index 248 medium gray
862 qRgb(160, 160, 164), // index 247 light gray
863 qRgb(192, 192, 192), // index 7 light gray
864 qRgb(255, 0, 0), // index 249 red
865 qRgb( 0, 255, 0), // index 250 green
866 qRgb( 0, 0, 255), // index 252 blue
867 qRgb( 0, 255, 255), // index 254 cyan
868 qRgb(255, 0, 255), // index 253 magenta
869 qRgb(255, 255, 0), // index 251 yellow
870 qRgb(128, 0, 0), // index 1 dark red
871 qRgb( 0, 128, 0), // index 2 dark green
872 qRgb( 0, 0, 128), // index 4 dark blue
873 qRgb( 0, 128, 128), // index 6 dark cyan
874 qRgb(128, 0, 128), // index 5 dark magenta
875 qRgb(128, 128, 0), // index 3 dark yellow
876 qRgba(0, 0, 0, 0) // transparent
877 };
878
879 setRgb(qRed(global_colors[color]),
880 qGreen(global_colors[color]),
881 qBlue(global_colors[color]),
882 qAlpha(global_colors[color]));
883}
884
885/*!
886 \fn QColor::QColor(int r, int g, int b, int a = 255)
887
888 Constructs a color with the RGB value \a r, \a g, \a b, and the
889 alpha-channel (transparency) value of \a a.
890
891 The color is left invalid if any of the arguments are invalid.
892
893 \sa setRgba(), isValid()
894*/
895
896/*!
897 Constructs a color with the value \a color. The alpha component is
898 ignored and set to solid.
899
900 \sa fromRgb(), isValid()
901*/
902
903QColor::QColor(QRgb color) noexcept
904{
905 cspec = Rgb;
906 ct.argb.alpha = 0xffff;
907 ct.argb.red = qRed(color) * 0x101;
908 ct.argb.green = qGreen(color) * 0x101;
909 ct.argb.blue = qBlue(color) * 0x101;
910 ct.argb.pad = 0;
911}
912
913/*!
914 \since 5.6
915
916 Constructs a color with the value \a rgba64.
917
918 \sa fromRgba64()
919*/
920
921QColor::QColor(QRgba64 rgba64) noexcept
922{
923 setRgba64(rgba64);
924}
925
926/*!
927 \internal
928
929 Constructs a color with the given \a spec.
930
931 This function is primarily present to avoid that QColor::Invalid
932 becomes a valid color by accident.
933*/
934
935QColor::QColor(Spec spec) noexcept
936{
937 switch (spec) {
938 case Invalid:
939 invalidate();
940 break;
941 case Rgb:
942 setRgb(0, 0, 0);
943 break;
944 case Hsv:
945 setHsv(0, 0, 0);
946 break;
947 case Cmyk:
948 setCmyk(0, 0, 0, 0);
949 break;
950 case Hsl:
951 setHsl(0, 0, 0, 0);
952 break;
953 case ExtendedRgb:
954 cspec = spec;
955 setRgbF(0, 0, 0, 0);
956 break;
957 }
958}
959
960// ### Qt 7: remove those after deprecating them for the last Qt 6 LTS release
961/*!
962 \fn QColor::QColor(const QString &name)
963
964 Constructs a named color in the same way as fromString() using
965 the given \a name.
966
967 The color is left invalid if the \a name cannot be parsed.
968
969 \sa fromString(), name(), isValid()
970*/
971
972/*!
973 \fn QColor::QColor(const char *name)
974
975 Constructs a named color in the same way as fromString() using
976 the given \a name.
977
978 \overload
979 \sa fromString(), name(), isValid()
980*/
981
982/*!
983 \fn QColor::QColor(QLatin1StringView name)
984
985 Constructs a named color in the same way as fromString() using
986 the given \a name.
987
988 \overload
989 \since 5.8
990 \sa fromString(), name(), isValid()
991*/
992
993/*!
994 \fn bool QColor::isValid() const
995
996 Returns \c true if the color is valid; otherwise returns \c false.
997*/
998
999/*!
1000 \since 5.2
1001
1002 Returns the name of the color in the specified \a format.
1003
1004 \sa fromString(), NameFormat
1005*/
1006
1007QString QColor::name(NameFormat format) const
1008{
1009 switch (format) {
1010 case HexRgb:
1011 return u'#' + QStringView{QString::number(rgba() | 0x1000000, 16)}.right(6);
1012 case HexArgb:
1013 // it's called rgba() but it does return AARRGGBB
1014 return u'#' + QStringView{QString::number(rgba() | Q_INT64_C(0x100000000), 16)}.right(8);
1015 }
1016 return QString();
1017}
1018
1019#if QT_DEPRECATED_SINCE(6, 6)
1020/*!
1021 \deprecated [6.6] Use fromString() instead.
1022
1023 Sets the RGB value of this QColor to \a name, which may be in one
1024 of these formats:
1025
1026 \list
1027 \li #RGB (each of R, G, and B is a single hex digit)
1028 \li #RRGGBB
1029 \li #AARRGGBB (Since 5.2)
1030 \li #RRRGGGBBB
1031 \li #RRRRGGGGBBBB
1032 \li A name from the list of colors defined in the list of
1033 \l{https://www.w3.org/TR/SVG11/types.html#ColorKeywords}{SVG color keyword names}
1034 provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
1035 These color names work on all platforms. Note that these color names are \e not the
1036 same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
1037 refer to the same color.
1038 \li \c transparent - representing the absence of a color.
1039 \endlist
1040
1041 The color is invalid if \a name cannot be parsed.
1042
1043 \sa QColor(), name(), isValid()
1044*/
1045
1046void QColor::setNamedColor(const QString &name)
1047{
1048 *this = fromString(qToAnyStringViewIgnoringNull(name));
1049}
1050
1051/*!
1052 \overload
1053 \since 5.10
1054 \deprecated [6.6] Use fromString() instead.
1055*/
1056
1057void QColor::setNamedColor(QStringView name)
1058{
1059 *this = fromString(name);
1060}
1061
1062/*!
1063 \overload
1064 \since 5.8
1065 \deprecated [6.6] Use fromString() instead.
1066*/
1067
1068void QColor::setNamedColor(QLatin1StringView name)
1069{
1070 *this = fromString(name);
1071}
1072
1073/*!
1074 \since 4.7
1075
1076 \deprecated [6.6] Use isValidColorName() instead.
1077
1078 Returns \c true if the \a name is a valid color name and can
1079 be used to construct a valid QColor object, otherwise returns
1080 false.
1081
1082 It uses the same algorithm used in fromString().
1083
1084 \sa fromString()
1085*/
1086bool QColor::isValidColor(const QString &name)
1087{
1088 return isValidColorName(qToAnyStringViewIgnoringNull(name));
1089}
1090
1091/*!
1092 \overload
1093 \since 5.10
1094 \deprecated [6.6] Use isValidColorName() instead.
1095*/
1096bool QColor::isValidColor(QStringView name) noexcept
1097{
1098 return isValidColorName(name);
1099}
1100
1101/*!
1102 \overload
1103 \since 5.8
1104 \deprecated [6.6] Use isValidColorName() instead.
1105*/
1106bool QColor::isValidColor(QLatin1StringView name) noexcept
1107{
1108 return isValidColorName(name);
1109}
1110#endif // QT_DEPRECATED_SINCE(6, 6)
1111
1112/*!
1113 \since 6.4
1114
1115 Returns \c true if the \a name is a valid color name and can
1116 be used to construct a valid QColor object, otherwise returns
1117 false.
1118
1119 It uses the same algorithm used in fromString().
1120
1121 \sa fromString()
1122*/
1123bool QColor::isValidColorName(QAnyStringView name) noexcept
1124{
1125 return fromString(name).isValid();
1126}
1127
1128/*!
1129 \since 6.4
1130
1131 Returns an RGB QColor parsed from \a name, which may be in one
1132 of these formats:
1133
1134 \list
1135 \li #RGB (each of R, G, and B is a single hex digit)
1136 \li #RRGGBB
1137 \li #AARRGGBB (Since 5.2)
1138 \li #RRRGGGBBB
1139 \li #RRRRGGGGBBBB
1140 \li A name from the list of colors defined in the list of
1141 \l{https://www.w3.org/TR/SVG11/types.html#ColorKeywords}{SVG color keyword names}
1142 provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
1143 These color names work on all platforms. Note that these color names are \e not the
1144 same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
1145 refer to the same color.
1146 \li \c transparent - representing the absence of a color.
1147 \endlist
1148
1149 Returns an invalid color if \a name cannot be parsed.
1150
1151 \sa isValidColorName()
1152*/
1153QColor QColor::fromString(QAnyStringView name) noexcept
1154{
1155 if (!name.size())
1156 return {};
1157
1158 if (name.front() == u'#') {
1159 if (std::optional<QRgba64> r = get_hex_rgb(name))
1160 return QColor::fromRgba64(*r);
1161#ifndef QT_NO_COLORNAMES
1162 } else if (std::optional<QRgb> r = get_named_rgb(name)) {
1163 return QColor::fromRgba(*r);
1164#endif
1165 }
1166
1167 return {};
1168}
1169
1170/*!
1171 Returns a QStringList containing the color names Qt knows about.
1172
1173 \sa {QColor#Predefined Colors}{Predefined Colors}
1174*/
1175QStringList QColor::colorNames()
1176{
1177 return get_colornames();
1178}
1179
1180/*!
1181 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
1182 saturation, value, and alpha-channel (transparency) components of the
1183 color's HSV value.
1184
1185 These components can be retrieved individually using the hueF(),
1186 saturationF(), valueF() and alphaF() functions.
1187
1188 \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1189*/
1190void QColor::getHsvF(float *h, float *s, float *v, float *a) const
1191{
1192 if (!h || !s || !v)
1193 return;
1194
1195 if (cspec != Invalid && cspec != Hsv) {
1196 toHsv().getHsvF(h, s, v, a);
1197 return;
1198 }
1199
1200 *h = ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f;
1201 *s = ct.ahsv.saturation / float(USHRT_MAX);
1202 *v = ct.ahsv.value / float(USHRT_MAX);
1203
1204 if (a)
1205 *a = ct.ahsv.alpha / float(USHRT_MAX);
1206}
1207
1208/*!
1209 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
1210 saturation, value, and alpha-channel (transparency) components of the
1211 color's HSV value.
1212
1213 These components can be retrieved individually using the hue(),
1214 saturation(), value() and alpha() functions.
1215
1216 \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1217*/
1218void QColor::getHsv(int *h, int *s, int *v, int *a) const
1219{
1220 if (!h || !s || !v)
1221 return;
1222
1223 if (cspec != Invalid && cspec != Hsv) {
1224 toHsv().getHsv(h, s, v, a);
1225 return;
1226 }
1227
1228 *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1229 *s = qt_div_257(ct.ahsv.saturation);
1230 *v = qt_div_257(ct.ahsv.value);
1231
1232 if (a)
1233 *a = qt_div_257(ct.ahsv.alpha);
1234}
1235
1236/*!
1237 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
1238 the value and \a a is the alpha component of the HSV color.
1239
1240 All the values must be in the range 0.0-1.0.
1241
1242 \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1243*/
1244void QColor::setHsvF(float h, float s, float v, float a)
1245{
1246 if (!qColorCheckHsvFValidity(h, s, v, a, __func__)) {
1247 invalidate();
1248 return;
1249 }
1250
1251 cspec = Hsv;
1252 ct.ahsv.alpha = qRound(a * USHRT_MAX);
1253 ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
1254 ct.ahsv.saturation = qRound(s * USHRT_MAX);
1255 ct.ahsv.value = qRound(v * USHRT_MAX);
1256 ct.ahsv.pad = 0;
1257}
1258
1259/*!
1260 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
1261 the value and \a a is the alpha component of the HSV color.
1262
1263 The saturation, value and alpha-channel values must be in the range 0-255,
1264 and the hue value must be greater than -1.
1265
1266 \sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1267*/
1268void QColor::setHsv(int h, int s, int v, int a)
1269{
1270 if (!qColorCheckHsvValidity(h, s, v, a, __func__, QColorHueLimit::None)) {
1271 invalidate();
1272 return;
1273 }
1274
1275 cspec = Hsv;
1276 ct.ahsv.alpha = a * 0x101;
1277 ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1278 ct.ahsv.saturation = s * 0x101;
1279 ct.ahsv.value = v * 0x101;
1280 ct.ahsv.pad = 0;
1281}
1282
1283/*!
1284 \since 4.6
1285
1286 Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
1287 saturation, lightness, and alpha-channel (transparency) components of the
1288 color's HSL value.
1289
1290 These components can be retrieved individually using the hslHueF(),
1291 hslSaturationF(), lightnessF() and alphaF() functions.
1292
1293 \sa getHsl(), setHslF(), {QColor#The HSL Color Model}{The HSL Color Model}
1294*/
1295void QColor::getHslF(float *h, float *s, float *l, float *a) const
1296{
1297 if (!h || !s || !l)
1298 return;
1299
1300 if (cspec != Invalid && cspec != Hsl) {
1301 toHsl().getHslF(h, s, l, a);
1302 return;
1303 }
1304
1305 *h = ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f;
1306 *s = ct.ahsl.saturation / float(USHRT_MAX);
1307 *l = ct.ahsl.lightness / float(USHRT_MAX);
1308
1309 if (a)
1310 *a = ct.ahsl.alpha / float(USHRT_MAX);
1311}
1312
1313/*!
1314 \since 4.6
1315
1316 Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
1317 saturation, lightness, and alpha-channel (transparency) components of the
1318 color's HSL value.
1319
1320 These components can be retrieved individually using the hslHue(),
1321 hslSaturation(), lightness() and alpha() functions.
1322
1323 \sa getHslF(), setHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
1324*/
1325void QColor::getHsl(int *h, int *s, int *l, int *a) const
1326{
1327 if (!h || !s || !l)
1328 return;
1329
1330 if (cspec != Invalid && cspec != Hsl) {
1331 toHsl().getHsl(h, s, l, a);
1332 return;
1333 }
1334
1335 *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1336 *s = qt_div_257(ct.ahsl.saturation);
1337 *l = qt_div_257(ct.ahsl.lightness);
1338
1339 if (a)
1340 *a = qt_div_257(ct.ahsl.alpha);
1341}
1342
1343/*!
1344 \since 4.6
1345
1346 Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
1347 the lightness and \a a is the alpha component of the HSL color.
1348
1349 All the values must be in the range 0.0-1.0.
1350
1351 \sa getHslF(), setHsl()
1352*/
1353void QColor::setHslF(float h, float s, float l, float a)
1354{
1355 if (!qColorCheckHslFValidity(h, s, l, a, __func__)) {
1356 invalidate();
1357 return;
1358 }
1359
1360 cspec = Hsl;
1361 ct.ahsl.alpha = qRound(a * USHRT_MAX);
1362 ct.ahsl.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
1363 ct.ahsl.saturation = qRound(s * USHRT_MAX);
1364 ct.ahsl.lightness = qRound(l * USHRT_MAX);
1365 ct.ahsl.pad = 0;
1366}
1367
1368/*!
1369 \since 4.6
1370
1371 Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
1372 the lightness and \a a is the alpha component of the HSL color.
1373
1374 The saturation, value and alpha-channel values must be in the range 0-255,
1375 and the hue value must be greater than -1.
1376
1377 \sa getHsl(), setHslF()
1378*/
1379void QColor::setHsl(int h, int s, int l, int a)
1380{
1381 if (!qColorCheckHslValidity(h, s, l, a, __func__, QColorHueLimit::None)) {
1382 invalidate();
1383 return;
1384 }
1385
1386 cspec = Hsl;
1387 ct.ahsl.alpha = a * 0x101;
1388 ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1389 ct.ahsl.saturation = s * 0x101;
1390 ct.ahsl.lightness = l * 0x101;
1391 ct.ahsl.pad = 0;
1392}
1393
1394static inline qfloat16 &castF16(quint16 &v)
1395{
1396 // this works because qfloat16 internally is a quint16
1397 return *reinterpret_cast<qfloat16 *>(&v);
1398}
1399
1400static inline const qfloat16 &castF16(const quint16 &v)
1401{
1402 return *reinterpret_cast<const qfloat16 *>(&v);
1403}
1404
1405/*!
1406 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
1407 green, blue, and alpha-channel (transparency) components of the color's
1408 RGB value.
1409
1410 These components can be retrieved individually using the redF(), greenF(),
1411 blueF() and alphaF() functions.
1412
1413 \sa rgb(), setRgb()
1414*/
1415void QColor::getRgbF(float *r, float *g, float *b, float *a) const
1416{
1417 if (!r || !g || !b)
1418 return;
1419
1420 if (cspec != Invalid && cspec != Rgb && cspec != ExtendedRgb) {
1421 toRgb().getRgbF(r, g, b, a);
1422 return;
1423 }
1424
1425 if (cspec == Rgb || cspec == Invalid) {
1426 *r = ct.argb.red / float(USHRT_MAX);
1427 *g = ct.argb.green / float(USHRT_MAX);
1428 *b = ct.argb.blue / float(USHRT_MAX);
1429 if (a)
1430 *a = ct.argb.alpha / float(USHRT_MAX);
1431 } else {
1432 *r = castF16(ct.argbExtended.redF16);
1433 *g = castF16(ct.argbExtended.greenF16);
1434 *b = castF16(ct.argbExtended.blueF16);
1435 if (a)
1436 *a = castF16(ct.argbExtended.alphaF16);
1437 }
1438}
1439
1440/*!
1441 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
1442 green, blue, and alpha-channel (transparency) components of the color's
1443 RGB value.
1444
1445 These components can be retrieved individually using the red(), green(),
1446 blue() and alpha() functions.
1447
1448 \sa rgb(), setRgb()
1449*/
1450void QColor::getRgb(int *r, int *g, int *b, int *a) const
1451{
1452 if (!r || !g || !b)
1453 return;
1454
1455 if (cspec != Invalid && cspec != Rgb) {
1456 toRgb().getRgb(r, g, b, a);
1457 return;
1458 }
1459
1460 *r = qt_div_257(ct.argb.red);
1461 *g = qt_div_257(ct.argb.green);
1462 *b = qt_div_257(ct.argb.blue);
1463
1464 if (a)
1465 *a = qt_div_257(ct.argb.alpha);
1466}
1467
1468/*!
1469 \fn void QColor::setRgbF(float r, float g, float b, float a)
1470
1471 Sets the color channels of this color to \a r (red), \a g (green),
1472 \a b (blue) and \a a (alpha, transparency).
1473
1474 The alpha value must be in the range 0.0-1.0.
1475 If any of the other values are outside the range of 0.0-1.0 the
1476 color model will be set as \c ExtendedRgb.
1477
1478 \sa rgb(), getRgbF(), setRgb()
1479*/
1480void QColor::setRgbF(float r, float g, float b, float a)
1481{
1482 switch (qColorCheckRgbFValidity(r, g, b, a, cspec, __func__)) {
1483 case QColorRgbFValidity::Invalid:
1484 invalidate();
1485 return;
1486 case QColorRgbFValidity::ExtendedRgb:
1487 cspec = ExtendedRgb;
1488 castF16(ct.argbExtended.redF16) = qfloat16(r);
1489 castF16(ct.argbExtended.greenF16) = qfloat16(g);
1490 castF16(ct.argbExtended.blueF16) = qfloat16(b);
1491 castF16(ct.argbExtended.alphaF16) = qfloat16(a);
1492 ct.argbExtended.pad = 0;
1493 return;
1494 case QColorRgbFValidity::Rgb:
1495 cspec = Rgb;
1496 ct.argb.red = qRound(r * USHRT_MAX);
1497 ct.argb.green = qRound(g * USHRT_MAX);
1498 ct.argb.blue = qRound(b * USHRT_MAX);
1499 ct.argb.alpha = qRound(a * USHRT_MAX);
1500 ct.argb.pad = 0;
1501 return;
1502 }
1503}
1504
1505/*!
1506 Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
1507
1508 All the values must be in the range 0-255.
1509
1510 \sa rgb(), getRgb(), setRgbF()
1511*/
1512void QColor::setRgb(int r, int g, int b, int a)
1513{
1514 if (!qColorCheckRgbValidity(r, g, b, a, __func__)) {
1515 invalidate();
1516 return;
1517 }
1518
1519 cspec = Rgb;
1520 ct.argb.alpha = a * 0x101;
1521 ct.argb.red = r * 0x101;
1522 ct.argb.green = g * 0x101;
1523 ct.argb.blue = b * 0x101;
1524 ct.argb.pad = 0;
1525}
1526
1527/*!
1528 \fn QRgb QColor::rgba() const
1529
1530 Returns the RGB value of the color, including its alpha.
1531
1532 For an invalid color, the alpha value of the returned color is unspecified.
1533
1534 \sa setRgba(), rgb(), rgba64()
1535*/
1536
1537QRgb QColor::rgba() const noexcept
1538{
1539 if (cspec != Invalid && cspec != Rgb)
1540 return toRgb().rgba();
1541 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));
1542}
1543
1544/*!
1545 Sets the RGB value to \a rgba, including its alpha.
1546
1547 \sa rgba(), rgb(), setRgba64()
1548*/
1549void QColor::setRgba(QRgb rgba) noexcept
1550{
1551 cspec = Rgb;
1552 ct.argb.alpha = qAlpha(rgba) * 0x101;
1553 ct.argb.red = qRed(rgba) * 0x101;
1554 ct.argb.green = qGreen(rgba) * 0x101;
1555 ct.argb.blue = qBlue(rgba) * 0x101;
1556 ct.argb.pad = 0;
1557}
1558
1559/*!
1560 \since 5.6
1561
1562 Returns the RGB64 value of the color, including its alpha.
1563
1564 For an invalid color, the alpha value of the returned color is unspecified.
1565
1566 \sa setRgba64(), rgba(), rgb()
1567*/
1568
1569QRgba64 QColor::rgba64() const noexcept
1570{
1571 if (cspec != Invalid && cspec != Rgb)
1572 return toRgb().rgba64();
1573 return qRgba64(ct.argb.red, ct.argb.green, ct.argb.blue, ct.argb.alpha);
1574}
1575
1576/*!
1577 \since 5.6
1578
1579 Sets the RGB64 value to \a rgba, including its alpha.
1580
1581 \sa setRgba(), rgba64()
1582*/
1583void QColor::setRgba64(QRgba64 rgba) noexcept
1584{
1585 cspec = Rgb;
1586 ct.argb.alpha = rgba.alpha();
1587 ct.argb.red = rgba.red();
1588 ct.argb.green = rgba.green();
1589 ct.argb.blue = rgba.blue();
1590 ct.argb.pad = 0;
1591}
1592
1593/*!
1594 \fn QRgb QColor::rgb() const
1595
1596 Returns the RGB value of the color. The alpha value is opaque.
1597
1598 \sa getRgb(), rgba()
1599*/
1600QRgb QColor::rgb() const noexcept
1601{
1602 if (cspec != Invalid && cspec != Rgb)
1603 return toRgb().rgb();
1604 return qRgb(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue));
1605}
1606
1607/*!
1608 \overload
1609
1610 Sets the RGB value to \a rgb. The alpha value is set to opaque.
1611*/
1612void QColor::setRgb(QRgb rgb) noexcept
1613{
1614 cspec = Rgb;
1615 ct.argb.alpha = 0xffff;
1616 ct.argb.red = qRed(rgb) * 0x101;
1617 ct.argb.green = qGreen(rgb) * 0x101;
1618 ct.argb.blue = qBlue(rgb) * 0x101;
1619 ct.argb.pad = 0;
1620}
1621
1622/*!
1623 Returns the alpha color component of this color.
1624
1625 \sa setAlpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1626*/
1627int QColor::alpha() const noexcept
1628{
1629 if (cspec == ExtendedRgb)
1630 return qRound(float(castF16(ct.argbExtended.alphaF16)) * 255);
1631 return qt_div_257(ct.argb.alpha);
1632}
1633
1634
1635/*!
1636 Sets the alpha of this color to \a alpha. Integer alpha is specified in the
1637 range 0-255.
1638
1639 \sa alpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1640*/
1641
1642void QColor::setAlpha(int alpha)
1643{
1644 alpha = qColorClampToIntRange(alpha, __func__);
1645
1646 if (cspec == ExtendedRgb) {
1647 constexpr float f = 1.0f / 255;
1648 castF16(ct.argbExtended.alphaF16) = qfloat16(alpha * f);
1649 return;
1650 }
1651 ct.argb.alpha = alpha * 0x101;
1652}
1653
1654/*!
1655 Returns the alpha color component of this color.
1656
1657 \sa setAlphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1658*/
1659float QColor::alphaF() const noexcept
1660{
1661 if (cspec == ExtendedRgb)
1662 return castF16(ct.argbExtended.alphaF16);
1663 return ct.argb.alpha / float(USHRT_MAX);
1664}
1665
1666/*!
1667 Sets the alpha of this color to \a alpha. float alpha is specified in the
1668 range 0.0-1.0.
1669
1670 \sa alphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1671
1672*/
1673void QColor::setAlphaF(float alpha)
1674{
1675 alpha = qColorClampToFloatRange(alpha, __func__);
1676
1677 if (cspec == ExtendedRgb) {
1678 castF16(ct.argbExtended.alphaF16) = qfloat16(alpha);
1679 return;
1680 }
1681 float tmp = alpha * USHRT_MAX;
1682 ct.argb.alpha = qRound(tmp);
1683}
1684
1685
1686/*!
1687 Returns the red color component of this color.
1688
1689 \sa setRed(), redF(), getRgb()
1690*/
1691int QColor::red() const noexcept
1692{
1693 if (cspec != Invalid && cspec != Rgb)
1694 return toRgb().red();
1695 return qt_div_257(ct.argb.red);
1696}
1697
1698/*!
1699 Sets the red color component of this color to \a red. Integer components
1700 are specified in the range 0-255.
1701
1702 \sa red(), redF(), setRgb()
1703*/
1704void QColor::setRed(int red)
1705{
1706 red = qColorClampToIntRange(red, __func__);
1707
1708 if (cspec != Rgb)
1709 setRgb(red, green(), blue(), alpha());
1710 else
1711 ct.argb.red = red * 0x101;
1712}
1713
1714/*!
1715 Returns the green color component of this color.
1716
1717 \sa setGreen(), greenF(), getRgb()
1718*/
1719int QColor::green() const noexcept
1720{
1721 if (cspec != Invalid && cspec != Rgb)
1722 return toRgb().green();
1723 return qt_div_257(ct.argb.green);
1724}
1725
1726/*!
1727 Sets the green color component of this color to \a green. Integer
1728 components are specified in the range 0-255.
1729
1730 \sa green(), greenF(), setRgb()
1731*/
1732void QColor::setGreen(int green)
1733{
1734 green = qColorClampToIntRange(green, __func__);
1735
1736 if (cspec != Rgb)
1737 setRgb(red(), green, blue(), alpha());
1738 else
1739 ct.argb.green = green * 0x101;
1740}
1741
1742
1743/*!
1744 Returns the blue color component of this color.
1745
1746 \sa setBlue(), blueF(), getRgb()
1747*/
1748int QColor::blue() const noexcept
1749{
1750 if (cspec != Invalid && cspec != Rgb)
1751 return toRgb().blue();
1752 return qt_div_257(ct.argb.blue);
1753}
1754
1755
1756/*!
1757 Sets the blue color component of this color to \a blue. Integer components
1758 are specified in the range 0-255.
1759
1760 \sa blue(), blueF(), setRgb()
1761*/
1762void QColor::setBlue(int blue)
1763{
1764 blue = qColorClampToIntRange(blue, __func__);
1765
1766 if (cspec != Rgb)
1767 setRgb(red(), green(), blue, alpha());
1768 else
1769 ct.argb.blue = blue * 0x101;
1770}
1771
1772/*!
1773 Returns the red color component of this color.
1774
1775 \sa setRedF(), red(), getRgbF()
1776*/
1777float QColor::redF() const noexcept
1778{
1779 if (cspec == Rgb || cspec == Invalid)
1780 return ct.argb.red / float(USHRT_MAX);
1781 if (cspec == ExtendedRgb)
1782 return castF16(ct.argbExtended.redF16);
1783
1784 return toRgb().redF();
1785}
1786
1787
1788/*!
1789 Sets the red color component of this color to \a red. If \a red lies outside
1790 the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1791
1792 \sa redF(), red(), setRgbF()
1793*/
1794void QColor::setRedF(float red)
1795{
1796 if (cspec == Rgb && qColorComponentIsInFloatRange(red)) {
1797 ct.argb.red = qRound(red * USHRT_MAX);
1798 return;
1799 }
1800
1801 red = qColorClampNaNToNull(red, __func__);
1802
1803 if (cspec == ExtendedRgb)
1804 castF16(ct.argbExtended.redF16) = qfloat16(red);
1805 else
1806 setRgbF(red, greenF(), blueF(), alphaF());
1807}
1808
1809/*!
1810 Returns the green color component of this color.
1811
1812 \sa setGreenF(), green(), getRgbF()
1813*/
1814float QColor::greenF() const noexcept
1815{
1816 if (cspec == Rgb || cspec == Invalid)
1817 return ct.argb.green / float(USHRT_MAX);
1818 if (cspec == ExtendedRgb)
1819 return castF16(ct.argbExtended.greenF16);
1820
1821 return toRgb().greenF();
1822}
1823
1824
1825/*!
1826 Sets the green color component of this color to \a green. If \a green lies outside
1827 the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1828
1829 \sa greenF(), green(), setRgbF()
1830*/
1831void QColor::setGreenF(float green)
1832{
1833 if (cspec == Rgb && qColorComponentIsInFloatRange(green)) {
1834 ct.argb.green = qRound(green * USHRT_MAX);
1835 return;
1836 }
1837
1838 green = qColorClampNaNToNull(green, __func__);
1839
1840 if (cspec == ExtendedRgb)
1841 castF16(ct.argbExtended.greenF16) = qfloat16(green);
1842 else
1843 setRgbF(redF(), green, blueF(), alphaF());
1844}
1845
1846/*!
1847 Returns the blue color component of this color.
1848
1849 \sa setBlueF(), blue(), getRgbF()
1850*/
1851float QColor::blueF() const noexcept
1852{
1853 if (cspec == Rgb || cspec == Invalid)
1854 return ct.argb.blue / float(USHRT_MAX);
1855 if (cspec == ExtendedRgb)
1856 return castF16(ct.argbExtended.blueF16);
1857
1858 return toRgb().blueF();
1859}
1860
1861/*!
1862 Sets the blue color component of this color to \a blue. If \a blue lies outside
1863 the 0.0-1.0 range, the color model will be changed to \c ExtendedRgb.
1864 \sa blueF(), blue(), setRgbF()
1865*/
1866void QColor::setBlueF(float blue)
1867{
1868 if (cspec == Rgb && qColorComponentIsInFloatRange(blue)) {
1869 ct.argb.blue = qRound(blue * USHRT_MAX);
1870 return;
1871 }
1872
1873 blue = qColorClampNaNToNull(blue, __func__);
1874
1875 if (cspec == ExtendedRgb)
1876 castF16(ct.argbExtended.blueF16) = qfloat16(blue);
1877 else
1878 setRgbF(redF(), greenF(), blue, alphaF());
1879}
1880
1881/*!
1882 Returns the HSV hue color component of this color.
1883
1884 The color is implicitly converted to HSV.
1885
1886 \sa hsvHue(), hslHue(), hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1887*/
1888
1889int QColor::hue() const noexcept
1890{
1891 return hsvHue();
1892}
1893
1894/*!
1895 Returns the HSV hue color component of this color.
1896
1897 \sa hueF(), hslHue(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1898*/
1899int QColor::hsvHue() const noexcept
1900{
1901 if (cspec != Invalid && cspec != Hsv)
1902 return toHsv().hue();
1903 return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1904}
1905
1906/*!
1907 Returns the HSV saturation color component of this color.
1908
1909 The color is implicitly converted to HSV.
1910
1911 \sa hsvSaturation(), hslSaturation(), saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1912 Model}
1913*/
1914
1915int QColor::saturation() const noexcept
1916{
1917 return hsvSaturation();
1918}
1919
1920/*!
1921 Returns the HSV saturation color component of this color.
1922
1923 \sa saturationF(), hslSaturation(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1924*/
1925int QColor::hsvSaturation() const noexcept
1926{
1927 if (cspec != Invalid && cspec != Hsv)
1928 return toHsv().saturation();
1929 return qt_div_257(ct.ahsv.saturation);
1930}
1931
1932/*!
1933 Returns the value color component of this color.
1934
1935 \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1936*/
1937int QColor::value() const noexcept
1938{
1939 if (cspec != Invalid && cspec != Hsv)
1940 return toHsv().value();
1941 return qt_div_257(ct.ahsv.value);
1942}
1943
1944/*!
1945 Returns the HSV hue color component of this color.
1946
1947 The color is implicitly converted to HSV.
1948
1949 \sa hsvHueF(), hslHueF(), hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1950*/
1951float QColor::hueF() const noexcept
1952{
1953 return hsvHueF();
1954}
1955
1956/*!
1957 Returns the hue color component of this color.
1958
1959 \sa hue(), hslHueF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1960 Model}
1961*/
1962float QColor::hsvHueF() const noexcept
1963{
1964 if (cspec != Invalid && cspec != Hsv)
1965 return toHsv().hueF();
1966 return ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f;
1967}
1968
1969/*!
1970 Returns the HSV saturation color component of this color.
1971
1972 The color is implicitly converted to HSV.
1973
1974 \sa hsvSaturationF(), hslSaturationF(), saturation(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1975 Model}
1976*/
1977float QColor::saturationF() const noexcept
1978{
1979 return hsvSaturationF();
1980}
1981
1982/*!
1983 Returns the HSV saturation color component of this color.
1984
1985 \sa saturation(), hslSaturationF(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1986*/
1987float QColor::hsvSaturationF() const noexcept
1988{
1989 if (cspec != Invalid && cspec != Hsv)
1990 return toHsv().saturationF();
1991 return ct.ahsv.saturation / float(USHRT_MAX);
1992}
1993
1994/*!
1995 Returns the value color component of this color.
1996
1997 \sa value(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1998*/
1999float QColor::valueF() const noexcept
2000{
2001 if (cspec != Invalid && cspec != Hsv)
2002 return toHsv().valueF();
2003 return ct.ahsv.value / float(USHRT_MAX);
2004}
2005
2006/*!
2007 \since 4.6
2008
2009 Returns the HSL hue color component of this color.
2010
2011 \sa hslHueF(), hsvHue(), getHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
2012*/
2013int QColor::hslHue() const noexcept
2014{
2015 if (cspec != Invalid && cspec != Hsl)
2016 return toHsl().hslHue();
2017 return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
2018}
2019
2020/*!
2021 \since 4.6
2022
2023 Returns the HSL saturation color component of this color.
2024
2025 \sa hslSaturationF(), hsvSaturation(), getHsl(), {QColor#The HSL Color Model}{The HSL Color Model}
2026*/
2027int QColor::hslSaturation() const noexcept
2028{
2029 if (cspec != Invalid && cspec != Hsl)
2030 return toHsl().hslSaturation();
2031 return qt_div_257(ct.ahsl.saturation);
2032}
2033
2034/*!
2035 \since 4.6
2036
2037 Returns the lightness color component of this color.
2038
2039 \sa lightnessF(), getHsl()
2040*/
2041int QColor::lightness() const noexcept
2042{
2043 if (cspec != Invalid && cspec != Hsl)
2044 return toHsl().lightness();
2045 return qt_div_257(ct.ahsl.lightness);
2046}
2047
2048/*!
2049 \since 4.6
2050
2051 Returns the HSL hue color component of this color.
2052
2053 \sa hslHue(), hsvHueF(), getHslF()
2054*/
2055float QColor::hslHueF() const noexcept
2056{
2057 if (cspec != Invalid && cspec != Hsl)
2058 return toHsl().hslHueF();
2059 return ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f;
2060}
2061
2062/*!
2063 \since 4.6
2064
2065 Returns the HSL saturation color component of this color.
2066
2067 \sa hslSaturation(), hsvSaturationF(), getHslF(), {QColor#The HSL Color Model}{The HSL Color Model}
2068*/
2069float QColor::hslSaturationF() const noexcept
2070{
2071 if (cspec != Invalid && cspec != Hsl)
2072 return toHsl().hslSaturationF();
2073 return ct.ahsl.saturation / float(USHRT_MAX);
2074}
2075
2076/*!
2077 \since 4.6
2078
2079 Returns the lightness color component of this color.
2080
2081 \sa value(), getHslF()
2082*/
2083float QColor::lightnessF() const noexcept
2084{
2085 if (cspec != Invalid && cspec != Hsl)
2086 return toHsl().lightnessF();
2087 return ct.ahsl.lightness / float(USHRT_MAX);
2088}
2089
2090/*!
2091 Returns the cyan color component of this color.
2092
2093 \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2094*/
2095int QColor::cyan() const noexcept
2096{
2097 if (cspec != Invalid && cspec != Cmyk)
2098 return toCmyk().cyan();
2099 return qt_div_257(ct.acmyk.cyan);
2100}
2101
2102/*!
2103 Returns the magenta color component of this color.
2104
2105 \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2106*/
2107int QColor::magenta() const noexcept
2108{
2109 if (cspec != Invalid && cspec != Cmyk)
2110 return toCmyk().magenta();
2111 return qt_div_257(ct.acmyk.magenta);
2112}
2113
2114/*!
2115 Returns the yellow color component of this color.
2116
2117 \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2118*/
2119int QColor::yellow() const noexcept
2120{
2121 if (cspec != Invalid && cspec != Cmyk)
2122 return toCmyk().yellow();
2123 return qt_div_257(ct.acmyk.yellow);
2124}
2125
2126/*!
2127 Returns the black color component of this color.
2128
2129 \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2130
2131*/
2132int QColor::black() const noexcept
2133{
2134 if (cspec != Invalid && cspec != Cmyk)
2135 return toCmyk().black();
2136 return qt_div_257(ct.acmyk.black);
2137}
2138
2139/*!
2140 Returns the cyan color component of this color.
2141
2142 \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2143*/
2144float QColor::cyanF() const noexcept
2145{
2146 if (cspec != Invalid && cspec != Cmyk)
2147 return toCmyk().cyanF();
2148 return ct.acmyk.cyan / float(USHRT_MAX);
2149}
2150
2151/*!
2152 Returns the magenta color component of this color.
2153
2154 \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2155*/
2156float QColor::magentaF() const noexcept
2157{
2158 if (cspec != Invalid && cspec != Cmyk)
2159 return toCmyk().magentaF();
2160 return ct.acmyk.magenta / float(USHRT_MAX);
2161}
2162
2163/*!
2164 Returns the yellow color component of this color.
2165
2166 \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2167*/
2168float QColor::yellowF() const noexcept
2169{
2170 if (cspec != Invalid && cspec != Cmyk)
2171 return toCmyk().yellowF();
2172 return ct.acmyk.yellow / float(USHRT_MAX);
2173}
2174
2175/*!
2176 Returns the black color component of this color.
2177
2178 \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2179*/
2180float QColor::blackF() const noexcept
2181{
2182 if (cspec != Invalid && cspec != Cmyk)
2183 return toCmyk().blackF();
2184 return ct.acmyk.black / float(USHRT_MAX);
2185}
2186
2187/*!
2188 Create and returns an extended RGB QColor based on this color.
2189 \since 5.14
2190
2191 \sa toRgb, convertTo()
2192*/
2193QColor QColor::toExtendedRgb() const noexcept
2194{
2195 if (!isValid() || cspec == ExtendedRgb)
2196 return *this;
2197 if (cspec != Rgb)
2198 return toRgb().toExtendedRgb();
2199
2200 constexpr float f = 1.0f / USHRT_MAX;
2201 QColor color;
2202 color.cspec = ExtendedRgb;
2203 castF16(color.ct.argbExtended.alphaF16) = qfloat16(ct.argb.alpha * f);
2204 castF16(color.ct.argbExtended.redF16) = qfloat16(ct.argb.red * f);
2205 castF16(color.ct.argbExtended.greenF16) = qfloat16(ct.argb.green * f);
2206 castF16(color.ct.argbExtended.blueF16) = qfloat16(ct.argb.blue * f);
2207 color.ct.argbExtended.pad = 0;
2208 return color;
2209}
2210
2211/*!
2212 Create and returns an RGB QColor based on this color.
2213
2214 \sa fromRgb(), convertTo(), isValid()
2215*/
2216QColor QColor::toRgb() const noexcept
2217{
2218 if (!isValid() || cspec == Rgb)
2219 return *this;
2220
2221 QColor color;
2222 color.cspec = Rgb;
2223 if (cspec != ExtendedRgb)
2224 color.ct.argb.alpha = ct.argb.alpha;
2225 color.ct.argb.pad = 0;
2226
2227 switch (cspec) {
2228 case Hsv:
2229 {
2230 if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
2231 // achromatic case
2232 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
2233 break;
2234 }
2235
2236 // chromatic case
2237 const float h = ct.ahsv.hue == 36000 ? 0.0f : ct.ahsv.hue / 6000.0f;
2238 const float s = ct.ahsv.saturation / float(USHRT_MAX);
2239 const float v = ct.ahsv.value / float(USHRT_MAX);
2240 const int i = int(h);
2241 const float f = h - i;
2242 const float p = v * (1.0f - s);
2243
2244 if (i & 1) {
2245 const float q = v * (1.0f - (s * f));
2246
2247 switch (i) {
2248 case 1:
2249 color.ct.argb.red = qRound(q * USHRT_MAX);
2250 color.ct.argb.green = qRound(v * USHRT_MAX);
2251 color.ct.argb.blue = qRound(p * USHRT_MAX);
2252 break;
2253 case 3:
2254 color.ct.argb.red = qRound(p * USHRT_MAX);
2255 color.ct.argb.green = qRound(q * USHRT_MAX);
2256 color.ct.argb.blue = qRound(v * USHRT_MAX);
2257 break;
2258 case 5:
2259 color.ct.argb.red = qRound(v * USHRT_MAX);
2260 color.ct.argb.green = qRound(p * USHRT_MAX);
2261 color.ct.argb.blue = qRound(q * USHRT_MAX);
2262 break;
2263 }
2264 } else {
2265 const float t = v * (1.0f - (s * (1.0f - f)));
2266
2267 switch (i) {
2268 case 0:
2269 color.ct.argb.red = qRound(v * USHRT_MAX);
2270 color.ct.argb.green = qRound(t * USHRT_MAX);
2271 color.ct.argb.blue = qRound(p * USHRT_MAX);
2272 break;
2273 case 2:
2274 color.ct.argb.red = qRound(p * USHRT_MAX);
2275 color.ct.argb.green = qRound(v * USHRT_MAX);
2276 color.ct.argb.blue = qRound(t * USHRT_MAX);
2277 break;
2278 case 4:
2279 color.ct.argb.red = qRound(t * USHRT_MAX);
2280 color.ct.argb.green = qRound(p * USHRT_MAX);
2281 color.ct.argb.blue = qRound(v * USHRT_MAX);
2282 break;
2283 }
2284 }
2285 break;
2286 }
2287 case Hsl:
2288 {
2289 if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
2290 // achromatic case
2291 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
2292 } else if (ct.ahsl.lightness == 0) {
2293 // lightness 0
2294 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
2295 } else {
2296 // chromatic case
2297 const float h = ct.ahsl.hue == 36000 ? 0.0f : ct.ahsl.hue / 36000.0f;
2298 const float s = ct.ahsl.saturation / float(USHRT_MAX);
2299 const float l = ct.ahsl.lightness / float(USHRT_MAX);
2300
2301 float temp2;
2302 if (l < 0.5f)
2303 temp2 = l * (1.0f + s);
2304 else
2305 temp2 = l + s - (l * s);
2306
2307 const float temp1 = (2.0f * l) - temp2;
2308 float temp3[3] = { h + (1.0f / 3.0f),
2309 h,
2310 h - (1.0f / 3.0f) };
2311
2312 for (int i = 0; i != 3; ++i) {
2313 if (temp3[i] < 0.0f)
2314 temp3[i] += 1.0f;
2315 else if (temp3[i] > 1.0f)
2316 temp3[i] -= 1.0f;
2317
2318 const float sixtemp3 = temp3[i] * 6.0f;
2319 if (sixtemp3 < 1.0f)
2320 color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
2321 else if ((temp3[i] * 2.0f) < 1.0f)
2322 color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
2323 else if ((temp3[i] * 3.0f) < 2.0f)
2324 color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (2.0f /3.0f - temp3[i]) * 6.0f) * USHRT_MAX);
2325 else
2326 color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
2327 }
2328 color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
2329 color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
2330 color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
2331 }
2332 break;
2333 }
2334 case Cmyk:
2335 {
2336 const float c = ct.acmyk.cyan / float(USHRT_MAX);
2337 const float m = ct.acmyk.magenta / float(USHRT_MAX);
2338 const float y = ct.acmyk.yellow / float(USHRT_MAX);
2339 const float k = ct.acmyk.black / float(USHRT_MAX);
2340
2341 color.ct.argb.red = qRound((1.0f - (c * (1.0f - k) + k)) * USHRT_MAX);
2342 color.ct.argb.green = qRound((1.0f - (m * (1.0f - k) + k)) * USHRT_MAX);
2343 color.ct.argb.blue = qRound((1.0f - (y * (1.0f - k) + k)) * USHRT_MAX);
2344 break;
2345 }
2346 case ExtendedRgb:
2347 color.ct.argb.alpha = qRound(USHRT_MAX * float(castF16(ct.argbExtended.alphaF16)));
2348 color.ct.argb.red = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.redF16)), 1.0f));
2349 color.ct.argb.green = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.greenF16)), 1.0f));
2350 color.ct.argb.blue = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.blueF16)), 1.0f));
2351 break;
2352 default:
2353 break;
2354 }
2355
2356 return color;
2357}
2358
2359/*!
2360 Creates and returns an HSV QColor based on this color.
2361
2362 \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2363*/
2364QColor QColor::toHsv() const noexcept
2365{
2366 if (!isValid() || cspec == Hsv)
2367 return *this;
2368
2369 if (cspec != Rgb)
2370 return toRgb().toHsv();
2371
2372 QColor color;
2373 color.cspec = Hsv;
2374 color.ct.ahsv.alpha = ct.argb.alpha;
2375 color.ct.ahsv.pad = 0;
2376
2377 const ushort r = ct.argb.red;
2378 const ushort g = ct.argb.green;
2379 const ushort b = ct.argb.blue;
2380
2381 // cf. https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
2382 const auto [min, max] = std::minmax({r, g, b});
2383 const auto value = max;
2384 color.ct.ahsv.value = value;
2385 if (max == min) {
2386 // achromatic case, hue is undefined
2387 color.ct.ahsv.hue = USHRT_MAX;
2388 color.ct.ahsv.saturation = 0;
2389 } else {
2390 // chromatic case
2391 const float chroma = max - min; // cannot overflow
2392 float hue;
2393 color.ct.ahsv.saturation = qRound((chroma / value) * USHRT_MAX);
2394 if (value == r) {
2395 hue = 0 + (g - b) / chroma;
2396 // hue = hue mod 6:
2397 if (hue < 0)
2398 hue += 6;
2399 } else if (value == g) {
2400 hue = 2 + (b - r) / chroma;
2401 } else {
2402 Q_ASSERT(value == b); // by construction, `value` is one of r, g, and b!
2403 hue = 4 + (r - g) / chroma;
2404 }
2405 color.ct.ahsv.hue = qRound(hue * (60 * 100));
2406 }
2407
2408 return color;
2409}
2410
2411/*!
2412 Creates and returns an HSL QColor based on this color.
2413
2414 \sa fromHsl(), convertTo(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2415*/
2416QColor QColor::toHsl() const noexcept
2417{
2418 if (!isValid() || cspec == Hsl)
2419 return *this;
2420
2421 if (cspec != Rgb)
2422 return toRgb().toHsl();
2423
2424 QColor color;
2425 color.cspec = Hsl;
2426 color.ct.ahsl.alpha = ct.argb.alpha;
2427 color.ct.ahsl.pad = 0;
2428
2429 const ushort r = ct.argb.red;
2430 const ushort g = ct.argb.green;
2431 const ushort b = ct.argb.blue;
2432
2433 // cf. https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB
2434 const auto [min, max] = std::minmax({r, g, b});
2435 const auto value = max;
2436 if (min == max) {
2437 // achromatic case, hue is undefined
2438 color.ct.ahsl.hue = USHRT_MAX;
2439 color.ct.ahsl.saturation = 0;
2440 color.ct.ahsl.lightness = value;
2441 } else {
2442 // chromatic case
2443 const float chroma = max - min;
2444 const float lightness = 0.5f * (uint{max} + min); // use uint to avoid overflow
2445 color.ct.ahsl.lightness = qRound(lightness);
2446 const float saturation = 0.5f * chroma / (std::min)(lightness, USHRT_MAX - lightness);
2447 color.ct.ahsl.saturation = qRound(saturation * USHRT_MAX);
2448 float hue;
2449 if (value == r) {
2450 hue = 0 + (g - b) / chroma;
2451 // hue = hue mod 6
2452 if (hue < 0)
2453 hue += 6;
2454 } else if (value == g) {
2455 hue = 2 + (b - r) / chroma;
2456 } else {
2457 Q_ASSERT(value == b); // by construction, `value` is one of r, g, and b!
2458 hue = 4 + (r - g) / chroma;
2459 }
2460 color.ct.ahsl.hue = qRound(hue * (60 * 100));
2461 }
2462
2463 return color;
2464}
2465
2466/*!
2467 Creates and returns a CMYK QColor based on this color.
2468
2469 \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2470*/
2471QColor QColor::toCmyk() const noexcept
2472{
2473 if (!isValid() || cspec == Cmyk)
2474 return *this;
2475 if (cspec != Rgb)
2476 return toRgb().toCmyk();
2477
2478 QColor color;
2479 color.cspec = Cmyk;
2480 color.ct.acmyk.alpha = ct.argb.alpha;
2481
2482 if (!ct.argb.red && !ct.argb.green && !ct.argb.blue) {
2483 // Avoid div-by-0 below
2484 color.ct.acmyk.cyan = 0;
2485 color.ct.acmyk.magenta = 0;
2486 color.ct.acmyk.yellow = 0;
2487 color.ct.acmyk.black = USHRT_MAX;
2488 } else {
2489 // rgb -> cmy
2490 const float r = ct.argb.red / float(USHRT_MAX);
2491 const float g = ct.argb.green / float(USHRT_MAX);
2492 const float b = ct.argb.blue / float(USHRT_MAX);
2493 float c = 1.0f - r;
2494 float m = 1.0f - g;
2495 float y = 1.0f - b;
2496
2497 // cmy -> cmyk
2498 const float k = qMin(c, qMin(m, y));
2499 c = (c - k) / (1.0f - k);
2500 m = (m - k) / (1.0f - k);
2501 y = (y - k) / (1.0f - k);
2502
2503 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2504 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2505 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2506 color.ct.acmyk.black = qRound(k * USHRT_MAX);
2507 }
2508
2509 return color;
2510}
2511
2512QColor QColor::convertTo(QColor::Spec colorSpec) const noexcept
2513{
2514 if (colorSpec == cspec)
2515 return *this;
2516 switch (colorSpec) {
2517 case Rgb:
2518 return toRgb();
2519 case ExtendedRgb:
2520 return toExtendedRgb();
2521 case Hsv:
2522 return toHsv();
2523 case Cmyk:
2524 return toCmyk();
2525 case Hsl:
2526 return toHsl();
2527 case Invalid:
2528 break;
2529 }
2530 return QColor(); // must be invalid
2531}
2532
2533
2534/*!
2535 Static convenience function that returns a QColor constructed from the
2536 given QRgb value \a rgb.
2537
2538 The alpha component of \a rgb is ignored (i.e. it is automatically set to
2539 255), use the fromRgba() function to include the alpha-channel specified by
2540 the given QRgb value.
2541
2542 \sa fromRgba(), fromRgbF(), toRgb(), isValid()
2543*/
2544
2545QColor QColor::fromRgb(QRgb rgb) noexcept
2546{
2547 return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
2548}
2549
2550
2551/*!
2552 Static convenience function that returns a QColor constructed from the
2553 given QRgb value \a rgba.
2554
2555 Unlike the fromRgb() function, the alpha-channel specified by the given
2556 QRgb value is included.
2557
2558 \sa fromRgb(), fromRgba64(), isValid()
2559*/
2560
2561QColor QColor::fromRgba(QRgb rgba) noexcept
2562{
2563 return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
2564}
2565
2566/*!
2567 Static convenience function that returns a QColor constructed from the RGB
2568 color values, \a r (red), \a g (green), \a b (blue), and \a a
2569 (alpha-channel, i.e. transparency).
2570
2571 All the values must be in the range 0-255.
2572
2573 \sa toRgb(), fromRgba64(), fromRgbF(), isValid()
2574*/
2575QColor QColor::fromRgb(int r, int g, int b, int a)
2576{
2577 if (!qColorCheckRgbValidity(r, g, b, a, __func__))
2578 return QColor();
2579
2580 QColor color;
2581 color.cspec = Rgb;
2582 color.ct.argb.alpha = a * 0x101;
2583 color.ct.argb.red = r * 0x101;
2584 color.ct.argb.green = g * 0x101;
2585 color.ct.argb.blue = b * 0x101;
2586 color.ct.argb.pad = 0;
2587 return color;
2588}
2589
2590/*!
2591 Static convenience function that returns a QColor constructed from the RGB
2592 color values, \a r (red), \a g (green), \a b (blue), and \a a
2593 (alpha-channel, i.e. transparency).
2594
2595 The alpha value must be in the range 0.0-1.0.
2596 If any of the other values are outside the range of 0.0-1.0 the
2597 color model will be set as \c ExtendedRgb.
2598
2599 \sa fromRgb(), fromRgba64(), toRgb(), isValid()
2600*/
2601QColor QColor::fromRgbF(float r, float g, float b, float a)
2602{
2603 QColor color;
2604
2605 switch (qColorCheckRgbFValidity(r, g, b, a, Invalid, __func__)) {
2606 case QColorRgbFValidity::Invalid:
2607 break;
2608 case QColorRgbFValidity::ExtendedRgb:
2609 color.cspec = ExtendedRgb;
2610 castF16(color.ct.argbExtended.alphaF16) = qfloat16(a);
2611 castF16(color.ct.argbExtended.redF16) = qfloat16(r);
2612 castF16(color.ct.argbExtended.greenF16) = qfloat16(g);
2613 castF16(color.ct.argbExtended.blueF16) = qfloat16(b);
2614 color.ct.argbExtended.pad = 0;
2615 break;
2616 case QColorRgbFValidity::Rgb:
2617 color.cspec = Rgb;
2618 color.ct.argb.alpha = qRound(a * USHRT_MAX);
2619 color.ct.argb.red = qRound(r * USHRT_MAX);
2620 color.ct.argb.green = qRound(g * USHRT_MAX);
2621 color.ct.argb.blue = qRound(b * USHRT_MAX);
2622 color.ct.argb.pad = 0;
2623 break;
2624 }
2625
2626 return color;
2627}
2628
2629
2630/*!
2631 \since 5.6
2632
2633 Static convenience function that returns a QColor constructed from the RGBA64
2634 color values, \a r (red), \a g (green), \a b (blue), and \a a
2635 (alpha-channel, i.e. transparency).
2636
2637 \sa fromRgb(), fromRgbF(), toRgb(), isValid()
2638*/
2639QColor QColor::fromRgba64(ushort r, ushort g, ushort b, ushort a) noexcept
2640{
2641 QColor color;
2642 color.setRgba64(qRgba64(r, g, b, a));
2643 return color;
2644}
2645
2646/*!
2647 \since 5.6
2648
2649 Static convenience function that returns a QColor constructed from the
2650 given QRgba64 value \a rgba64.
2651
2652 \sa fromRgb(), fromRgbF(), toRgb(), isValid()
2653*/
2654QColor QColor::fromRgba64(QRgba64 rgba64) noexcept
2655{
2656 QColor color;
2657 color.setRgba64(rgba64);
2658 return color;
2659}
2660
2661/*!
2662 Static convenience function that returns a QColor constructed from the HSV
2663 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
2664 (alpha-channel, i.e. transparency).
2665
2666 The value of \a s, \a v, and \a a must all be in the range 0-255; the value
2667 of \a h must be in the range 0-359.
2668
2669 \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2670*/
2671QColor QColor::fromHsv(int h, int s, int v, int a)
2672{
2673 if (!qColorCheckHsvValidity(h, s, v, a, __func__, QColorHueLimit::Degrees))
2674 return QColor();
2675
2676 QColor color;
2677 color.cspec = Hsv;
2678 color.ct.ahsv.alpha = a * 0x101;
2679 color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2680 color.ct.ahsv.saturation = s * 0x101;
2681 color.ct.ahsv.value = v * 0x101;
2682 color.ct.ahsv.pad = 0;
2683 return color;
2684}
2685
2686/*!
2687 \overload
2688
2689 Static convenience function that returns a QColor constructed from the HSV
2690 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
2691 (alpha-channel, i.e. transparency).
2692
2693 All the values must be in the range 0.0-1.0.
2694
2695 \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
2696*/
2697QColor QColor::fromHsvF(float h, float s, float v, float a)
2698{
2699 if (!qColorCheckHsvFValidity(h, s, v, a, __func__))
2700 return QColor();
2701
2702 QColor color;
2703 color.cspec = Hsv;
2704 color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
2705 color.ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
2706 color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
2707 color.ct.ahsv.value = qRound(v * USHRT_MAX);
2708 color.ct.ahsv.pad = 0;
2709 return color;
2710}
2711
2712/*!
2713 \since 4.6
2714
2715 Static convenience function that returns a QColor constructed from the HSV
2716 color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2717 (alpha-channel, i.e. transparency).
2718
2719 The value of \a s, \a l, and \a a must all be in the range 0-255; the value
2720 of \a h must be in the range 0-359.
2721
2722 \sa toHsl(), fromHslF(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2723*/
2724QColor QColor::fromHsl(int h, int s, int l, int a)
2725{
2726 if (!qColorCheckHslValidity(h, s, l, a, __func__, QColorHueLimit::Degrees))
2727 return QColor();
2728
2729 QColor color;
2730 color.cspec = Hsl;
2731 color.ct.ahsl.alpha = a * 0x101;
2732 color.ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2733 color.ct.ahsl.saturation = s * 0x101;
2734 color.ct.ahsl.lightness = l * 0x101;
2735 color.ct.ahsl.pad = 0;
2736 return color;
2737}
2738
2739/*!
2740 \overload
2741 \since 4.6
2742
2743 Static convenience function that returns a QColor constructed from the HSV
2744 color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2745 (alpha-channel, i.e. transparency).
2746
2747 All the values must be in the range 0.0-1.0.
2748
2749 \sa toHsl(), fromHsl(), isValid(), {QColor#The HSL Color Model}{The HSL Color Model}
2750*/
2751QColor QColor::fromHslF(float h, float s, float l, float a)
2752{
2753 if (!qColorCheckHslFValidity(h, s, l, a, __func__))
2754 return QColor();
2755
2756 QColor color;
2757 color.cspec = Hsl;
2758 color.ct.ahsl.alpha = qRound(a * USHRT_MAX);
2759 color.ct.ahsl.hue = (h == -1.0f) ? USHRT_MAX : qRound(h * 36000.0f);
2760 if (color.ct.ahsl.hue == 36000)
2761 color.ct.ahsl.hue = 0;
2762 color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2763 color.ct.ahsl.lightness = qRound(l * USHRT_MAX);
2764 color.ct.ahsl.pad = 0;
2765 return color;
2766}
2767
2768/*!
2769 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2770 cyan, magenta, yellow, black, and alpha-channel (transparency) components
2771 of the color's CMYK value.
2772
2773 These components can be retrieved individually using the cyan(), magenta(),
2774 yellow(), black() and alpha() functions.
2775
2776 \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2777*/
2778void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a) const
2779{
2780 if (!c || !m || !y || !k)
2781 return;
2782
2783 if (cspec != Invalid && cspec != Cmyk) {
2784 toCmyk().getCmyk(c, m, y, k, a);
2785 return;
2786 }
2787
2788 *c = qt_div_257(ct.acmyk.cyan);
2789 *m = qt_div_257(ct.acmyk.magenta);
2790 *y = qt_div_257(ct.acmyk.yellow);
2791 *k = qt_div_257(ct.acmyk.black);
2792
2793 if (a)
2794 *a = qt_div_257(ct.acmyk.alpha);
2795}
2796
2797/*!
2798 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2799 cyan, magenta, yellow, black, and alpha-channel (transparency) components
2800 of the color's CMYK value.
2801
2802 These components can be retrieved individually using the cyanF(),
2803 magentaF(), yellowF(), blackF() and alphaF() functions.
2804
2805 \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2806*/
2807void QColor::getCmykF(float *c, float *m, float *y, float *k, float *a) const
2808{
2809 if (!c || !m || !y || !k)
2810 return;
2811
2812 if (cspec != Invalid && cspec != Cmyk) {
2813 toCmyk().getCmykF(c, m, y, k, a);
2814 return;
2815 }
2816
2817 *c = ct.acmyk.cyan / float(USHRT_MAX);
2818 *m = ct.acmyk.magenta / float(USHRT_MAX);
2819 *y = ct.acmyk.yellow / float(USHRT_MAX);
2820 *k = ct.acmyk.black / float(USHRT_MAX);
2821
2822 if (a)
2823 *a = ct.acmyk.alpha / float(USHRT_MAX);
2824}
2825
2826/*!
2827 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2828 \a k (black), and \a a (alpha-channel, i.e. transparency).
2829
2830 All the values must be in the range 0-255.
2831
2832 \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2833*/
2834void QColor::setCmyk(int c, int m, int y, int k, int a)
2835{
2836 if (!qColorCheckCmykValidity(c, m, y, k, a, __func__)) {
2837 invalidate();
2838 return;
2839 }
2840
2841 cspec = Cmyk;
2842 ct.acmyk.alpha = a * 0x101;
2843 ct.acmyk.cyan = c * 0x101;
2844 ct.acmyk.magenta = m * 0x101;
2845 ct.acmyk.yellow = y * 0x101;
2846 ct.acmyk.black = k * 0x101;
2847}
2848
2849/*!
2850 \overload
2851
2852 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2853 \a k (black), and \a a (alpha-channel, i.e. transparency).
2854
2855 All the values must be in the range 0.0-1.0.
2856
2857 \sa getCmykF(), setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2858*/
2859void QColor::setCmykF(float c, float m, float y, float k, float a)
2860{
2861 if (!qColorCheckCmykFValidity(c, m, y, k, a, __func__)) {
2862 invalidate();
2863 return;
2864 }
2865
2866 cspec = Cmyk;
2867 ct.acmyk.alpha = qRound(a * USHRT_MAX);
2868 ct.acmyk.cyan = qRound(c * USHRT_MAX);
2869 ct.acmyk.magenta = qRound(m * USHRT_MAX);
2870 ct.acmyk.yellow = qRound(y * USHRT_MAX);
2871 ct.acmyk.black = qRound(k * USHRT_MAX);
2872}
2873
2874/*!
2875 Static convenience function that returns a QColor constructed from the
2876 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2877 (black), and \a a (alpha-channel, i.e. transparency).
2878
2879 All the values must be in the range 0-255.
2880
2881 \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2882*/
2883QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2884{
2885 if (!qColorCheckCmykValidity(c, m, y, k, a, __func__))
2886 return QColor();
2887
2888 QColor color;
2889 color.cspec = Cmyk;
2890 color.ct.acmyk.alpha = a * 0x101;
2891 color.ct.acmyk.cyan = c * 0x101;
2892 color.ct.acmyk.magenta = m * 0x101;
2893 color.ct.acmyk.yellow = y * 0x101;
2894 color.ct.acmyk.black = k * 0x101;
2895 return color;
2896}
2897
2898/*!
2899 \overload
2900
2901 Static convenience function that returns a QColor constructed from the
2902 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2903 (black), and \a a (alpha-channel, i.e. transparency).
2904
2905 All the values must be in the range 0.0-1.0.
2906
2907 \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2908*/
2909QColor QColor::fromCmykF(float c, float m, float y, float k, float a)
2910{
2911 if (!qColorCheckCmykFValidity(c, m, y, k, a, __func__))
2912 return QColor();
2913
2914 QColor color;
2915 color.cspec = Cmyk;
2916 color.ct.acmyk.alpha = qRound(a * USHRT_MAX);
2917 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2918 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2919 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2920 color.ct.acmyk.black = qRound(k * USHRT_MAX);
2921 return color;
2922}
2923
2924/*!
2925 \fn QColor QColor::lighter(int factor) const
2926 \since 4.3
2927
2928 Returns a lighter (or darker) color, but does not change this object.
2929
2930 If the \a factor is greater than 100, this functions returns a lighter
2931 color. Setting \a factor to 150 returns a color that is 50% brighter. If
2932 the \a factor is less than 100, the return color is darker, but we
2933 recommend using the darker() function for this purpose. If the \a factor
2934 is 0 or negative, the return value is unspecified.
2935
2936 The function converts the current color to HSV, multiplies the value
2937 (V) component by \a factor and converts the color back to it's original
2938 color spec.
2939
2940 \sa darker(), isValid()
2941*/
2942QColor QColor::lighter(int factor) const noexcept
2943{
2944 if (factor <= 0) // invalid lightness factor
2945 return *this;
2946 else if (factor < 100) // makes color darker
2947 return darker(10000 / factor);
2948
2949 QColor hsv = toHsv();
2950 int s = hsv.ct.ahsv.saturation;
2951 uint v = hsv.ct.ahsv.value;
2952
2953 v = (factor*v)/100;
2954 if (v > USHRT_MAX) {
2955 // overflow... adjust saturation
2956 s -= v - USHRT_MAX;
2957 if (s < 0)
2958 s = 0;
2959 v = USHRT_MAX;
2960 }
2961
2962 hsv.ct.ahsv.saturation = s;
2963 hsv.ct.ahsv.value = v;
2964
2965 // convert back to same color spec as original color
2966 return hsv.convertTo(cspec);
2967}
2968
2969/*!
2970 \fn QColor QColor::darker(int factor) const
2971 \since 4.3
2972
2973 Returns a darker (or lighter) color, but does not change this object.
2974
2975 If the \a factor is greater than 100, this functions returns a darker
2976 color. Setting \a factor to 300 returns a color that has one-third the
2977 brightness. If the \a factor is less than 100, the return color is lighter,
2978 but we recommend using the lighter() function for this purpose. If the
2979 \a factor is 0 or negative, the return value is unspecified.
2980
2981 The function converts the current color to HSV, divides the value (V)
2982 component by \a factor and converts the color back to it's original
2983 color spec.
2984
2985 \sa lighter(), isValid()
2986*/
2987QColor QColor::darker(int factor) const noexcept
2988{
2989 if (factor <= 0) // invalid darkness factor
2990 return *this;
2991 else if (factor < 100) // makes color lighter
2992 return lighter(10000 / factor);
2993
2994 QColor hsv = toHsv();
2995 hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2996
2997 // convert back to same color spec as original color
2998 return hsv.convertTo(cspec);
2999}
3000
3001/*! \overload
3002 Assigns a copy of \a color and returns a reference to this color.
3003 */
3004QColor &QColor::operator=(Qt::GlobalColor color) noexcept
3005{
3006 return operator=(QColor(color));
3007}
3008
3009/*!
3010 Returns \c true if this color has the same color specification and component values as \a color;
3011 otherwise returns \c false.
3012
3013 ExtendedRgb and Rgb specifications are considered matching in this context.
3014
3015 \sa spec()
3016*/
3017bool QColor::operator==(const QColor &color) const noexcept
3018{
3019 if ((cspec == ExtendedRgb || color.cspec == ExtendedRgb) &&
3020 (cspec == color.cspec || cspec == Rgb || color.cspec == Rgb)) {
3021 return qFuzzyCompare(alphaF(), color.alphaF())
3022 && qFuzzyCompare(redF(), color.redF())
3023 && qFuzzyCompare(greenF(), color.greenF())
3024 && qFuzzyCompare(blueF(), color.blueF());
3025 } else {
3026 return (cspec == color.cspec
3027 && ct.argb.alpha == color.ct.argb.alpha
3028 && (((cspec == QColor::Hsv || cspec == QColor::Hsl)
3029 && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
3030 || (ct.argb.red == color.ct.argb.red))
3031 && ct.argb.green == color.ct.argb.green
3032 && ct.argb.blue == color.ct.argb.blue
3033 && ct.argb.pad == color.ct.argb.pad);
3034 }
3035}
3036
3037/*!
3038 Returns \c true if this color has different color specification or component values from
3039 \a color; otherwise returns \c false.
3040
3041 ExtendedRgb and Rgb specifications are considered matching in this context.
3042
3043 \sa spec()
3044*/
3045bool QColor::operator!=(const QColor &color) const noexcept
3046{ return !operator==(color); }
3047
3048
3049/*!
3050 Returns the color as a QVariant
3051*/
3052QColor::operator QVariant() const
3053{
3054 return QVariant::fromValue(*this);
3055}
3056
3057/*! \internal
3058
3059 Marks the color as invalid and sets all components to zero (alpha is set
3060 to fully opaque for compatibility with Qt 3).
3061*/
3062void QColor::invalidate() noexcept
3063{
3064 cspec = Invalid;
3065 ct.argb.alpha = USHRT_MAX;
3066 ct.argb.red = 0;
3067 ct.argb.green = 0;
3068 ct.argb.blue = 0;
3069 ct.argb.pad = 0;
3070}
3071
3072/*****************************************************************************
3073 QColor stream functions
3074 *****************************************************************************/
3075
3076#ifndef QT_NO_DEBUG_STREAM
3077QDebug operator<<(QDebug dbg, const QColor &c)
3078{
3079 QDebugStateSaver saver(dbg);
3080 if (!c.isValid())
3081 dbg.nospace() << "QColor(Invalid)";
3082 else if (c.spec() == QColor::Rgb)
3083 dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
3084 else if (c.spec() == QColor::ExtendedRgb)
3085 dbg.nospace() << "QColor(Ext. ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
3086 else if (c.spec() == QColor::Hsv)
3087 dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
3088 else if (c.spec() == QColor::Cmyk)
3089 dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
3090 << c.blackF()<< ')';
3091 else if (c.spec() == QColor::Hsl)
3092 dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
3093
3094 return dbg;
3095}
3096#endif
3097
3098#ifndef QT_NO_DATASTREAM
3099/*!
3100 \fn QDataStream &operator<<(QDataStream &stream, const QColor &color)
3101 \relates QColor
3102
3103 Writes the \a color to the \a stream.
3104
3105 \sa {Serializing Qt Data Types}
3106*/
3107QDataStream &operator<<(QDataStream &stream, const QColor &color)
3108{
3109 if (stream.version() < 7) {
3110 if (!color.isValid())
3111 return stream << quint32(0x49000000);
3112 quint32 p = (quint32)color.rgb();
3113 if (stream.version() == 1) // Swap red and blue
3114 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
3115 return stream << p;
3116 }
3117
3118 qint8 s = color.cspec;
3119 quint16 a = color.ct.argb.alpha;
3120 quint16 r = color.ct.argb.red;
3121 quint16 g = color.ct.argb.green;
3122 quint16 b = color.ct.argb.blue;
3123 quint16 p = color.ct.argb.pad;
3124
3125 stream << s;
3126 stream << a;
3127 stream << r;
3128 stream << g;
3129 stream << b;
3130 stream << p;
3131
3132 return stream;
3133}
3134
3135/*!
3136 \fn QDataStream &operator>>(QDataStream &stream, QColor &color)
3137 \relates QColor
3138
3139 Reads the \a color from the \a stream.
3140
3141 \sa {Serializing Qt Data Types}
3142*/
3143QDataStream &operator>>(QDataStream &stream, QColor &color)
3144{
3145 if (stream.version() < 7) {
3146 quint32 p;
3147 stream >> p;
3148 if (p == 0x49000000) {
3149 color.invalidate();
3150 return stream;
3151 }
3152 if (stream.version() == 1) // Swap red and blue
3153 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
3154 color.setRgb(p);
3155 return stream;
3156 }
3157
3158 qint8 s;
3159 quint16 a, r, g, b, p;
3160 stream >> s;
3161 stream >> a;
3162 stream >> r;
3163 stream >> g;
3164 stream >> b;
3165 stream >> p;
3166
3167 color.cspec = QColor::Spec(s);
3168 color.ct.argb.alpha = a;
3169 color.ct.argb.red = r;
3170 color.ct.argb.green = g;
3171 color.ct.argb.blue = b;
3172 color.ct.argb.pad = p;
3173
3174 return stream;
3175}
3176#endif // QT_NO_DATASTREAM
3177
3178// A table of precalculated results of 0x00ff00ff/alpha use by qUnpremultiply:
3179const uint qt_inv_premul_factor[256] = {
3180 0, 16711935, 8355967, 5570645, 4177983, 3342387, 2785322, 2387419,
3181 2088991, 1856881, 1671193, 1519266, 1392661, 1285533, 1193709, 1114129,
3182 1044495, 983055, 928440, 879575, 835596, 795806, 759633, 726605,
3183 696330, 668477, 642766, 618960, 596854, 576273, 557064, 539094,
3184 522247, 506422, 491527, 477483, 464220, 451673, 439787, 428511,
3185 417798, 407608, 397903, 388649, 379816, 371376, 363302, 355573,
3186 348165, 341059, 334238, 327685, 321383, 315319, 309480, 303853,
3187 298427, 293191, 288136, 283253, 278532, 273966, 269547, 265268,
3188 261123, 257106, 253211, 249431, 245763, 242201, 238741, 235379,
3189 232110, 228930, 225836, 222825, 219893, 217038, 214255, 211543,
3190 208899, 206320, 203804, 201348, 198951, 196611, 194324, 192091,
3191 189908, 187774, 185688, 183647, 181651, 179698, 177786, 175915,
3192 174082, 172287, 170529, 168807, 167119, 165464, 163842, 162251,
3193 160691, 159161, 157659, 156186, 154740, 153320, 151926, 150557,
3194 149213, 147893, 146595, 145321, 144068, 142837, 141626, 140436,
3195 139266, 138115, 136983, 135869, 134773, 133695, 132634, 131590,
3196 130561, 129549, 128553, 127572, 126605, 125653, 124715, 123792,
3197 122881, 121984, 121100, 120229, 119370, 118524, 117689, 116866,
3198 116055, 115254, 114465, 113686, 112918, 112160, 111412, 110675,
3199 109946, 109228, 108519, 107818, 107127, 106445, 105771, 105106,
3200 104449, 103800, 103160, 102527, 101902, 101284, 100674, 100071,
3201 99475, 98887, 98305, 97730, 97162, 96600, 96045, 95496,
3202 94954, 94417, 93887, 93362, 92844, 92331, 91823, 91322,
3203 90825, 90334, 89849, 89368, 88893, 88422, 87957, 87497,
3204 87041, 86590, 86143, 85702, 85264, 84832, 84403, 83979,
3205 83559, 83143, 82732, 82324, 81921, 81521, 81125, 80733,
3206 80345, 79961, 79580, 79203, 78829, 78459, 78093, 77729,
3207 77370, 77013, 76660, 76310, 75963, 75619, 75278, 74941,
3208 74606, 74275, 73946, 73620, 73297, 72977, 72660, 72346,
3209 72034, 71725, 71418, 71114, 70813, 70514, 70218, 69924,
3210 69633, 69344, 69057, 68773, 68491, 68211, 67934, 67659,
3211 67386, 67116, 66847, 66581, 66317, 66055, 65795, 65537
3212};
3213
3214/*****************************************************************************
3215 QColor global functions (documentation only)
3216 *****************************************************************************/
3217
3218/*!
3219 \fn int qRed(QRgb rgb)
3220 \relates QColor
3221
3222 Returns the red component of the ARGB quadruplet \a rgb.
3223
3224 \sa qRgb(), QColor::red()
3225*/
3226
3227/*!
3228 \fn int qGreen(QRgb rgb)
3229 \relates QColor
3230
3231 Returns the green component of the ARGB quadruplet \a rgb.
3232
3233 \sa qRgb(), QColor::green()
3234*/
3235
3236/*!
3237 \fn int qBlue(QRgb rgb)
3238 \relates QColor
3239
3240 Returns the blue component of the ARGB quadruplet \a rgb.
3241
3242 \sa qRgb(), QColor::blue()
3243*/
3244
3245/*!
3246 \fn int qAlpha(QRgb rgba)
3247 \relates QColor
3248
3249 Returns the alpha component of the ARGB quadruplet \a rgba.
3250
3251 \sa qRgb(), QColor::alpha()
3252*/
3253
3254/*!
3255 \fn QRgb qRgb(int r, int g, int b)
3256 \relates QColor
3257
3258 Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}).
3259
3260 \sa qRgba(), qRed(), qGreen(), qBlue(), qAlpha()
3261*/
3262
3263/*!
3264 \fn QRgb qRgba(int r, int g, int b, int a)
3265 \relates QColor
3266
3267 Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}).
3268
3269 \sa qRgb(), qRed(), qGreen(), qBlue(), qAlpha()
3270*/
3271
3272/*!
3273 \fn int qGray(int r, int g, int b)
3274 \relates QColor
3275
3276 Returns a gray value (0 to 255) from the (\a r, \a g, \a b)
3277 triplet.
3278
3279 The gray value is calculated using the formula (\a r * 11 + \a g * 16 +
3280 \a b * 5)/32.
3281*/
3282
3283/*!
3284 \fn int qGray(QRgb rgb)
3285 \overload
3286 \relates QColor
3287
3288 Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb.
3289
3290 The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32;
3291 the alpha-channel is ignored.
3292*/
3293
3294/*!
3295 \fn QRgb qPremultiply(QRgb rgb)
3296 \since 5.3
3297 \relates QColor
3298
3299 Converts an unpremultiplied ARGB quadruplet \a rgb into a premultiplied ARGB quadruplet.
3300
3301 \sa qUnpremultiply()
3302*/
3303
3304/*!
3305 \fn QRgb qUnpremultiply(QRgb rgb)
3306 \since 5.3
3307 \relates QColor
3308
3309 Converts a premultiplied ARGB quadruplet \a rgb into an unpremultiplied ARGB quadruplet.
3310
3311 \sa qPremultiply()
3312*/
3313
3314/*!
3315 \fn QColor QColor::convertTo(Spec colorSpec) const
3316
3317 Creates a copy of \e this color in the format specified by \a colorSpec.
3318
3319 \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()
3320*/
3321
3322/*!
3323 \typedef QRgb
3324 \relates QColor
3325
3326 An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
3327
3328 The type also holds a value for the alpha-channel. The default alpha
3329 channel is \c ff, i.e opaque. For more information, see the
3330 \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section.
3331
3332 Here are some examples of how QRgb values can be created:
3333
3334 \snippet code/src_gui_painting_qcolor.cpp QRgb
3335
3336 \sa qRgb(), qRgba(), QColor::rgb(), QColor::rgba()
3337*/
3338
3339/*!
3340 \namespace QColorConstants
3341 \inmodule QtGui
3342 \since 5.14
3343
3344 \brief The QColorConstants namespace contains QColor predefined constants.
3345
3346 These constants are usable everywhere a QColor object is expected:
3347
3348 \code
3349 painter.setBrush(QColorConstants::Svg::lightblue);
3350 \endcode
3351
3352 Their usage is much cheaper than e.g. passing a string to QColor's constructor,
3353 as they don't require any parsing of the string, and always result in a valid
3354 QColor object:
3355
3356 \badcode
3357 object.setColor(QColor("lightblue")); // expensive
3358 \endcode
3359
3360 \section1 Qt Colors
3361
3362 The following colors are defined in the \c{QColorConstants} namespace:
3363
3364 \include qt-colors.qdocinc
3365
3366 \section1 SVG Colors
3367
3368 The following table lists the available
3369 \l {http://www.w3.org/TR/SVG/types.html#ColorKeywords}{SVG colors}.
3370 They are available in the \c{QColorConstants::Svg} inner namespace.
3371
3372 \include svg-colors.qdocinc
3373
3374 \sa QColor, Qt::GlobalColor
3375*/
3376
3377QT_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:3143
const uint qt_inv_premul_factor[256]
Definition qcolor.cpp:3179
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:1400
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:1394
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
QDebug operator<<(QDebug dbg, const QColor &c)
Definition qcolor.cpp:3077
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
const char name[21]
Definition qcolor.cpp:131
uint value
Definition qcolor.cpp:132