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 | |
49 | QT_BEGIN_NAMESPACE |
50 | |
51 | |
52 | class QColor; |
53 | class QColormap; |
54 | class QVariant; |
55 | |
56 | #ifndef QT_NO_DEBUG_STREAM |
57 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &); |
58 | #endif |
59 | #ifndef QT_NO_DATASTREAM |
60 | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &); |
61 | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &); |
62 | #endif |
63 | |
64 | class Q_GUI_EXPORT QColor |
65 | { |
66 | public: |
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 = 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 = 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 = 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 = 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 = nullptr); // ### Qt 6: remove |
176 | void getCmyk(int *c, int *m, int *y, int *k, int *a = nullptr) const; |
177 | void setCmyk(int c, int m, int y, int k, int a = 255); |
178 | |
179 | void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = nullptr); // ### Qt 6: remove |
180 | void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = nullptr) const; |
181 | void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0); |
182 | |
183 | int hslHue() const Q_DECL_NOTHROW; // 0 <= hue < 360 |
184 | int hslSaturation() const Q_DECL_NOTHROW; |
185 | int lightness() const Q_DECL_NOTHROW; |
186 | |
187 | qreal hslHueF() const Q_DECL_NOTHROW; // 0.0 <= hueF < 360.0 |
188 | qreal hslSaturationF() const Q_DECL_NOTHROW; |
189 | qreal lightnessF() const Q_DECL_NOTHROW; |
190 | |
191 | void getHsl(int *h, int *s, int *l, int *a = nullptr) const; |
192 | void setHsl(int h, int s, int l, int a = 255); |
193 | |
194 | void getHslF(qreal *h, qreal *s, qreal *l, qreal *a = nullptr) const; |
195 | void setHslF(qreal h, qreal s, qreal l, qreal a = 1.0); |
196 | |
197 | QColor toRgb() const Q_DECL_NOTHROW; |
198 | QColor toHsv() const Q_DECL_NOTHROW; |
199 | QColor toCmyk() const Q_DECL_NOTHROW; |
200 | QColor toHsl() const Q_DECL_NOTHROW; |
201 | |
202 | Q_REQUIRED_RESULT QColor convertTo(Spec colorSpec) const Q_DECL_NOTHROW; |
203 | |
204 | static QColor fromRgb(QRgb rgb) Q_DECL_NOTHROW; |
205 | static QColor fromRgba(QRgb rgba) Q_DECL_NOTHROW; |
206 | |
207 | static QColor fromRgb(int r, int g, int b, int a = 255); |
208 | static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0); |
209 | |
210 | static QColor fromRgba64(ushort r, ushort g, ushort b, ushort a = USHRT_MAX) Q_DECL_NOTHROW; |
211 | static QColor fromRgba64(QRgba64 rgba) Q_DECL_NOTHROW; |
212 | |
213 | static QColor fromHsv(int h, int s, int v, int a = 255); |
214 | static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0); |
215 | |
216 | static QColor fromCmyk(int c, int m, int y, int k, int a = 255); |
217 | static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0); |
218 | |
219 | static QColor fromHsl(int h, int s, int l, int a = 255); |
220 | static QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0); |
221 | |
222 | #if QT_DEPRECATED_SINCE(5, 13) |
223 | QT_DEPRECATED_X("Use QColor::lighter() instead" ) |
224 | Q_REQUIRED_RESULT QColor light(int f = 150) const Q_DECL_NOTHROW; |
225 | QT_DEPRECATED_X("Use QColor::darker() instead" ) |
226 | Q_REQUIRED_RESULT QColor dark(int f = 200) const Q_DECL_NOTHROW; |
227 | #endif |
228 | Q_REQUIRED_RESULT QColor lighter(int f = 150) const Q_DECL_NOTHROW; |
229 | Q_REQUIRED_RESULT QColor darker(int f = 200) const Q_DECL_NOTHROW; |
230 | |
231 | bool operator==(const QColor &c) const Q_DECL_NOTHROW; |
232 | bool operator!=(const QColor &c) const Q_DECL_NOTHROW; |
233 | |
234 | operator QVariant() const; |
235 | |
236 | #if QT_STRINGVIEW_LEVEL < 2 |
237 | static bool isValidColor(const QString &name); |
238 | #endif |
239 | static bool isValidColor(QStringView) Q_DECL_NOTHROW; |
240 | static bool isValidColor(QLatin1String) Q_DECL_NOTHROW; |
241 | |
242 | private: |
243 | |
244 | void invalidate() Q_DECL_NOTHROW; |
245 | template <typename String> |
246 | bool setColorFromString(String name); |
247 | |
248 | Spec cspec; |
249 | union { |
250 | struct { |
251 | ushort alpha; |
252 | ushort red; |
253 | ushort green; |
254 | ushort blue; |
255 | ushort pad; |
256 | } argb; |
257 | struct { |
258 | ushort alpha; |
259 | ushort hue; |
260 | ushort saturation; |
261 | ushort value; |
262 | ushort pad; |
263 | } ahsv; |
264 | struct { |
265 | ushort alpha; |
266 | ushort cyan; |
267 | ushort magenta; |
268 | ushort yellow; |
269 | ushort black; |
270 | } acmyk; |
271 | struct { |
272 | ushort alpha; |
273 | ushort hue; |
274 | ushort saturation; |
275 | ushort lightness; |
276 | ushort pad; |
277 | } ahsl; |
278 | ushort array[5]; |
279 | } ct; |
280 | |
281 | friend class QColormap; |
282 | #ifndef QT_NO_DATASTREAM |
283 | friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &); |
284 | friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &); |
285 | #endif |
286 | }; |
287 | Q_DECLARE_TYPEINFO(QColor, QT_VERSION >= QT_VERSION_CHECK(6,0,0) ? Q_MOVABLE_TYPE : Q_RELOCATABLE_TYPE); |
288 | |
289 | inline QColor::QColor() Q_DECL_NOTHROW |
290 | { invalidate(); } |
291 | |
292 | inline QColor::QColor(int r, int g, int b, int a) |
293 | { setRgb(r, g, b, a); } |
294 | |
295 | inline QColor::QColor(QLatin1String aname) |
296 | { setNamedColor(aname); } |
297 | |
298 | inline QColor::QColor(QStringView aname) |
299 | { setNamedColor(aname); } |
300 | |
301 | #if QT_STRINGVIEW_LEVEL < 2 |
302 | inline QColor::QColor(const QString& aname) |
303 | { setNamedColor(aname); } |
304 | #endif |
305 | |
306 | #if QT_VERSION < QT_VERSION_CHECK(6,0,0) |
307 | inline QColor::QColor(const QColor &acolor) Q_DECL_NOTHROW |
308 | : cspec(acolor.cspec) |
309 | { ct.argb = acolor.ct.argb; } |
310 | #endif |
311 | |
312 | inline bool QColor::isValid() const Q_DECL_NOTHROW |
313 | { return cspec != Invalid; } |
314 | |
315 | QT_END_NAMESPACE |
316 | |
317 | #endif // QCOLOR_H |
318 | |