Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qcolor.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qcolor.h"
43 #include "qcolor_p.h"
44 #include "qnamespace.h"
45 #include "qdatastream.h"
46 #include "qvariant.h"
47 #include "qdebug.h"
48
49 #include <math.h>
50 #include <stdio.h>
51 #include <limits.h>
52
53 QT_BEGIN_NAMESPACE
54
55 /*!
56     \class QColor
57     \brief The QColor class provides colors based on RGB, HSV or CMYK values.
58
59     \ingroup painting
60     \ingroup appearance
61
62
63     A color is normally specified in terms of RGB (red, green, and
64     blue) components, but it is also possible to specify it in terms
65     of HSV (hue, saturation, and value) and CMYK (cyan, magenta,
66     yellow and black) components. In addition a color can be specified
67     using a color name. The color name can be any of the SVG 1.0 color
68     names.
69
70     \table
71     \header
72     \li RGB \li HSV \li CMYK
73     \row
74     \li \inlineimage qcolor-rgb.png
75     \li \inlineimage qcolor-hsv.png
76     \li \inlineimage qcolor-cmyk.png
77     \endtable
78
79     The QColor constructor creates the color based on RGB values.  To
80     create a QColor based on either HSV or CMYK values, use the
81     toHsv() and toCmyk() functions respectively. These functions
82     return a copy of the color using the desired format. In addition
83     the static fromRgb(), fromHsv() and fromCmyk() functions create
84     colors from the specified values. Alternatively, a color can be
85     converted to any of the three formats using the convertTo()
86     function (returning a copy of the color in the desired format), or
87     any of the setRgb(), setHsv() and setCmyk() functions altering \e
88     this color's format. The spec() function tells how the color was
89     specified.
90
91     A color can be set by passing an RGB string (such as "#112233"),
92     or a color name (such as "blue"), to the setNamedColor() function.
93     The color names are taken from the SVG 1.0 color names. The name()
94     function returns the name of the color in the format
95     "#RRGGBB". Colors can also be set using setRgb(), setHsv() and
96     setCmyk(). To get a lighter or darker color use the lighter() and
97     darker() functions respectively.
98
99     The isValid() function indicates whether a QColor is legal at
100     all. For example, a RGB color with RGB values out of range is
101     illegal. For performance reasons, QColor mostly disregards illegal
102     colors, and for that reason, the result of using an invalid color
103     is undefined.
104
105     The color components can be retrieved individually, e.g with
106     red(), hue() and cyan(). The values of the color components can
107     also be retrieved in one go using the getRgb(), getHsv() and
108     getCmyk() functions. Using the RGB color model, the color
109     components can in addition be accessed with rgb().
110
111     There are several related non-members: QRgb is a typdef for an
112     unsigned int representing the RGB value triplet (r, g, b). Note
113     that it also can hold a value for the alpha-channel (for more
114     information, see the \l {QColor#Alpha-Blended
115     Drawing}{Alpha-Blended Drawing} section). The qRed(), qBlue() and
116     qGreen() functions return the respective component of the given
117     QRgb value, while the qRgb() and qRgba() functions create and
118     return the QRgb triplet based on the given component
119     values. Finally, the qAlpha() function returns the alpha component
120     of the provided QRgb, and the qGray() function calculates and
121     return a gray value based on the given value.
122
123     QColor is platform and device independent. The QColormap class
124     maps the color to the hardware.
125
126     For more information about painting in general, see the \l{Paint
127     System} documentation.
128
129     \tableofcontents
130
131     \section1 Integer vs. Floating Point Precision
132
133     QColor supports floating point precision and provides floating
134     point versions of all the color components functions,
135     e.g. getRgbF(), hueF() and fromCmykF(). Note that since the
136     components are stored using 16-bit integers, there might be minor
137     deviations between the values set using, for example, setRgbF()
138     and the values returned by the getRgbF() function due to rounding.
139
140     While the integer based functions take values in the range 0-255
141     (except hue() which must have values within the range 0-359),
142     the floating point functions accept values in the range 0.0 - 1.0.
143
144     \section1 Alpha-Blended Drawing
145
146     QColor also support alpha-blended outlining and filling. The
147     alpha channel of a color specifies the transparency effect, 0
148     represents a fully transparent color, while 255 represents a fully
149     opaque color. For example:
150
151     \snippet code/src_gui_painting_qcolor.cpp 0
152
153     The code above produces the following output:
154
155     \img alphafill.png
156
157     The alpha channel of a color can be retrieved and set using the
158     alpha() and setAlpha() functions if its value is an integer, and
159     alphaF() and setAlphaF() if its value is qreal (double). By
160     default, the alpha-channel is set to 255 (opaque). To retrieve and
161     set \e all the RGB color components (including the alpha-channel)
162     in one go, use the rgba() and setRgba() functions.
163
164     \section1 Predefined Colors
165
166     There are 20 predefined QColors described by the Qt::GlobalColor enum,
167     including black, white, primary and secondary colors, darker versions
168     of these colors and three shades of gray. QColor also recognizes a
169     variety of color names; the static colorNames() function returns a
170     QStringList color names that QColor knows about.
171
172     \img qt-colors.png Qt Colors
173
174     Additionally, the Qt::color0, Qt::color1 and Qt::transparent colors
175     are used for special purposes.
176
177     Qt::color0 (zero pixel value) and Qt::color1 (non-zero pixel value)
178     are special colors for drawing in QBitmaps. Painting with Qt::color0
179     sets the bitmap bits to 0 (transparent; i.e., background), and painting
180     with Qt::color1 sets the bits to 1 (opaque; i.e., foreground).
181
182     Qt::transparent is used to indicate a transparent pixel. When painting
183     with this value, a pixel value will be used that is appropriate for the
184     underlying pixel format in use.
185
186     \section1 The HSV Color Model
187
188     The RGB model is hardware-oriented. Its representation is close to
189     what most monitors show. In contrast, HSV represents color in a way
190     more suited to the human perception of color. For example, the
191     relationships "stronger than", "darker than", and "the opposite of"
192     are easily expressed in HSV but are much harder to express in RGB.
193
194     HSV, like RGB, has three components:
195
196     \list
197     \li H, for hue, is in the range 0 to 359 if the color is chromatic (not
198     gray), or meaningless if it is gray. It represents degrees on the
199     color wheel familiar to most people. Red is 0 (degrees), green is
200     120, and blue is 240.
201
202     \inlineimage qcolor-hue.png
203
204     \li S, for saturation, is in the range 0 to 255, and the bigger it is,
205     the stronger the color is. Grayish colors have saturation near 0; very
206     strong colors have saturation near 255.
207
208     \inlineimage qcolor-saturation.png
209
210     \li V, for value, is in the range 0 to 255 and represents lightness or
211     brightness of the color. 0 is black; 255 is as far from black as
212     possible.
213
214     \inlineimage qcolor-value.png
215     \endlist
216
217     Here are some examples: pure red is H=0, S=255, V=255; a dark red,
218     moving slightly towards the magenta, could be H=350 (equivalent to
219     -10), S=255, V=180; a grayish light red could have H about 0 (say
220     350-359 or 0-10), S about 50-100, and S=255.
221
222     Qt returns a hue value of -1 for achromatic colors. If you pass a
223     hue value that is too large, Qt forces it into range. Hue 360 or 720 is
224     treated as 0; hue 540 is treated as 180.
225
226     In addition to the standard HSV model, Qt provides an
227     alpha-channel to feature \l {QColor#Alpha-Blended
228     Drawing}{alpha-blended drawing}.
229
230     \section1 The HSL Color Model
231
232     HSL is similar to HSV. Instead of value parameter from HSV,
233     HSL has the lightness parameter.
234     The lightness parameter goes from black to color and from color to white.
235     If you go outside at the night its black or dark gray. At day its colorful but
236     if you look in a really strong light a things they are going to white and
237     wash out.
238
239     \section1 The CMYK Color Model
240
241     While the RGB and HSV color models are used for display on
242     computer monitors, the CMYK model is used in the four-color
243     printing process of printing presses and some hard-copy
244     devices.
245
246     CMYK has four components, all in the range 0-255: cyan (C),
247     magenta (M), yellow (Y) and black (K).  Cyan, magenta and yellow
248     are called subtractive colors; the CMYK color model creates color
249     by starting with a white surface and then subtracting color by
250     applying the appropriate components. While combining cyan, magenta
251     and yellow gives the color black, subtracting one or more will
252     yield any other color. When combined in various percentages, these
253     three colors can create the entire spectrum of colors.
254
255     Mixing 100 percent of cyan, magenta and yellow \e does produce
256     black, but the result is unsatisfactory since it wastes ink,
257     increases drying time, and gives a muddy colour when printing. For
258     that reason, black is added in professional printing to provide a
259     solid black tone; hence the term 'four color process'.
260
261     In addition to the standard CMYK model, Qt provides an
262     alpha-channel to feature \l {QColor#Alpha-Blended
263     Drawing}{alpha-blended drawing}.
264
265     \sa QPalette, QBrush, QApplication::setColorSpec()
266 */
267
268 #define QCOLOR_INT_RANGE_CHECK(fn, var) \
269     do { \
270         if (var < 0 || var > 255) { \
271             qWarning(#fn": invalid value %d", var); \
272             var = qMax(0, qMin(var, 255)); \
273         } \
274     } while (0)
275
276 #define QCOLOR_REAL_RANGE_CHECK(fn, var) \
277     do { \
278         if (var < qreal(0.0) || var > qreal(1.0)) { \
279             qWarning(#fn": invalid value %g", var); \
280             var = qMax(qreal(0.0), qMin(var, qreal(1.0)));      \
281         } \
282     } while (0)
283
284 /*****************************************************************************
285   QColor member functions
286  *****************************************************************************/
287
288 /*!
289     \enum QColor::Spec
290
291     The type of color specified, either RGB, HSV, CMYK or HSL.
292
293     \value Rgb
294     \value Hsv
295     \value Cmyk
296     \value Hsl
297     \value Invalid
298
299     \sa spec(), convertTo()
300 */
301
302 /*!
303     \fn Spec QColor::spec() const
304
305     Returns how the color was specified.
306
307     \sa Spec, convertTo()
308 */
309
310
311 /*!
312     \fn QColor::QColor()
313
314     Constructs an invalid color with the RGB value (0, 0, 0). An
315     invalid color is a color that is not properly set up for the
316     underlying window system.
317
318     The alpha value of an invalid color is unspecified.
319
320     \sa isValid()
321 */
322
323 /*!
324     \overload
325
326     Constructs a new color with a color value of \a color.
327
328     \sa isValid(), {QColor#Predefined Colors}{Predefined Colors}
329  */
330 QColor::QColor(Qt::GlobalColor color)
331 {
332 #define QRGB(r, g, b) \
333     QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
334 #define QRGBA(r, g, b, a) \
335     QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
336
337     static const QRgb global_colors[] = {
338         QRGB(255, 255, 255), // Qt::color0
339         QRGB(  0,   0,   0), // Qt::color1
340         QRGB(  0,   0,   0), // black
341         QRGB(255, 255, 255), // white
342         /*
343          * From the "The Palette Manager: How and Why" by Ron Gery,
344          * March 23, 1992, archived on MSDN:
345          *
346          *     The Windows system palette is broken up into two
347          *     sections, one with fixed colors and one with colors
348          *     that can be changed by applications. The system palette
349          *     predefines 20 entries; these colors are known as the
350          *     static or reserved colors and consist of the 16 colors
351          *     found in the Windows version 3.0 VGA driver and 4
352          *     additional colors chosen for their visual appeal.  The
353          *     DEFAULT_PALETTE stock object is, as the name implies,
354          *     the default palette selected into a device context (DC)
355          *     and consists of these static colors. Applications can
356          *     set the remaining 236 colors using the Palette Manager.
357          *
358          * The 20 reserved entries have indices in [0,9] and
359          * [246,255]. We reuse 17 of them.
360          */
361         QRGB(128, 128, 128), // index 248   medium gray
362         QRGB(160, 160, 164), // index 247   light gray
363         QRGB(192, 192, 192), // index 7     light gray
364         QRGB(255,   0,   0), // index 249   red
365         QRGB(  0, 255,   0), // index 250   green
366         QRGB(  0,   0, 255), // index 252   blue
367         QRGB(  0, 255, 255), // index 254   cyan
368         QRGB(255,   0, 255), // index 253   magenta
369         QRGB(255, 255,   0), // index 251   yellow
370         QRGB(128,   0,   0), // index 1     dark red
371         QRGB(  0, 128,   0), // index 2     dark green
372         QRGB(  0,   0, 128), // index 4     dark blue
373         QRGB(  0, 128, 128), // index 6     dark cyan
374         QRGB(128,   0, 128), // index 5     dark magenta
375         QRGB(128, 128,   0), // index 3     dark yellow
376         QRGBA(0, 0, 0, 0)    //             transparent
377     };
378 #undef QRGB
379 #undef QRGBA
380
381     setRgb(qRed(global_colors[color]),
382            qGreen(global_colors[color]),
383            qBlue(global_colors[color]),
384            qAlpha(global_colors[color]));
385 }
386
387 /*!
388     \fn QColor::QColor(int r, int g, int b, int a = 255)
389
390     Constructs a color with the RGB value \a r, \a g, \a b, and the
391     alpha-channel (transparency) value of \a a.
392
393     The color is left invalid if any of the arguments are invalid.
394
395     \sa setRgba(), isValid()
396 */
397
398 /*!
399     Constructs a color with the value \a color. The alpha component is
400     ignored and set to solid.
401
402     \sa fromRgb(), isValid()
403 */
404
405 QColor::QColor(QRgb color)
406 {
407     cspec = Rgb;
408     ct.argb.alpha = 0xffff;
409     ct.argb.red   = qRed(color)   * 0x101;
410     ct.argb.green = qGreen(color) * 0x101;
411     ct.argb.blue  = qBlue(color)  * 0x101;
412     ct.argb.pad   = 0;
413 }
414
415
416 /*!
417     \internal
418
419     Constructs a color with the given \a spec.
420
421     This function is primarly present to avoid that QColor::Invalid
422     becomes a valid color by accident.
423 */
424
425 QColor::QColor(Spec spec)
426 {
427     switch (spec) {
428     case Invalid:
429         invalidate();
430         break;
431     case Rgb:
432         setRgb(0, 0, 0);
433         break;
434     case Hsv:
435         setHsv(0, 0, 0);
436         break;
437     case Cmyk:
438         setCmyk(0, 0, 0, 0);
439         break;
440     case Hsl:
441         setHsl(0, 0, 0, 0);
442         break;
443     }
444 }
445
446 /*!
447     \fn QColor::QColor(const QString &name)
448
449     Constructs a named color in the same way as setNamedColor() using
450     the given \a name.
451
452     The color is left invalid if the \a name cannot be parsed.
453
454     \sa setNamedColor(), name(), isValid()
455 */
456
457 /*!
458     \fn QColor::QColor(const char *name)
459
460     Constructs a named color in the same way as setNamedColor() using
461     the given \a name.
462
463     The color is left invalid if the \a name cannot be parsed.
464
465     \sa setNamedColor(), name(), isValid()
466 */
467
468 /*!
469     \fn QColor::QColor(const QColor &color)
470
471     Constructs a color that is a copy of \a color.
472
473     \sa isValid()
474 */
475
476 /*!
477     \fn bool QColor::isValid() const
478
479     Returns true if the color is valid; otherwise returns false.
480 */
481
482 /*!
483     Returns the name of the color in the format "#RRGGBB"; i.e. a "#"
484     character followed by three two-digit hexadecimal numbers.
485
486     \sa setNamedColor()
487 */
488
489 QString QColor::name() const
490 {
491     QString s;
492     s.sprintf("#%02x%02x%02x", red(), green(), blue());
493     return s;
494 }
495
496 /*!
497     Sets the RGB value of this QColor to \a name, which may be in one
498     of these formats:
499
500     \list
501     \li #RGB (each of R, G, and B is a single hex digit)
502     \li #RRGGBB
503     \li #RRRGGGBBB
504     \li #RRRRGGGGBBBB
505     \li A name from the list of colors defined in the list of \l{SVG color keyword names}
506        provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
507        These color names work on all platforms. Note that these color names are \e not the
508        same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
509        refer to the same color.
510     \li \c transparent - representing the absence of a color.
511     \endlist
512
513     The color is invalid if \a name cannot be parsed.
514
515     \sa QColor(), name(), isValid()
516 */
517
518 void QColor::setNamedColor(const QString &name)
519 {
520     setColorFromString(name);
521 }
522
523 /*!
524    \since 4.7
525
526    Returns true if the \a name is a valid color name and can
527    be used to construct a valid QColor object, otherwise returns
528    false.
529
530    It uses the same algorithm used in setNamedColor().
531
532    \sa setNamedColor()
533 */
534 bool QColor::isValidColor(const QString &name)
535 {
536     return !name.isEmpty() && QColor().setColorFromString(name);
537 }
538
539 bool QColor::setColorFromString(const QString &name)
540 {
541     if (name.isEmpty()) {
542         invalidate();
543         return true;
544     }
545
546     if (name.startsWith(QLatin1Char('#'))) {
547         QRgb rgb;
548         if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
549             setRgb(rgb);
550             return true;
551         } else {
552             invalidate();
553             return false;
554         }
555     }
556
557 #ifndef QT_NO_COLORNAMES
558     QRgb rgb;
559     if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
560         setRgba(rgb);
561         return true;
562     } else
563 #endif
564     {
565         invalidate();
566         return false;
567     }
568 }
569
570 /*!
571     Returns a QStringList containing the color names Qt knows about.
572
573     \sa {QColor#Predefined Colors}{Predefined Colors}
574 */
575 QStringList QColor::colorNames()
576 {
577 #ifndef QT_NO_COLORNAMES
578     return qt_get_colornames();
579 #else
580     return QStringList();
581 #endif
582 }
583
584 /*!
585     Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
586     saturation, value, and alpha-channel (transparency) components of the
587     color's HSV value.
588
589     These components can be retrieved individually using the hueF(),
590     saturationF(), valueF() and alphaF() functions.
591
592     \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
593 */
594 void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
595 {
596         if (!h || !s || !v)
597         return;
598
599     if (cspec != Invalid && cspec != Hsv) {
600         toHsv().getHsvF(h, s, v, a);
601         return;
602     }
603
604     *h = ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
605     *s = ct.ahsv.saturation / qreal(USHRT_MAX);
606     *v = ct.ahsv.value / qreal(USHRT_MAX);
607
608     if (a)
609         *a = ct.ahsv.alpha / qreal(USHRT_MAX);
610 }
611
612 /*!
613     Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
614     saturation, value, and alpha-channel (transparency) components of the
615     color's HSV value.
616
617     These components can be retrieved individually using the hue(),
618     saturation(), value() and alpha() functions.
619
620     \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
621 */
622 void QColor::getHsv(int *h, int *s, int *v, int *a) const
623 {
624     if (!h || !s || !v)
625         return;
626
627     if (cspec != Invalid && cspec != Hsv) {
628         toHsv().getHsv(h, s, v, a);
629         return;
630     }
631
632     *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
633     *s = ct.ahsv.saturation >> 8;
634     *v = ct.ahsv.value      >> 8;
635
636     if (a)
637         *a = ct.ahsv.alpha >> 8;
638 }
639
640 /*!
641     Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
642     the value and \a a is the alpha component of the HSV color.
643
644     All the values must be in the range 0.0-1.0.
645
646     \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
647 */
648 void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a)
649 {
650     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
651         || (s < qreal(0.0) || s > qreal(1.0))
652         || (v < qreal(0.0) || v > qreal(1.0))
653         || (a < qreal(0.0) || a > qreal(1.0))) {
654         qWarning("QColor::setHsvF: HSV parameters out of range");
655         return;
656     }
657
658     cspec = Hsv;
659     ct.ahsv.alpha      = qRound(a * USHRT_MAX);
660     ct.ahsv.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
661     ct.ahsv.saturation = qRound(s * USHRT_MAX);
662     ct.ahsv.value      = qRound(v * USHRT_MAX);
663     ct.ahsv.pad        = 0;
664 }
665
666 /*!
667     Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
668     the value and \a a is the alpha component of the HSV color.
669
670     The saturation, value and alpha-channel values must be in the range 0-255,
671     and the hue value must be greater than -1.
672
673     \sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
674 */
675 void QColor::setHsv(int h, int s, int v, int a)
676 {
677     if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
678         qWarning("QColor::setHsv: HSV parameters out of range");
679         invalidate();
680         return;
681     }
682
683     cspec = Hsv;
684     ct.ahsv.alpha      = a * 0x101;
685     ct.ahsv.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
686     ct.ahsv.saturation = s * 0x101;
687     ct.ahsv.value      = v * 0x101;
688     ct.ahsv.pad        = 0;
689 }
690
691 /*!
692     \since 4.6
693
694     Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
695     saturation, lightness, and alpha-channel (transparency) components of the
696     color's HSL value.
697
698     These components can be retrieved individually using the hueHslF(),
699     saturationHslF(), lightnessF() and alphaF() functions.
700
701     \sa setHsl()
702 */
703 void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
704 {
705         if (!h || !s || !l)
706         return;
707
708     if (cspec != Invalid && cspec != Hsl) {
709         toHsl().getHslF(h, s, l, a);
710         return;
711     }
712
713     *h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
714     *s = ct.ahsl.saturation / qreal(USHRT_MAX);
715     *l = ct.ahsl.lightness / qreal(USHRT_MAX);
716
717     if (a)
718         *a = ct.ahsl.alpha / qreal(USHRT_MAX);
719 }
720
721 /*!
722     \since 4.6
723
724     Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
725     saturation, lightness, and alpha-channel (transparency) components of the
726     color's HSL value.
727
728     These components can be retrieved individually using the hueHsl(),
729     saturationHsl(), lightness() and alpha() functions.
730
731     \sa setHsl()
732 */
733 void QColor::getHsl(int *h, int *s, int *l, int *a) const
734 {
735     if (!h || !s || !l)
736         return;
737
738     if (cspec != Invalid && cspec != Hsl) {
739         toHsl().getHsl(h, s, l, a);
740         return;
741     }
742
743     *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
744     *s = ct.ahsl.saturation >> 8;
745     *l = ct.ahsl.lightness  >> 8;
746
747     if (a)
748         *a = ct.ahsl.alpha >> 8;
749 }
750
751 /*!
752     \since 4.6
753
754     Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
755     the lightness and \a a is the alpha component of the HSL color.
756
757     All the values must be in the range 0.0-1.0.
758
759     \sa getHslF(), setHsl()
760 */
761 void QColor::setHslF(qreal h, qreal s, qreal l, qreal a)
762 {
763     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
764         || (s < qreal(0.0) || s > qreal(1.0))
765         || (l < qreal(0.0) || l > qreal(1.0))
766         || (a < qreal(0.0) || a > qreal(1.0))) {
767         qWarning("QColor::setHsvF: HSV parameters out of range");
768         return;
769     }
770
771     cspec = Hsl;
772     ct.ahsl.alpha      = qRound(a * USHRT_MAX);
773     ct.ahsl.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
774     ct.ahsl.saturation = qRound(s * USHRT_MAX);
775     ct.ahsl.lightness  = qRound(l * USHRT_MAX);
776     ct.ahsl.pad        = 0;
777 }
778
779 /*!
780     \since 4.6
781
782     Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
783     the lightness and \a a is the alpha component of the HSL color.
784
785     The saturation, value and alpha-channel values must be in the range 0-255,
786     and the hue value must be greater than -1.
787
788     \sa getHsl(), setHslF()
789 */
790 void QColor::setHsl(int h, int s, int l, int a)
791 {
792     if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
793         qWarning("QColor::setHsv: HSV parameters out of range");
794         invalidate();
795         return;
796     }
797
798     cspec = Hsl;
799     ct.ahsl.alpha      = a * 0x101;
800     ct.ahsl.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
801     ct.ahsl.saturation = s * 0x101;
802     ct.ahsl.lightness  = l * 0x101;
803     ct.ahsl.pad        = 0;
804 }
805
806 /*!
807     Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
808     green, blue, and alpha-channel (transparency) components of the color's
809     RGB value.
810
811     These components can be retrieved individually using the redF(), greenF(),
812     blueF() and alphaF() functions.
813
814     \sa rgb(), setRgb()
815 */
816 void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
817 {
818     if (!r || !g || !b)
819         return;
820
821     if (cspec != Invalid && cspec != Rgb) {
822         toRgb().getRgbF(r, g, b, a);
823         return;
824     }
825
826     *r = ct.argb.red   / qreal(USHRT_MAX);
827     *g = ct.argb.green / qreal(USHRT_MAX);
828     *b = ct.argb.blue  / qreal(USHRT_MAX);
829
830     if (a)
831         *a = ct.argb.alpha / qreal(USHRT_MAX);
832
833 }
834
835 /*!
836     Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
837     green, blue, and alpha-channel (transparency) components of the color's
838     RGB value.
839
840     These components can be retrieved individually using the red(), green(),
841     blue() and alpha() functions.
842
843     \sa rgb(), setRgb()
844 */
845 void QColor::getRgb(int *r, int *g, int *b, int *a) const
846 {
847     if (!r || !g || !b)
848         return;
849
850     if (cspec != Invalid && cspec != Rgb) {
851         toRgb().getRgb(r, g, b, a);
852         return;
853     }
854
855     *r = ct.argb.red   >> 8;
856     *g = ct.argb.green >> 8;
857     *b = ct.argb.blue  >> 8;
858
859     if (a)
860         *a = ct.argb.alpha >> 8;
861 }
862
863 /*!
864     \fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
865
866     Sets the color channels of this color to \a r (red), \a g (green),
867     \a b (blue) and \a a (alpha, transparency).
868
869     All values must be in the range 0.0-1.0.
870
871     \sa rgb(), getRgbF(), setRgb()
872 */
873 void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
874 {
875     if (r < qreal(0.0) || r > qreal(1.0)
876         || g < qreal(0.0) || g > qreal(1.0)
877         || b < qreal(0.0) || b > qreal(1.0)
878         || a < qreal(0.0) || a > qreal(1.0)) {
879         qWarning("QColor::setRgbF: RGB parameters out of range");
880         invalidate();
881         return;
882     }
883
884     cspec = Rgb;
885     ct.argb.alpha = qRound(a * USHRT_MAX);
886     ct.argb.red   = qRound(r * USHRT_MAX);
887     ct.argb.green = qRound(g * USHRT_MAX);
888     ct.argb.blue  = qRound(b * USHRT_MAX);
889     ct.argb.pad   = 0;
890 }
891
892 /*!
893     Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
894
895     All the values must be in the range 0-255.
896
897     \sa rgb(), getRgb(), setRgbF()
898 */
899 void QColor::setRgb(int r, int g, int b, int a)
900 {
901     if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
902         qWarning("QColor::setRgb: RGB parameters out of range");
903         invalidate();
904         return;
905     }
906
907     cspec = Rgb;
908     ct.argb.alpha = a * 0x101;
909     ct.argb.red   = r * 0x101;
910     ct.argb.green = g * 0x101;
911     ct.argb.blue  = b * 0x101;
912     ct.argb.pad   = 0;
913 }
914
915 /*!
916     \fn QRgb QColor::rgba() const
917
918     Returns the RGB value of the color, including its alpha.
919
920     For an invalid color, the alpha value of the returned color is unspecified.
921
922     \sa setRgba(), rgb()
923 */
924
925 QRgb QColor::rgba() const
926 {
927     if (cspec != Invalid && cspec != Rgb)
928         return toRgb().rgba();
929     return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
930 }
931
932 /*!
933     Sets the RGB value to \a rgba, including its alpha.
934
935     \sa rgba(), rgb()
936 */
937 void QColor::setRgba(QRgb rgba)
938 {
939     cspec = Rgb;
940     ct.argb.alpha = qAlpha(rgba) * 0x101;
941     ct.argb.red   = qRed(rgba)   * 0x101;
942     ct.argb.green = qGreen(rgba) * 0x101;
943     ct.argb.blue  = qBlue(rgba)  * 0x101;
944     ct.argb.pad   = 0;
945 }
946
947 /*!
948     \fn QRgb QColor::rgb() const
949
950     Returns the RGB value of the color. The alpha value is opaque.
951
952     \sa getRgb(), rgba()
953 */
954 QRgb QColor::rgb() const
955 {
956     if (cspec != Invalid && cspec != Rgb)
957         return toRgb().rgb();
958     return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
959 }
960
961 /*!
962     \overload
963
964     Sets the RGB value to \a rgb. The alpha value is set to opaque.
965 */
966 void QColor::setRgb(QRgb rgb)
967 {
968     cspec = Rgb;
969     ct.argb.alpha = 0xffff;
970     ct.argb.red   = qRed(rgb)   * 0x101;
971     ct.argb.green = qGreen(rgb) * 0x101;
972     ct.argb.blue  = qBlue(rgb)  * 0x101;
973     ct.argb.pad   = 0;
974 }
975
976 /*!
977     Returns the alpha color component of this color.
978
979     \sa setAlpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
980 */
981 int QColor::alpha() const
982 { return ct.argb.alpha >> 8; }
983
984
985 /*!
986     Sets the alpha of this color to \a alpha. Integer alpha is specified in the
987     range 0-255.
988
989     \sa alpha(), alphaF(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
990 */
991
992 void QColor::setAlpha(int alpha)
993 {
994     QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
995     ct.argb.alpha = alpha * 0x101;
996 }
997
998 /*!
999     Returns the alpha color component of this color.
1000
1001     \sa setAlphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1002 */
1003 qreal QColor::alphaF() const
1004 { return ct.argb.alpha / qreal(USHRT_MAX); }
1005
1006 /*!
1007     Sets the alpha of this color to \a alpha. qreal alpha is specified in the
1008     range 0.0-1.0.
1009
1010     \sa alphaF(), alpha(), {QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing}
1011
1012 */
1013 void QColor::setAlphaF(qreal alpha)
1014 {
1015     QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
1016     qreal tmp = alpha * USHRT_MAX;
1017     ct.argb.alpha = qRound(tmp);
1018 }
1019
1020
1021 /*!
1022     Returns the red color component of this color.
1023
1024     \sa setRed(), redF(), getRgb()
1025 */
1026 int QColor::red() const
1027 {
1028     if (cspec != Invalid && cspec != Rgb)
1029         return toRgb().red();
1030     return ct.argb.red >> 8;
1031 }
1032
1033 /*!
1034     Sets the red color component of this color to \a red. Integer components
1035     are specified in the range 0-255.
1036
1037     \sa red(), redF(), setRgb()
1038 */
1039 void QColor::setRed(int red)
1040 {
1041     QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
1042     if (cspec != Rgb)
1043         setRgb(red, green(), blue(), alpha());
1044     else
1045         ct.argb.red = red * 0x101;
1046 }
1047
1048 /*!
1049     Returns the green color component of this color.
1050
1051     \sa setGreen(), greenF(), getRgb()
1052 */
1053 int QColor::green() const
1054 {
1055     if (cspec != Invalid && cspec != Rgb)
1056         return toRgb().green();
1057     return ct.argb.green >> 8;
1058 }
1059
1060 /*!
1061     Sets the green color component of this color to \a green. Integer
1062     components are specified in the range 0-255.
1063
1064     \sa green(), greenF(), setRgb()
1065 */
1066 void QColor::setGreen(int green)
1067 {
1068     QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
1069     if (cspec != Rgb)
1070         setRgb(red(), green, blue(), alpha());
1071     else
1072         ct.argb.green = green * 0x101;
1073 }
1074
1075
1076 /*!
1077     Returns the blue color component of this color.
1078
1079     \sa setBlue(), blueF(), getRgb()
1080 */
1081 int QColor::blue() const
1082 {
1083     if (cspec != Invalid && cspec != Rgb)
1084         return toRgb().blue();
1085     return ct.argb.blue >> 8;
1086 }
1087
1088
1089 /*!
1090     Sets the blue color component of this color to \a blue. Integer components
1091     are specified in the range 0-255.
1092
1093     \sa blue(), blueF(), setRgb()
1094 */
1095 void QColor::setBlue(int blue)
1096 {
1097     QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
1098     if (cspec != Rgb)
1099         setRgb(red(), green(), blue, alpha());
1100     else
1101         ct.argb.blue = blue * 0x101;
1102 }
1103
1104 /*!
1105     Returns the red color component of this color.
1106
1107     \sa setRedF(), red(), getRgbF()
1108 */
1109 qreal QColor::redF() const
1110 {
1111     if (cspec != Invalid && cspec != Rgb)
1112         return toRgb().redF();
1113     return ct.argb.red / qreal(USHRT_MAX);
1114 }
1115
1116
1117 /*!
1118     Sets the red color component of this color to \a red. Float components
1119     are specified in the range 0.0-1.0.
1120
1121     \sa redF(), red(), setRgbF()
1122 */
1123 void QColor::setRedF(qreal red)
1124 {
1125     QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red);
1126     if (cspec != Rgb)
1127         setRgbF(red, greenF(), blueF(), alphaF());
1128     else
1129         ct.argb.red = qRound(red * USHRT_MAX);
1130 }
1131
1132 /*!
1133     Returns the green color component of this color.
1134
1135     \sa setGreenF(), green(), getRgbF()
1136 */
1137 qreal QColor::greenF() const
1138 {
1139     if (cspec != Invalid && cspec != Rgb)
1140         return toRgb().greenF();
1141     return ct.argb.green / qreal(USHRT_MAX);
1142 }
1143
1144
1145 /*!
1146     Sets the green color component of this color to \a green. Float components
1147     are specified in the range 0.0-1.0.
1148
1149     \sa greenF(), green(), setRgbF()
1150 */
1151 void QColor::setGreenF(qreal green)
1152 {
1153     QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green);
1154     if (cspec != Rgb)
1155         setRgbF(redF(), green, blueF(), alphaF());
1156     else
1157         ct.argb.green = qRound(green * USHRT_MAX);
1158 }
1159
1160 /*!
1161     Returns the blue color component of this color.
1162
1163      \sa setBlueF(), blue(), getRgbF()
1164 */
1165 qreal QColor::blueF() const
1166 {
1167     if (cspec != Invalid && cspec != Rgb)
1168         return toRgb().blueF();
1169     return ct.argb.blue / qreal(USHRT_MAX);
1170 }
1171
1172 /*!
1173     Sets the blue color component of this color to \a blue. Float components
1174     are specified in the range 0.0-1.0.
1175
1176     \sa blueF(), blue(), setRgbF()
1177 */
1178 void QColor::setBlueF(qreal blue)
1179 {
1180     QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue);
1181     if (cspec != Rgb)
1182         setRgbF(redF(), greenF(), blue, alphaF());
1183     else
1184         ct.argb.blue = qRound(blue * USHRT_MAX);
1185 }
1186
1187 /*!
1188     Returns the hue color component of this color.
1189
1190     The color is implicitly converted to HSV.
1191
1192     \sa hsvHue(), hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1193 */
1194
1195 int QColor::hue() const
1196 {
1197     return hsvHue();
1198 }
1199
1200 /*!
1201     Returns the hue color component of this color.
1202
1203     \sa hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1204 */
1205 int QColor::hsvHue() const
1206 {
1207     if (cspec != Invalid && cspec != Hsv)
1208         return toHsv().hue();
1209     return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1210 }
1211
1212 /*!
1213     Returns the saturation color component of this color.
1214
1215     The color is implicitly converted to HSV.
1216
1217     \sa hsvSaturation(), saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1218     Model}
1219 */
1220
1221 int QColor::saturation() const
1222 {
1223     return hsvSaturation();
1224 }
1225
1226 /*!
1227     Returns the saturation color component of this color.
1228
1229     \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1230 */
1231 int QColor::hsvSaturation() const
1232 {
1233     if (cspec != Invalid && cspec != Hsv)
1234         return toHsv().saturation();
1235     return ct.ahsv.saturation >> 8;
1236 }
1237
1238 /*!
1239     Returns the value color component of this color.
1240
1241     \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1242 */
1243 int QColor::value() const
1244 {
1245     if (cspec != Invalid && cspec != Hsv)
1246         return toHsv().value();
1247     return ct.ahsv.value >> 8;
1248 }
1249
1250 /*!
1251     Returns the hue color component of this color.
1252
1253     The color is implicitly converted to HSV.
1254
1255     \sa hsvHueF(), hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1256 */
1257 qreal QColor::hueF() const
1258 {
1259     return hsvHueF();
1260 }
1261
1262 /*!
1263     Returns the hue color component of this color.
1264
1265     \sa hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1266     Model}
1267 */
1268 qreal QColor::hsvHueF() const
1269 {
1270     if (cspec != Invalid && cspec != Hsv)
1271         return toHsv().hueF();
1272     return ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
1273 }
1274
1275 /*!
1276     Returns the saturation color component of this color.
1277
1278      The color is implicitly converted to HSV.
1279
1280     \sa hsvSaturationF(), saturation(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1281     Model}
1282 */
1283 qreal QColor::saturationF() const
1284 {
1285     return hsvSaturationF();
1286 }
1287
1288 /*!
1289     Returns the saturation color component of this color.
1290
1291     \sa saturation(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1292 */
1293 qreal QColor::hsvSaturationF() const
1294 {
1295     if (cspec != Invalid && cspec != Hsv)
1296         return toHsv().saturationF();
1297     return ct.ahsv.saturation / qreal(USHRT_MAX);
1298 }
1299
1300 /*!
1301     Returns the value color component of this color.
1302
1303     \sa value(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
1304 */
1305 qreal QColor::valueF() const
1306 {
1307     if (cspec != Invalid && cspec != Hsv)
1308         return toHsv().valueF();
1309     return ct.ahsv.value / qreal(USHRT_MAX);
1310 }
1311
1312 /*!
1313     \since 4.6
1314
1315     Returns the hue color component of this color.
1316
1317     \sa getHslF(), getHsl()
1318 */
1319 int QColor::hslHue() const
1320 {
1321     if (cspec != Invalid && cspec != Hsl)
1322         return toHsl().hslHue();
1323     return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1324 }
1325
1326 /*!
1327     \since 4.6
1328
1329     Returns the saturation color component of this color.
1330
1331     \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
1332 */
1333 int QColor::hslSaturation() const
1334 {
1335     if (cspec != Invalid && cspec != Hsl)
1336         return toHsl().hslSaturation();
1337     return ct.ahsl.saturation >> 8;
1338 }
1339
1340 /*!
1341     \since 4.6
1342
1343     Returns the lightness color component of this color.
1344
1345     \sa lightnessF(), getHsl()
1346 */
1347 int QColor::lightness() const
1348 {
1349     if (cspec != Invalid && cspec != Hsl)
1350         return toHsl().lightness();
1351     return ct.ahsl.lightness >> 8;
1352 }
1353
1354 /*!
1355     \since 4.6
1356
1357     Returns the hue color component of this color.
1358
1359     \sa hue(), getHslF()
1360 */
1361 qreal QColor::hslHueF() const
1362 {
1363     if (cspec != Invalid && cspec != Hsl)
1364         return toHsl().hslHueF();
1365     return ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
1366 }
1367
1368 /*!
1369     \since 4.6
1370
1371     Returns the saturation color component of this color.
1372
1373     \sa saturationF(), getHslF()
1374 */
1375 qreal QColor::hslSaturationF() const
1376 {
1377     if (cspec != Invalid && cspec != Hsl)
1378         return toHsl().hslSaturationF();
1379     return ct.ahsl.saturation / qreal(USHRT_MAX);
1380 }
1381
1382 /*!
1383     \since 4.6
1384
1385     Returns the lightness color component of this color.
1386
1387     \sa value(), getHslF()
1388 */
1389 qreal QColor::lightnessF() const
1390 {
1391     if (cspec != Invalid && cspec != Hsl)
1392         return toHsl().lightnessF();
1393     return ct.ahsl.lightness / qreal(USHRT_MAX);
1394 }
1395
1396 /*!
1397     Returns the cyan color component of this color.
1398
1399     \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1400 */
1401 int QColor::cyan() const
1402 {
1403     if (cspec != Invalid && cspec != Cmyk)
1404         return toCmyk().cyan();
1405     return ct.acmyk.cyan >> 8;
1406 }
1407
1408 /*!
1409     Returns the magenta color component of this color.
1410
1411     \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1412 */
1413 int QColor::magenta() const
1414 {
1415     if (cspec != Invalid && cspec != Cmyk)
1416         return toCmyk().magenta();
1417     return ct.acmyk.magenta >> 8;
1418 }
1419
1420 /*!
1421     Returns the yellow color component of this color.
1422
1423     \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1424 */
1425 int QColor::yellow() const
1426 {
1427     if (cspec != Invalid && cspec != Cmyk)
1428         return toCmyk().yellow();
1429     return ct.acmyk.yellow >> 8;
1430 }
1431
1432 /*!
1433     Returns the black color component of this color.
1434
1435     \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1436
1437 */
1438 int QColor::black() const
1439 {
1440     if (cspec != Invalid && cspec != Cmyk)
1441         return toCmyk().black();
1442     return ct.acmyk.black >> 8;
1443 }
1444
1445 /*!
1446     Returns the cyan color component of this color.
1447
1448     \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1449 */
1450 qreal QColor::cyanF() const
1451 {
1452     if (cspec != Invalid && cspec != Cmyk)
1453         return toCmyk().cyanF();
1454     return ct.acmyk.cyan / qreal(USHRT_MAX);
1455 }
1456
1457 /*!
1458     Returns the magenta color component of this color.
1459
1460     \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1461 */
1462 qreal QColor::magentaF() const
1463 {
1464     if (cspec != Invalid && cspec != Cmyk)
1465         return toCmyk().magentaF();
1466     return ct.acmyk.magenta / qreal(USHRT_MAX);
1467 }
1468
1469 /*!
1470     Returns the yellow color component of this color.
1471
1472      \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1473 */
1474 qreal QColor::yellowF() const
1475 {
1476     if (cspec != Invalid && cspec != Cmyk)
1477         return toCmyk().yellowF();
1478     return ct.acmyk.yellow / qreal(USHRT_MAX);
1479 }
1480
1481 /*!
1482     Returns the black color component of this color.
1483
1484     \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1485 */
1486 qreal QColor::blackF() const
1487 {
1488     if (cspec != Invalid && cspec != Cmyk)
1489         return toCmyk().blackF();
1490     return ct.acmyk.black / qreal(USHRT_MAX);
1491 }
1492
1493 /*!
1494     Create and returns an RGB QColor based on this color.
1495
1496     \sa fromRgb(), convertTo(), isValid()
1497 */
1498 QColor QColor::toRgb() const
1499 {
1500     if (!isValid() || cspec == Rgb)
1501         return *this;
1502
1503     QColor color;
1504     color.cspec = Rgb;
1505     color.ct.argb.alpha = ct.argb.alpha;
1506     color.ct.argb.pad = 0;
1507
1508     switch (cspec) {
1509     case Hsv:
1510         {
1511             if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
1512                 // achromatic case
1513                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
1514                 break;
1515             }
1516
1517             // chromatic case
1518             const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / 6000.;
1519             const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX);
1520             const qreal v = ct.ahsv.value / qreal(USHRT_MAX);
1521             const int i = int(h);
1522             const qreal f = h - i;
1523             const qreal p = v * (qreal(1.0) - s);
1524
1525             if (i & 1) {
1526                 const qreal q = v * (qreal(1.0) - (s * f));
1527
1528                 switch (i) {
1529                 case 1:
1530                     color.ct.argb.red   = qRound(q * USHRT_MAX);
1531                     color.ct.argb.green = qRound(v * USHRT_MAX);
1532                     color.ct.argb.blue  = qRound(p * USHRT_MAX);
1533                     break;
1534                 case 3:
1535                     color.ct.argb.red   = qRound(p * USHRT_MAX);
1536                     color.ct.argb.green = qRound(q * USHRT_MAX);
1537                     color.ct.argb.blue  = qRound(v * USHRT_MAX);
1538                     break;
1539                 case 5:
1540                     color.ct.argb.red   = qRound(v * USHRT_MAX);
1541                     color.ct.argb.green = qRound(p * USHRT_MAX);
1542                     color.ct.argb.blue  = qRound(q * USHRT_MAX);
1543                     break;
1544                 }
1545             } else {
1546                 const qreal t = v * (qreal(1.0) - (s * (qreal(1.0) - f)));
1547
1548                 switch (i) {
1549                 case 0:
1550                     color.ct.argb.red   = qRound(v * USHRT_MAX);
1551                     color.ct.argb.green = qRound(t * USHRT_MAX);
1552                     color.ct.argb.blue  = qRound(p * USHRT_MAX);
1553                     break;
1554                 case 2:
1555                     color.ct.argb.red   = qRound(p * USHRT_MAX);
1556                     color.ct.argb.green = qRound(v * USHRT_MAX);
1557                     color.ct.argb.blue  = qRound(t * USHRT_MAX);
1558                     break;
1559                 case 4:
1560                     color.ct.argb.red   = qRound(t * USHRT_MAX);
1561                     color.ct.argb.green = qRound(p * USHRT_MAX);
1562                     color.ct.argb.blue  = qRound(v * USHRT_MAX);
1563                     break;
1564                 }
1565             }
1566             break;
1567         }
1568     case Hsl:
1569         {
1570             if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
1571                 // achromatic case
1572                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
1573             } else if (ct.ahsl.lightness == 0) {
1574                 // lightness 0 
1575                 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
1576             } else {
1577                 // chromatic case
1578                 const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / 36000.;
1579                 const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX);
1580                 const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX);
1581
1582                 qreal temp2;
1583                 if (l < qreal(0.5))
1584                     temp2 = l * (qreal(1.0) + s);
1585                 else
1586                     temp2 = l + s - (l * s);
1587
1588                 const qreal temp1 = (qreal(2.0) * l) - temp2;
1589                 qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)),
1590                                    h,
1591                                    h - (qreal(1.0) / qreal(3.0)) };
1592
1593                 for (int i = 0; i != 3; ++i) {
1594                     if (temp3[i] < qreal(0.0))
1595                         temp3[i] += qreal(1.0);
1596                     else if (temp3[i] > qreal(1.0))
1597                         temp3[i] -= qreal(1.0);
1598
1599                     const qreal sixtemp3 = temp3[i] * qreal(6.0);
1600                     if (sixtemp3 < qreal(1.0))
1601                         color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
1602                     else if ((temp3[i] * qreal(2.0)) < qreal(1.0))
1603                         color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
1604                     else if ((temp3[i] * qreal(3.0)) < qreal(2.0))
1605                         color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX);
1606                     else
1607                         color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
1608                 }
1609                 color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
1610                 color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
1611                 color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
1612             }
1613             break;
1614         }
1615     case Cmyk:
1616         {
1617             const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX);
1618             const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX);
1619             const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX);
1620             const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
1621
1622             color.ct.argb.red   = qRound((qreal(1.0) - (c * (qreal(1.0) - k) + k)) * USHRT_MAX);
1623             color.ct.argb.green = qRound((qreal(1.0) - (m * (qreal(1.0) - k) + k)) * USHRT_MAX);
1624             color.ct.argb.blue  = qRound((qreal(1.0) - (y * (qreal(1.0) - k) + k)) * USHRT_MAX);
1625             break;
1626         }
1627     default:
1628         break;
1629     }
1630
1631     return color;
1632 }
1633
1634
1635 #define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
1636 #define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
1637
1638
1639 /*!
1640     Creates and returns an HSV QColor based on this color.
1641
1642     \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
1643 */
1644 QColor QColor::toHsv() const
1645 {
1646     if (!isValid() || cspec == Hsv)
1647         return *this;
1648
1649     if (cspec != Rgb)
1650         return toRgb().toHsv();
1651
1652     QColor color;
1653     color.cspec = Hsv;
1654     color.ct.ahsv.alpha = ct.argb.alpha;
1655     color.ct.ahsv.pad = 0;
1656
1657     const qreal r = ct.argb.red   / qreal(USHRT_MAX);
1658     const qreal g = ct.argb.green / qreal(USHRT_MAX);
1659     const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
1660     const qreal max = Q_MAX_3(r, g, b);
1661     const qreal min = Q_MIN_3(r, g, b);
1662     const qreal delta = max - min;
1663     color.ct.ahsv.value = qRound(max * USHRT_MAX);
1664     if (qFuzzyIsNull(delta)) {
1665         // achromatic case, hue is undefined
1666         color.ct.ahsv.hue = USHRT_MAX;
1667         color.ct.ahsv.saturation = 0;
1668     } else {
1669         // chromatic case
1670         qreal hue = 0;
1671         color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
1672         if (qFuzzyCompare(r, max)) {
1673             hue = ((g - b) /delta);
1674         } else if (qFuzzyCompare(g, max)) {
1675             hue = (qreal(2.0) + (b - r) / delta);
1676         } else if (qFuzzyCompare(b, max)) {
1677             hue = (qreal(4.0) + (r - g) / delta);
1678         } else {
1679             Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1680         }
1681         hue *= qreal(60.0);
1682         if (hue < qreal(0.0))
1683             hue += qreal(360.0);
1684         color.ct.ahsv.hue = qRound(hue * 100);
1685     }
1686
1687     return color;
1688 }
1689
1690 /*!
1691     Creates and returns an HSL QColor based on this color.
1692
1693     \sa fromHsl(), convertTo(), isValid()
1694 */
1695 QColor QColor::toHsl() const
1696 {
1697     if (!isValid() || cspec == Hsl)
1698         return *this;
1699
1700     if (cspec != Rgb)
1701         return toRgb().toHsl();
1702
1703     QColor color;
1704     color.cspec = Hsl;
1705     color.ct.ahsl.alpha = ct.argb.alpha;
1706     color.ct.ahsl.pad = 0;
1707
1708     const qreal r = ct.argb.red   / qreal(USHRT_MAX);
1709     const qreal g = ct.argb.green / qreal(USHRT_MAX);
1710     const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
1711     const qreal max = Q_MAX_3(r, g, b);
1712     const qreal min = Q_MIN_3(r, g, b);
1713     const qreal delta = max - min;
1714     const qreal delta2 = max + min;
1715     const qreal lightness = qreal(0.5) * delta2;
1716     color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
1717     if (qFuzzyIsNull(delta)) {
1718         // achromatic case, hue is undefined
1719         color.ct.ahsl.hue = USHRT_MAX;
1720         color.ct.ahsl.saturation = 0;
1721     } else {
1722         // chromatic case
1723         qreal hue = 0;
1724         if (lightness < qreal(0.5))
1725             color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
1726         else
1727             color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX);
1728         if (qFuzzyCompare(r, max)) {
1729             hue = ((g - b) /delta);
1730         } else if (qFuzzyCompare(g, max)) {
1731             hue = (qreal(2.0) + (b - r) / delta);
1732         } else if (qFuzzyCompare(b, max)) {
1733             hue = (qreal(4.0) + (r - g) / delta);
1734         } else {
1735             Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1736         }
1737         hue *= qreal(60.0);
1738         if (hue < qreal(0.0))
1739             hue += qreal(360.0);
1740         color.ct.ahsl.hue = qRound(hue * 100);
1741     }
1742
1743     return color;
1744 }
1745
1746 /*!
1747     Creates and returns a CMYK QColor based on this color.
1748
1749     \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1750 */
1751 QColor QColor::toCmyk() const
1752 {
1753     if (!isValid() || cspec == Cmyk)
1754         return *this;
1755     if (cspec != Rgb)
1756         return toRgb().toCmyk();
1757
1758     QColor color;
1759     color.cspec = Cmyk;
1760     color.ct.acmyk.alpha = ct.argb.alpha;
1761
1762     // rgb -> cmy
1763     const qreal r = ct.argb.red   / qreal(USHRT_MAX);
1764     const qreal g = ct.argb.green / qreal(USHRT_MAX);
1765     const qreal b = ct.argb.blue  / qreal(USHRT_MAX);
1766     qreal c = qreal(1.0) - r;
1767     qreal m = qreal(1.0) - g;
1768     qreal y = qreal(1.0) - b;
1769
1770     // cmy -> cmyk
1771     const qreal k = qMin(c, qMin(m, y));
1772
1773     if (!qFuzzyIsNull(k - 1)) {
1774         c = (c - k) / (qreal(1.0) - k);
1775         m = (m - k) / (qreal(1.0) - k);
1776         y = (y - k) / (qreal(1.0) - k);
1777     }
1778
1779     color.ct.acmyk.cyan    = qRound(c * USHRT_MAX);
1780     color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
1781     color.ct.acmyk.yellow  = qRound(y * USHRT_MAX);
1782     color.ct.acmyk.black   = qRound(k * USHRT_MAX);
1783
1784     return color;
1785 }
1786
1787 QColor QColor::convertTo(QColor::Spec colorSpec) const
1788 {
1789     if (colorSpec == cspec)
1790         return *this;
1791     switch (colorSpec) {
1792     case Rgb:
1793         return toRgb();
1794     case Hsv:
1795         return toHsv();
1796     case Cmyk:
1797         return toCmyk();
1798     case Hsl:
1799         return toHsl();
1800     case Invalid:
1801         break;
1802     }
1803     return QColor(); // must be invalid
1804 }
1805
1806
1807 /*!
1808     Static convenience function that returns a QColor constructed from the
1809     given QRgb value \a rgb.
1810
1811     The alpha component of \a rgb is ignored (i.e. it is automatically set to
1812     255), use the fromRgba() function to include the alpha-channel specified by
1813     the given QRgb value.
1814
1815     \sa fromRgba(), fromRgbF(), toRgb(), isValid()
1816 */
1817
1818 QColor QColor::fromRgb(QRgb rgb)
1819 {
1820     return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
1821 }
1822
1823
1824 /*!
1825     Static convenience function that returns a QColor constructed from the
1826     given QRgb value \a rgba.
1827
1828     Unlike the fromRgb() function, the alpha-channel specified by the given
1829     QRgb value is included.
1830
1831     \sa fromRgb(), isValid()
1832 */
1833
1834 QColor QColor::fromRgba(QRgb rgba)
1835 {
1836     return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
1837 }
1838
1839 /*!
1840     Static convenience function that returns a QColor constructed from the RGB
1841     color values, \a r (red), \a g (green), \a b (blue), and \a a
1842     (alpha-channel, i.e. transparency).
1843
1844     All the values must be in the range 0-255.
1845
1846     \sa toRgb(), fromRgbF(), isValid()
1847 */
1848 QColor QColor::fromRgb(int r, int g, int b, int a)
1849 {
1850     if (r < 0 || r > 255
1851         || g < 0 || g > 255
1852         || b < 0 || b > 255
1853         || a < 0 || a > 255) {
1854         qWarning("QColor::fromRgb: RGB parameters out of range");
1855         return QColor();
1856     }
1857
1858     QColor color;
1859     color.cspec = Rgb;
1860     color.ct.argb.alpha = a * 0x101;
1861     color.ct.argb.red   = r * 0x101;
1862     color.ct.argb.green = g * 0x101;
1863     color.ct.argb.blue  = b * 0x101;
1864     color.ct.argb.pad   = 0;
1865     return color;
1866 }
1867
1868 /*!
1869     Static convenience function that returns a QColor constructed from the RGB
1870     color values, \a r (red), \a g (green), \a b (blue), and \a a
1871     (alpha-channel, i.e. transparency).
1872
1873     All the values must be in the range 0.0-1.0.
1874
1875     \sa fromRgb(), toRgb(), isValid()
1876 */
1877 QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a)
1878 {
1879     if (r < qreal(0.0) || r > qreal(1.0)
1880         || g < qreal(0.0) || g > qreal(1.0)
1881         || b < qreal(0.0) || b > qreal(1.0)
1882         || a < qreal(0.0) || a > qreal(1.0)) {
1883         qWarning("QColor::fromRgbF: RGB parameters out of range");
1884         return QColor();
1885     }
1886
1887     QColor color;
1888     color.cspec = Rgb;
1889     color.ct.argb.alpha = qRound(a * USHRT_MAX);
1890     color.ct.argb.red   = qRound(r * USHRT_MAX);
1891     color.ct.argb.green = qRound(g * USHRT_MAX);
1892     color.ct.argb.blue  = qRound(b * USHRT_MAX);
1893     color.ct.argb.pad   = 0;
1894     return color;
1895 }
1896
1897 /*!
1898     Static convenience function that returns a QColor constructed from the HSV
1899     color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1900     (alpha-channel, i.e. transparency).
1901
1902     The value of \a s, \a v, and \a a must all be in the range 0-255; the value
1903     of \a h must be in the range 0-359.
1904
1905     \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
1906 */
1907 QColor QColor::fromHsv(int h, int s, int v, int a)
1908 {
1909     if (((h < 0 || h >= 360) && h != -1)
1910         || s < 0 || s > 255
1911         || v < 0 || v > 255
1912         || a < 0 || a > 255) {
1913         qWarning("QColor::fromHsv: HSV parameters out of range");
1914         return QColor();
1915     }
1916
1917     QColor color;
1918     color.cspec = Hsv;
1919     color.ct.ahsv.alpha      = a * 0x101;
1920     color.ct.ahsv.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
1921     color.ct.ahsv.saturation = s * 0x101;
1922     color.ct.ahsv.value      = v * 0x101;
1923     color.ct.ahsv.pad        = 0;
1924     return color;
1925 }
1926
1927 /*!
1928     \overload
1929
1930     Static convenience function that returns a QColor constructed from the HSV
1931     color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1932     (alpha-channel, i.e. transparency).
1933
1934     All the values must be in the range 0.0-1.0.
1935
1936     \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color Model}{The HSV Color Model}
1937 */
1938 QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a)
1939 {
1940     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
1941         || (s < qreal(0.0) || s > qreal(1.0))
1942         || (v < qreal(0.0) || v > qreal(1.0))
1943         || (a < qreal(0.0) || a > qreal(1.0))) {
1944         qWarning("QColor::fromHsvF: HSV parameters out of range");
1945         return QColor();
1946     }
1947
1948     QColor color;
1949     color.cspec = Hsv;
1950     color.ct.ahsv.alpha      = qRound(a * USHRT_MAX);
1951     color.ct.ahsv.hue        = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
1952     color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
1953     color.ct.ahsv.value      = qRound(v * USHRT_MAX);
1954     color.ct.ahsv.pad        = 0;
1955     return color;
1956 }
1957
1958 /*!
1959     \since 4.6
1960
1961     Static convenience function that returns a QColor constructed from the HSV
1962     color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
1963     (alpha-channel, i.e. transparency).
1964
1965     The value of \a s, \a l, and \a a must all be in the range 0-255; the value
1966     of \a h must be in the range 0-359.
1967
1968     \sa toHsl(), fromHslF(), isValid()
1969 */
1970 QColor QColor::fromHsl(int h, int s, int l, int a)
1971 {
1972     if (((h < 0 || h >= 360) && h != -1)
1973         || s < 0 || s > 255
1974         || l < 0 || l > 255
1975         || a < 0 || a > 255) {
1976         qWarning("QColor::fromHsv: HSV parameters out of range");
1977         return QColor();
1978     }
1979
1980     QColor color;
1981     color.cspec = Hsl;
1982     color.ct.ahsl.alpha      = a * 0x101;
1983     color.ct.ahsl.hue        = h == -1 ? USHRT_MAX : (h % 360) * 100;
1984     color.ct.ahsl.saturation = s * 0x101;
1985     color.ct.ahsl.lightness  = l * 0x101;
1986     color.ct.ahsl.pad        = 0;
1987     return color;
1988 }
1989
1990 /*!
1991     \overload
1992     \since 4.6
1993
1994     Static convenience function that returns a QColor constructed from the HSV
1995     color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
1996     (alpha-channel, i.e. transparency).
1997
1998     All the values must be in the range 0.0-1.0.
1999
2000     \sa toHsl(), fromHsl(), isValid()
2001 */
2002 QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a)
2003 {
2004     if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
2005         || (s < qreal(0.0) || s > qreal(1.0))
2006         || (l < qreal(0.0) || l > qreal(1.0))
2007         || (a < qreal(0.0) || a > qreal(1.0))) {
2008         qWarning("QColor::fromHsvF: HSV parameters out of range");
2009         return QColor();
2010     }
2011
2012     QColor color;
2013     color.cspec = Hsl;
2014     color.ct.ahsl.alpha      = qRound(a * USHRT_MAX);
2015     color.ct.ahsl.hue        = (h == qreal(-1.0)) ? USHRT_MAX : qRound(h * 36000);
2016     if (color.ct.ahsl.hue == 36000)
2017         color.ct.ahsl.hue = 0;
2018     color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2019     color.ct.ahsl.lightness  = qRound(l * USHRT_MAX);
2020     color.ct.ahsl.pad        = 0;
2021     return color;
2022 }
2023
2024
2025 /*!
2026     Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2027     cyan, magenta, yellow, black, and alpha-channel (transparency) components
2028     of the color's CMYK value.
2029
2030     These components can be retrieved individually using the cyan(), magenta(),
2031     yellow(), black() and alpha() functions.
2032
2033     \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2034 */
2035 void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a)
2036 {
2037     if (!c || !m || !y || !k)
2038         return;
2039
2040     if (cspec != Invalid && cspec != Cmyk) {
2041         toCmyk().getCmyk(c, m, y, k, a);
2042         return;
2043     }
2044
2045     *c = ct.acmyk.cyan >> 8;
2046     *m = ct.acmyk.magenta >> 8;
2047     *y = ct.acmyk.yellow >> 8;
2048     *k = ct.acmyk.black >> 8;
2049
2050     if (a)
2051         *a = ct.acmyk.alpha >> 8;
2052 }
2053
2054 /*!
2055     Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2056     cyan, magenta, yellow, black, and alpha-channel (transparency) components
2057     of the color's CMYK value.
2058
2059     These components can be retrieved individually using the cyanF(),
2060     magentaF(), yellowF(), blackF() and alphaF() functions.
2061
2062     \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2063 */
2064 void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a)
2065 {
2066     if (!c || !m || !y || !k)
2067         return;
2068
2069     if (cspec != Invalid && cspec != Cmyk) {
2070         toCmyk().getCmykF(c, m, y, k, a);
2071         return;
2072     }
2073
2074     *c = ct.acmyk.cyan    / qreal(USHRT_MAX);
2075     *m = ct.acmyk.magenta / qreal(USHRT_MAX);
2076     *y = ct.acmyk.yellow  / qreal(USHRT_MAX);
2077     *k = ct.acmyk.black   / qreal(USHRT_MAX);
2078
2079     if (a)
2080         *a = ct.acmyk.alpha / qreal(USHRT_MAX);
2081 }
2082
2083 /*!
2084     Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2085     \a k (black), and \a a (alpha-channel, i.e. transparency).
2086
2087     All the values must be in the range 0-255.
2088
2089     \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2090 */
2091 void QColor::setCmyk(int c, int m, int y, int k, int a)
2092 {
2093     if (c < 0 || c > 255
2094         || m < 0 || m > 255
2095         || y < 0 || y > 255
2096         || k < 0 || k > 255
2097         || a < 0 || a > 255) {
2098         qWarning("QColor::setCmyk: CMYK parameters out of range");
2099         return;
2100     }
2101
2102     cspec = Cmyk;
2103     ct.acmyk.alpha   = a * 0x101;
2104     ct.acmyk.cyan    = c * 0x101;
2105     ct.acmyk.magenta = m * 0x101;
2106     ct.acmyk.yellow  = y * 0x101;
2107     ct.acmyk.black   = k * 0x101;
2108 }
2109
2110 /*!
2111     \overload
2112
2113     Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2114     \a k (black), and \a a (alpha-channel, i.e. transparency).
2115
2116     All the values must be in the range 0.0-1.0.
2117
2118     \sa getCmykF(), setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2119 */
2120 void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2121 {
2122     if (c < qreal(0.0) || c > qreal(1.0)
2123         || m < qreal(0.0) || m > qreal(1.0)
2124         || y < qreal(0.0) || y > qreal(1.0)
2125         || k < qreal(0.0) || k > qreal(1.0)
2126         || a < qreal(0.0) || a > qreal(1.0)) {
2127         qWarning("QColor::setCmykF: CMYK parameters out of range");
2128         return;
2129     }
2130
2131     cspec = Cmyk;
2132     ct.acmyk.alpha   = qRound(a * USHRT_MAX);
2133     ct.acmyk.cyan    = qRound(c * USHRT_MAX);
2134     ct.acmyk.magenta = qRound(m * USHRT_MAX);
2135     ct.acmyk.yellow  = qRound(y * USHRT_MAX);
2136     ct.acmyk.black   = qRound(k * USHRT_MAX);
2137 }
2138
2139 /*!
2140     Static convenience function that returns a QColor constructed from the
2141     given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2142     (black), and \a a (alpha-channel, i.e. transparency).
2143
2144     All the values must be in the range 0-255.
2145
2146     \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2147 */
2148 QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2149 {
2150     if (c < 0 || c > 255
2151         || m < 0 || m > 255
2152         || y < 0 || y > 255
2153         || k < 0 || k > 255
2154         || a < 0 || a > 255) {
2155         qWarning("QColor::fromCmyk: CMYK parameters out of range");
2156         return QColor();
2157     }
2158
2159     QColor color;
2160     color.cspec = Cmyk;
2161     color.ct.acmyk.alpha   = a * 0x101;
2162     color.ct.acmyk.cyan    = c * 0x101;
2163     color.ct.acmyk.magenta = m * 0x101;
2164     color.ct.acmyk.yellow  = y * 0x101;
2165     color.ct.acmyk.black   = k * 0x101;
2166     return color;
2167 }
2168
2169 /*!
2170     \overload
2171
2172     Static convenience function that returns a QColor constructed from the
2173     given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2174     (black), and \a a (alpha-channel, i.e. transparency).
2175
2176     All the values must be in the range 0.0-1.0.
2177
2178     \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2179 */
2180 QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2181 {
2182     if (c < qreal(0.0) || c > qreal(1.0)
2183         || m < qreal(0.0) || m > qreal(1.0)
2184         || y < qreal(0.0) || y > qreal(1.0)
2185         || k < qreal(0.0) || k > qreal(1.0)
2186         || a < qreal(0.0) || a > qreal(1.0)) {
2187         qWarning("QColor::fromCmykF: CMYK parameters out of range");
2188         return QColor();
2189     }
2190
2191     QColor color;
2192     color.cspec = Cmyk;
2193     color.ct.acmyk.alpha   = qRound(a * USHRT_MAX);
2194     color.ct.acmyk.cyan    = qRound(c * USHRT_MAX);
2195     color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2196     color.ct.acmyk.yellow  = qRound(y * USHRT_MAX);
2197     color.ct.acmyk.black   = qRound(k * USHRT_MAX);
2198     return color;
2199 }
2200
2201 /*!
2202     \fn QColor QColor::lighter(int factor) const
2203     \since 4.3
2204
2205     Returns a lighter (or darker) color, but does not change this object.
2206
2207     If the \a factor is greater than 100, this functions returns a lighter
2208     color. Setting \a factor to 150 returns a color that is 50% brighter. If
2209     the \a factor is less than 100, the return color is darker, but we
2210     recommend using the darker() function for this purpose. If the \a factor
2211     is 0 or negative, the return value is unspecified.
2212
2213     The function converts the current RGB color to HSV, multiplies the value
2214     (V) component by \a factor and converts the color back to RGB.
2215
2216     \sa darker(), isValid()
2217 */
2218
2219 /*!
2220     \obsolete
2221
2222     Use lighter(\a factor) instead.
2223 */
2224 QColor QColor::light(int factor) const
2225 {
2226     if (factor <= 0)                                // invalid lightness factor
2227         return *this;
2228     else if (factor < 100)                        // makes color darker
2229         return darker(10000 / factor);
2230
2231     QColor hsv = toHsv();
2232     int s = hsv.ct.ahsv.saturation;
2233     uint v = hsv.ct.ahsv.value;
2234
2235     v = (factor*v)/100;
2236     if (v > USHRT_MAX) {
2237         // overflow... adjust saturation
2238         s -= v - USHRT_MAX;
2239         if (s < 0)
2240             s = 0;
2241         v = USHRT_MAX;
2242     }
2243
2244     hsv.ct.ahsv.saturation = s;
2245     hsv.ct.ahsv.value = v;
2246
2247     // convert back to same color spec as original color
2248     return hsv.convertTo(cspec);
2249 }
2250
2251 /*!
2252     \fn QColor QColor::darker(int factor) const
2253     \since 4.3
2254
2255     Returns a darker (or lighter) color, but does not change this object.
2256
2257     If the \a factor is greater than 100, this functions returns a darker
2258     color. Setting \a factor to 300 returns a color that has one-third the
2259     brightness. If the \a factor is less than 100, the return color is lighter,
2260     but we recommend using the lighter() function for this purpose. If the
2261     \a factor is 0 or negative, the return value is unspecified.
2262
2263     The function converts the current RGB color to HSV, divides the value (V)
2264     component by \a factor and converts the color back to RGB.
2265
2266     \sa lighter(), isValid()
2267 */
2268
2269 /*!
2270     \obsolete
2271
2272     Use darker(\a factor) instead.
2273 */
2274 QColor QColor::dark(int factor) const
2275 {
2276     if (factor <= 0)                                // invalid darkness factor
2277         return *this;
2278     else if (factor < 100)                        // makes color lighter
2279         return lighter(10000 / factor);
2280
2281     QColor hsv = toHsv();
2282     hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2283
2284     // convert back to same color spec as original color
2285     return hsv.convertTo(cspec);
2286 }
2287
2288 /*!
2289     Assigns a copy of \a color to this color, and returns a reference to it.
2290 */
2291 QColor &QColor::operator=(const QColor &color)
2292 {
2293     cspec = color.cspec;
2294     ct.argb = color.ct.argb;
2295     return *this;
2296 }
2297
2298 /*! \overload
2299     Assigns a copy of \a color and returns a reference to this color.
2300  */
2301 QColor &QColor::operator=(Qt::GlobalColor color)
2302 {
2303     return operator=(QColor(color));
2304 }
2305
2306 /*!
2307     Returns true if this color has the same RGB and alpha values as \a color;
2308     otherwise returns false.
2309 */
2310 bool QColor::operator==(const QColor &color) const
2311 {
2312     if (cspec == Hsl && cspec == color.cspec) {
2313         return (ct.argb.alpha == color.ct.argb.alpha
2314                 && ((((ct.ahsl.hue % 36000) == (color.ct.ahsl.hue % 36000)))
2315                     || (ct.ahsl.hue == color.ct.ahsl.hue))
2316                 && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50
2317                     || ct.ahsl.lightness == 0
2318                     || color.ct.ahsl.lightness == 0
2319                     || ct.ahsl.lightness == USHRT_MAX
2320                     || color.ct.ahsl.lightness == USHRT_MAX)
2321                 && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
2322     } else {
2323         return (cspec == color.cspec
2324                 && ct.argb.alpha == color.ct.argb.alpha
2325                 && (((cspec == QColor::Hsv)
2326                      && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
2327                     || (ct.ahsv.hue == color.ct.ahsv.hue))
2328                 && ct.argb.green == color.ct.argb.green
2329                 && ct.argb.blue  == color.ct.argb.blue
2330                 && ct.argb.pad   == color.ct.argb.pad);
2331     }
2332 }
2333
2334 /*!
2335     Returns true if this color has a different RGB and alpha values from
2336     \a color; otherwise returns false.
2337 */
2338 bool QColor::operator!=(const QColor &color) const
2339 { return !operator==(color); }
2340
2341
2342 /*!
2343     Returns the color as a QVariant
2344 */
2345 QColor::operator QVariant() const
2346 {
2347     return QVariant(QVariant::Color, this);
2348 }
2349
2350 /*! \internal
2351
2352     Marks the color as invalid and sets all components to zero (alpha is set
2353     to fully opaque for compatibility with Qt 3).
2354 */
2355 void QColor::invalidate()
2356 {
2357     cspec = Invalid;
2358     ct.argb.alpha = USHRT_MAX;
2359     ct.argb.red = 0;
2360     ct.argb.green = 0;
2361     ct.argb.blue = 0;
2362     ct.argb.pad = 0;
2363 }
2364
2365 /*****************************************************************************
2366   QColor stream functions
2367  *****************************************************************************/
2368
2369 #ifndef QT_NO_DEBUG_STREAM
2370 QDebug operator<<(QDebug dbg, const QColor &c)
2371 {
2372     if (!c.isValid())
2373         dbg.nospace() << "QColor(Invalid)";
2374     else if (c.spec() == QColor::Rgb)
2375         dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2376     else if (c.spec() == QColor::Hsv)
2377         dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
2378     else if (c.spec() == QColor::Cmyk)
2379         dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
2380                       << c.blackF()<< ')';
2381     else if (c.spec() == QColor::Hsl)
2382         dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
2383
2384     return dbg.space();
2385 }
2386 #endif
2387
2388 #ifndef QT_NO_DATASTREAM
2389 /*!
2390     \fn QDataStream &operator<<(QDataStream &stream, const QColor &color)
2391     \relates QColor
2392
2393     Writes the \a color to the \a stream.
2394
2395     \sa {Serializing Qt Data Types}
2396 */
2397 QDataStream &operator<<(QDataStream &stream, const QColor &color)
2398 {
2399     if (stream.version() < 7) {
2400         if (!color.isValid())
2401             return stream << quint32(0x49000000);
2402         quint32 p = (quint32)color.rgb();
2403         if (stream.version() == 1) // Swap red and blue
2404             p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2405         return stream << p;
2406     }
2407
2408     qint8   s = color.cspec;
2409     quint16 a = color.ct.argb.alpha;
2410     quint16 r = color.ct.argb.red;
2411     quint16 g = color.ct.argb.green;
2412     quint16 b = color.ct.argb.blue;
2413     quint16 p = color.ct.argb.pad;
2414
2415     stream << s;
2416     stream << a;
2417     stream << r;
2418     stream << g;
2419     stream << b;
2420     stream << p;
2421
2422     return stream;
2423 }
2424
2425 /*!
2426     \fn QDataStream &operator>>(QDataStream &stream, QColor &color)
2427     \relates QColor
2428
2429     Reads the \a color from the \a stream.
2430
2431     \sa {Serializing Qt Data Types}
2432 */
2433 QDataStream &operator>>(QDataStream &stream, QColor &color)
2434 {
2435     if (stream.version() < 7) {
2436         quint32 p;
2437         stream >> p;
2438         if (p == 0x49000000) {
2439             color.invalidate();
2440             return stream;
2441         }
2442         if (stream.version() == 1) // Swap red and blue
2443             p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2444         color.setRgb(p);
2445         return stream;
2446     }
2447
2448     qint8 s;
2449     quint16 a, r, g, b, p;
2450     stream >> s;
2451     stream >> a;
2452     stream >> r;
2453     stream >> g;
2454     stream >> b;
2455     stream >> p;
2456
2457     color.cspec = QColor::Spec(s);
2458     color.ct.argb.alpha = a;
2459     color.ct.argb.red   = r;
2460     color.ct.argb.green = g;
2461     color.ct.argb.blue  = b;
2462     color.ct.argb.pad   = p;
2463
2464     return stream;
2465 }
2466 #endif // QT_NO_DATASTREAM
2467
2468
2469 /*****************************************************************************
2470   QColor global functions (documentation only)
2471  *****************************************************************************/
2472
2473 /*!
2474     \fn int qRed(QRgb rgb)
2475     \relates QColor
2476
2477     Returns the red component of the ARGB quadruplet \a rgb.
2478
2479     \sa qRgb(), QColor::red()
2480 */
2481
2482 /*!
2483     \fn int qGreen(QRgb rgb)
2484     \relates QColor
2485
2486     Returns the green component of the ARGB quadruplet \a rgb.
2487
2488     \sa qRgb(), QColor::green()
2489 */
2490
2491 /*!
2492     \fn int qBlue(QRgb rgb)
2493     \relates QColor
2494
2495     Returns the blue component of the ARGB quadruplet \a rgb.
2496
2497     \sa qRgb(), QColor::blue()
2498 */
2499
2500 /*!
2501     \fn int qAlpha(QRgb rgba)
2502     \relates QColor
2503
2504     Returns the alpha component of the ARGB quadruplet \a rgba.
2505
2506     \sa qRgb(), QColor::alpha()
2507 */
2508
2509 /*!
2510     \fn QRgb qRgb(int r, int g, int b)
2511     \relates QColor
2512
2513     Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}).
2514
2515     \sa qRgba(), qRed(), qGreen(), qBlue()
2516 */
2517
2518 /*!
2519     \fn QRgb qRgba(int r, int g, int b, int a)
2520     \relates QColor
2521
2522     Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}).
2523
2524     \sa qRgb(), qRed(), qGreen(), qBlue()
2525 */
2526
2527 /*!
2528     \fn int qGray(int r, int g, int b)
2529     \relates QColor
2530
2531     Returns a gray value (0 to 255) from the (\a r, \a g, \a b)
2532     triplet.
2533
2534     The gray value is calculated using the formula (\a r * 11 + \a g * 16 +
2535     \a b * 5)/32.
2536 */
2537
2538 /*!
2539     \fn int qGray(QRgb rgb)
2540     \overload
2541     \relates QColor
2542
2543     Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb.
2544
2545     The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32;
2546     the alpha-channel is ignored.
2547 */
2548
2549 /*!
2550     \fn QColor QColor::convertTo(Spec colorSpec) const
2551
2552     Creates a copy of \e this color in the format specified by \a colorSpec.
2553
2554     \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()
2555 */
2556
2557 /*!
2558     \typedef QRgb
2559     \relates QColor
2560
2561     An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
2562
2563     The type also holds a value for the alpha-channel. The default alpha
2564     channel is \c ff, i.e opaque. For more information, see the
2565     \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section.
2566
2567     \sa QColor::rgb(), QColor::rgba()
2568 */
2569
2570 QT_END_NAMESPACE