1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QCOLOR_H
41#define QCOLOR_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qrgb.h>
45#include <QtCore/qnamespace.h>
46#include <QtCore/qstringlist.h>
47#include <QtGui/qrgba64.h>
48
49QT_BEGIN_NAMESPACE
50
51
52class QColor;
53class QColormap;
54class QVariant;
55
56#ifndef QT_NO_DEBUG_STREAM
57Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &);
58#endif
59#ifndef QT_NO_DATASTREAM
60Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
61Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
62#endif
63
64class Q_GUI_EXPORT QColor
65{
66public:
67 enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl, ExtendedRgb };
68 enum NameFormat { HexRgb, HexArgb };
69
70 Q_DECL_CONSTEXPR QColor() noexcept
71 : cspec(Invalid), ct(USHRT_MAX, 0, 0, 0, 0) {}
72 QColor(Qt::GlobalColor color) noexcept;
73 Q_DECL_CONSTEXPR QColor(int r, int g, int b, int a = 255) noexcept
74 : cspec(isRgbaValid(r, g, b, a) ? Rgb : Invalid),
75 ct(cspec == Rgb ? a * 0x0101 : 0,
76 cspec == Rgb ? r * 0x0101 : 0,
77 cspec == Rgb ? g * 0x0101 : 0,
78 cspec == Rgb ? b * 0x0101 : 0,
79 0) {}
80 QColor(QRgb rgb) noexcept;
81 QColor(QRgba64 rgba64) noexcept;
82#if QT_STRINGVIEW_LEVEL < 2
83 inline QColor(const QString& name);
84#endif
85 explicit inline QColor(QStringView name);
86 inline QColor(const char *aname) : QColor(QLatin1String(aname)) {}
87 inline QColor(QLatin1String name);
88 QColor(Spec spec) noexcept;
89
90#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
91 // ### Qt 6: remove all of these, the trivial ones are fine.
92 Q_DECL_CONSTEXPR QColor(const QColor &color) noexcept
93 : cspec(color.cspec), ct(color.ct)
94 {}
95 Q_DECL_CONSTEXPR QColor(QColor &&other) noexcept : cspec(other.cspec), ct(other.ct) {}
96 QColor &operator=(QColor &&other) noexcept
97 { cspec = other.cspec; ct = other.ct; return *this; }
98 QColor &operator=(const QColor &) noexcept;
99#endif // Qt < 6
100
101 QColor &operator=(Qt::GlobalColor color) noexcept;
102
103 bool isValid() const noexcept;
104
105 // ### Qt 6: merge overloads
106 QString name() const;
107 QString name(NameFormat format) const;
108
109#if QT_STRINGVIEW_LEVEL < 2
110 void setNamedColor(const QString& name);
111#endif
112 void setNamedColor(QStringView name);
113 void setNamedColor(QLatin1String name);
114
115 static QStringList colorNames();
116
117 inline Spec spec() const noexcept
118 { return cspec; }
119
120 int alpha() const noexcept;
121 void setAlpha(int alpha);
122
123 qreal alphaF() const noexcept;
124 void setAlphaF(qreal alpha);
125
126 int red() const noexcept;
127 int green() const noexcept;
128 int blue() const noexcept;
129 void setRed(int red);
130 void setGreen(int green);
131 void setBlue(int blue);
132
133 qreal redF() const noexcept;
134 qreal greenF() const noexcept;
135 qreal blueF() const noexcept;
136 void setRedF(qreal red);
137 void setGreenF(qreal green);
138 void setBlueF(qreal blue);
139
140 void getRgb(int *r, int *g, int *b, int *a = nullptr) const;
141 void setRgb(int r, int g, int b, int a = 255);
142
143 void getRgbF(qreal *r, qreal *g, qreal *b, qreal *a = nullptr) const;
144 void setRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
145
146 QRgba64 rgba64() const noexcept;
147 void setRgba64(QRgba64 rgba) noexcept;
148
149 QRgb rgba() const noexcept;
150 void setRgba(QRgb rgba) noexcept;
151
152 QRgb rgb() const noexcept;
153 void setRgb(QRgb rgb) noexcept;
154
155 int hue() const noexcept; // 0 <= hue < 360
156 int saturation() const noexcept;
157 int hsvHue() const noexcept; // 0 <= hue < 360
158 int hsvSaturation() const noexcept;
159 int value() const noexcept;
160
161 qreal hueF() const noexcept; // 0.0 <= hueF < 360.0
162 qreal saturationF() const noexcept;
163 qreal hsvHueF() const noexcept; // 0.0 <= hueF < 360.0
164 qreal hsvSaturationF() const noexcept;
165 qreal valueF() const noexcept;
166
167 void getHsv(int *h, int *s, int *v, int *a = nullptr) const;
168 void setHsv(int h, int s, int v, int a = 255);
169
170 void getHsvF(qreal *h, qreal *s, qreal *v, qreal *a = nullptr) const;
171 void setHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
172
173 int cyan() const noexcept;
174 int magenta() const noexcept;
175 int yellow() const noexcept;
176 int black() const noexcept;
177
178 qreal cyanF() const noexcept;
179 qreal magentaF() const noexcept;
180 qreal yellowF() const noexcept;
181 qreal blackF() const noexcept;
182
183 void getCmyk(int *c, int *m, int *y, int *k, int *a = nullptr); // ### Qt 6: remove
184 void getCmyk(int *c, int *m, int *y, int *k, int *a = nullptr) const;
185 void setCmyk(int c, int m, int y, int k, int a = 255);
186
187 void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = nullptr); // ### Qt 6: remove
188 void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = nullptr) const;
189 void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
190
191 int hslHue() const noexcept; // 0 <= hue < 360
192 int hslSaturation() const noexcept;
193 int lightness() const noexcept;
194
195 qreal hslHueF() const noexcept; // 0.0 <= hueF < 360.0
196 qreal hslSaturationF() const noexcept;
197 qreal lightnessF() const noexcept;
198
199 void getHsl(int *h, int *s, int *l, int *a = nullptr) const;
200 void setHsl(int h, int s, int l, int a = 255);
201
202 void getHslF(qreal *h, qreal *s, qreal *l, qreal *a = nullptr) const;
203 void setHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
204
205 QColor toRgb() const noexcept;
206 QColor toHsv() const noexcept;
207 QColor toCmyk() const noexcept;
208 QColor toHsl() const noexcept;
209 QColor toExtendedRgb() const noexcept;
210
211 Q_REQUIRED_RESULT QColor convertTo(Spec colorSpec) const noexcept;
212
213 static QColor fromRgb(QRgb rgb) noexcept;
214 static QColor fromRgba(QRgb rgba) noexcept;
215
216 static QColor fromRgb(int r, int g, int b, int a = 255);
217 static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
218
219 static QColor fromRgba64(ushort r, ushort g, ushort b, ushort a = USHRT_MAX) noexcept;
220 static QColor fromRgba64(QRgba64 rgba) noexcept;
221
222 static QColor fromHsv(int h, int s, int v, int a = 255);
223 static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
224
225 static QColor fromCmyk(int c, int m, int y, int k, int a = 255);
226 static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
227
228 static QColor fromHsl(int h, int s, int l, int a = 255);
229 static QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
230
231#if QT_DEPRECATED_SINCE(5, 13)
232 QT_DEPRECATED_X("Use QColor::lighter() instead")
233 Q_REQUIRED_RESULT QColor light(int f = 150) const noexcept;
234 QT_DEPRECATED_X("Use QColor::darker() instead")
235 Q_REQUIRED_RESULT QColor dark(int f = 200) const noexcept;
236#endif
237 Q_REQUIRED_RESULT QColor lighter(int f = 150) const noexcept;
238 Q_REQUIRED_RESULT QColor darker(int f = 200) const noexcept;
239
240 bool operator==(const QColor &c) const noexcept;
241 bool operator!=(const QColor &c) const noexcept;
242
243 operator QVariant() const;
244
245#if QT_STRINGVIEW_LEVEL < 2
246 static bool isValidColor(const QString &name);
247#endif
248 static bool isValidColor(QStringView) noexcept;
249 static bool isValidColor(QLatin1String) noexcept;
250
251private:
252
253 void invalidate() noexcept;
254 template <typename String>
255 bool setColorFromString(String name);
256
257 static Q_DECL_CONSTEXPR bool isRgbaValid(int r, int g, int b, int a = 255) noexcept Q_DECL_CONST_FUNCTION
258 {
259 return uint(r) <= 255 && uint(g) <= 255 && uint(b) <= 255 && uint(a) <= 255;
260 }
261
262 Spec cspec;
263 union CT {
264#ifdef Q_COMPILER_UNIFORM_INIT
265 CT() {} // doesn't init anything, thus can't be constexpr
266 Q_DECL_CONSTEXPR explicit CT(ushort a1, ushort a2, ushort a3, ushort a4, ushort a5) noexcept
267 : array{a1, a2, a3, a4, a5} {}
268#endif
269 struct {
270 ushort alpha;
271 ushort red;
272 ushort green;
273 ushort blue;
274 ushort pad;
275 } argb;
276 struct {
277 ushort alpha;
278 ushort hue;
279 ushort saturation;
280 ushort value;
281 ushort pad;
282 } ahsv;
283 struct {
284 ushort alpha;
285 ushort cyan;
286 ushort magenta;
287 ushort yellow;
288 ushort black;
289 } acmyk;
290 struct {
291 ushort alpha;
292 ushort hue;
293 ushort saturation;
294 ushort lightness;
295 ushort pad;
296 } ahsl;
297 struct {
298 ushort alphaF16;
299 ushort redF16;
300 ushort greenF16;
301 ushort blueF16;
302 ushort pad;
303 } argbExtended;
304 ushort array[5];
305 } ct;
306
307 friend class QColormap;
308#ifndef QT_NO_DATASTREAM
309 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
310 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
311#endif
312};
313Q_DECLARE_TYPEINFO(QColor, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE);
314
315inline QColor::QColor(QLatin1String aname)
316{ setNamedColor(aname); }
317
318inline QColor::QColor(QStringView aname)
319{ setNamedColor(aname); }
320
321#if QT_STRINGVIEW_LEVEL < 2
322inline QColor::QColor(const QString& aname)
323{ setNamedColor(aname); }
324#endif
325
326inline bool QColor::isValid() const noexcept
327{ return cspec != Invalid; }
328
329QT_END_NAMESPACE
330
331#endif // QCOLOR_H
332