qcolor.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QCOLOR_H
00043 #define QCOLOR_H
00044 
00045 #include <QtGui/qrgb.h>
00046 #include <QtCore/qnamespace.h>
00047 #include <QtCore/qstringlist.h>
00048 
00049 QT_BEGIN_HEADER
00050 
00051 QT_BEGIN_NAMESPACE
00052 
00053 QT_MODULE(Gui)
00054 
00055 class QColor;
00056 class QColormap;
00057 class QVariant;
00058 
00059 #ifndef QT_NO_DEBUG_STREAM
00060 Q_GUI_EXPORT QDebug operator<<(QDebug, const QColor &);
00061 #endif
00062 #ifndef QT_NO_DATASTREAM
00063 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
00064 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
00065 #endif
00066 
00067 class Q_GUI_EXPORT QColor
00068 {
00069 public:
00070     enum Spec { Invalid, Rgb, Hsv, Cmyk, Hsl };
00071 
00072     QColor();
00073     QColor(Qt::GlobalColor color);
00074     QColor(int r, int g, int b, int a = 255);
00075     QColor(QRgb rgb);
00076     QColor(const QString& name);
00077     QColor(const char *name);
00078     QColor(const QColor &color);
00079     QColor(Spec spec);
00080 
00081     bool isValid() const;
00082 
00083     QString name() const;
00084     void setNamedColor(const QString& name);
00085 
00086     static QStringList colorNames();
00087 
00088     inline Spec spec() const
00089     { return cspec; }
00090 
00091     int alpha() const;
00092     void setAlpha(int alpha);
00093 
00094     qreal alphaF() const;
00095     void setAlphaF(qreal alpha);
00096 
00097     int red() const;
00098     int green() const;
00099     int blue() const;
00100     void setRed(int red);
00101     void setGreen(int green);
00102     void setBlue(int blue);
00103 
00104     qreal redF() const;
00105     qreal greenF() const;
00106     qreal blueF() const;
00107     void setRedF(qreal red);
00108     void setGreenF(qreal green);
00109     void setBlueF(qreal blue);
00110 
00111     void getRgb(int *r, int *g, int *b, int *a = 0) const;
00112     void setRgb(int r, int g, int b, int a = 255);
00113 
00114     void getRgbF(qreal *r, qreal *g, qreal *b, qreal *a = 0) const;
00115     void setRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
00116 
00117     QRgb rgba() const;
00118     void setRgba(QRgb rgba);
00119 
00120     QRgb rgb() const;
00121     void setRgb(QRgb rgb);
00122 
00123     int hue() const; // 0 <= hue < 360
00124     int saturation() const;
00125     int hsvHue() const; // 0 <= hue < 360
00126     int hsvSaturation() const;
00127     int value() const;
00128 
00129     qreal hueF() const; // 0.0 <= hueF < 360.0
00130     qreal saturationF() const;
00131     qreal hsvHueF() const; // 0.0 <= hueF < 360.0
00132     qreal hsvSaturationF() const;
00133     qreal valueF() const;
00134 
00135     void getHsv(int *h, int *s, int *v, int *a = 0) const;
00136     void setHsv(int h, int s, int v, int a = 255);
00137 
00138     void getHsvF(qreal *h, qreal *s, qreal *v, qreal *a = 0) const;
00139     void setHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
00140 
00141     int cyan() const;
00142     int magenta() const;
00143     int yellow() const;
00144     int black() const;
00145 
00146     qreal cyanF() const;
00147     qreal magentaF() const;
00148     qreal yellowF() const;
00149     qreal blackF() const;
00150 
00151     void getCmyk(int *c, int *m, int *y, int *k, int *a = 0);
00152     void setCmyk(int c, int m, int y, int k, int a = 255);
00153 
00154     void getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a = 0);
00155     void setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
00156 
00157     int hslHue() const; // 0 <= hue < 360
00158     int hslSaturation() const;
00159     int lightness() const;
00160 
00161     qreal hslHueF() const; // 0.0 <= hueF < 360.0
00162     qreal hslSaturationF() const;
00163     qreal lightnessF() const;
00164 
00165     void getHsl(int *h, int *s, int *l, int *a = 0) const;
00166     void setHsl(int h, int s, int l, int a = 255);
00167 
00168     void getHslF(qreal *h, qreal *s, qreal *l, qreal *a = 0) const;
00169     void setHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
00170 
00171     QColor toRgb() const;
00172     QColor toHsv() const;
00173     QColor toCmyk() const;
00174     QColor toHsl() const;
00175 
00176     QColor convertTo(Spec colorSpec) const;
00177 
00178     static QColor fromRgb(QRgb rgb);
00179     static QColor fromRgba(QRgb rgba);
00180 
00181     static QColor fromRgb(int r, int g, int b, int a = 255);
00182     static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0);
00183 
00184     static QColor fromHsv(int h, int s, int v, int a = 255);
00185     static QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0);
00186 
00187     static QColor fromCmyk(int c, int m, int y, int k, int a = 255);
00188     static QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0);
00189 
00190     static QColor fromHsl(int h, int s, int l, int a = 255);
00191     static QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0);
00192 
00193     QColor light(int f = 150) const;
00194     QColor lighter(int f = 150) const;
00195     QColor dark(int f = 200) const;
00196     QColor darker(int f = 200) const;
00197 
00198     QColor &operator=(const QColor &);
00199     QColor &operator=(Qt::GlobalColor color);
00200 
00201     bool operator==(const QColor &c) const;
00202     bool operator!=(const QColor &c) const;
00203 
00204     operator QVariant() const;
00205 
00206 #ifdef Q_WS_X11
00207     static bool allowX11ColorNames();
00208     static void setAllowX11ColorNames(bool enabled);
00209 #endif
00210 
00211 #ifdef QT3_SUPPORT
00212     inline QT3_SUPPORT_CONSTRUCTOR QColor(int x, int y, int z, Spec colorSpec)
00213     { if (colorSpec == Hsv) setHsv(x, y, z); else setRgb(x, y, z); }
00214 
00215     inline QT3_SUPPORT void rgb(int *r, int *g, int *b) const
00216     { getRgb(r, g, b); }
00217     inline QT3_SUPPORT void hsv(int *h, int *s, int *v) const
00218     { getHsv(h, s, v); }
00219 
00220     inline QT3_SUPPORT void setRgba(int r, int g, int b, int a)
00221     { setRgb(r, g, b, a); }
00222     inline QT3_SUPPORT void getRgba(int *r, int *g, int *b, int *a) const
00223     { getRgb(r, g, b, a); }
00224 
00225     QT3_SUPPORT uint pixel(int screen = -1) const;
00226 #endif
00227 
00228     static bool isValidColor(const QString &name);
00229 
00230 private:
00231 #ifndef QT3_SUPPORT
00232     // do not allow a spec to be used as an alpha value
00233     QColor(int, int, int, Spec);
00234 #endif
00235 
00236     void invalidate();
00237     bool setColorFromString(const QString &name);
00238 
00239     Spec cspec;
00240     union {
00241         struct {
00242             ushort alpha;
00243             ushort red;
00244             ushort green;
00245             ushort blue;
00246             ushort pad;
00247         } argb;
00248         struct {
00249             ushort alpha;
00250             ushort hue;
00251             ushort saturation;
00252             ushort value;
00253             ushort pad;
00254         } ahsv;
00255         struct {
00256             ushort alpha;
00257             ushort cyan;
00258             ushort magenta;
00259             ushort yellow;
00260             ushort black;
00261         } acmyk;
00262         struct {
00263             ushort alpha;
00264             ushort hue;
00265             ushort saturation;
00266             ushort lightness;
00267             ushort pad;
00268         } ahsl;
00269         ushort array[5];
00270     } ct;
00271 
00272     friend class QColormap;
00273 #ifndef QT_NO_DATASTREAM
00274     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QColor &);
00275     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QColor &);
00276 #endif
00277 };
00278 
00279 inline QColor::QColor()
00280 { invalidate(); }
00281 
00282 inline QColor::QColor(int r, int g, int b, int a)
00283 { setRgb(r, g, b, a); }
00284 
00285 inline QColor::QColor(const char *aname)
00286 { setNamedColor(QLatin1String(aname)); }
00287 
00288 inline QColor::QColor(const QString& aname)
00289 { setNamedColor(aname); }
00290 
00291 inline QColor::QColor(const QColor &acolor)
00292     : cspec(acolor.cspec)
00293 { ct.argb = acolor.ct.argb; }
00294 
00295 inline bool QColor::isValid() const
00296 { return cspec != Invalid; }
00297 
00298 inline QColor QColor::lighter(int f) const 
00299 { return light(f); }
00300 
00301 inline QColor QColor::darker(int f) const 
00302 { return dark(f); }
00303 
00304 QT_END_NAMESPACE
00305 
00306 QT_END_HEADER
00307 
00308 #endif // QCOLOR_H