1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
45 #include <QtGui/qrgb.h>
46 #include <QtCore/qnamespace.h>
47 #include <QtCore/qstringlist.h>
59 #ifndef QT_NO_DEBUG_STREAM
60 Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &);
62 #ifndef QT_NO_DATASTREAM
63 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
64 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
67 class Q_GUI_EXPORT QColor
70 enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl };
73 QColor(Qt::GlobalColor color);
74 QColor(int r, int g, int b, int a = 255);
76 QColor(const QString& name);
77 QColor(const char *name);
78 QColor(const QColor &color);
84 void setNamedColor(const QString& name);
86 static QStringList colorNames();
88 inline Spec spec() const
92 void setAlpha(int alpha);
95 void setAlphaF(qreal alpha);
100 void setRed(int red);
101 void setGreen(int green);
102 void setBlue(int blue);
105 qreal greenF() const;
107 void setRedF(qreal red);
108 void setGreenF(qreal green);
109 void setBlueF(qreal blue);
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);
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);
118 void setRgba(QRgb rgba);
121 void setRgb(QRgb rgb);
123 int hue() const; // 0 <= hue < 360
124 int saturation() const;
125 int hsvHue() const; // 0 <= hue < 360
126 int hsvSaturation() const;
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;
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);
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);
147 qreal magentaF() const;
148 qreal yellowF() const;
149 qreal blackF() const;
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);
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);
157 int hslHue() const; // 0 <= hue < 360
158 int hslSaturation() const;
159 int lightness() const;
161 qreal hslHueF() const; // 0.0 <= hueF < 360.0
162 qreal hslSaturationF() const;
163 qreal lightnessF() const;
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);
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);
171 QColor toRgb() const;
172 QColor toHsv() const;
173 QColor toCmyk() const;
174 QColor toHsl() const;
176 QColor convertTo(Spec colorSpec) const;
178 static QColor fromRgb(QRgb rgb);
179 static QColor fromRgba(QRgb rgba);
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);
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);
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);
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);
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;
198 QColor &operator=(const QColor &);
199 QColor &operator=(Qt::GlobalColor color);
201 bool operator==(const QColor &c) const;
202 bool operator!=(const QColor &c) const;
204 operator QVariant() const;
207 static bool allowX11ColorNames();
208 static void setAllowX11ColorNames(bool enabled);
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); }
215 inline QT3_SUPPORT void rgb(int *r, int *g, int *b) const
217 inline QT3_SUPPORT void hsv(int *h, int *s, int *v) const
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); }
225 QT3_SUPPORT uint pixel(int screen = -1) const;
228 static bool isValidColor(const QString &name);
232 // do not allow a spec to be used as an alpha value
233 QColor(int, int, int, Spec);
237 bool setColorFromString(const QString &name);
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 &);
279 inline QColor::QColor()
282 inline QColor::QColor(int r, int g, int b, int a)
283 { setRgb(r, g, b, a); }
285 inline QColor::QColor(const char *aname)
286 { setNamedColor(QLatin1String(aname)); }
288 inline QColor::QColor(const QString& aname)
289 { setNamedColor(aname); }
291 inline QColor::QColor(const QColor &acolor)
292 : cspec(acolor.cspec)
293 { ct.argb = acolor.ct.argb; }
295 inline bool QColor::isValid() const
296 { return cspec != Invalid; }
298 inline QColor QColor::lighter(int f) const
301 inline QColor QColor::darker(int f) const