Qt 4.8
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
61 #endif
62 #ifndef QT_NO_DATASTREAM
65 #endif
66 
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 
240  union {
241  struct {
247  } argb;
248  struct {
249  ushort alpha;
253  ushort pad;
254  } ahsv;
255  struct {
256  ushort alpha;
261  } acmyk;
262  struct {
263  ushort alpha;
264  ushort hue;
265  ushort saturation;
267  ushort pad;
268  } ahsl;
269  ushort array[5];
270  } ct;
271 
272  friend class QColormap;
273 #ifndef QT_NO_DATASTREAM
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
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
ushort pad
Definition: qcolor.h:246
unsigned int QRgb
Definition: qrgb.h:53
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
union QColor::@220 ct
ushort hue
Returns the hue color component of this color.
Definition: qcolor.h:250
#define QT_MODULE(x)
Definition: qglobal.h:2783
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
Spec
The type of color specified, either RGB, HSV, CMYK or HSL.
Definition: qcolor.h:70
Spec cspec
Definition: qcolor.h:239
ushort lightness
Returns the lightness color component of this color.
Definition: qcolor.h:266
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QColor &)
Definition: qcolor.cpp:2677
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
long ASN1_INTEGER_get ASN1_INTEGER * a
struct QColor::@220::@221 argb
bool operator!=(QBool b1, bool b2)
Definition: qglobal.h:2026
ushort red
Returns the red color component of this color.
Definition: qcolor.h:243
The QString class provides a Unicode character string.
Definition: qstring.h:83
GlobalColor
Definition: qnamespace.h:104
ushort yellow
Returns the yellow color component of this color.
Definition: qcolor.h:259
ushort magenta
Returns the magenta color component of this color.
Definition: qcolor.h:258
QColor darker(int f=200) const
Returns a darker (or lighter) color, but does not change this object.
Definition: qcolor.h:301
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
ushort value
Returns the value color component of this color.
Definition: qcolor.h:252
ushort saturation
Returns the saturation color component of this color.
Definition: qcolor.h:251
const char * name
QColor dark(int f=200) const
Use darker(factor) instead.
Definition: qcolor.cpp:2447
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
unsigned int uint
Definition: qglobal.h:996
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
ushort blue
Returns the blue color component of this color.
Definition: qcolor.h:245
ushort alpha
Returns the alpha color component of this color.
Definition: qcolor.h:242
unsigned short ushort
Definition: qglobal.h:995
static bool allowX11ColorNames
Definition: qcolor.cpp:55
QFactoryLoader * l
Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &)
Definition: qcolor.cpp:2602
ushort black
Returns the black color component of this color.
Definition: qcolor.h:260
QColor()
Constructs an invalid color with the RGB value (0, 0, 0).
Definition: qcolor.h:279
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
bool isValid() const
Returns true if the color is valid; otherwise returns false.
Definition: qcolor.h:295
QColor lighter(int f=150) const
Returns a lighter (or darker) color, but does not change this object.
Definition: qcolor.h:298
ushort cyan
Returns the cyan color component of this color.
Definition: qcolor.h:257
#define QT_END_HEADER
Definition: qglobal.h:137
QDataStream & operator<<(QDataStream &out, const QUrl &url)
Writes url url to the stream out and returns a reference to the stream.
Definition: qurl.cpp:6757
ushort green
Returns the green color component of this color.
Definition: qcolor.h:244
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
#define enabled
QColor light(int f=150) const
Use lighter(factor) instead.
Definition: qcolor.cpp:2391
Spec spec() const
Returns how the color was specified.
Definition: qcolor.h:88
QDataStream & operator>>(QDataStream &in, QUrl &url)
Reads a url into url from the stream in and returns a reference to the stream.
Definition: qurl.cpp:6774