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