QtGui/qcolor.h Source File

qcolor.h
Go to the documentation of this file.
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 
50 
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 
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 
305 
307 
308 #endif // QCOLOR_H
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLenum GLint GLint y
Definition: GLee.h:876
GLclampf green
Definition: GLee.h:870
#define QT_END_NAMESPACE
Definition: qglobal.h:128
ushort red
Definition: qcolor.h:243
GLdouble GLdouble z
Definition: GLee.h:1393
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
int screen
Definition: GLee.h:10533
ushort black
Definition: qcolor.h:260
ushort blue
Definition: qcolor.h:245
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QColor &)
Definition: qcolor.h:67
ushort pad
Definition: qcolor.h:246
QColor darker(int f=200) const
Definition: qcolor.h:301
ushort alpha
Definition: qcolor.h:242
QT_BEGIN_HEADER QT_BEGIN_NAMESPACE typedef unsigned int QRgb
Definition: qrgb.h:53
ushort value
Definition: qcolor.h:252
Definition: qdebug.h:62
QColor lighter(int f=150) const
Definition: qcolor.h:298
void setNamedColor(const QString &name)
QColor()
Definition: qcolor.h:279
bool operator==(const Attribute &cA, const AttributeInstance< type > &cB)
This operator compares the two attributes and NOT their values.
Definition: node.h:577
GlobalColor
Definition: qnamespace.h:104
AttributeInstance< Color > acolor
Definition: math.h:806
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
QColor light(int f=150) const
GLclampf GLclampf blue
Definition: GLee.h:870
GLubyte g
Definition: GLee.h:5404
GLclampf GLclampf GLclampf alpha
Definition: GLee.h:870
ushort lightness
Definition: qcolor.h:266
ushort hue
Definition: qcolor.h:250
ushort saturation
Definition: qcolor.h:251
GLenum GLint x
Definition: GLee.h:876
bool operator!=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:533
const GLdouble * v
Definition: GLee.h:1174
struct QColor::@88::@89 argb
GLubyte GLubyte b
Definition: GLee.h:5404
GLsizei const GLfloat * value
Definition: GLee.h:1742
const GLubyte * c
Definition: GLee.h:5419
int int int int int int h
Definition: GLee.h:10534
GLuint const GLchar * name
Definition: GLee.h:1704
ushort magenta
Definition: qcolor.h:258
Spec
Definition: qcolor.h:70
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &)
bool isValid() const
Definition: qcolor.h:295
ushort cyan
Definition: qcolor.h:257
GLdouble s
Definition: GLee.h:1173
ushort green
Definition: qcolor.h:244
ushort yellow
Definition: qcolor.h:259
#define QT_END_HEADER
Definition: qglobal.h:142
GLclampf f
Definition: GLee.h:9303
void setRgb(int r, int g, int b, int a=255)
QColor dark(int f=200) const
GLuint color
Definition: GLee.h:8456
Spec spec() const
Definition: qcolor.h:88