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 };
68 enum NameFormat { HexRgb, HexArgb };
69
70 inline QColor() Q_DECL_NOTHROW;
71 QColor(Qt::GlobalColor color) Q_DECL_NOTHROW;
72 inline QColor(int r, int g, int b, int a = 255);
73 QColor(QRgb rgb) Q_DECL_NOTHROW;
74 QColor(QRgba64 rgba64) Q_DECL_NOTHROW;
75#if QT_STRINGVIEW_LEVEL < 2
76 inline QColor(const QString& name);
77#endif
78 explicit inline QColor(QStringView name);
79 inline QColor(const char *aname) : QColor(QLatin1String(aname)) {}
80 inline QColor(QLatin1String name);
81 QColor(Spec spec) Q_DECL_NOTHROW;
82
83#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
84 inline QColor(const QColor &color) Q_DECL_NOTHROW; // ### Qt 6: remove all of these, the trivial ones are fine.
85# ifdef Q_COMPILER_RVALUE_REFS
86 QColor(QColor &&other) Q_DECL_NOTHROW : cspec(other.cspec), ct(other.ct) {}
87 QColor &operator=(QColor &&other) Q_DECL_NOTHROW
88 { cspec = other.cspec; ct = other.ct; return *this; }
89# endif
90 QColor &operator=(const QColor &) Q_DECL_NOTHROW;
91#endif // Qt < 6
92
93 QColor &operator=(Qt::GlobalColor color) Q_DECL_NOTHROW;
94
95 bool isValid() const Q_DECL_NOTHROW;
96
97 // ### Qt 6: merge overloads
98 QString name() const;
99 QString name(NameFormat format) const;
100
101#if QT_STRINGVIEW_LEVEL < 2
102 void setNamedColor(const QString& name);
103#endif
104 void setNamedColor(QStringView name);
105 void setNamedColor(QLatin1String name);
106
107 static QStringList colorNames();
108
109 inline Spec spec() const Q_DECL_NOTHROW
110 { return cspec; }
111
112 int alpha() const Q_DECL_NOTHROW;
113 void setAlpha(int alpha);
114
115 qreal alphaF() const Q_DECL_NOTHROW;
116 void setAlphaF(qreal alpha);
117
118 int red() const Q_DECL_NOTHROW;
119 int green() const Q_DECL_NOTHROW;
120 int blue() const Q_DECL_NOTHROW;
121 void setRed(int red);
122 void setGreen(int green);
123 void setBlue(int blue);
124
125 qreal redF() const Q_DECL_NOTHROW;
126 qreal greenF() const Q_DECL_NOTHROW;
127 qreal blueF() const Q_DECL_NOTHROW;
128 void setRedF(qreal red);
129 void setGreenF(qreal green);
130 void setBlueF(qreal blue);
131
132 void getRgb(int *r, int *g, int *b, int *a = Q_NULLPTR) const;
133 void setRgb(int r, int g, int b, int a = 255);
134
135 void getRgbF(qreal *r, qreal *g, qreal *b, qreal *a = Q_NULLPTR) const;
136 void setRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
137
138 QRgba64 rgba64() const Q_DECL_NOTHROW;
139 void setRgba64(QRgba64 rgba) Q_DECL_NOTHROW;
140
141 QRgb rgba() const Q_DECL_NOTHROW;
142 void setRgba(QRgb rgba) Q_DECL_NOTHROW;
143
144 QRgb rgb() const Q_DECL_NOTHROW;
145 void setRgb(QRgb rgb) Q_DECL_NOTHROW;
146
147 int hue() const Q_DECL_NOTHROW; // 0 <= hue < 360
148 int saturation() const Q_DECL_NOTHROW;
149 int hsvHue() const Q_DECL_NOTHROW; // 0 <= hue < 360
150 int hsvSaturation() const Q_DECL_NOTHROW;
151 int value() const Q_DECL_NOTHROW;
152
153 qreal hueF() const Q_DECL_NOTHROW; // 0.0 <= hueF < 360.0
154 qreal saturationF() const Q_DECL_NOTHROW;
155 qreal hsvHueF() const Q_DECL_NOTHROW; // 0.0 <= hueF < 360.0
156 qreal hsvSaturationF() const Q_DECL_NOTHROW;
157 qreal valueF() const Q_DECL_NOTHROW;
158
159 void getHsv(int *h, int *s, int *v, int *a = Q_NULLPTR) const;
160 void setHsv(int h, int s, int v, int a = 255);
161
162 void getHsvF(qreal *h, qreal *s, qreal *v, qreal *a = Q_NULLPTR) const;
163 void setHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
164
165 int cyan() const Q_DECL_NOTHROW;
166 int magenta() const Q_DECL_NOTHROW;
167 int yellow() const Q_DECL_NOTHROW;
168 int black() const Q_DECL_NOTHROW;
169
170 qreal cyanF() const Q_DECL_NOTHROW;
171 qreal magentaF() const Q_DECL_NOTHROW;
172 qreal yellowF() const Q_DECL_NOTHROW;
173 qreal blackF() const Q_DECL_NOTHROW;
174
175 void getCmyk(int *c, int *m, int *y, int *k, int *a = Q_NULLPTR);
176 void setCmyk(int c, int m, int y, int k, int a = 255);
177
178 void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = Q_NULLPTR);
179 void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
180
181 int hslHue() const Q_DECL_NOTHROW; // 0 <= hue < 360
182 int hslSaturation() const Q_DECL_NOTHROW;
183 int lightness() const Q_DECL_NOTHROW;
184
185 qreal hslHueF() const Q_DECL_NOTHROW; // 0.0 <= hueF < 360.0
186 qreal hslSaturationF() const Q_DECL_NOTHROW;
187 qreal lightnessF() const Q_DECL_NOTHROW;
188
189 void getHsl(int *h, int *s, int *l, int *a = Q_NULLPTR) const;
190 void setHsl(int h, int s, int l, int a = 255);
191
192 void getHslF(qreal *h, qreal *s, qreal *l, qreal *a = Q_NULLPTR) const;
193 void setHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
194
195 QColor toRgb() const Q_DECL_NOTHROW;
196 QColor toHsv() const Q_DECL_NOTHROW;
197 QColor toCmyk() const Q_DECL_NOTHROW;
198 QColor toHsl() const Q_DECL_NOTHROW;
199
200 Q_REQUIRED_RESULT QColor convertTo(Spec colorSpec) const Q_DECL_NOTHROW;
201
202 static QColor fromRgb(QRgb rgb) Q_DECL_NOTHROW;
203 static QColor fromRgba(QRgb rgba) Q_DECL_NOTHROW;
204
205 static QColor fromRgb(int r, int g, int b, int a = 255);
206 static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
207
208 static QColor fromRgba64(ushort r, ushort g, ushort b, ushort a = USHRT_MAX) Q_DECL_NOTHROW;
209 static QColor fromRgba64(QRgba64 rgba) Q_DECL_NOTHROW;
210
211 static QColor fromHsv(int h, int s, int v, int a = 255);
212 static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
213
214 static QColor fromCmyk(int c, int m, int y, int k, int a = 255);
215 static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
216
217 static QColor fromHsl(int h, int s, int l, int a = 255);
218 static QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
219
220 Q_REQUIRED_RESULT QColor light(int f = 150) const Q_DECL_NOTHROW;
221 Q_REQUIRED_RESULT QColor lighter(int f = 150) const Q_DECL_NOTHROW;
222 Q_REQUIRED_RESULT QColor dark(int f = 200) const Q_DECL_NOTHROW;
223 Q_REQUIRED_RESULT QColor darker(int f = 200) const Q_DECL_NOTHROW;
224
225 bool operator==(const QColor &c) const Q_DECL_NOTHROW;
226 bool operator!=(const QColor &c) const Q_DECL_NOTHROW;
227
228 operator QVariant() const;
229
230#if QT_STRINGVIEW_LEVEL < 2
231 static bool isValidColor(const QString &name);
232#endif
233 static bool isValidColor(QStringView) Q_DECL_NOTHROW;
234 static bool isValidColor(QLatin1String) Q_DECL_NOTHROW;
235
236private:
237
238 void invalidate() Q_DECL_NOTHROW;
239 template <typename String>
240 bool setColorFromString(String name);
241
242 Spec cspec;
243 union {
244 struct {
245 ushort alpha;
246 ushort red;
247 ushort green;
248 ushort blue;
249 ushort pad;
250 } argb;
251 struct {
252 ushort alpha;
253 ushort hue;
254 ushort saturation;
255 ushort value;
256 ushort pad;
257 } ahsv;
258 struct {
259 ushort alpha;
260 ushort cyan;
261 ushort magenta;
262 ushort yellow;
263 ushort black;
264 } acmyk;
265 struct {
266 ushort alpha;
267 ushort hue;
268 ushort saturation;
269 ushort lightness;
270 ushort pad;
271 } ahsl;
272 ushort array[5];
273 } ct;
274
275 friend class QColormap;
276#ifndef QT_NO_DATASTREAM
277 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
278 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
279#endif
280};
281Q_DECLARE_TYPEINFO(QColor, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE);
282
283inline QColor::QColor() Q_DECL_NOTHROW
284{ invalidate(); }
285
286inline QColor::QColor(int r, int g, int b, int a)
287{ setRgb(r, g, b, a); }
288
289inline QColor::QColor(QLatin1String aname)
290{ setNamedColor(aname); }
291
292inline QColor::QColor(QStringView aname)
293{ setNamedColor(aname); }
294
295#if QT_STRINGVIEW_LEVEL < 2
296inline QColor::QColor(const QString& aname)
297{ setNamedColor(aname); }
298#endif
299
300#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
301inline QColor::QColor(const QColor &acolor) Q_DECL_NOTHROW
302 : cspec(acolor.cspec)
303{ ct.argb = acolor.ct.argb; }
304#endif
305
306inline bool QColor::isValid() const Q_DECL_NOTHROW
307{ return cspec != Invalid; }
308
309inline QColor QColor::lighter(int f) const Q_DECL_NOTHROW
310{ return light(f); }
311
312inline QColor QColor::darker(int f) const Q_DECL_NOTHROW
313{ return dark(f); }
314
315QT_END_NAMESPACE
316
317#endif // QCOLOR_H
318