Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4#include "qcolor.h"
5#include "qcolor_p.h"
6#include "qdrawhelper_p.h"
7#include "qfloat16.h"
8#include "qnamespace.h"
9#include "qdatastream.h"
10#include "qvariant.h"
11#include "qdebug.h"
12#include "private/qtools_p.h"
13
14#include <algorithm>
15#include <optional>
16
17#include <stdio.h>
18#include <limits.h>
19
21
22// QColor fits into QVariant's internal storage on 64bit systems.
23// It could also fit on 32bit systems, but we cannot make it happen in Qt6, due to BC.
24#if QT_VERSION >= QT_VERSION_CHECK(7,0,0) || QT_POINTER_SIZE > 4
26#endif
27
33static inline int hex2int(const char *s, int n)
34{
35 if (n < 0)
36 return -1;
37 int result = 0;
38 for (; n > 0; --n) {
39 result = result * 16;
40 const int h = QtMiscUtils::fromHex(*s++);
41 if (h < 0)
42 return -1;
43 result += h;
44 }
45 return result;
46}
47
48static std::optional<QRgba64> get_hex_rgb(const char *name, size_t len)
49{
50 if (name[0] != '#')
51 return std::nullopt;
52 name++;
53 --len;
54 int a, r, g, b;
55 a = 65535;
56 if (len == 12) {
57 r = hex2int(name + 0, 4);
58 g = hex2int(name + 4, 4);
59 b = hex2int(name + 8, 4);
60 } else if (len == 9) {
61 r = hex2int(name + 0, 3);
62 g = hex2int(name + 3, 3);
63 b = hex2int(name + 6, 3);
64 if (r == -1 || g == -1 || b == -1)
65 return std::nullopt;
66 r = (r << 4) | (r >> 8);
67 g = (g << 4) | (g >> 8);
68 b = (b << 4) | (b >> 8);
69 } else if (len == 8) {
70 a = hex2int(name + 0, 2) * 0x101;
71 r = hex2int(name + 2, 2) * 0x101;
72 g = hex2int(name + 4, 2) * 0x101;
73 b = hex2int(name + 6, 2) * 0x101;
74 } else if (len == 6) {
75 r = hex2int(name + 0, 2) * 0x101;
76 g = hex2int(name + 2, 2) * 0x101;
77 b = hex2int(name + 4, 2) * 0x101;
78 } else if (len == 3) {
79 r = hex2int(name + 0, 1) * 0x1111;
80 g = hex2int(name + 1, 1) * 0x1111;
81 b = hex2int(name + 2, 1) * 0x1111;
82 } else {
83 r = g = b = -1;
84 }
85 if (uint(r) > 65535 || uint(g) > 65535 || uint(b) > 65535 || uint(a) > 65535)
86 return std::nullopt;
87 return qRgba64(r, g ,b, a);
88}
89
90std::optional<QRgb> qt_get_hex_rgb(const char *name)
91{
92 if (std::optional<QRgba64> rgba64 = get_hex_rgb(name, qstrlen(name)))
93 return rgba64->toArgb32();
94 return std::nullopt;
95}
96
97static std::optional<QRgba64> get_hex_rgb(const QChar *str, size_t len)
98{
99 if (len > 13)
100 return std::nullopt;
101 char tmp[16];
102 for (size_t i = 0; i < len; ++i)
103 tmp[i] = str[i].toLatin1();
104 tmp[len] = 0;
105 return get_hex_rgb(tmp, len);
106}
107
108static std::optional<QRgba64> get_hex_rgb(QAnyStringView name)
109{
110 return name.visit([] (auto name) {
111 return get_hex_rgb(name.data(), name.size());
112 });
113}
114
115#ifndef QT_NO_COLORNAMES
116
117/*
118 CSS color names = SVG 1.0 color names + transparent (rgba(0,0,0,0))
119*/
120
121#ifdef rgb
122# undef rgb
123#endif
124#define rgb(r,g,b) (0xff000000 | (r << 16) | (g << 8) | b)
125
126// keep this is in sync with QColorConstants
127static constexpr struct RGBData {
128 const char name[21];
130} rgbTbl[] = {
131 { "aliceblue", rgb(240, 248, 255) },
132 { "antiquewhite", rgb(250, 235, 215) },
133 { "aqua", rgb( 0, 255, 255) },
134 { "aquamarine", rgb(127, 255, 212) },
135 { "azure", rgb(240, 255, 255) },
136 { "beige", rgb(245, 245, 220) },
137 { "bisque", rgb(255, 228, 196) },
138 { "black", rgb( 0, 0, 0) },
139 { "blanchedalmond", rgb(255, 235, 205) },
140 { "blue", rgb( 0, 0, 255) },
141 { "blueviolet", rgb(138, 43, 226) },
142 { "brown", rgb(165, 42, 42) },
143 { "burlywood", rgb(222, 184, 135) },
144 { "cadetblue", rgb( 95, 158, 160) },
145 { "chartreuse", rgb(127, 255, 0) },
146 { "chocolate", rgb(210, 105, 30) },
147 { "coral", rgb(255, 127, 80) },
148 { "cornflowerblue", rgb(100, 149, 237) },
149 { "cornsilk", rgb(255, 248, 220) },
150 { "crimson", rgb(220, 20, 60) },
151 { "cyan", rgb( 0, 255, 255) },
152 { "darkblue", rgb( 0, 0, 139) },
153 { "darkcyan", rgb( 0, 139, 139) },
154 { "darkgoldenrod", rgb(184, 134, 11) },
155 { "darkgray", rgb(169, 169, 169) },
156 { "darkgreen", rgb( 0, 100, 0) },
157 { "darkgrey", rgb(169, 169, 169) },
158 { "darkkhaki", rgb(189, 183, 107) },
159 { "darkmagenta", rgb(139, 0, 139) },
160 { "darkolivegreen", rgb( 85, 107, 47) },
161 { "darkorange", rgb(255, 140, 0) },
162 { "darkorchid", rgb(153, 50, 204) },
163 { "darkred", rgb(139, 0, 0) },
164 { "darksalmon", rgb(233, 150, 122) },
165 { "darkseagreen", rgb(143, 188, 143) },
166 { "darkslateblue", rgb( 72, 61, 139) },
167 { "darkslategray", rgb( 47, 79, 79) },
168 { "darkslategrey", rgb( 47, 79, 79) },
169 { "darkturquoise", rgb( 0, 206, 209) },
170 { "darkviolet", rgb(148, 0, 211) },
171 { "deeppink", rgb(255, 20, 147) },
172 { "deepskyblue", rgb( 0, 191, 255) },
173 { "dimgray", rgb(105, 105, 105) },
174 { "dimgrey", rgb(105, 105, 105) },
175 { "dodgerblue", rgb( 30, 144, 255) },
176 { "firebrick", rgb(178, 34, 34) },
177 { "floralwhite", rgb(255, 250, 240) },
178 { "forestgreen", rgb( 34, 139, 34) },
179 { "fuchsia", rgb(255, 0, 255) },
180 { "gainsboro", rgb(220, 220, 220) },
181 { "ghostwhite", rgb(248, 248, 255) },
182 { "gold", rgb(255, 215, 0) },
183 { "goldenrod", rgb(218, 165, 32) },
184 { "gray", rgb(128, 128, 128) },
185 { "green", rgb( 0, 128, 0) },
186 { "greenyellow", rgb(173, 255, 47) },
187 { "grey", rgb(128, 128, 128) },
188 { "honeydew", rgb(240, 255, 240) },
189 { "hotpink", rgb(255, 105, 180) },
190 { "indianred", rgb(205, 92, 92) },
191 { "indigo", rgb( 75, 0, 130) },
192 { "ivory", rgb(255, 255, 240) },
193 { "khaki", rgb(240, 230, 140) },
194 { "lavender", rgb(230, 230, 250) },
195 { "lavenderblush", rgb(255, 240, 245) },
196 { "lawngreen", rgb(124, 252, 0) },
197 { "lemonchiffon", rgb(255, 250, 205) },
198 { "lightblue", rgb(173, 216, 230) },
199 { "lightcoral", rgb(240, 128, 128) },
200 { "lightcyan", rgb(224, 255, 255) },
201 { "lightgoldenrodyellow", rgb(250, 250, 210) },
202 { "lightgray", rgb(211, 211, 211) },
203 { "lightgreen", rgb(144, 238, 144) },
204 { "lightgrey", rgb(211, 211, 211) },
205 { "lightpink", rgb(255, 182, 193) },
206 { "lightsalmon", rgb(255, 160, 122) },
207 { "lightseagreen", rgb( 32, 178, 170) },
208 { "lightskyblue", rgb(135, 206, 250) },
209 { "lightslategray", rgb(119, 136, 153) },
210 { "lightslategrey", rgb(119, 136, 153) },
211 { "lightsteelblue", rgb(176, 196, 222) },
212 { "lightyellow", rgb(255, 255, 224) },
213 { "lime", rgb( 0, 255, 0) },
214 { "limegreen", rgb( 50, 205, 50) },
215 { "linen", rgb(250, 240, 230) },
216 { "magenta", rgb(255, 0, 255) },
217 { "maroon", rgb(128, 0, 0) },
218 { "mediumaquamarine", rgb(102, 205, 170) },
219 { "mediumblue", rgb( 0, 0, 205) },
220 { "mediumorchid", rgb(186, 85, 211) },
221 { "mediumpurple", rgb(147, 112, 219) },
222 { "mediumseagreen", rgb( 60, 179, 113) },
223 { "mediumslateblue", rgb(123, 104, 238) },
224 { "mediumspringgreen", rgb( 0, 250, 154) },
225 { "mediumturquoise", rgb( 72, 209, 204) },
226 { "mediumvioletred", rgb(199, 21, 133) },
227 { "midnightblue", rgb( 25, 25, 112) },
228 { "mintcream", rgb(245, 255, 250) },
229 { "mistyrose", rgb(255, 228, 225) },
230 { "moccasin", rgb(255, 228, 181) },
231 { "navajowhite", rgb(255, 222, 173) },
232 { "navy", rgb( 0, 0, 128) },
233 { "oldlace", rgb(253, 245, 230) },
234 { "olive", rgb(128, 128, 0) },
235 { "olivedrab", rgb(107, 142, 35) },
236 { "orange", rgb(255, 165, 0) },
237 { "orangered", rgb(255, 69, 0) },
238 { "orchid", rgb(218, 112, 214) },
239 { "palegoldenrod", rgb(238, 232, 170) },
240 { "palegreen", rgb(152, 251, 152) },
241 { "paleturquoise", rgb(175, 238, 238) },
242 { "palevioletred", rgb(219, 112, 147) },
243 { "papayawhip", rgb(255, 239, 213) },
244 { "peachpuff", rgb(255, 218, 185) },
245 { "peru", rgb(205, 133, 63) },
246 { "pink", rgb(255, 192, 203) },
247 { "plum", rgb(221, 160, 221) },
248 { "powderblue", rgb(176, 224, 230) },
249 { "purple", rgb(128, 0, 128) },
250 { "red", rgb(255, 0, 0) },
251 { "rosybrown", rgb(188, 143, 143) },
252 { "royalblue", rgb( 65, 105, 225) },
253 { "saddlebrown", rgb(139, 69, 19) },
254 { "salmon", rgb(250, 128, 114) },
255 { "sandybrown", rgb(244, 164, 96) },
256 { "seagreen", rgb( 46, 139, 87) },
257 { "seashell", rgb(255, 245, 238) },
258 { "sienna", rgb(160, 82, 45) },
259 { "silver", rgb(192, 192, 192) },
260 { "skyblue", rgb(135, 206, 235) },
261 { "slateblue", rgb(106, 90, 205) },
262 { "slategray", rgb(112, 128, 144) },
263 { "slategrey", rgb(112, 128, 144) },
264 { "snow", rgb(255, 250, 250) },
265 { "springgreen", rgb( 0, 255, 127) },
266 { "steelblue", rgb( 70, 130, 180) },
267 { "tan", rgb(210, 180, 140) },
268 { "teal", rgb( 0, 128, 128) },
269 { "thistle", rgb(216, 191, 216) },
270 { "tomato", rgb(255, 99, 71) },
271 { "transparent", 0 },
272 { "turquoise", rgb( 64, 224, 208) },
273 { "violet", rgb(238, 130, 238) },
274 { "wheat", rgb(245, 222, 179) },
275 { "white", rgb(255, 255, 255) },
276 { "whitesmoke", rgb(245, 245, 245) },
277 { "yellow", rgb(255, 255, 0) },
278 { "yellowgreen", rgb(154, 205, 50) }
280
281static const int rgbTblSize = sizeof(rgbTbl) / sizeof(RGBData);
282
283static_assert([] {
284 for (auto e : rgbTbl) {
285 for (auto it = e.name; *it ; ++it) {
286 if (uchar(*it) > 127)
287 return false;
288 }
289 }
290 return true;
291 }(), "the lookup code expects color names to be US-ASCII-only");
292
293#undef rgb
294
295inline bool operator<(const char *name, const RGBData &data)
296{ return qstrcmp(name, data.name) < 0; }
297inline bool operator<(const RGBData &data, const char *name)
298{ return qstrcmp(data.name, name) < 0; }
299
300static std::optional<QRgb> get_named_rgb_no_space(const char *name_no_space)
301{
302 const RGBData *r = std::lower_bound(rgbTbl, rgbTbl + rgbTblSize, name_no_space);
303 if ((r != rgbTbl + rgbTblSize) && !(name_no_space < *r))
304 return r->value;
305 return std::nullopt;
306}
307
308namespace {
309// named colors are US-ASCII (enforced by static_assert above):
310static char to_char(char ch) noexcept { return ch; }
311static char to_char(QChar ch) noexcept { return ch.toLatin1(); }
312}
313
314static std::optional<QRgb> get_named_rgb(QAnyStringView name)
315{
316 if (name.size() > 255)
317 return std::nullopt;
318 char name_no_space[256];
319 int pos = 0;
320 name.visit([&pos, &name_no_space] (auto name) {
321 for (auto c : name) {
322 if (c != u'\t' && c != u' ')
323 name_no_space[pos++] = QtMiscUtils::toAsciiLower(to_char(c));
324 }
325 });
326 name_no_space[pos] = 0;
327
328 return get_named_rgb_no_space(name_no_space);
329}
330
331#endif // QT_NO_COLORNAMES
332
334{
335 QStringList lst;
336#ifndef QT_NO_COLORNAMES
337 lst.reserve(rgbTblSize);
338 for (int i = 0; i < rgbTblSize; i++)
340#endif
341 return lst;
342}
343
581#define QCOLOR_INT_RANGE_CHECK(fn, var) \
582 do { \
583 if (var < 0 || var > 255) { \
584 qWarning(#fn": invalid value %d", var); \
585 var = qMax(0, qMin(var, 255)); \
586 } \
587 } while (0)
588
589#define QCOLOR_REAL_RANGE_CHECK(fn, var) \
590 do { \
591 if (var < 0.0f || var > 1.0f) { \
592 qWarning(#fn": invalid value %g", var); \
593 var = qMax(0.0f, qMin(var, 1.0f)); \
594 } \
595 } while (0)
596
597/*****************************************************************************
598 QColor member functions
599 *****************************************************************************/
600
656{
657#define QRGB(r, g, b) \
658 QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
659#define QRGBA(r, g, b, a) \
660 QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
661
662 static const QRgb global_colors[] = {
663 QRGB(255, 255, 255), // Qt::color0
664 QRGB( 0, 0, 0), // Qt::color1
665 QRGB( 0, 0, 0), // black
666 QRGB(255, 255, 255), // white
667 /*
668 * From the "The Palette Manager: How and Why" by Ron Gery,
669 * March 23, 1992, archived on MSDN:
670 *
671 * The Windows system palette is broken up into two
672 * sections, one with fixed colors and one with colors
673 * that can be changed by applications. The system palette
674 * predefines 20 entries; these colors are known as the
675 * static or reserved colors and consist of the 16 colors
676 * found in the Windows version 3.0 VGA driver and 4
677 * additional colors chosen for their visual appeal. The
678 * DEFAULT_PALETTE stock object is, as the name implies,
679 * the default palette selected into a device context (DC)
680 * and consists of these static colors. Applications can
681 * set the remaining 236 colors using the Palette Manager.
682 *
683 * The 20 reserved entries have indices in [0,9] and
684 * [246,255]. We reuse 17 of them.
685 */
686 QRGB(128, 128, 128), // index 248 medium gray
687 QRGB(160, 160, 164), // index 247 light gray
688 QRGB(192, 192, 192), // index 7 light gray
689 QRGB(255, 0, 0), // index 249 red
690 QRGB( 0, 255, 0), // index 250 green
691 QRGB( 0, 0, 255), // index 252 blue
692 QRGB( 0, 255, 255), // index 254 cyan
693 QRGB(255, 0, 255), // index 253 magenta
694 QRGB(255, 255, 0), // index 251 yellow
695 QRGB(128, 0, 0), // index 1 dark red
696 QRGB( 0, 128, 0), // index 2 dark green
697 QRGB( 0, 0, 128), // index 4 dark blue
698 QRGB( 0, 128, 128), // index 6 dark cyan
699 QRGB(128, 0, 128), // index 5 dark magenta
700 QRGB(128, 128, 0), // index 3 dark yellow
701 QRGBA(0, 0, 0, 0) // transparent
702 };
703#undef QRGB
704#undef QRGBA
705
706 setRgb(qRed(global_colors[color]),
707 qGreen(global_colors[color]),
708 qBlue(global_colors[color]),
709 qAlpha(global_colors[color]));
710}
711
731{
732 cspec = Rgb;
733 ct.argb.alpha = 0xffff;
734 ct.argb.red = qRed(color) * 0x101;
735 ct.argb.green = qGreen(color) * 0x101;
736 ct.argb.blue = qBlue(color) * 0x101;
737 ct.argb.pad = 0;
738}
739
748QColor::QColor(QRgba64 rgba64) noexcept
749{
750 setRgba64(rgba64);
751}
752
762QColor::QColor(Spec spec) noexcept
763{
764 switch (spec) {
765 case Invalid:
766 invalidate();
767 break;
768 case Rgb:
769 setRgb(0, 0, 0);
770 break;
771 case Hsv:
772 setHsv(0, 0, 0);
773 break;
774 case Cmyk:
775 setCmyk(0, 0, 0, 0);
776 break;
777 case Hsl:
778 setHsl(0, 0, 0, 0);
779 break;
780 case ExtendedRgb:
781 cspec = spec;
782 setRgbF(0, 0, 0, 0);
783 break;
784 }
785}
786
787// ### Qt 7: remove those after deprecating them for the last Qt 6 LTS release
835{
836 switch (format) {
837 case HexRgb:
838 return u'#' + QStringView{QString::number(rgba() | 0x1000000, 16)}.right(6);
839 case HexArgb:
840 // it's called rgba() but it does return AARRGGBB
841 return u'#' + QStringView{QString::number(rgba() | Q_INT64_C(0x100000000), 16)}.right(8);
842 }
843 return QString();
844}
845
846#if QT_DEPRECATED_SINCE(6, 6)
873void QColor::setNamedColor(const QString &name)
874{
876}
877
884void QColor::setNamedColor(QStringView name)
885{
886 *this = fromString(name);
887}
888
895void QColor::setNamedColor(QLatin1StringView name)
896{
897 *this = fromString(name);
898}
899
913bool QColor::isValidColor(const QString &name)
914{
916}
917
923bool QColor::isValidColor(QStringView name) noexcept
924{
925 return isValidColorName(name);
926}
927
933bool QColor::isValidColor(QLatin1StringView name) noexcept
934{
935 return isValidColorName(name);
936}
937#endif // QT_DEPRECATED_SINCE(6, 6)
938
951{
952 return fromString(name).isValid();
953}
954
981{
982 if (!name.size())
983 return {};
984
985 if (name.front() == u'#') {
986 if (std::optional<QRgba64> r = get_hex_rgb(name))
987 return QColor::fromRgba64(*r);
988#ifndef QT_NO_COLORNAMES
989 } else if (std::optional<QRgb> r = get_named_rgb(name)) {
990 return QColor::fromRgba(*r);
991#endif
992 }
993
994 return {};
995}
996
1003{
1004 return get_colornames();
1005}
1006
1017void QColor::getHsvF(float *h, float *s, float *v, float *a) const
1018{
1019 if (!h || !s || !v)
1020 return;
1021
1022 if (cspec != Invalid && cspec != Hsv) {
1023 toHsv().getHsvF(h, s, v, a);
1024 return;
1025 }
1026
1027 *h = ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f;
1028 *s = ct.ahsv.saturation / float(USHRT_MAX);
1029 *v = ct.ahsv.value / float(USHRT_MAX);
1030
1031 if (a)
1032 *a = ct.ahsv.alpha / float(USHRT_MAX);
1033}
1034
1045void QColor::getHsv(int *h, int *s, int *v, int *a) const
1046{
1047 if (!h || !s || !v)
1048 return;
1049
1050 if (cspec != Invalid && cspec != Hsv) {
1051 toHsv().getHsv(h, s, v, a);
1052 return;
1053 }
1054
1055 *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1056 *s = qt_div_257(ct.ahsv.saturation);
1057 *v = qt_div_257(ct.ahsv.value);
1058
1059 if (a)
1060 *a = qt_div_257(ct.ahsv.alpha);
1061}
1062
1071void QColor::setHsvF(float h, float s, float v, float a)
1072{
1073 if (((h < 0.0f || h > 1.0f) && h != -1.0f)
1074 || (s < 0.0f || s > 1.0f)
1075 || (v < 0.0f || v > 1.0f)
1076 || (a < 0.0f || a > 1.0f)) {
1077 qWarning("QColor::setHsvF: HSV parameters out of range");
1078 invalidate();
1079 return;
1080 }
1081
1082 cspec = Hsv;
1083 ct.ahsv.alpha = qRound(a * USHRT_MAX);
1084 ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
1085 ct.ahsv.saturation = qRound(s * USHRT_MAX);
1086 ct.ahsv.value = qRound(v * USHRT_MAX);
1087 ct.ahsv.pad = 0;
1088}
1089
1099void QColor::setHsv(int h, int s, int v, int a)
1100{
1101 if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
1102 qWarning("QColor::setHsv: HSV parameters out of range");
1103 invalidate();
1104 return;
1105 }
1106
1107 cspec = Hsv;
1108 ct.ahsv.alpha = a * 0x101;
1109 ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1110 ct.ahsv.saturation = s * 0x101;
1111 ct.ahsv.value = v * 0x101;
1112 ct.ahsv.pad = 0;
1113}
1114
1127void QColor::getHslF(float *h, float *s, float *l, float *a) const
1128{
1129 if (!h || !s || !l)
1130 return;
1131
1132 if (cspec != Invalid && cspec != Hsl) {
1133 toHsl().getHslF(h, s, l, a);
1134 return;
1135 }
1136
1137 *h = ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f;
1138 *s = ct.ahsl.saturation / float(USHRT_MAX);
1139 *l = ct.ahsl.lightness / float(USHRT_MAX);
1140
1141 if (a)
1142 *a = ct.ahsl.alpha / float(USHRT_MAX);
1143}
1144
1157void QColor::getHsl(int *h, int *s, int *l, int *a) const
1158{
1159 if (!h || !s || !l)
1160 return;
1161
1162 if (cspec != Invalid && cspec != Hsl) {
1163 toHsl().getHsl(h, s, l, a);
1164 return;
1165 }
1166
1167 *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1168 *s = qt_div_257(ct.ahsl.saturation);
1169 *l = qt_div_257(ct.ahsl.lightness);
1170
1171 if (a)
1172 *a = qt_div_257(ct.ahsl.alpha);
1173}
1174
1185void QColor::setHslF(float h, float s, float l, float a)
1186{
1187 if (((h < 0.0f || h > 1.0f) && h != -1.0f)
1188 || (s < 0.0f || s > 1.0f)
1189 || (l < 0.0f || l > 1.0f)
1190 || (a < 0.0f || a > 1.0f)) {
1191 qWarning("QColor::setHslF: HSL parameters out of range");
1192 invalidate();
1193 return;
1194 }
1195
1196 cspec = Hsl;
1197 ct.ahsl.alpha = qRound(a * USHRT_MAX);
1198 ct.ahsl.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
1199 ct.ahsl.saturation = qRound(s * USHRT_MAX);
1200 ct.ahsl.lightness = qRound(l * USHRT_MAX);
1201 ct.ahsl.pad = 0;
1202}
1203
1215void QColor::setHsl(int h, int s, int l, int a)
1216{
1217 if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
1218 qWarning("QColor::setHsl: HSL parameters out of range");
1219 invalidate();
1220 return;
1221 }
1222
1223 cspec = Hsl;
1224 ct.ahsl.alpha = a * 0x101;
1225 ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1226 ct.ahsl.saturation = s * 0x101;
1227 ct.ahsl.lightness = l * 0x101;
1228 ct.ahsl.pad = 0;
1229}
1230
1231static inline qfloat16 &castF16(quint16 &v)
1232{
1233 // this works because qfloat16 internally is a quint16
1234 return *reinterpret_cast<qfloat16 *>(&v);
1235}
1236
1237static inline const qfloat16 &castF16(const quint16 &v)
1238{
1239 return *reinterpret_cast<const qfloat16 *>(&v);
1240}
1241
1252void QColor::getRgbF(float *r, float *g, float *b, float *a) const
1253{
1254 if (!r || !g || !b)
1255 return;
1256
1257 if (cspec != Invalid && cspec != Rgb && cspec != ExtendedRgb) {
1258 toRgb().getRgbF(r, g, b, a);
1259 return;
1260 }
1261
1262 if (cspec == Rgb || cspec == Invalid) {
1263 *r = ct.argb.red / float(USHRT_MAX);
1264 *g = ct.argb.green / float(USHRT_MAX);
1265 *b = ct.argb.blue / float(USHRT_MAX);
1266 if (a)
1267 *a = ct.argb.alpha / float(USHRT_MAX);
1268 } else {
1269 *r = castF16(ct.argbExtended.redF16);
1270 *g = castF16(ct.argbExtended.greenF16);
1271 *b = castF16(ct.argbExtended.blueF16);
1272 if (a)
1273 *a = castF16(ct.argbExtended.alphaF16);
1274 }
1275}
1276
1287void QColor::getRgb(int *r, int *g, int *b, int *a) const
1288{
1289 if (!r || !g || !b)
1290 return;
1291
1292 if (cspec != Invalid && cspec != Rgb) {
1293 toRgb().getRgb(r, g, b, a);
1294 return;
1295 }
1296
1297 *r = qt_div_257(ct.argb.red);
1298 *g = qt_div_257(ct.argb.green);
1299 *b = qt_div_257(ct.argb.blue);
1300
1301 if (a)
1302 *a = qt_div_257(ct.argb.alpha);
1303}
1304
1317void QColor::setRgbF(float r, float g, float b, float a)
1318{
1319 if (a < 0.0f || a > 1.0f) {
1320 qWarning("QColor::setRgbF: Alpha parameter is out of range");
1321 invalidate();
1322 return;
1323 }
1324 if (r < 0.0f || r > 1.0f ||
1325 g < 0.0f || g > 1.0f ||
1326 b < 0.0f || b > 1.0f || cspec == ExtendedRgb) {
1327 cspec = ExtendedRgb;
1328 castF16(ct.argbExtended.redF16) = qfloat16(r);
1329 castF16(ct.argbExtended.greenF16) = qfloat16(g);
1330 castF16(ct.argbExtended.blueF16) = qfloat16(b);
1331 castF16(ct.argbExtended.alphaF16) = qfloat16(a);
1332 ct.argbExtended.pad = 0;
1333 return;
1334 }
1335 cspec = Rgb;
1336 ct.argb.red = qRound(r * USHRT_MAX);
1337 ct.argb.green = qRound(g * USHRT_MAX);
1338 ct.argb.blue = qRound(b * USHRT_MAX);
1339 ct.argb.alpha = qRound(a * USHRT_MAX);
1340 ct.argb.pad = 0;
1341}
1342
1350void QColor::setRgb(int r, int g, int b, int a)
1351{
1352 if (!isRgbaValid(r, g, b, a)) {
1353 qWarning("QColor::setRgb: RGB parameters out of range");
1354 invalidate();
1355 return;
1356 }
1357
1358 cspec = Rgb;
1359 ct.argb.alpha = a * 0x101;
1360 ct.argb.red = r * 0x101;
1361 ct.argb.green = g * 0x101;
1362 ct.argb.blue = b * 0x101;
1363 ct.argb.pad = 0;
1364}
1365
1376QRgb QColor::rgba() const noexcept
1377{
1378 if (cspec != Invalid && cspec != Rgb)
1379 return toRgb().rgba();
1380 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));
1381}
1382
1388void QColor::setRgba(QRgb rgba) noexcept
1389{
1390 cspec = Rgb;
1391 ct.argb.alpha = qAlpha(rgba) * 0x101;
1392 ct.argb.red = qRed(rgba) * 0x101;
1393 ct.argb.green = qGreen(rgba) * 0x101;
1394 ct.argb.blue = qBlue(rgba) * 0x101;
1395 ct.argb.pad = 0;
1396}
1397
1408QRgba64 QColor::rgba64() const noexcept
1409{
1410 if (cspec != Invalid && cspec != Rgb)
1411 return toRgb().rgba64();
1412 return qRgba64(ct.argb.red, ct.argb.green, ct.argb.blue, ct.argb.alpha);
1413}
1414
1422void QColor::setRgba64(QRgba64 rgba) noexcept
1423{
1424 cspec = Rgb;
1425 ct.argb.alpha = rgba.alpha();
1426 ct.argb.red = rgba.red();
1427 ct.argb.green = rgba.green();
1428 ct.argb.blue = rgba.blue();
1429 ct.argb.pad = 0;
1430}
1431
1439QRgb QColor::rgb() const noexcept
1440{
1441 if (cspec != Invalid && cspec != Rgb)
1442 return toRgb().rgb();
1443 return qRgb(qt_div_257(ct.argb.red), qt_div_257(ct.argb.green), qt_div_257(ct.argb.blue));
1444}
1445
1452{
1453 cspec = Rgb;
1454 ct.argb.alpha = 0xffff;
1455 ct.argb.red = qRed(rgb) * 0x101;
1456 ct.argb.green = qGreen(rgb) * 0x101;
1457 ct.argb.blue = qBlue(rgb) * 0x101;
1458 ct.argb.pad = 0;
1459}
1460
1466int QColor::alpha() const noexcept
1467{
1468 if (cspec == ExtendedRgb)
1469 return qRound(float(castF16(ct.argbExtended.alphaF16)) * 255);
1470 return qt_div_257(ct.argb.alpha);
1471}
1472
1473
1482{
1483 QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
1484 if (cspec == ExtendedRgb) {
1485 constexpr float f = 1.0f / 255;
1486 castF16(ct.argbExtended.alphaF16) = qfloat16(alpha * f);
1487 return;
1488 }
1489 ct.argb.alpha = alpha * 0x101;
1490}
1491
1497float QColor::alphaF() const noexcept
1498{
1499 if (cspec == ExtendedRgb)
1500 return castF16(ct.argbExtended.alphaF16);
1501 return ct.argb.alpha / float(USHRT_MAX);
1502}
1503
1512{
1513 QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
1514 if (cspec == ExtendedRgb) {
1515 castF16(ct.argbExtended.alphaF16) = qfloat16(alpha);
1516 return;
1517 }
1518 float tmp = alpha * USHRT_MAX;
1519 ct.argb.alpha = qRound(tmp);
1520}
1521
1522
1528int QColor::red() const noexcept
1529{
1530 if (cspec != Invalid && cspec != Rgb)
1531 return toRgb().red();
1532 return qt_div_257(ct.argb.red);
1533}
1534
1542{
1543 QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
1544 if (cspec != Rgb)
1545 setRgb(red, green(), blue(), alpha());
1546 else
1547 ct.argb.red = red * 0x101;
1548}
1549
1555int QColor::green() const noexcept
1556{
1557 if (cspec != Invalid && cspec != Rgb)
1558 return toRgb().green();
1559 return qt_div_257(ct.argb.green);
1560}
1561
1569{
1570 QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
1571 if (cspec != Rgb)
1572 setRgb(red(), green, blue(), alpha());
1573 else
1574 ct.argb.green = green * 0x101;
1575}
1576
1577
1583int QColor::blue() const noexcept
1584{
1585 if (cspec != Invalid && cspec != Rgb)
1586 return toRgb().blue();
1587 return qt_div_257(ct.argb.blue);
1588}
1589
1590
1598{
1599 QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
1600 if (cspec != Rgb)
1601 setRgb(red(), green(), blue, alpha());
1602 else
1603 ct.argb.blue = blue * 0x101;
1604}
1605
1611float QColor::redF() const noexcept
1612{
1613 if (cspec == Rgb || cspec == Invalid)
1614 return ct.argb.red / float(USHRT_MAX);
1615 if (cspec == ExtendedRgb)
1616 return castF16(ct.argbExtended.redF16);
1617
1618 return toRgb().redF();
1619}
1620
1621
1629{
1630 if (cspec == Rgb && red >= 0.0f && red <= 1.0f)
1631 ct.argb.red = qRound(red * USHRT_MAX);
1632 else if (cspec == ExtendedRgb)
1633 castF16(ct.argbExtended.redF16) = qfloat16(red);
1634 else
1635 setRgbF(red, greenF(), blueF(), alphaF());
1636}
1637
1643float QColor::greenF() const noexcept
1644{
1645 if (cspec == Rgb || cspec == Invalid)
1646 return ct.argb.green / float(USHRT_MAX);
1647 if (cspec == ExtendedRgb)
1648 return castF16(ct.argbExtended.greenF16);
1649
1650 return toRgb().greenF();
1651}
1652
1653
1661{
1662 if (cspec == Rgb && green >= 0.0f && green <= 1.0f)
1663 ct.argb.green = qRound(green * USHRT_MAX);
1664 else if (cspec == ExtendedRgb)
1665 castF16(ct.argbExtended.greenF16) = qfloat16(green);
1666 else
1667 setRgbF(redF(), green, blueF(), alphaF());
1668}
1669
1675float QColor::blueF() const noexcept
1676{
1677 if (cspec == Rgb || cspec == Invalid)
1678 return ct.argb.blue / float(USHRT_MAX);
1679 if (cspec == ExtendedRgb)
1680 return castF16(ct.argbExtended.blueF16);
1681
1682 return toRgb().blueF();
1683}
1684
1691{
1692 if (cspec == Rgb && blue >= 0.0f && blue <= 1.0f)
1693 ct.argb.blue = qRound(blue * USHRT_MAX);
1694 else if (cspec == ExtendedRgb)
1695 castF16(ct.argbExtended.blueF16) = qfloat16(blue);
1696 else
1697 setRgbF(redF(), greenF(), blue, alphaF());
1698}
1699
1708int QColor::hue() const noexcept
1709{
1710 return hsvHue();
1711}
1712
1718int QColor::hsvHue() const noexcept
1719{
1720 if (cspec != Invalid && cspec != Hsv)
1721 return toHsv().hue();
1722 return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1723}
1724
1734int QColor::saturation() const noexcept
1735{
1736 return hsvSaturation();
1737}
1738
1744int QColor::hsvSaturation() const noexcept
1745{
1746 if (cspec != Invalid && cspec != Hsv)
1747 return toHsv().saturation();
1748 return qt_div_257(ct.ahsv.saturation);
1749}
1750
1756int QColor::value() const noexcept
1757{
1758 if (cspec != Invalid && cspec != Hsv)
1759 return toHsv().value();
1760 return qt_div_257(ct.ahsv.value);
1761}
1762
1770float QColor::hueF() const noexcept
1771{
1772 return hsvHueF();
1773}
1774
1781float QColor::hsvHueF() const noexcept
1782{
1783 if (cspec != Invalid && cspec != Hsv)
1784 return toHsv().hueF();
1785 return ct.ahsv.hue == USHRT_MAX ? -1.0f : ct.ahsv.hue / 36000.0f;
1786}
1787
1796float QColor::saturationF() const noexcept
1797{
1798 return hsvSaturationF();
1799}
1800
1806float QColor::hsvSaturationF() const noexcept
1807{
1808 if (cspec != Invalid && cspec != Hsv)
1809 return toHsv().saturationF();
1810 return ct.ahsv.saturation / float(USHRT_MAX);
1811}
1812
1818float QColor::valueF() const noexcept
1819{
1820 if (cspec != Invalid && cspec != Hsv)
1821 return toHsv().valueF();
1822 return ct.ahsv.value / float(USHRT_MAX);
1823}
1824
1832int QColor::hslHue() const noexcept
1833{
1834 if (cspec != Invalid && cspec != Hsl)
1835 return toHsl().hslHue();
1836 return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1837}
1838
1846int QColor::hslSaturation() const noexcept
1847{
1848 if (cspec != Invalid && cspec != Hsl)
1849 return toHsl().hslSaturation();
1850 return qt_div_257(ct.ahsl.saturation);
1851}
1852
1860int QColor::lightness() const noexcept
1861{
1862 if (cspec != Invalid && cspec != Hsl)
1863 return toHsl().lightness();
1864 return qt_div_257(ct.ahsl.lightness);
1865}
1866
1874float QColor::hslHueF() const noexcept
1875{
1876 if (cspec != Invalid && cspec != Hsl)
1877 return toHsl().hslHueF();
1878 return ct.ahsl.hue == USHRT_MAX ? -1.0f : ct.ahsl.hue / 36000.0f;
1879}
1880
1888float QColor::hslSaturationF() const noexcept
1889{
1890 if (cspec != Invalid && cspec != Hsl)
1891 return toHsl().hslSaturationF();
1892 return ct.ahsl.saturation / float(USHRT_MAX);
1893}
1894
1902float QColor::lightnessF() const noexcept
1903{
1904 if (cspec != Invalid && cspec != Hsl)
1905 return toHsl().lightnessF();
1906 return ct.ahsl.lightness / float(USHRT_MAX);
1907}
1908
1914int QColor::cyan() const noexcept
1915{
1916 if (cspec != Invalid && cspec != Cmyk)
1917 return toCmyk().cyan();
1918 return qt_div_257(ct.acmyk.cyan);
1919}
1920
1926int QColor::magenta() const noexcept
1927{
1928 if (cspec != Invalid && cspec != Cmyk)
1929 return toCmyk().magenta();
1930 return qt_div_257(ct.acmyk.magenta);
1931}
1932
1938int QColor::yellow() const noexcept
1939{
1940 if (cspec != Invalid && cspec != Cmyk)
1941 return toCmyk().yellow();
1942 return qt_div_257(ct.acmyk.yellow);
1943}
1944
1951int QColor::black() const noexcept
1952{
1953 if (cspec != Invalid && cspec != Cmyk)
1954 return toCmyk().black();
1955 return qt_div_257(ct.acmyk.black);
1956}
1957
1963float QColor::cyanF() const noexcept
1964{
1965 if (cspec != Invalid && cspec != Cmyk)
1966 return toCmyk().cyanF();
1967 return ct.acmyk.cyan / float(USHRT_MAX);
1968}
1969
1975float QColor::magentaF() const noexcept
1976{
1977 if (cspec != Invalid && cspec != Cmyk)
1978 return toCmyk().magentaF();
1979 return ct.acmyk.magenta / float(USHRT_MAX);
1980}
1981
1987float QColor::yellowF() const noexcept
1988{
1989 if (cspec != Invalid && cspec != Cmyk)
1990 return toCmyk().yellowF();
1991 return ct.acmyk.yellow / float(USHRT_MAX);
1992}
1993
1999float QColor::blackF() const noexcept
2000{
2001 if (cspec != Invalid && cspec != Cmyk)
2002 return toCmyk().blackF();
2003 return ct.acmyk.black / float(USHRT_MAX);
2004}
2005
2013{
2014 if (!isValid() || cspec == ExtendedRgb)
2015 return *this;
2016 if (cspec != Rgb)
2017 return toRgb().toExtendedRgb();
2018
2019 constexpr float f = 1.0f / USHRT_MAX;
2020 QColor color;
2021 color.cspec = ExtendedRgb;
2022 castF16(color.ct.argbExtended.alphaF16) = qfloat16(ct.argb.alpha * f);
2023 castF16(color.ct.argbExtended.redF16) = qfloat16(ct.argb.red * f);
2024 castF16(color.ct.argbExtended.greenF16) = qfloat16(ct.argb.green * f);
2025 castF16(color.ct.argbExtended.blueF16) = qfloat16(ct.argb.blue * f);
2026 color.ct.argbExtended.pad = 0;
2027 return color;
2028}
2029
2035QColor QColor::toRgb() const noexcept
2036{
2037 if (!isValid() || cspec == Rgb)
2038 return *this;
2039
2040 QColor color;
2041 color.cspec = Rgb;
2042 if (cspec != ExtendedRgb)
2043 color.ct.argb.alpha = ct.argb.alpha;
2044 color.ct.argb.pad = 0;
2045
2046 switch (cspec) {
2047 case Hsv:
2048 {
2049 if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
2050 // achromatic case
2051 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
2052 break;
2053 }
2054
2055 // chromatic case
2056 const float h = ct.ahsv.hue == 36000 ? 0.0f : ct.ahsv.hue / 6000.0f;
2057 const float s = ct.ahsv.saturation / float(USHRT_MAX);
2058 const float v = ct.ahsv.value / float(USHRT_MAX);
2059 const int i = int(h);
2060 const float f = h - i;
2061 const float p = v * (1.0f - s);
2062
2063 if (i & 1) {
2064 const float q = v * (1.0f - (s * f));
2065
2066 switch (i) {
2067 case 1:
2068 color.ct.argb.red = qRound(q * USHRT_MAX);
2069 color.ct.argb.green = qRound(v * USHRT_MAX);
2070 color.ct.argb.blue = qRound(p * USHRT_MAX);
2071 break;
2072 case 3:
2073 color.ct.argb.red = qRound(p * USHRT_MAX);
2074 color.ct.argb.green = qRound(q * USHRT_MAX);
2075 color.ct.argb.blue = qRound(v * USHRT_MAX);
2076 break;
2077 case 5:
2078 color.ct.argb.red = qRound(v * USHRT_MAX);
2079 color.ct.argb.green = qRound(p * USHRT_MAX);
2080 color.ct.argb.blue = qRound(q * USHRT_MAX);
2081 break;
2082 }
2083 } else {
2084 const float t = v * (1.0f - (s * (1.0f - f)));
2085
2086 switch (i) {
2087 case 0:
2088 color.ct.argb.red = qRound(v * USHRT_MAX);
2089 color.ct.argb.green = qRound(t * USHRT_MAX);
2090 color.ct.argb.blue = qRound(p * USHRT_MAX);
2091 break;
2092 case 2:
2093 color.ct.argb.red = qRound(p * USHRT_MAX);
2094 color.ct.argb.green = qRound(v * USHRT_MAX);
2095 color.ct.argb.blue = qRound(t * USHRT_MAX);
2096 break;
2097 case 4:
2098 color.ct.argb.red = qRound(t * USHRT_MAX);
2099 color.ct.argb.green = qRound(p * USHRT_MAX);
2100 color.ct.argb.blue = qRound(v * USHRT_MAX);
2101 break;
2102 }
2103 }
2104 break;
2105 }
2106 case Hsl:
2107 {
2108 if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
2109 // achromatic case
2110 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
2111 } else if (ct.ahsl.lightness == 0) {
2112 // lightness 0
2113 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
2114 } else {
2115 // chromatic case
2116 const float h = ct.ahsl.hue == 36000 ? 0.0f : ct.ahsl.hue / 36000.0f;
2117 const float s = ct.ahsl.saturation / float(USHRT_MAX);
2118 const float l = ct.ahsl.lightness / float(USHRT_MAX);
2119
2120 float temp2;
2121 if (l < 0.5f)
2122 temp2 = l * (1.0f + s);
2123 else
2124 temp2 = l + s - (l * s);
2125
2126 const float temp1 = (2.0f * l) - temp2;
2127 float temp3[3] = { h + (1.0f / 3.0f),
2128 h,
2129 h - (1.0f / 3.0f) };
2130
2131 for (int i = 0; i != 3; ++i) {
2132 if (temp3[i] < 0.0f)
2133 temp3[i] += 1.0f;
2134 else if (temp3[i] > 1.0f)
2135 temp3[i] -= 1.0f;
2136
2137 const float sixtemp3 = temp3[i] * 6.0f;
2138 if (sixtemp3 < 1.0f)
2139 color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
2140 else if ((temp3[i] * 2.0f) < 1.0f)
2141 color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
2142 else if ((temp3[i] * 3.0f) < 2.0f)
2143 color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (2.0f /3.0f - temp3[i]) * 6.0f) * USHRT_MAX);
2144 else
2145 color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
2146 }
2147 color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
2148 color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
2149 color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
2150 }
2151 break;
2152 }
2153 case Cmyk:
2154 {
2155 const float c = ct.acmyk.cyan / float(USHRT_MAX);
2156 const float m = ct.acmyk.magenta / float(USHRT_MAX);
2157 const float y = ct.acmyk.yellow / float(USHRT_MAX);
2158 const float k = ct.acmyk.black / float(USHRT_MAX);
2159
2160 color.ct.argb.red = qRound((1.0f - (c * (1.0f - k) + k)) * USHRT_MAX);
2161 color.ct.argb.green = qRound((1.0f - (m * (1.0f - k) + k)) * USHRT_MAX);
2162 color.ct.argb.blue = qRound((1.0f - (y * (1.0f - k) + k)) * USHRT_MAX);
2163 break;
2164 }
2165 case ExtendedRgb:
2166 color.ct.argb.alpha = qRound(USHRT_MAX * float(castF16(ct.argbExtended.alphaF16)));
2167 color.ct.argb.red = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.redF16)), 1.0f));
2168 color.ct.argb.green = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.greenF16)), 1.0f));
2169 color.ct.argb.blue = qRound(USHRT_MAX * qBound(0.0f, float(castF16(ct.argbExtended.blueF16)), 1.0f));
2170 break;
2171 default:
2172 break;
2173 }
2174
2175 return color;
2176}
2177
2178
2179#define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
2180#define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
2181
2182
2188QColor QColor::toHsv() const noexcept
2189{
2190 if (!isValid() || cspec == Hsv)
2191 return *this;
2192
2193 if (cspec != Rgb)
2194 return toRgb().toHsv();
2195
2196 QColor color;
2197 color.cspec = Hsv;
2198 color.ct.ahsv.alpha = ct.argb.alpha;
2199 color.ct.ahsv.pad = 0;
2200
2201 const float r = ct.argb.red / float(USHRT_MAX);
2202 const float g = ct.argb.green / float(USHRT_MAX);
2203 const float b = ct.argb.blue / float(USHRT_MAX);
2204 const float max = Q_MAX_3(r, g, b);
2205 const float min = Q_MIN_3(r, g, b);
2206 const float delta = max - min;
2207 color.ct.ahsv.value = qRound(max * USHRT_MAX);
2208 if (qFuzzyIsNull(delta)) {
2209 // achromatic case, hue is undefined
2210 color.ct.ahsv.hue = USHRT_MAX;
2211 color.ct.ahsv.saturation = 0;
2212 } else {
2213 // chromatic case
2214 float hue = 0;
2215 color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
2216 if (qFuzzyCompare(r, max)) {
2217 hue = ((g - b) /delta);
2218 } else if (qFuzzyCompare(g, max)) {
2219 hue = (2.0f + (b - r) / delta);
2220 } else if (qFuzzyCompare(b, max)) {
2221 hue = (4.0f + (r - g) / delta);
2222 } else {
2223 Q_ASSERT_X(false, "QColor::toHsv", "internal error");
2224 }
2225 hue *= 60.0f;
2226 if (hue < 0.0f)
2227 hue += 360.0f;
2228 color.ct.ahsv.hue = qRound(hue * 100.0f);
2229 }
2230
2231 return color;
2232}
2233
2239QColor QColor::toHsl() const noexcept
2240{
2241 if (!isValid() || cspec == Hsl)
2242 return *this;
2243
2244 if (cspec != Rgb)
2245 return toRgb().toHsl();
2246
2247 QColor color;
2248 color.cspec = Hsl;
2249 color.ct.ahsl.alpha = ct.argb.alpha;
2250 color.ct.ahsl.pad = 0;
2251
2252 const float r = ct.argb.red / float(USHRT_MAX);
2253 const float g = ct.argb.green / float(USHRT_MAX);
2254 const float b = ct.argb.blue / float(USHRT_MAX);
2255 const float max = Q_MAX_3(r, g, b);
2256 const float min = Q_MIN_3(r, g, b);
2257 const float delta = max - min;
2258 const float delta2 = max + min;
2259 const float lightness = 0.5f * delta2;
2260 color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
2261 if (qFuzzyIsNull(delta)) {
2262 // achromatic case, hue is undefined
2263 color.ct.ahsl.hue = USHRT_MAX;
2264 color.ct.ahsl.saturation = 0;
2265 } else {
2266 // chromatic case
2267 float hue = 0;
2268 if (lightness < 0.5f)
2269 color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
2270 else
2271 color.ct.ahsl.saturation = qRound((delta / (2.0f - delta2)) * USHRT_MAX);
2272 if (qFuzzyCompare(r, max)) {
2273 hue = ((g - b) /delta);
2274 } else if (qFuzzyCompare(g, max)) {
2275 hue = (2.0f + (b - r) / delta);
2276 } else if (qFuzzyCompare(b, max)) {
2277 hue = (4.0f + (r - g) / delta);
2278 } else {
2279 Q_ASSERT_X(false, "QColor::toHsv", "internal error");
2280 }
2281 hue *= 60.0f;
2282 if (hue < 0.0f)
2283 hue += 360.0f;
2284 color.ct.ahsl.hue = qRound(hue * 100.0f);
2285 }
2286
2287 return color;
2288}
2289
2295QColor QColor::toCmyk() const noexcept
2296{
2297 if (!isValid() || cspec == Cmyk)
2298 return *this;
2299 if (cspec != Rgb)
2300 return toRgb().toCmyk();
2301
2302 QColor color;
2303 color.cspec = Cmyk;
2304 color.ct.acmyk.alpha = ct.argb.alpha;
2305
2306 if (!ct.argb.red && !ct.argb.green && !ct.argb.blue) {
2307 // Avoid div-by-0 below
2308 color.ct.acmyk.cyan = 0;
2309 color.ct.acmyk.magenta = 0;
2310 color.ct.acmyk.yellow = 0;
2311 color.ct.acmyk.black = USHRT_MAX;
2312 } else {
2313 // rgb -> cmy
2314 const float r = ct.argb.red / float(USHRT_MAX);
2315 const float g = ct.argb.green / float(USHRT_MAX);
2316 const float b = ct.argb.blue / float(USHRT_MAX);
2317 float c = 1.0f - r;
2318 float m = 1.0f - g;
2319 float y = 1.0f - b;
2320
2321 // cmy -> cmyk
2322 const float k = qMin(c, qMin(m, y));
2323 c = (c - k) / (1.0f - k);
2324 m = (m - k) / (1.0f - k);
2325 y = (y - k) / (1.0f - k);
2326
2327 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2328 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2329 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2330 color.ct.acmyk.black = qRound(k * USHRT_MAX);
2331 }
2332
2333 return color;
2334}
2335
2336QColor QColor::convertTo(QColor::Spec colorSpec) const noexcept
2337{
2338 if (colorSpec == cspec)
2339 return *this;
2340 switch (colorSpec) {
2341 case Rgb:
2342 return toRgb();
2343 case ExtendedRgb:
2344 return toExtendedRgb();
2345 case Hsv:
2346 return toHsv();
2347 case Cmyk:
2348 return toCmyk();
2349 case Hsl:
2350 return toHsl();
2351 case Invalid:
2352 break;
2353 }
2354 return QColor(); // must be invalid
2355}
2356
2357
2370{
2371 return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
2372}
2373
2374
2386{
2387 return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
2388}
2389
2399QColor QColor::fromRgb(int r, int g, int b, int a)
2400{
2401 if (!isRgbaValid(r, g, b, a)) {
2402 qWarning("QColor::fromRgb: RGB parameters out of range");
2403 return QColor();
2404 }
2405
2406 QColor color;
2407 color.cspec = Rgb;
2408 color.ct.argb.alpha = a * 0x101;
2409 color.ct.argb.red = r * 0x101;
2410 color.ct.argb.green = g * 0x101;
2411 color.ct.argb.blue = b * 0x101;
2412 color.ct.argb.pad = 0;
2413 return color;
2414}
2415
2427QColor QColor::fromRgbF(float r, float g, float b, float a)
2428{
2429 if (a < 0.0f || a > 1.0f) {
2430 qWarning("QColor::fromRgbF: Alpha parameter out of range");
2431 return QColor();
2432 }
2433
2434 if (r < 0.0f || r > 1.0f
2435 || g < 0.0f || g > 1.0f
2436 || b < 0.0f || b > 1.0f) {
2437 QColor color;
2438 color.cspec = ExtendedRgb;
2439 castF16(color.ct.argbExtended.alphaF16) = qfloat16(a);
2440 castF16(color.ct.argbExtended.redF16) = qfloat16(r);
2441 castF16(color.ct.argbExtended.greenF16) = qfloat16(g);
2442 castF16(color.ct.argbExtended.blueF16) = qfloat16(b);
2443 color.ct.argbExtended.pad = 0;
2444 return color;
2445 }
2446
2447 QColor color;
2448 color.cspec = Rgb;
2449 color.ct.argb.alpha = qRound(a * USHRT_MAX);
2450 color.ct.argb.red = qRound(r * USHRT_MAX);
2451 color.ct.argb.green = qRound(g * USHRT_MAX);
2452 color.ct.argb.blue = qRound(b * USHRT_MAX);
2453 color.ct.argb.pad = 0;
2454 return color;
2455}
2456
2457
2468{
2469 QColor color;
2470 color.setRgba64(qRgba64(r, g, b, a));
2471 return color;
2472}
2473
2483{
2484 QColor color;
2485 color.setRgba64(rgba64);
2486 return color;
2487}
2488
2499QColor QColor::fromHsv(int h, int s, int v, int a)
2500{
2501 if (((h < 0 || h >= 360) && h != -1)
2502 || s < 0 || s > 255
2503 || v < 0 || v > 255
2504 || a < 0 || a > 255) {
2505 qWarning("QColor::fromHsv: HSV parameters out of range");
2506 return QColor();
2507 }
2508
2509 QColor color;
2510 color.cspec = Hsv;
2511 color.ct.ahsv.alpha = a * 0x101;
2512 color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2513 color.ct.ahsv.saturation = s * 0x101;
2514 color.ct.ahsv.value = v * 0x101;
2515 color.ct.ahsv.pad = 0;
2516 return color;
2517}
2518
2530QColor QColor::fromHsvF(float h, float s, float v, float a)
2531{
2532 if (((h < 0.0f || h > 1.0f) && h != -1.0f)
2533 || (s < 0.0f || s > 1.0f)
2534 || (v < 0.0f || v > 1.0f)
2535 || (a < 0.0f || a > 1.0f)) {
2536 qWarning("QColor::fromHsvF: HSV parameters out of range");
2537 return QColor();
2538 }
2539
2540 QColor color;
2541 color.cspec = Hsv;
2542 color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
2543 color.ct.ahsv.hue = h == -1.0f ? USHRT_MAX : qRound(h * 36000.0f);
2544 color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
2545 color.ct.ahsv.value = qRound(v * USHRT_MAX);
2546 color.ct.ahsv.pad = 0;
2547 return color;
2548}
2549
2562QColor QColor::fromHsl(int h, int s, int l, int a)
2563{
2564 if (((h < 0 || h >= 360) && h != -1)
2565 || s < 0 || s > 255
2566 || l < 0 || l > 255
2567 || a < 0 || a > 255) {
2568 qWarning("QColor::fromHsl: HSL parameters out of range");
2569 return QColor();
2570 }
2571
2572 QColor color;
2573 color.cspec = Hsl;
2574 color.ct.ahsl.alpha = a * 0x101;
2575 color.ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2576 color.ct.ahsl.saturation = s * 0x101;
2577 color.ct.ahsl.lightness = l * 0x101;
2578 color.ct.ahsl.pad = 0;
2579 return color;
2580}
2581
2594QColor QColor::fromHslF(float h, float s, float l, float a)
2595{
2596 if (((h < 0.0f || h > 1.0f) && h != -1.0f)
2597 || (s < 0.0f || s > 1.0f)
2598 || (l < 0.0f || l > 1.0f)
2599 || (a < 0.0f || a > 1.0f)) {
2600 qWarning("QColor::fromHslF: HSL parameters out of range");
2601 return QColor();
2602 }
2603
2604 QColor color;
2605 color.cspec = Hsl;
2606 color.ct.ahsl.alpha = qRound(a * USHRT_MAX);
2607 color.ct.ahsl.hue = (h == -1.0f) ? USHRT_MAX : qRound(h * 36000.0f);
2608 if (color.ct.ahsl.hue == 36000)
2609 color.ct.ahsl.hue = 0;
2610 color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2611 color.ct.ahsl.lightness = qRound(l * USHRT_MAX);
2612 color.ct.ahsl.pad = 0;
2613 return color;
2614}
2615
2626void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a) const
2627{
2628 if (!c || !m || !y || !k)
2629 return;
2630
2631 if (cspec != Invalid && cspec != Cmyk) {
2632 toCmyk().getCmyk(c, m, y, k, a);
2633 return;
2634 }
2635
2636 *c = qt_div_257(ct.acmyk.cyan);
2637 *m = qt_div_257(ct.acmyk.magenta);
2638 *y = qt_div_257(ct.acmyk.yellow);
2639 *k = qt_div_257(ct.acmyk.black);
2640
2641 if (a)
2642 *a = qt_div_257(ct.acmyk.alpha);
2643}
2644
2655void QColor::getCmykF(float *c, float *m, float *y, float *k, float *a) const
2656{
2657 if (!c || !m || !y || !k)
2658 return;
2659
2660 if (cspec != Invalid && cspec != Cmyk) {
2661 toCmyk().getCmykF(c, m, y, k, a);
2662 return;
2663 }
2664
2665 *c = ct.acmyk.cyan / float(USHRT_MAX);
2666 *m = ct.acmyk.magenta / float(USHRT_MAX);
2667 *y = ct.acmyk.yellow / float(USHRT_MAX);
2668 *k = ct.acmyk.black / float(USHRT_MAX);
2669
2670 if (a)
2671 *a = ct.acmyk.alpha / float(USHRT_MAX);
2672}
2673
2682void QColor::setCmyk(int c, int m, int y, int k, int a)
2683{
2684 if (c < 0 || c > 255
2685 || m < 0 || m > 255
2686 || y < 0 || y > 255
2687 || k < 0 || k > 255
2688 || a < 0 || a > 255) {
2689 qWarning("QColor::setCmyk: CMYK parameters out of range");
2690 invalidate();
2691 return;
2692 }
2693
2694 cspec = Cmyk;
2695 ct.acmyk.alpha = a * 0x101;
2696 ct.acmyk.cyan = c * 0x101;
2697 ct.acmyk.magenta = m * 0x101;
2698 ct.acmyk.yellow = y * 0x101;
2699 ct.acmyk.black = k * 0x101;
2700}
2701
2712void QColor::setCmykF(float c, float m, float y, float k, float a)
2713{
2714 if (c < 0.0f || c > 1.0f
2715 || m < 0.0f || m > 1.0f
2716 || y < 0.0f || y > 1.0f
2717 || k < 0.0f || k > 1.0f
2718 || a < 0.0f || a > 1.0f) {
2719 qWarning("QColor::setCmykF: CMYK parameters out of range");
2720 invalidate();
2721 return;
2722 }
2723
2724 cspec = Cmyk;
2725 ct.acmyk.alpha = qRound(a * USHRT_MAX);
2726 ct.acmyk.cyan = qRound(c * USHRT_MAX);
2727 ct.acmyk.magenta = qRound(m * USHRT_MAX);
2728 ct.acmyk.yellow = qRound(y * USHRT_MAX);
2729 ct.acmyk.black = qRound(k * USHRT_MAX);
2730}
2731
2741QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2742{
2743 if (c < 0 || c > 255
2744 || m < 0 || m > 255
2745 || y < 0 || y > 255
2746 || k < 0 || k > 255
2747 || a < 0 || a > 255) {
2748 qWarning("QColor::fromCmyk: CMYK parameters out of range");
2749 return QColor();
2750 }
2751
2752 QColor color;
2753 color.cspec = Cmyk;
2754 color.ct.acmyk.alpha = a * 0x101;
2755 color.ct.acmyk.cyan = c * 0x101;
2756 color.ct.acmyk.magenta = m * 0x101;
2757 color.ct.acmyk.yellow = y * 0x101;
2758 color.ct.acmyk.black = k * 0x101;
2759 return color;
2760}
2761
2773QColor QColor::fromCmykF(float c, float m, float y, float k, float a)
2774{
2775 if (c < 0.0f || c > 1.0f
2776 || m < 0.0f || m > 1.0f
2777 || y < 0.0f || y > 1.0f
2778 || k < 0.0f || k > 1.0f
2779 || a < 0.0f || a > 1.0f) {
2780 qWarning("QColor::fromCmykF: CMYK parameters out of range");
2781 return QColor();
2782 }
2783
2784 QColor color;
2785 color.cspec = Cmyk;
2786 color.ct.acmyk.alpha = qRound(a * USHRT_MAX);
2787 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2788 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2789 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2790 color.ct.acmyk.black = qRound(k * USHRT_MAX);
2791 return color;
2792}
2793
2812QColor QColor::lighter(int factor) const noexcept
2813{
2814 if (factor <= 0) // invalid lightness factor
2815 return *this;
2816 else if (factor < 100) // makes color darker
2817 return darker(10000 / factor);
2818
2819 QColor hsv = toHsv();
2820 int s = hsv.ct.ahsv.saturation;
2821 uint v = hsv.ct.ahsv.value;
2822
2823 v = (factor*v)/100;
2824 if (v > USHRT_MAX) {
2825 // overflow... adjust saturation
2826 s -= v - USHRT_MAX;
2827 if (s < 0)
2828 s = 0;
2829 v = USHRT_MAX;
2830 }
2831
2832 hsv.ct.ahsv.saturation = s;
2833 hsv.ct.ahsv.value = v;
2834
2835 // convert back to same color spec as original color
2836 return hsv.convertTo(cspec);
2837}
2838
2857QColor QColor::darker(int factor) const noexcept
2858{
2859 if (factor <= 0) // invalid darkness factor
2860 return *this;
2861 else if (factor < 100) // makes color lighter
2862 return lighter(10000 / factor);
2863
2864 QColor hsv = toHsv();
2865 hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2866
2867 // convert back to same color spec as original color
2868 return hsv.convertTo(cspec);
2869}
2870
2875{
2876 return operator=(QColor(color));
2877}
2878
2887bool QColor::operator==(const QColor &color) const noexcept
2888{
2889 if ((cspec == ExtendedRgb || color.cspec == ExtendedRgb) &&
2890 (cspec == color.cspec || cspec == Rgb || color.cspec == Rgb)) {
2891 return qFuzzyCompare(alphaF(), color.alphaF())
2892 && qFuzzyCompare(redF(), color.redF())
2893 && qFuzzyCompare(greenF(), color.greenF())
2894 && qFuzzyCompare(blueF(), color.blueF());
2895 } else {
2896 return (cspec == color.cspec
2897 && ct.argb.alpha == color.ct.argb.alpha
2898 && (((cspec == QColor::Hsv || cspec == QColor::Hsl)
2899 && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
2900 || (ct.argb.red == color.ct.argb.red))
2901 && ct.argb.green == color.ct.argb.green
2902 && ct.argb.blue == color.ct.argb.blue
2903 && ct.argb.pad == color.ct.argb.pad);
2904 }
2905}
2906
2915bool QColor::operator!=(const QColor &color) const noexcept
2916{ return !operator==(color); }
2917
2918
2922QColor::operator QVariant() const
2923{
2924 return QVariant::fromValue(*this);
2925}
2926
2932void QColor::invalidate() noexcept
2933{
2934 cspec = Invalid;
2935 ct.argb.alpha = USHRT_MAX;
2936 ct.argb.red = 0;
2937 ct.argb.green = 0;
2938 ct.argb.blue = 0;
2939 ct.argb.pad = 0;
2940}
2941
2942/*****************************************************************************
2943 QColor stream functions
2944 *****************************************************************************/
2945
2946#ifndef QT_NO_DEBUG_STREAM
2948{
2949 QDebugStateSaver saver(dbg);
2950 if (!c.isValid())
2951 dbg.nospace() << "QColor(Invalid)";
2952 else if (c.spec() == QColor::Rgb)
2953 dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2954 else if (c.spec() == QColor::ExtendedRgb)
2955 dbg.nospace() << "QColor(Ext. ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2956 else if (c.spec() == QColor::Hsv)
2957 dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
2958 else if (c.spec() == QColor::Cmyk)
2959 dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
2960 << c.blackF()<< ')';
2961 else if (c.spec() == QColor::Hsl)
2962 dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
2963
2964 return dbg;
2965}
2966#endif
2967
2968#ifndef QT_NO_DATASTREAM
2978{
2979 if (stream.version() < 7) {
2980 if (!color.isValid())
2981 return stream << quint32(0x49000000);
2982 quint32 p = (quint32)color.rgb();
2983 if (stream.version() == 1) // Swap red and blue
2984 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2985 return stream << p;
2986 }
2987
2988 qint8 s = color.cspec;
2989 quint16 a = color.ct.argb.alpha;
2990 quint16 r = color.ct.argb.red;
2991 quint16 g = color.ct.argb.green;
2992 quint16 b = color.ct.argb.blue;
2993 quint16 p = color.ct.argb.pad;
2994
2995 stream << s;
2996 stream << a;
2997 stream << r;
2998 stream << g;
2999 stream << b;
3000 stream << p;
3001
3002 return stream;
3003}
3004
3014{
3015 if (stream.version() < 7) {
3016 quint32 p;
3017 stream >> p;
3018 if (p == 0x49000000) {
3019 color.invalidate();
3020 return stream;
3021 }
3022 if (stream.version() == 1) // Swap red and blue
3023 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
3024 color.setRgb(p);
3025 return stream;
3026 }
3027
3028 qint8 s;
3029 quint16 a, r, g, b, p;
3030 stream >> s;
3031 stream >> a;
3032 stream >> r;
3033 stream >> g;
3034 stream >> b;
3035 stream >> p;
3036
3037 color.cspec = QColor::Spec(s);
3038 color.ct.argb.alpha = a;
3039 color.ct.argb.red = r;
3040 color.ct.argb.green = g;
3041 color.ct.argb.blue = b;
3042 color.ct.argb.pad = p;
3043
3044 return stream;
3045}
3046#endif // QT_NO_DATASTREAM
3047
3048// A table of precalculated results of 0x00ff00ff/alpha use by qUnpremultiply:
3050 0, 16711935, 8355967, 5570645, 4177983, 3342387, 2785322, 2387419,
3051 2088991, 1856881, 1671193, 1519266, 1392661, 1285533, 1193709, 1114129,
3052 1044495, 983055, 928440, 879575, 835596, 795806, 759633, 726605,
3053 696330, 668477, 642766, 618960, 596854, 576273, 557064, 539094,
3054 522247, 506422, 491527, 477483, 464220, 451673, 439787, 428511,
3055 417798, 407608, 397903, 388649, 379816, 371376, 363302, 355573,
3056 348165, 341059, 334238, 327685, 321383, 315319, 309480, 303853,
3057 298427, 293191, 288136, 283253, 278532, 273966, 269547, 265268,
3058 261123, 257106, 253211, 249431, 245763, 242201, 238741, 235379,
3059 232110, 228930, 225836, 222825, 219893, 217038, 214255, 211543,
3060 208899, 206320, 203804, 201348, 198951, 196611, 194324, 192091,
3061 189908, 187774, 185688, 183647, 181651, 179698, 177786, 175915,
3062 174082, 172287, 170529, 168807, 167119, 165464, 163842, 162251,
3063 160691, 159161, 157659, 156186, 154740, 153320, 151926, 150557,
3064 149213, 147893, 146595, 145321, 144068, 142837, 141626, 140436,
3065 139266, 138115, 136983, 135869, 134773, 133695, 132634, 131590,
3066 130561, 129549, 128553, 127572, 126605, 125653, 124715, 123792,
3067 122881, 121984, 121100, 120229, 119370, 118524, 117689, 116866,
3068 116055, 115254, 114465, 113686, 112918, 112160, 111412, 110675,
3069 109946, 109228, 108519, 107818, 107127, 106445, 105771, 105106,
3070 104449, 103800, 103160, 102527, 101902, 101284, 100674, 100071,
3071 99475, 98887, 98305, 97730, 97162, 96600, 96045, 95496,
3072 94954, 94417, 93887, 93362, 92844, 92331, 91823, 91322,
3073 90825, 90334, 89849, 89368, 88893, 88422, 87957, 87497,
3074 87041, 86590, 86143, 85702, 85264, 84832, 84403, 83979,
3075 83559, 83143, 82732, 82324, 81921, 81521, 81125, 80733,
3076 80345, 79961, 79580, 79203, 78829, 78459, 78093, 77729,
3077 77370, 77013, 76660, 76310, 75963, 75619, 75278, 74941,
3078 74606, 74275, 73946, 73620, 73297, 72977, 72660, 72346,
3079 72034, 71725, 71418, 71114, 70813, 70514, 70218, 69924,
3080 69633, 69344, 69057, 68773, 68491, 68211, 67934, 67659,
3081 67386, 67116, 66847, 66581, 66317, 66055, 65795, 65537
3082};
3083
3084/*****************************************************************************
3085 QColor global functions (documentation only)
3086 *****************************************************************************/
3087
\inmodule QtCore
\inmodule QtCore
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
float blackF() const noexcept
Returns the black color component of this color.
Definition qcolor.cpp:1999
float valueF() const noexcept
Returns the value color component of this color.
Definition qcolor.cpp:1818
int saturation() const noexcept
Returns the HSV saturation color component of this color.
Definition qcolor.cpp:1734
void setHslF(float h, float s, float l, float a=1.0)
Definition qcolor.cpp:1185
void getHsl(int *h, int *s, int *l, int *a=nullptr) const
Definition qcolor.cpp:1157
QColor toExtendedRgb() const noexcept
Create and returns an extended RGB QColor based on this color.
Definition qcolor.cpp:2012
QColor convertTo(Spec colorSpec) const noexcept
Creates a copy of this color in the format specified by colorSpec.
Definition qcolor.cpp:2336
void getRgbF(float *r, float *g, float *b, float *a=nullptr) const
Sets the contents pointed to by r, g, b, and a, to the red, green, blue, and alpha-channel (transpare...
Definition qcolor.cpp:1252
void setRedF(float red)
Sets the red color component of this color to red.
Definition qcolor.cpp:1628
float hsvHueF() const noexcept
Returns the hue color component of this color.
Definition qcolor.cpp:1781
void setAlphaF(float alpha)
Sets the alpha of this color to alpha.
Definition qcolor.cpp:1511
Spec
The type of color specified, either RGB, extended RGB, HSV, CMYK or HSL.
Definition qcolor.h:35
@ Cmyk
Definition qcolor.h:35
@ Invalid
Definition qcolor.h:35
@ Hsv
Definition qcolor.h:35
@ ExtendedRgb
Definition qcolor.h:35
@ Rgb
Definition qcolor.h:35
@ Hsl
Definition qcolor.h:35
static QColor fromHslF(float h, float s, float l, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2594
int hsvSaturation() const noexcept
Returns the HSV saturation color component of this color.
Definition qcolor.cpp:1744
QColor & operator=(Qt::GlobalColor color) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2874
QColor darker(int f=200) const noexcept
Definition qcolor.cpp:2857
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition qrgb.h:30
float yellowF() const noexcept
Returns the yellow color component of this color.
Definition qcolor.cpp:1987
void setHsl(int h, int s, int l, int a=255)
Definition qcolor.cpp:1215
float hueF() const noexcept
Returns the HSV hue color component of this color.
Definition qcolor.cpp:1770
constexpr QColor() noexcept
Constructs an invalid color with the RGB value (0, 0, 0).
Definition qcolor.h:38
static QColor fromRgb(QRgb rgb) noexcept
Static convenience function that returns a QColor constructed from the given QRgb value rgb.
Definition qcolor.cpp:2369
QColor toHsv() const noexcept
Creates and returns an HSV QColor based on this color.
Definition qcolor.cpp:2188
QColor toRgb() const noexcept
Create and returns an RGB QColor based on this color.
Definition qcolor.cpp:2035
void setBlueF(float blue)
Sets the blue color component of this color to blue.
Definition qcolor.cpp:1690
QRgb rgb() const noexcept
Returns the RGB value of the color.
Definition qcolor.cpp:1439
float greenF() const noexcept
Returns the green color component of this color.
Definition qcolor.cpp:1643
void setRgba64(QRgba64 rgba) noexcept
Definition qcolor.cpp:1422
void getHslF(float *h, float *s, float *l, float *a=nullptr) const
Definition qcolor.cpp:1127
void setGreen(int green)
Sets the green color component of this color to green.
Definition qcolor.cpp:1568
QRgb qRgba(int r, int g, int b, int a)
Returns the ARGB quadruplet ({a}, {r}, {g}, {b}).
Definition qrgb.h:33
QRgb rgba() const noexcept
Returns the RGB value of the color, including its alpha.
Definition qcolor.cpp:1376
void getCmykF(float *c, float *m, float *y, float *k, float *a=nullptr) const
Sets the contents pointed to by c, m, y, k, and a, to the cyan, magenta, yellow, black,...
Definition qcolor.cpp:2655
int lightness() const noexcept
Definition qcolor.cpp:1860
void getHsvF(float *h, float *s, float *v, float *a=nullptr) const
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha-channel (tra...
Definition qcolor.cpp:1017
QRgba64 rgba64() const noexcept
Definition qcolor.cpp:1408
int cyan() const noexcept
Returns the cyan color component of this color.
Definition qcolor.cpp:1914
void setBlue(int blue)
Sets the blue color component of this color to blue.
Definition qcolor.cpp:1597
static QColor fromRgba(QRgb rgba) noexcept
Static convenience function that returns a QColor constructed from the given QRgb value rgba.
Definition qcolor.cpp:2385
bool operator!=(const QColor &c) const noexcept
Returns true if this color has different color specification or component values from color; otherwis...
Definition qcolor.cpp:2915
int black() const noexcept
Returns the black color component of this color.
Definition qcolor.cpp:1951
void setGreenF(float green)
Sets the green color component of this color to green.
Definition qcolor.cpp:1660
int alpha() const noexcept
Returns the alpha color component of this color.
Definition qcolor.cpp:1466
int red() const noexcept
Returns the red color component of this color.
Definition qcolor.cpp:1528
static QColor fromString(QAnyStringView name) noexcept
Definition qcolor.cpp:980
float magentaF() const noexcept
Returns the magenta color component of this color.
Definition qcolor.cpp:1975
int hue() const noexcept
Returns the HSV hue color component of this color.
Definition qcolor.cpp:1708
int blue() const noexcept
Returns the blue color component of this color.
Definition qcolor.cpp:1583
int green() const noexcept
Returns the green color component of this color.
Definition qcolor.cpp:1555
void setAlpha(int alpha)
Sets the alpha of this color to alpha.
Definition qcolor.cpp:1481
float cyanF() const noexcept
Returns the cyan color component of this color.
Definition qcolor.cpp:1963
float redF() const noexcept
Returns the red color component of this color.
Definition qcolor.cpp:1611
void setHsv(int h, int s, int v, int a=255)
Sets a HSV color value; h is the hue, s is the saturation, v is the value and a is the alpha componen...
Definition qcolor.cpp:1099
static QColor fromCmyk(int c, int m, int y, int k, int a=255)
Static convenience function that returns a QColor constructed from the given CMYK color values: c (cy...
Definition qcolor.cpp:2741
void setRgba(QRgb rgba) noexcept
Sets the RGB value to rgba, including its alpha.
Definition qcolor.cpp:1388
void setCmyk(int c, int m, int y, int k, int a=255)
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha-channel,...
Definition qcolor.cpp:2682
int hsvHue() const noexcept
Returns the HSV hue color component of this color.
Definition qcolor.cpp:1718
float saturationF() const noexcept
Returns the HSV saturation color component of this color.
Definition qcolor.cpp:1796
int value() const noexcept
Returns the value color component of this color.
Definition qcolor.cpp:1756
int magenta() const noexcept
Returns the magenta color component of this color.
Definition qcolor.cpp:1926
void setRgb(int r, int g, int b, int a=255)
Sets the RGB value to r, g, b and the alpha value to a.
Definition qcolor.cpp:1350
static QColor fromHsvF(float h, float s, float v, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2530
float hsvSaturationF() const noexcept
Returns the HSV saturation color component of this color.
Definition qcolor.cpp:1806
float lightnessF() const noexcept
Definition qcolor.cpp:1902
float hslSaturationF() const noexcept
Definition qcolor.cpp:1888
QColor lighter(int f=150) const noexcept
Definition qcolor.cpp:2812
float hslHueF() const noexcept
Definition qcolor.cpp:1874
QString name(NameFormat format=HexRgb) const
Definition qcolor.cpp:834
void getRgb(int *r, int *g, int *b, int *a=nullptr) const
Sets the contents pointed to by r, g, b, and a, to the red, green, blue, and alpha-channel (transpare...
Definition qcolor.cpp:1287
QColor toHsl() const noexcept
Creates and returns an HSL QColor based on this color.
Definition qcolor.cpp:2239
static QColor fromCmykF(float c, float m, float y, float k, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2773
bool operator==(const QColor &c) const noexcept
Returns true if this color has the same color specification and component values as color; otherwise ...
Definition qcolor.cpp:2887
void setRgbF(float r, float g, float b, float a=1.0)
Sets the color channels of this color to r (red), g (green), b (blue) and a (alpha,...
Definition qcolor.cpp:1317
QColor toCmyk() const noexcept
Creates and returns a CMYK QColor based on this color.
Definition qcolor.cpp:2295
void setCmykF(float c, float m, float y, float k, float a=1.0)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qcolor.cpp:2712
int hslHue() const noexcept
Definition qcolor.cpp:1832
void getCmyk(int *c, int *m, int *y, int *k, int *a=nullptr) const
Sets the contents pointed to by c, m, y, k, and a, to the cyan, magenta, yellow, black,...
Definition qcolor.cpp:2626
float alphaF() const noexcept
Returns the alpha color component of this color.
Definition qcolor.cpp:1497
static QColor fromHsv(int h, int s, int v, int a=255)
Static convenience function that returns a QColor constructed from the HSV color values,...
Definition qcolor.cpp:2499
int hslSaturation() const noexcept
Definition qcolor.cpp:1846
static QColor fromHsl(int h, int s, int l, int a=255)
Definition qcolor.cpp:2562
int yellow() const noexcept
Returns the yellow color component of this color.
Definition qcolor.cpp:1938
static bool isValidColorName(QAnyStringView) noexcept
Definition qcolor.cpp:950
float blueF() const noexcept
Returns the blue color component of this color.
Definition qcolor.cpp:1675
void setHsvF(float h, float s, float v, float a=1.0)
Sets a HSV color value; h is the hue, s is the saturation, v is the value and a is the alpha componen...
Definition qcolor.cpp:1071
NameFormat
How to format the output of the name() function.
Definition qcolor.h:36
@ HexArgb
Definition qcolor.h:36
@ HexRgb
Definition qcolor.h:36
void setRed(int red)
Sets the red color component of this color to red.
Definition qcolor.cpp:1541
void getHsv(int *h, int *s, int *v, int *a=nullptr) const
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha-channel (tra...
Definition qcolor.cpp:1045
bool isValid() const noexcept
Returns true if the color is valid; otherwise returns false.
Definition qcolor.h:285
static QColor fromRgbF(float r, float g, float b, float a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values,...
Definition qcolor.cpp:2427
static QStringList colorNames()
Returns a QStringList containing the color names Qt knows about.
Definition qcolor.cpp:1002
static QColor fromRgba64(ushort r, ushort g, ushort b, ushort a=USHRT_MAX) noexcept
Definition qcolor.cpp:2467
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString number(int, int base=10)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:8084
\inmodule QtCore
Definition qvariant.h:65
static auto fromValue(T &&value) noexcept(std::is_nothrow_copy_constructible_v< T > &&Private::CanUseInternalSpace< T >) -> std::enable_if_t< std::conjunction_v< std::is_copy_constructible< T >, std::is_destructible< T > >, QVariant >
Definition qvariant.h:536
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:47
QString str
[2]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
constexpr int fromHex(char32_t c) noexcept
Definition qtools_p.h:44
constexpr char toAsciiLower(char ch) noexcept
Definition qtools_p.h:87
GlobalColor
Definition qnamespace.h:27
QAnyStringView qToAnyStringViewIgnoringNull(const QStringLike &s) noexcept
size_t qstrlen(const char *str)
Q_CORE_EXPORT int qstrcmp(const char *str1, const char *str2)
static QStringList get_colornames()
Definition qcolor.cpp:333
std::optional< QRgb > qt_get_hex_rgb(const char *name)
Definition qcolor.cpp:90
const uint qt_inv_premul_factor[256]
Definition qcolor.cpp:3049
QDataStream & operator>>(QDataStream &stream, QColor &color)
Definition qcolor.cpp:3013
static constexpr struct RGBData rgbTbl[]
#define Q_MAX_3(a, b, c)
Definition qcolor.cpp:2179
#define rgb(r, g, b)
Definition qcolor.cpp:124
static std::optional< QRgb > get_named_rgb(QAnyStringView name)
Definition qcolor.cpp:314
static const int rgbTblSize
Definition qcolor.cpp:281
static std::optional< QRgba64 > get_hex_rgb(const char *name, size_t len)
Definition qcolor.cpp:48
static std::optional< QRgb > get_named_rgb_no_space(const char *name_no_space)
Definition qcolor.cpp:300
#define Q_MIN_3(a, b, c)
Definition qcolor.cpp:2180
#define QCOLOR_INT_RANGE_CHECK(fn, var)
Definition qcolor.cpp:581
static qfloat16 & castF16(quint16 &v)
Definition qcolor.cpp:1231
static int hex2int(const char *s, int n)
Definition qcolor.cpp:33
#define QRGBA(r, g, b, a)
QDebug operator<<(QDebug dbg, const QColor &c)
Definition qcolor.cpp:2947
#define QCOLOR_REAL_RANGE_CHECK(fn, var)
Definition qcolor.cpp:589
#define QRGB(r, g, b)
bool operator<(const char *name, const RGBData &data)
Definition qcolor.cpp:295
static constexpr uint qt_div_257(uint x)
EGLStreamKHR stream
bool qFuzzyCompare(qfloat16 p1, qfloat16 p2) noexcept
Definition qfloat16.h:333
bool qFuzzyIsNull(qfloat16 f) noexcept
Definition qfloat16.h:349
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
#define qWarning
Definition qlogging.h:166
@ Invalid
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qBound(const T &min, const T &val, const T &max)
Definition qminmax.h:44
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
const GLfloat * m
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLuint color
[2]
GLboolean GLboolean g
GLuint name
GLfloat n
GLint GLsizei GLsizei GLenum format
GLint y
GLfloat GLfloat GLfloat GLfloat h
GLdouble s
[6]
Definition qopenglext.h:235
GLbyte GLbyte blue
Definition qopenglext.h:385
const GLubyte * c
GLdouble GLdouble t
Definition qopenglext.h:243
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
GLfloat GLfloat p
[1]
GLenum GLsizei len
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
GLbyte green
Definition qopenglext.h:385
static bool fromString(const QMetaObject *mo, QString s, Allocate &&allocate)
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition qrgb.h:13
constexpr int qRed(QRgb rgb)
Definition qrgb.h:18
constexpr int qGreen(QRgb rgb)
Definition qrgb.h:21
constexpr int qBlue(QRgb rgb)
Definition qrgb.h:24
constexpr int qAlpha(QRgb rgb)
Definition qrgb.h:27
constexpr QRgba64 qRgba64(quint16 r, quint16 g, quint16 b, quint16 a)
Definition qrgba64.h:180
unsigned int quint32
Definition qtypes.h:50
unsigned char uchar
Definition qtypes.h:32
unsigned short quint16
Definition qtypes.h:48
unsigned int uint
Definition qtypes.h:34
unsigned short ushort
Definition qtypes.h:33
QT_BEGIN_NAMESPACE typedef signed char qint8
Definition qtypes.h:45
#define Q_INT64_C(c)
Definition qtypes.h:57
QGraphicsSvgItem * red
static constexpr size_t MaxInternalSize
Definition qvariant.h:99
uint value
Definition qcolor.cpp:129