1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). |
4 | ** Contact: http://www.qt-project.org/legal |
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 Digia. For licensing terms and |
14 | ** conditions see http://qt.digia.com/licensing. For further information |
15 | ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
24 | ** |
25 | ** In addition, as a special exception, Digia gives you certain additional |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
28 | ** |
29 | ** GNU General Public License Usage |
30 | ** Alternatively, this file may be used under the terms of the GNU |
31 | ** General Public License version 3.0 as published by the Free Software |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the |
33 | ** packaging of this file. Please review the following information to |
34 | ** ensure the GNU General Public License version 3.0 requirements will be |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. |
36 | ** |
37 | ** |
38 | ** $QT_END_LICENSE$ |
39 | ** |
40 | ****************************************************************************/ |
41 | |
42 | #ifndef QCOLOR_H |
43 | #define QCOLOR_H |
44 | |
45 | #include <QtGui/qrgb.h> |
46 | #include <QtCore/qnamespace.h> |
47 | #include <QtCore/qstringlist.h> |
48 | |
49 | QT_BEGIN_HEADER |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | QT_MODULE(Gui) |
54 | |
55 | class QColor; |
56 | class QColormap; |
57 | class QVariant; |
58 | |
59 | #ifndef QT_NO_DEBUG_STREAM |
60 | Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &); |
61 | #endif |
62 | #ifndef QT_NO_DATASTREAM |
63 | Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &); |
64 | Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &); |
65 | #endif |
66 | |
67 | class Q_GUI_EXPORT QColor |
68 | { |
69 | public: |
70 | enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl }; |
71 | |
72 | QColor(); |
73 | QColor(Qt::GlobalColor color); |
74 | QColor(int r, int g, int b, int a = 255); |
75 | QColor(QRgb rgb); |
76 | QColor(const QString& name); |
77 | QColor(const char *name); |
78 | QColor(const QColor &color); |
79 | QColor(Spec spec); |
80 | |
81 | bool isValid() const; |
82 | |
83 | QString name() const; |
84 | void setNamedColor(const QString& name); |
85 | |
86 | static QStringList colorNames(); |
87 | |
88 | inline Spec spec() const |
89 | { return cspec; } |
90 | |
91 | int alpha() const; |
92 | void setAlpha(int alpha); |
93 | |
94 | qreal alphaF() const; |
95 | void setAlphaF(qreal alpha); |
96 | |
97 | int red() const; |
98 | int green() const; |
99 | int blue() const; |
100 | void setRed(int red); |
101 | void setGreen(int green); |
102 | void setBlue(int blue); |
103 | |
104 | qreal redF() const; |
105 | qreal greenF() const; |
106 | qreal blueF() const; |
107 | void setRedF(qreal red); |
108 | void setGreenF(qreal green); |
109 | void setBlueF(qreal blue); |
110 | |
111 | void getRgb(int *r, int *g, int *b, int *a = 0) const; |
112 | void setRgb(int r, int g, int b, int a = 255); |
113 | |
114 | void getRgbF(qreal *r, qreal *g, qreal *b, qreal *a = 0) const; |
115 | void setRgbF(qreal r, qreal g, qreal b, qreal a = 1.0); |
116 | |
117 | QRgb rgba() const; |
118 | void setRgba(QRgb rgba); |
119 | |
120 | QRgb rgb() const; |
121 | void setRgb(QRgb rgb); |
122 | |
123 | int hue() const; // 0 <= hue < 360 |
124 | int saturation() const; |
125 | int hsvHue() const; // 0 <= hue < 360 |
126 | int hsvSaturation() const; |
127 | int value() const; |
128 | |
129 | qreal hueF() const; // 0.0 <= hueF < 360.0 |
130 | qreal saturationF() const; |
131 | qreal hsvHueF() const; // 0.0 <= hueF < 360.0 |
132 | qreal hsvSaturationF() const; |
133 | qreal valueF() const; |
134 | |
135 | void getHsv(int *h, int *s, int *v, int *a = 0) const; |
136 | void setHsv(int h, int s, int v, int a = 255); |
137 | |
138 | void getHsvF(qreal *h, qreal *s, qreal *v, qreal *a = 0) const; |
139 | void setHsvF(qreal h, qreal s, qreal v, qreal a = 1.0); |
140 | |
141 | int cyan() const; |
142 | int magenta() const; |
143 | int yellow() const; |
144 | int black() const; |
145 | |
146 | qreal cyanF() const; |
147 | qreal magentaF() const; |
148 | qreal yellowF() const; |
149 | qreal blackF() const; |
150 | |
151 | void getCmyk(int *c, int *m, int *y, int *k, int *a = 0); |
152 | void setCmyk(int c, int m, int y, int k, int a = 255); |
153 | |
154 | void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = 0); |
155 | void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0); |
156 | |
157 | int hslHue() const; // 0 <= hue < 360 |
158 | int hslSaturation() const; |
159 | int lightness() const; |
160 | |
161 | qreal hslHueF() const; // 0.0 <= hueF < 360.0 |
162 | qreal hslSaturationF() const; |
163 | qreal lightnessF() const; |
164 | |
165 | void getHsl(int *h, int *s, int *l, int *a = 0) const; |
166 | void setHsl(int h, int s, int l, int a = 255); |
167 | |
168 | void getHslF(qreal *h, qreal *s, qreal *l, qreal *a = 0) const; |
169 | void setHslF(qreal h, qreal s, qreal l, qreal a = 1.0); |
170 | |
171 | QColor toRgb() const; |
172 | QColor toHsv() const; |
173 | QColor toCmyk() const; |
174 | QColor toHsl() const; |
175 | |
176 | QColor convertTo(Spec colorSpec) const; |
177 | |
178 | static QColor fromRgb(QRgb rgb); |
179 | static QColor fromRgba(QRgb rgba); |
180 | |
181 | static QColor fromRgb(int r, int g, int b, int a = 255); |
182 | static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0); |
183 | |
184 | static QColor fromHsv(int h, int s, int v, int a = 255); |
185 | static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0); |
186 | |
187 | static QColor fromCmyk(int c, int m, int y, int k, int a = 255); |
188 | static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0); |
189 | |
190 | static QColor fromHsl(int h, int s, int l, int a = 255); |
191 | static QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0); |
192 | |
193 | QColor light(int f = 150) const; |
194 | QColor lighter(int f = 150) const; |
195 | QColor dark(int f = 200) const; |
196 | QColor darker(int f = 200) const; |
197 | |
198 | QColor &operator=(const QColor &); |
199 | QColor &operator=(Qt::GlobalColor color); |
200 | |
201 | bool operator==(const QColor &c) const; |
202 | bool operator!=(const QColor &c) const; |
203 | |
204 | operator QVariant() const; |
205 | |
206 | #ifdef Q_WS_X11 |
207 | static bool allowX11ColorNames(); |
208 | static void setAllowX11ColorNames(bool enabled); |
209 | #endif |
210 | |
211 | #ifdef QT3_SUPPORT |
212 | inline QT3_SUPPORT_CONSTRUCTOR QColor(int x, int y, int z, Spec colorSpec) |
213 | { if (colorSpec == Hsv) setHsv(x, y, z); else setRgb(x, y, z); } |
214 | |
215 | inline QT3_SUPPORT void rgb(int *r, int *g, int *b) const |
216 | { getRgb(r, g, b); } |
217 | inline QT3_SUPPORT void hsv(int *h, int *s, int *v) const |
218 | { getHsv(h, s, v); } |
219 | |
220 | inline QT3_SUPPORT void setRgba(int r, int g, int b, int a) |
221 | { setRgb(r, g, b, a); } |
222 | inline QT3_SUPPORT void getRgba(int *r, int *g, int *b, int *a) const |
223 | { getRgb(r, g, b, a); } |
224 | |
225 | QT3_SUPPORT uint pixel(int screen = -1) const; |
226 | #endif |
227 | |
228 | static bool isValidColor(const QString &name); |
229 | |
230 | private: |
231 | #ifndef QT3_SUPPORT |
232 | // do not allow a spec to be used as an alpha value |
233 | QColor(int, int, int, Spec); |
234 | #endif |
235 | |
236 | void invalidate(); |
237 | bool setColorFromString(const QString &name); |
238 | |
239 | Spec cspec; |
240 | union { |
241 | struct { |
242 | ushort alpha; |
243 | ushort red; |
244 | ushort green; |
245 | ushort blue; |
246 | ushort pad; |
247 | } argb; |
248 | struct { |
249 | ushort alpha; |
250 | ushort hue; |
251 | ushort saturation; |
252 | ushort value; |
253 | ushort pad; |
254 | } ahsv; |
255 | struct { |
256 | ushort alpha; |
257 | ushort cyan; |
258 | ushort magenta; |
259 | ushort yellow; |
260 | ushort black; |
261 | } acmyk; |
262 | struct { |
263 | ushort alpha; |
264 | ushort hue; |
265 | ushort saturation; |
266 | ushort lightness; |
267 | ushort pad; |
268 | } ahsl; |
269 | ushort array[5]; |
270 | } ct; |
271 | |
272 | friend class QColormap; |
273 | #ifndef QT_NO_DATASTREAM |
274 | friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &); |
275 | friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &); |
276 | #endif |
277 | }; |
278 | |
279 | inline QColor::QColor() |
280 | { invalidate(); } |
281 | |
282 | inline QColor::QColor(int r, int g, int b, int a) |
283 | { setRgb(r, g, b, a); } |
284 | |
285 | inline QColor::QColor(const char *aname) |
286 | { setNamedColor(QLatin1String(aname)); } |
287 | |
288 | inline QColor::QColor(const QString& aname) |
289 | { setNamedColor(aname); } |
290 | |
291 | inline QColor::QColor(const QColor &acolor) |
292 | : cspec(acolor.cspec) |
293 | { ct.argb = acolor.ct.argb; } |
294 | |
295 | inline bool QColor::isValid() const |
296 | { return cspec != Invalid; } |
297 | |
298 | inline QColor QColor::lighter(int f) const |
299 | { return light(f); } |
300 | |
301 | inline QColor QColor::darker(int f) const |
302 | { return dark(f); } |
303 | |
304 | QT_END_NAMESPACE |
305 | |
306 | QT_END_HEADER |
307 | |
308 | #endif // QCOLOR_H |
309 | |