Fix QT_NO_DATASTREAM macro checks and improve readability
[qt-netbsd.git] / src / gui / painting / qcolor.cpp
blobfa833db572be1ba393af89869c492adbc42fe834
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** No Commercial Usage
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the either Technology Preview License Agreement or the
13 ** Beta Release License Agreement.
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
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"
50 #ifdef Q_WS_X11
51 # include "qapplication.h"
52 # include "qx11info_x11.h"
53 # include "private/qt_x11_p.h"
55 static bool allowX11ColorNames = false;
57 #endif
59 #include <math.h>
60 #include <stdio.h>
61 #include <limits.h>
63 QT_BEGIN_NAMESPACE
65 /*!
66 \class QColor
67 \brief The QColor class provides colors based on RGB, HSV or CMYK values.
69 \ingroup multimedia
70 \ingroup appearance
71 \mainclass
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.
80 \table
81 \row
82 \o \inlineimage qcolor-rgb.png
83 \o \inlineimage qcolor-hsv.png
84 \o \inlineimage qcolor-cmyk.png
85 \header
86 \o RGB \o HSV \o CMYK
87 \endtable
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.
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.
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.
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().
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.
133 QColor is platform and device independent. The QColormap class
134 maps the color to the hardware.
136 For more information about painting in general, see \l{The Paint
137 System} documentation.
139 \tableofcontents
141 \section1 Integer vs. Floating Point Precision
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.
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.
154 \section1 Alpha-Blended Drawing
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:
161 \snippet doc/src/snippets/code/src_gui_painting_qcolor.cpp 0
163 The code above produces the following output:
165 \img alphafill.png
167 Alpha-blended drawing is supported on Windows, Mac OS X, and on
168 X11 systems that have the X Render extension installed.
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.
177 \section1 Predefined Colors
179 There are 20 predefined QColors: Qt::white, Qt::black,
180 Qt::red, Qt::darkRed, Qt::green, Qt::darkGreen, Qt::blue,
181 Qt::darkBlue, Qt::cyan, Qt::darkCyan, Qt::magenta,
182 Qt::darkMagenta, Qt::yellow, Qt::darkYellow, Qt::gray,
183 Qt::darkGray, Qt::lightGray, Qt::color0, Qt::color1, and
184 Qt::transparent.
186 \img qt-colors.png Qt Colors
188 QColor provides the static colorNames() function which returns a
189 QStringList containing the color names Qt knows about.
191 The colors Qt::color0 (zero pixel value) and Qt::color1 (non-zero
192 pixel value) are special colors for drawing in QBitmaps. Painting with
193 Qt::color0 sets the bitmap bits to 0 (transparent, i.e. background), and
194 painting with Qt::color1 sets the bits to 1 (opaque, i.e. foreground).
196 \section1 The HSV Color Model
198 The RGB model is hardware-oriented. Its representation is close to
199 what most monitors show. In contrast, HSV represents color in a way
200 more suited to the human perception of color. For example, the
201 relationships "stronger than", "darker than", and "the opposite of"
202 are easily expressed in HSV but are much harder to express in RGB.
204 HSV, like RGB, has three components:
206 \list
207 \o H, for hue, is in the range 0 to 359 if the color is chromatic (not
208 gray), or meaningless if it is gray. It represents degrees on the
209 color wheel familiar to most people. Red is 0 (degrees), green is
210 120, and blue is 240.
212 \inlineimage qcolor-hue.png
214 \o S, for saturation, is in the range 0 to 255, and the bigger it is,
215 the stronger the color is. Grayish colors have saturation near 0; very
216 strong colors have saturation near 255.
218 \inlineimage qcolor-saturation.png
220 \o V, for value, is in the range 0 to 255 and represents lightness or
221 brightness of the color. 0 is black; 255 is as far from black as
222 possible.
224 \inlineimage qcolor-value.png
225 \endlist
227 Here are some examples: pure red is H=0, S=255, V=255; a dark red,
228 moving slightly towards the magenta, could be H=350 (equivalent to
229 -10), S=255, V=180; a grayish light red could have H about 0 (say
230 350-359 or 0-10), S about 50-100, and S=255.
232 Qt returns a hue value of -1 for achromatic colors. If you pass a
233 hue value that is too large, Qt forces it into range. Hue 360 or 720 is
234 treated as 0; hue 540 is treated as 180.
236 In addition to the standard HSV model, Qt provides an
237 alpha-channel to feature \l {QColor#Alpha-Blended
238 Drawing}{alpha-blended drawing}.
240 \section1 The CMYK Color Model
242 While the RGB and HSV color models are used for display on
243 computer monitors, the CMYK model is used in the four-color
244 printing process of printing presses and some hard-copy
245 devices.
247 CMYK has four components, all in the range 0-255: cyan (C),
248 magenta (M), yellow (Y) and black (K). Cyan, magenta and yellow
249 are called subtractive colors; the CMYK color model creates color
250 by starting with a white surface and then subtracting color by
251 applying the appropriate components. While combining cyan, magenta
252 and yellow gives the color black, subtracting one or more will
253 yield any other color. When combined in various percentages, these
254 three colors can create the entire spectrum of colors.
256 Mixing 100 percent of cyan, magenta and yellow \e does produce
257 black, but the result is unsatisfactory since it wastes ink,
258 increases drying time, and gives a muddy colour when printing. For
259 that reason, black is added in professional printing to provide a
260 solid black tone; hence the term 'four color process'.
262 In addition to the standard CMYK model, Qt provides an
263 alpha-channel to feature \l {QColor#Alpha-Blended
264 Drawing}{alpha-blended drawing}.
266 \sa QPalette, QBrush, QApplication::setColorSpec()
269 #define QCOLOR_INT_RANGE_CHECK(fn, var) \
270 do { \
271 if (var < 0 || var > 255) { \
272 qWarning(#fn": invalid value %d", var); \
273 var = qMax(0, qMin(var, 255)); \
275 } while (0)
277 #define QCOLOR_REAL_RANGE_CHECK(fn, var) \
278 do { \
279 if (var < qreal(0.0) || var > qreal(1.0)) { \
280 qWarning(#fn": invalid value %g", var); \
281 var = qMax(qreal(0.0), qMin(var, qreal(1.0))); \
283 } while (0)
285 /*****************************************************************************
286 QColor member functions
287 *****************************************************************************/
290 \enum QColor::Spec
292 The type of color specified, either RGB, HSV or CMYK.
294 \value Rgb
295 \value Hsv
296 \value Cmyk
297 \value Invalid
299 \sa spec(), convertTo()
303 \fn Spec QColor::spec() const
305 Returns how the color was specified.
307 \sa Spec, convertTo()
312 \fn QColor::QColor()
314 Constructs an invalid color with the RGB value (0, 0, 0). An
315 invalid color is a color that is not properly set up for the
316 underlying window system.
318 The alpha value of an invalid color is unspecified.
320 \sa isValid()
324 \overload
326 Constructs a new color with a color value of \a color.
328 \sa isValid(), {QColor#Predefined Colors}{Predefined Colors}
330 QColor::QColor(Qt::GlobalColor color)
332 #define QRGB(r, g, b) \
333 QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
334 #define QRGBA(r, g, b, a) \
335 QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
337 static const QRgb global_colors[] = {
338 QRGB(255, 255, 255), // Qt::color0
339 QRGB( 0, 0, 0), // Qt::color1
340 QRGB( 0, 0, 0), // black
341 QRGB(255, 255, 255), // white
343 * From the "The Palette Manager: How and Why" by Ron Gery,
344 * March 23, 1992, archived on MSDN:
346 * The Windows system palette is broken up into two
347 * sections, one with fixed colors and one with colors
348 * that can be changed by applications. The system palette
349 * predefines 20 entries; these colors are known as the
350 * static or reserved colors and consist of the 16 colors
351 * found in the Windows version 3.0 VGA driver and 4
352 * additional colors chosen for their visual appeal. The
353 * DEFAULT_PALETTE stock object is, as the name implies,
354 * the default palette selected into a device context (DC)
355 * and consists of these static colors. Applications can
356 * set the remaining 236 colors using the Palette Manager.
358 * The 20 reserved entries have indices in [0,9] and
359 * [246,255]. We reuse 17 of them.
361 QRGB(128, 128, 128), // index 248 medium gray
362 QRGB(160, 160, 164), // index 247 light gray
363 QRGB(192, 192, 192), // index 7 light gray
364 QRGB(255, 0, 0), // index 249 red
365 QRGB( 0, 255, 0), // index 250 green
366 QRGB( 0, 0, 255), // index 252 blue
367 QRGB( 0, 255, 255), // index 254 cyan
368 QRGB(255, 0, 255), // index 253 magenta
369 QRGB(255, 255, 0), // index 251 yellow
370 QRGB(128, 0, 0), // index 1 dark red
371 QRGB( 0, 128, 0), // index 2 dark green
372 QRGB( 0, 0, 128), // index 4 dark blue
373 QRGB( 0, 128, 128), // index 6 dark cyan
374 QRGB(128, 0, 128), // index 5 dark magenta
375 QRGB(128, 128, 0), // index 3 dark yellow
376 QRGBA(0, 0, 0, 0) // transparent
378 #undef QRGB
379 #undef QRGBA
381 setRgb(qRed(global_colors[color]),
382 qGreen(global_colors[color]),
383 qBlue(global_colors[color]),
384 qAlpha(global_colors[color]));
388 \fn QColor::QColor(int r, int g, int b, int a = 255)
390 Constructs a color with the RGB value \a r, \a g, \a b, and the
391 alpha-channel (transparency) value of \a a.
393 The color is left invalid if any of the arguments are invalid.
395 \sa setRgba(), isValid()
399 Constructs a color with the value \a color. The alpha component is
400 ignored and set to solid.
402 \sa fromRgb(), isValid()
405 QColor::QColor(QRgb color)
407 cspec = Rgb;
408 ct.argb.alpha = 0xffff;
409 ct.argb.red = qRed(color) * 0x101;
410 ct.argb.green = qGreen(color) * 0x101;
411 ct.argb.blue = qBlue(color) * 0x101;
412 ct.argb.pad = 0;
417 \internal
419 Constructs a color with the given \a spec.
421 This function is primarly present to avoid that QColor::Invalid
422 becomes a valid color by accident.
425 QColor::QColor(Spec spec)
427 switch (spec) {
428 case Invalid:
429 invalidate();
430 break;
431 case Rgb:
432 setRgb(0, 0, 0);
433 break;
434 case Hsv:
435 setHsv(0, 0, 0);
436 break;
437 case Cmyk:
438 setCmyk(0, 0, 0, 0);
439 break;
444 \fn QColor::QColor(const QString &name)
446 Constructs a named color in the same way as setNamedColor() using
447 the given \a name.
449 The color is left invalid if the \a name cannot be parsed.
451 \sa setNamedColor(), name(), isValid()
455 \fn QColor::QColor(const char *name)
457 Constructs a named color in the same way as setNamedColor() using
458 the given \a name.
460 The color is left invalid if the \a name cannot be parsed.
462 \sa setNamedColor(), name(), isValid()
466 \fn QColor::QColor(const QColor &color)
468 Constructs a color that is a copy of \a color.
470 \sa isValid()
474 \fn bool QColor::isValid() const
476 Returns true if the color is valid; otherwise returns false.
480 Returns the name of the color in the format "#RRGGBB"; i.e. a "#"
481 character followed by three two-digit hexadecimal numbers.
483 \sa setNamedColor()
486 QString QColor::name() const
488 QString s;
489 s.sprintf("#%02x%02x%02x", red(), green(), blue());
490 return s;
494 Sets the RGB value of this QColor to \a name, which may be in one
495 of these formats:
497 \list
498 \i #RGB (each of R, G, and B is a single hex digit)
499 \i #RRGGBB
500 \i #RRRGGGBBB
501 \i #RRRRGGGGBBBB
502 \i A name from the list of colors defined in the list of \l{SVG color keyword names}
503 provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
504 These color names work on all platforms. Note that these color names are \i not the
505 same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
506 refer to the same color.
507 \i \c transparent - representing the absence of a color.
508 \i \e{X11 only}: If allowX11ColorNames() returns true, any valid X11 color name. See
509 the documentation for \c XParseColor() for information about valid X11 color names.
510 \endlist
512 The color is invalid if \a name cannot be parsed.
514 \sa QColor(), name(), isValid(), allowX11ColorNames()
517 void QColor::setNamedColor(const QString &name)
519 if (name.isEmpty()) {
520 invalidate();
521 return;
524 if (name.startsWith(QLatin1Char('#'))) {
525 QRgb rgb;
526 if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
527 setRgb(rgb);
528 } else {
529 invalidate();
531 return;
534 #ifndef QT_NO_COLORNAMES
535 QRgb rgb;
536 if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
537 setRgba(rgb);
538 } else
539 #endif
541 #ifdef Q_WS_X11
542 XColor result;
543 if (allowX11ColorNames()
544 && QApplication::instance()
545 && QX11Info::display()
546 && XParseColor(QX11Info::display(), QX11Info::appColormap(), name.toLatin1().constData(), &result)) {
547 setRgb(result.red >> 8, result.green >> 8, result.blue >> 8);
548 } else
549 #endif
551 qWarning("QColor::setNamedColor: Unknown color name '%s'", name.toLatin1().constData());
552 invalidate();
558 Returns a QStringList containing the color names Qt knows about.
560 \sa {QColor#Predefined Colors}{Predefined Colors}
562 QStringList QColor::colorNames()
564 #ifndef QT_NO_COLORNAMES
565 return qt_get_colornames();
566 #else
567 return QStringList();
568 #endif
572 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
573 saturation, value, and alpha-channel (transparency) components of the
574 color's HSV value.
576 These components can be retrieved individually using the hueF(),
577 saturationF(), valueF() and alphaF() functions.
579 \sa setHsv() {QColor#The HSV Color Model}{The HSV Color Model}
581 void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
583 if (!h || !s || !v)
584 return;
586 if (cspec != Invalid && cspec != Hsv) {
587 toHsv().getHsvF(h, s, v, a);
588 return;
591 *h = ct.ahsv.hue == USHRT_MAX ? -1.0 : ct.ahsv.hue / 36000.0;
592 *s = ct.ahsv.saturation / qreal(USHRT_MAX);
593 *v = ct.ahsv.value / qreal(USHRT_MAX);
595 if (a)
596 *a = ct.ahsv.alpha / qreal(USHRT_MAX);
600 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
601 saturation, value, and alpha-channel (transparency) components of the
602 color's HSV value.
604 These components can be retrieved individually using the hue(),
605 saturation(), value() and alpha() functions.
607 \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
609 void QColor::getHsv(int *h, int *s, int *v, int *a) const
611 if (!h || !s || !v)
612 return;
614 if (cspec != Invalid && cspec != Hsv) {
615 toHsv().getHsv(h, s, v, a);
616 return;
619 *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
620 *s = ct.ahsv.saturation >> 8;
621 *v = ct.ahsv.value >> 8;
623 if (a)
624 *a = ct.ahsv.alpha >> 8;
628 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
629 the value and \a a is the alpha component of the HSV color.
631 All the values must be in the range 0.0-1.0.
633 \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV
634 Color Model}
636 void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a)
638 if (((h < 0.0 || h > 1.0) && h != -1.0)
639 || (s < 0.0 || s > 1.0)
640 || (v < 0.0 || v > 1.0)
641 || (a < 0.0 || a > 1.0)) {
642 qWarning("QColor::setHsvF: HSV parameters out of range");
643 return;
646 cspec = Hsv;
647 ct.ahsv.alpha = qRound(a * USHRT_MAX);
648 ct.ahsv.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000);
649 ct.ahsv.saturation = qRound(s * USHRT_MAX);
650 ct.ahsv.value = qRound(v * USHRT_MAX);
651 ct.ahsv.pad = 0;
655 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
656 the value and \a a is the alpha component of the HSV color.
658 The saturation, value and alpha-channel values must be in the range 0-255,
659 and the hue value must be greater than -1.
661 \sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV
662 Color Model}
664 void QColor::setHsv(int h, int s, int v, int a)
666 if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
667 qWarning("QColor::setHsv: HSV parameters out of range");
668 invalidate();
669 return;
672 cspec = Hsv;
673 ct.ahsv.alpha = a * 0x101;
674 ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
675 ct.ahsv.saturation = s * 0x101;
676 ct.ahsv.value = v * 0x101;
677 ct.ahsv.pad = 0;
681 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
682 green, blue, and alpha-channel (transparency) components of the color's
683 RGB value.
685 These components can be retrieved individually using the redF(), greenF(),
686 blueF() and alphaF() functions.
688 \sa rgb(), setRgb()
690 void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
692 if (!r || !g || !b)
693 return;
695 if (cspec != Invalid && cspec != Rgb) {
696 toRgb().getRgbF(r, g, b, a);
697 return;
700 *r = ct.argb.red / qreal(USHRT_MAX);
701 *g = ct.argb.green / qreal(USHRT_MAX);
702 *b = ct.argb.blue / qreal(USHRT_MAX);
704 if (a)
705 *a = ct.argb.alpha / qreal(USHRT_MAX);
710 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
711 green, blue, and alpha-channel (transparency) components of the color's
712 RGB value.
714 These components can be retrieved individually using the red(), green(),
715 blue() and alpha() functions.
717 \sa rgb(), setRgb()
719 void QColor::getRgb(int *r, int *g, int *b, int *a) const
721 if (!r || !g || !b)
722 return;
724 if (cspec != Invalid && cspec != Rgb) {
725 toRgb().getRgb(r, g, b, a);
726 return;
729 *r = ct.argb.red >> 8;
730 *g = ct.argb.green >> 8;
731 *b = ct.argb.blue >> 8;
733 if (a)
734 *a = ct.argb.alpha >> 8;
738 \obsolete
739 \fn void QColor::getRgba(int *r, int *g, int *b, int *a) const
741 Use getRgb() instead.
745 \fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
747 Sets the color channels of this color to \a r (red), \a g (green),
748 \a b (blue) and \a a (alpha, transparency).
750 All values must be in the range 0.0-1.0.
752 \sa rgb(), getRgbF(), setRgb()
754 void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
756 if (r < 0.0 || r > 1.0
757 || g < 0.0 || g > 1.0
758 || b < 0.0 || b > 1.0
759 || a < 0.0 || a > 1.0) {
760 qWarning("QColor::setRgbF: RGB parameters out of range");
761 invalidate();
762 return;
765 cspec = Rgb;
766 ct.argb.alpha = qRound(a * USHRT_MAX);
767 ct.argb.red = qRound(r * USHRT_MAX);
768 ct.argb.green = qRound(g * USHRT_MAX);
769 ct.argb.blue = qRound(b * USHRT_MAX);
770 ct.argb.pad = 0;
774 Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
776 All the values must be in the range 0-255.
778 \sa rgb(), getRgb(), setRgbF()
780 void QColor::setRgb(int r, int g, int b, int a)
782 if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
783 qWarning("QColor::setRgb: RGB parameters out of range");
784 invalidate();
785 return;
788 cspec = Rgb;
789 ct.argb.alpha = a * 0x101;
790 ct.argb.red = r * 0x101;
791 ct.argb.green = g * 0x101;
792 ct.argb.blue = b * 0x101;
793 ct.argb.pad = 0;
797 \obsolete
798 \fn void QColor::setRgba(int r, int g, int b, int a)
800 Use setRgb() instead.
804 \fn QRgb QColor::rgba() const
806 Returns the RGB value of the color. Unlike rgb(), the alpha is not
807 stripped.
809 For an invalid color, the alpha value of the returned color is unspecified.
811 \sa setRgba(), rgb()
814 QRgb QColor::rgba() const
816 if (cspec != Invalid && cspec != Rgb)
817 return toRgb().rgba();
818 return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
822 Sets the RGBA value to \a rgba. Unlike setRgb(QRgb rgb), this function does
823 not ignore the alpha.
825 \sa rgba(), rgb()
827 void QColor::setRgba(QRgb rgba)
829 cspec = Rgb;
830 ct.argb.alpha = qAlpha(rgba) * 0x101;
831 ct.argb.red = qRed(rgba) * 0x101;
832 ct.argb.green = qGreen(rgba) * 0x101;
833 ct.argb.blue = qBlue(rgba) * 0x101;
834 ct.argb.pad = 0;
838 \fn QRgb QColor::rgb() const
840 Returns the RGB value of the color. The alpha is stripped for
841 compatibility.
843 \sa getRgb(), rgba()
845 QRgb QColor::rgb() const
847 if (cspec != Invalid && cspec != Rgb)
848 return toRgb().rgb();
849 return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
853 \overload
855 Sets the RGB value to \a rgb, ignoring the alpha.
857 void QColor::setRgb(QRgb rgb)
859 cspec = Rgb;
860 ct.argb.alpha = 0xffff;
861 ct.argb.red = qRed(rgb) * 0x101;
862 ct.argb.green = qGreen(rgb) * 0x101;
863 ct.argb.blue = qBlue(rgb) * 0x101;
864 ct.argb.pad = 0;
868 Returns the alpha color component of this color.
870 \sa setAlpha(), alphaF(), {QColor#Alpha-Blended
871 Drawing}{Alpha-Blended Drawing}
873 int QColor::alpha() const
874 { return ct.argb.alpha >> 8; }
878 Sets the alpha of this color to \a alpha. Integer alpha is specified in the
879 range 0-255.
881 \sa alpha(), alphaF(), {QColor#Alpha-Blended
882 Drawing}{Alpha-Blended Drawing}
885 void QColor::setAlpha(int alpha)
887 QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
888 ct.argb.alpha = alpha * 0x101;
892 Returns the alpha color component of this color.
894 \sa setAlphaF(), alpha(), {QColor#Alpha-Blended
895 Drawing}{Alpha-Blended Drawing}
897 qreal QColor::alphaF() const
898 { return ct.argb.alpha / qreal(USHRT_MAX); }
901 Sets the alpha of this color to \a alpha. qreal alpha is specified in the
902 range 0.0-1.0.
904 \sa alphaF(), alpha(), {QColor#Alpha-Blended
905 Drawing}{Alpha-Blended Drawing}
908 void QColor::setAlphaF(qreal alpha)
910 QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
911 qreal tmp = alpha * USHRT_MAX;
912 ct.argb.alpha = qRound(tmp);
917 Returns the red color component of this color.
919 \sa setRed(), redF(), getRgb()
921 int QColor::red() const
923 if (cspec != Invalid && cspec != Rgb)
924 return toRgb().red();
925 return ct.argb.red >> 8;
929 Sets the red color component of this color to \a red. Integer components
930 are specified in the range 0-255.
932 \sa red(), redF(), setRgb()
934 void QColor::setRed(int red)
936 QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
937 if (cspec != Rgb)
938 setRgb(red, green(), blue(), alpha());
939 else
940 ct.argb.red = red * 0x101;
944 Returns the green color component of this color.
946 \sa setGreen(), greenF(), getRgb()
948 int QColor::green() const
950 if (cspec != Invalid && cspec != Rgb)
951 return toRgb().green();
952 return ct.argb.green >> 8;
956 Sets the green color component of this color to \a green. Integer
957 components are specified in the range 0-255.
959 \sa green(), greenF(), setRgb()
961 void QColor::setGreen(int green)
963 QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
964 if (cspec != Rgb)
965 setRgb(red(), green, blue(), alpha());
966 else
967 ct.argb.green = green * 0x101;
972 Returns the blue color component of this color.
974 \sa setBlue(), blueF(), getRgb()
976 int QColor::blue() const
978 if (cspec != Invalid && cspec != Rgb)
979 return toRgb().blue();
980 return ct.argb.blue >> 8;
985 Sets the blue color component of this color to \a blue. Integer components
986 are specified in the range 0-255.
988 \sa blue(), blueF(), setRgb()
990 void QColor::setBlue(int blue)
992 QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
993 if (cspec != Rgb)
994 setRgb(red(), green(), blue, alpha());
995 else
996 ct.argb.blue = blue * 0x101;
1000 Returns the red color component of this color.
1002 \sa setRedF(), red(), getRgbF()
1004 qreal QColor::redF() const
1006 if (cspec != Invalid && cspec != Rgb)
1007 return toRgb().redF();
1008 return ct.argb.red / qreal(USHRT_MAX);
1013 Sets the red color component of this color to \a red. Float components
1014 are specified in the range 0.0-1.0.
1016 \sa redF(), red(), setRgbF()
1018 void QColor::setRedF(qreal red)
1020 QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red);
1021 if (cspec != Rgb)
1022 setRgbF(red, greenF(), blueF(), alphaF());
1023 else
1024 ct.argb.red = qRound(red * USHRT_MAX);
1028 Returns the green color component of this color.
1030 \sa setGreenF(), green(), getRgbF()
1032 qreal QColor::greenF() const
1034 if (cspec != Invalid && cspec != Rgb)
1035 return toRgb().greenF();
1036 return ct.argb.green / qreal(USHRT_MAX);
1041 Sets the green color component of this color to \a green. Float components
1042 are specified in the range 0.0-1.0.
1044 \sa greenF(), green(), setRgbF()
1046 void QColor::setGreenF(qreal green)
1048 QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green);
1049 if (cspec != Rgb)
1050 setRgbF(redF(), green, blueF(), alphaF());
1051 else
1052 ct.argb.green = qRound(green * USHRT_MAX);
1056 Returns the blue color component of this color.
1058 \sa setBlueF(), blue(), getRgbF()
1060 qreal QColor::blueF() const
1062 if (cspec != Invalid && cspec != Rgb)
1063 return toRgb().blueF();
1064 return ct.argb.blue / qreal(USHRT_MAX);
1068 Sets the blue color component of this color to \a blue. Float components
1069 are specified in the range 0.0-1.0.
1071 \sa blueF(), blue(), setRgbF()
1073 void QColor::setBlueF(qreal blue)
1075 QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue);
1076 if (cspec != Rgb)
1077 setRgbF(redF(), greenF(), blue, alphaF());
1078 else
1079 ct.argb.blue = qRound(blue * USHRT_MAX);
1083 Returns the hue color component of this color.
1085 \sa hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1086 Model}
1088 int QColor::hue() const
1090 if (cspec != Invalid && cspec != Hsv)
1091 return toHsv().hue();
1092 return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1096 Returns the saturation color component of this color.
1098 \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1099 Model}
1101 int QColor::saturation() const
1103 if (cspec != Invalid && cspec != Hsv)
1104 return toHsv().saturation();
1105 return ct.ahsv.saturation >> 8;
1109 Returns the value color component of this color.
1111 \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1112 Model}
1114 int QColor::value() const
1116 if (cspec != Invalid && cspec != Hsv)
1117 return toHsv().value();
1118 return ct.ahsv.value >> 8;
1122 Returns the hue color component of this color.
1124 \sa hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1125 Model}
1127 qreal QColor::hueF() const
1129 if (cspec != Invalid && cspec != Hsv)
1130 return toHsv().hueF();
1131 return ct.ahsv.hue == USHRT_MAX ? -1.0 : ct.ahsv.hue / 36000.0;
1135 Returns the saturation color component of this color.
1137 \sa saturation() getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1138 Model}
1140 qreal QColor::saturationF() const
1142 if (cspec != Invalid && cspec != Hsv)
1143 return toHsv().saturationF();
1144 return ct.ahsv.saturation / qreal(USHRT_MAX);
1148 Returns the value color component of this color.
1150 \sa value() getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1151 Model}
1153 qreal QColor::valueF() const
1155 if (cspec != Invalid && cspec != Hsv)
1156 return toHsv().valueF();
1157 return ct.ahsv.value / qreal(USHRT_MAX);
1161 Returns the cyan color component of this color.
1163 \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1164 Color Model}
1166 int QColor::cyan() const
1168 if (cspec != Invalid && cspec != Cmyk)
1169 return toCmyk().cyan();
1170 return ct.acmyk.cyan >> 8;
1174 Returns the magenta color component of this color.
1176 \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1177 Color Model}
1179 int QColor::magenta() const
1181 if (cspec != Invalid && cspec != Cmyk)
1182 return toCmyk().magenta();
1183 return ct.acmyk.magenta >> 8;
1187 Returns the yellow color component of this color.
1189 \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1190 Color Model}
1192 int QColor::yellow() const
1194 if (cspec != Invalid && cspec != Cmyk)
1195 return toCmyk().yellow();
1196 return ct.acmyk.yellow >> 8;
1200 Returns the black color component of this color.
1202 \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1203 Color Model}
1206 int QColor::black() const
1208 if (cspec != Invalid && cspec != Cmyk)
1209 return toCmyk().black();
1210 return ct.acmyk.black >> 8;
1214 Returns the cyan color component of this color.
1216 \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1217 Color Model}
1219 qreal QColor::cyanF() const
1221 if (cspec != Invalid && cspec != Cmyk)
1222 return toCmyk().cyanF();
1223 return ct.acmyk.cyan / qreal(USHRT_MAX);
1227 Returns the magenta color component of this color.
1229 \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1230 Color Model}
1232 qreal QColor::magentaF() const
1234 if (cspec != Invalid && cspec != Cmyk)
1235 return toCmyk().magentaF();
1236 return ct.acmyk.magenta / qreal(USHRT_MAX);
1240 Returns the yellow color component of this color.
1242 \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1243 Color Model}
1245 qreal QColor::yellowF() const
1247 if (cspec != Invalid && cspec != Cmyk)
1248 return toCmyk().yellowF();
1249 return ct.acmyk.yellow / qreal(USHRT_MAX);
1253 Returns the black color component of this color.
1255 \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1256 Color Model}
1258 qreal QColor::blackF() const
1260 if (cspec != Invalid && cspec != Cmyk)
1261 return toCmyk().blackF();
1262 return ct.acmyk.black / qreal(USHRT_MAX);
1266 Create and returns an RGB QColor based on this color.
1268 \sa fromRgb(), convertTo(), isValid()
1270 QColor QColor::toRgb() const
1272 if (!isValid() || cspec == Rgb)
1273 return *this;
1275 QColor color;
1276 color.cspec = Rgb;
1277 color.ct.argb.alpha = ct.argb.alpha;
1278 color.ct.argb.pad = 0;
1280 switch (cspec) {
1281 case Hsv:
1283 if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
1284 // achromatic case
1285 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
1286 break;
1289 // chromatic case
1290 const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / 6000.;
1291 const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX);
1292 const qreal v = ct.ahsv.value / qreal(USHRT_MAX);
1293 const int i = int(h);
1294 const qreal f = h - i;
1295 const qreal p = v * (1.0 - s);
1297 if (i & 1) {
1298 const qreal q = v * (1.0 - (s * f));
1300 switch (i) {
1301 case 1:
1302 color.ct.argb.red = qRound(q * USHRT_MAX);
1303 color.ct.argb.green = qRound(v * USHRT_MAX);
1304 color.ct.argb.blue = qRound(p * USHRT_MAX);
1305 break;
1306 case 3:
1307 color.ct.argb.red = qRound(p * USHRT_MAX);
1308 color.ct.argb.green = qRound(q * USHRT_MAX);
1309 color.ct.argb.blue = qRound(v * USHRT_MAX);
1310 break;
1311 case 5:
1312 color.ct.argb.red = qRound(v * USHRT_MAX);
1313 color.ct.argb.green = qRound(p * USHRT_MAX);
1314 color.ct.argb.blue = qRound(q * USHRT_MAX);
1315 break;
1317 } else {
1318 const qreal t = v * (1.0 - (s * (1.0 - f)));
1320 switch (i) {
1321 case 0:
1322 color.ct.argb.red = qRound(v * USHRT_MAX);
1323 color.ct.argb.green = qRound(t * USHRT_MAX);
1324 color.ct.argb.blue = qRound(p * USHRT_MAX);
1325 break;
1326 case 2:
1327 color.ct.argb.red = qRound(p * USHRT_MAX);
1328 color.ct.argb.green = qRound(v * USHRT_MAX);
1329 color.ct.argb.blue = qRound(t * USHRT_MAX);
1330 break;
1331 case 4:
1332 color.ct.argb.red = qRound(t * USHRT_MAX);
1333 color.ct.argb.green = qRound(p * USHRT_MAX);
1334 color.ct.argb.blue = qRound(v * USHRT_MAX);
1335 break;
1338 break;
1340 case Cmyk:
1342 const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX);
1343 const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX);
1344 const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX);
1345 const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
1347 color.ct.argb.red = qRound((1.0 - (c * (1.0 - k) + k)) * USHRT_MAX);
1348 color.ct.argb.green = qRound((1.0 - (m * (1.0 - k) + k)) * USHRT_MAX);
1349 color.ct.argb.blue = qRound((1.0 - (y * (1.0 - k) + k)) * USHRT_MAX);
1350 break;
1352 default:
1353 break;
1356 return color;
1360 #define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
1361 #define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
1365 Creates and returns an HSV QColor based on this color.
1367 \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color
1368 Model}{The HSV Color Model}
1370 QColor QColor::toHsv() const
1372 if (!isValid() || cspec == Hsv)
1373 return *this;
1375 if (cspec != Rgb)
1376 return toRgb().toHsv();
1378 QColor color;
1379 color.cspec = Hsv;
1380 color.ct.ahsv.alpha = ct.argb.alpha;
1381 color.ct.ahsv.pad = 0;
1383 const qreal r = ct.argb.red / qreal(USHRT_MAX);
1384 const qreal g = ct.argb.green / qreal(USHRT_MAX);
1385 const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1386 const qreal max = Q_MAX_3(r, g, b);
1387 const qreal min = Q_MIN_3(r, g, b);
1388 const qreal delta = max - min;
1389 color.ct.ahsv.value = qRound(max * USHRT_MAX);
1390 if (qFuzzyIsNull(delta)) {
1391 // achromatic case, hue is undefined
1392 color.ct.ahsv.hue = USHRT_MAX;
1393 color.ct.ahsv.saturation = 0;
1394 } else {
1395 // chromatic case
1396 qreal hue = 0;
1397 color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
1398 if (qFuzzyCompare(r, max)) {
1399 hue = ((g - b) /delta);
1400 } else if (qFuzzyCompare(g, max)) {
1401 hue = (2.0 + (b - r) / delta);
1402 } else if (qFuzzyCompare(b, max)) {
1403 hue = (4.0 + (r - g) / delta);
1404 } else {
1405 Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1407 hue *= 60.0;
1408 if (hue < 0.0)
1409 hue += 360.0;
1410 color.ct.ahsv.hue = qRound(hue * 100);
1413 return color;
1417 Creates and returns a CMYK QColor based on this color.
1419 \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color
1420 Model}{The CMYK Color Model}
1422 QColor QColor::toCmyk() const
1424 if (!isValid() || cspec == Cmyk)
1425 return *this;
1426 if (cspec != Rgb)
1427 return toRgb().toCmyk();
1429 QColor color;
1430 color.cspec = Cmyk;
1431 color.ct.acmyk.alpha = ct.argb.alpha;
1433 // rgb -> cmy
1434 const qreal r = ct.argb.red / qreal(USHRT_MAX);
1435 const qreal g = ct.argb.green / qreal(USHRT_MAX);
1436 const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1437 qreal c = 1.0 - r;
1438 qreal m = 1.0 - g;
1439 qreal y = 1.0 - b;
1441 // cmy -> cmyk
1442 const qreal k = qMin(c, qMin(m, y));
1444 if (!qFuzzyIsNull(k - 1)) {
1445 c = (c - k) / (1.0 - k);
1446 m = (m - k) / (1.0 - k);
1447 y = (y - k) / (1.0 - k);
1450 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
1451 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
1452 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
1453 color.ct.acmyk.black = qRound(k * USHRT_MAX);
1455 return color;
1458 QColor QColor::convertTo(QColor::Spec colorSpec) const
1460 if (colorSpec == cspec)
1461 return *this;
1462 switch (colorSpec) {
1463 case Rgb:
1464 return toRgb();
1465 case Hsv:
1466 return toHsv();
1467 case Cmyk:
1468 return toCmyk();
1469 case Invalid:
1470 break;
1472 return QColor(); // must be invalid
1477 Static convenience function that returns a QColor constructed from the
1478 given QRgb value \a rgb.
1480 The alpha component of \a rgb is ignored (i.e. it is automatically set to
1481 255), use the fromRgba() function to include the alpha-channel specified by
1482 the given QRgb value.
1484 \sa fromRgba(), fromRgbF(), toRgb(), isValid()
1487 QColor QColor::fromRgb(QRgb rgb)
1489 return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
1494 Static convenience function that returns a QColor constructed from the
1495 given QRgb value \a rgba.
1497 Unlike the fromRgb() function, the alpha-channel specified by the given
1498 QRgb value is included.
1500 \sa fromRgb(), isValid()
1503 QColor QColor::fromRgba(QRgb rgba)
1505 return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
1509 Static convenience function that returns a QColor constructed from the RGB
1510 color values, \a r (red), \a g (green), \a b (blue), and \a a
1511 (alpha-channel, i.e. transparency).
1513 All the values must be in the range 0-255.
1515 \sa toRgb(), fromRgbF(), isValid()
1517 QColor QColor::fromRgb(int r, int g, int b, int a)
1519 if (r < 0 || r > 255
1520 || g < 0 || g > 255
1521 || b < 0 || b > 255
1522 || a < 0 || a > 255) {
1523 qWarning("QColor::fromRgb: RGB parameters out of range");
1524 return QColor();
1527 QColor color;
1528 color.cspec = Rgb;
1529 color.ct.argb.alpha = a * 0x101;
1530 color.ct.argb.red = r * 0x101;
1531 color.ct.argb.green = g * 0x101;
1532 color.ct.argb.blue = b * 0x101;
1533 color.ct.argb.pad = 0;
1534 return color;
1538 Static convenience function that returns a QColor constructed from the RGB
1539 color values, \a r (red), \a g (green), \a b (blue), and \a a
1540 (alpha-channel, i.e. transparency).
1542 All the values must be in the range 0.0-1.0.
1544 \sa fromRgb(), toRgb(), isValid()
1546 QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a)
1548 if (r < 0.0 || r > 1.0
1549 || g < 0.0 || g > 1.0
1550 || b < 0.0 || b > 1.0
1551 || a < 0.0 || a > 1.0) {
1552 qWarning("QColor::fromRgbF: RGB parameters out of range");
1553 return QColor();
1556 QColor color;
1557 color.cspec = Rgb;
1558 color.ct.argb.alpha = qRound(a * USHRT_MAX);
1559 color.ct.argb.red = qRound(r * USHRT_MAX);
1560 color.ct.argb.green = qRound(g * USHRT_MAX);
1561 color.ct.argb.blue = qRound(b * USHRT_MAX);
1562 color.ct.argb.pad = 0;
1563 return color;
1567 Static convenience function that returns a QColor constructed from the HSV
1568 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1569 (alpha-channel, i.e. transparency).
1571 The value of \a s, \a v, and \a a must all be in the range 0-255; the value
1572 of \a h must be in the range 0-359.
1574 \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color
1575 Model}{The HSV Color Model}
1577 QColor QColor::fromHsv(int h, int s, int v, int a)
1579 if (((h < 0 || h >= 360) && h != -1)
1580 || s < 0 || s > 255
1581 || v < 0 || v > 255
1582 || a < 0 || a > 255) {
1583 qWarning("QColor::fromHsv: HSV parameters out of range");
1584 return QColor();
1587 QColor color;
1588 color.cspec = Hsv;
1589 color.ct.ahsv.alpha = a * 0x101;
1590 color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1591 color.ct.ahsv.saturation = s * 0x101;
1592 color.ct.ahsv.value = v * 0x101;
1593 color.ct.ahsv.pad = 0;
1594 return color;
1598 \overload
1600 Static convenience function that returns a QColor constructed from the HSV
1601 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1602 (alpha-channel, i.e. transparency).
1604 All the values must be in the range 0.0-1.0.
1606 \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color
1607 Model}{The HSV Color Model}
1609 QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a)
1611 if (((h < 0.0 || h > 1.0) && h != -1.0)
1612 || (s < 0.0 || s > 1.0)
1613 || (v < 0.0 || v > 1.0)
1614 || (a < 0.0 || a > 1.0)) {
1615 qWarning("QColor::fromHsvF: HSV parameters out of range");
1616 return QColor();
1619 QColor color;
1620 color.cspec = Hsv;
1621 color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
1622 color.ct.ahsv.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000);
1623 color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
1624 color.ct.ahsv.value = qRound(v * USHRT_MAX);
1625 color.ct.ahsv.pad = 0;
1626 return color;
1630 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
1631 cyan, magenta, yellow, black, and alpha-channel (transparency) components
1632 of the color's CMYK value.
1634 These components can be retrieved individually using the cyan(), magenta(),
1635 yellow(), black() and alpha() functions.
1637 \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1639 void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a)
1641 if (!c || !m || !y || !k)
1642 return;
1644 if (cspec != Invalid && cspec != Cmyk) {
1645 toCmyk().getCmyk(c, m, y, k, a);
1646 return;
1649 *c = ct.acmyk.cyan >> 8;
1650 *m = ct.acmyk.magenta >> 8;
1651 *y = ct.acmyk.yellow >> 8;
1652 *k = ct.acmyk.black >> 8;
1654 if (a)
1655 *a = ct.acmyk.alpha >> 8;
1659 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
1660 cyan, magenta, yellow, black, and alpha-channel (transparency) components
1661 of the color's CMYK value.
1663 These components can be retrieved individually using the cyanF(),
1664 magentaF(), yellowF(), blackF() and alphaF() functions.
1666 \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
1668 void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a)
1670 if (!c || !m || !y || !k)
1671 return;
1673 if (cspec != Invalid && cspec != Cmyk) {
1674 toCmyk().getCmykF(c, m, y, k, a);
1675 return;
1678 *c = ct.acmyk.cyan / qreal(USHRT_MAX);
1679 *m = ct.acmyk.magenta / qreal(USHRT_MAX);
1680 *y = ct.acmyk.yellow / qreal(USHRT_MAX);
1681 *k = ct.acmyk.black / qreal(USHRT_MAX);
1683 if (a)
1684 *a = ct.acmyk.alpha / qreal(USHRT_MAX);
1688 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
1689 \a k (black), and \a a (alpha-channel, i.e. transparency).
1691 All the values must be in the range 0-255.
1693 \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The
1694 CMYK Color Model}
1696 void QColor::setCmyk(int c, int m, int y, int k, int a)
1698 if (c < 0 || c > 255
1699 || m < 0 || m > 255
1700 || y < 0 || y > 255
1701 || k < 0 || k > 255
1702 || a < 0 || a > 255) {
1703 qWarning("QColor::setCmyk: CMYK parameters out of range");
1704 return;
1707 cspec = Cmyk;
1708 ct.acmyk.alpha = a * 0x101;
1709 ct.acmyk.cyan = c * 0x101;
1710 ct.acmyk.magenta = m * 0x101;
1711 ct.acmyk.yellow = y * 0x101;
1712 ct.acmyk.black = k * 0x101;
1716 \overload
1718 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
1719 \a k (black), and \a a (alpha-channel, i.e. transparency).
1721 All the values must be in the range 0.0-1.0.
1723 \sa getCmykF() setCmyk(), {QColor#The CMYK Color Model}{The CMYK
1724 Color Model}
1726 void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
1728 if (c < 0.0 || c > 1.0
1729 || m < 0.0 || m > 1.0
1730 || y < 0.0 || y > 1.0
1731 || k < 0.0 || k > 1.0
1732 || a < 0.0 || a > 1.0) {
1733 qWarning("QColor::setCmykF: CMYK parameters out of range");
1734 return;
1737 cspec = Cmyk;
1738 ct.acmyk.alpha = qRound(a * USHRT_MAX);
1739 ct.acmyk.cyan = qRound(c * USHRT_MAX);
1740 ct.acmyk.magenta = qRound(m * USHRT_MAX);
1741 ct.acmyk.yellow = qRound(y * USHRT_MAX);
1742 ct.acmyk.black = qRound(k * USHRT_MAX);
1746 Static convenience function that returns a QColor constructed from the
1747 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
1748 (black), and \a a (alpha-channel, i.e. transparency).
1750 All the values must be in the range 0-255.
1752 \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK
1753 Color Model}
1755 QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
1757 if (c < 0 || c > 255
1758 || m < 0 || m > 255
1759 || y < 0 || y > 255
1760 || k < 0 || k > 255
1761 || a < 0 || a > 255) {
1762 qWarning("QColor::fromCmyk: CMYK parameters out of range");
1763 return QColor();
1766 QColor color;
1767 color.cspec = Cmyk;
1768 color.ct.acmyk.alpha = a * 0x101;
1769 color.ct.acmyk.cyan = c * 0x101;
1770 color.ct.acmyk.magenta = m * 0x101;
1771 color.ct.acmyk.yellow = y * 0x101;
1772 color.ct.acmyk.black = k * 0x101;
1773 return color;
1777 \overload
1779 Static convenience function that returns a QColor constructed from the
1780 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
1781 (black), and \a a (alpha-channel, i.e. transparency).
1783 All the values must be in the range 0.0-1.0.
1785 \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color
1786 Model}{The CMYK Color Model}
1788 QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
1790 if (c < 0.0 || c > 1.0
1791 || m < 0.0 || m > 1.0
1792 || y < 0.0 || y > 1.0
1793 || k < 0.0 || k > 1.0
1794 || a < 0.0 || a > 1.0) {
1795 qWarning("QColor::fromCmykF: CMYK parameters out of range");
1796 return QColor();
1799 QColor color;
1800 color.cspec = Cmyk;
1801 color.ct.acmyk.alpha = qRound(a * USHRT_MAX);
1802 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
1803 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
1804 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
1805 color.ct.acmyk.black = qRound(k * USHRT_MAX);
1806 return color;
1810 \fn QColor QColor::lighter(int factor) const
1811 \since 4.3
1813 Returns a lighter (or darker) color, but does not change this object.
1815 If the \a factor is greater than 100, this functions returns a lighter
1816 color. Setting \a factor to 150 returns a color that is 50% brighter. If
1817 the \a factor is less than 100, the return color is darker, but we
1818 recommend using the darker() function for this purpose. If the \a factor
1819 is 0 or negative, the return value is unspecified.
1821 The function converts the current RGB color to HSV, multiplies the value
1822 (V) component by \a factor and converts the color back to RGB.
1824 \sa darker(), isValid()
1828 \obsolete
1830 Use lighter(\a factor) instead.
1832 QColor QColor::light(int factor) const
1834 if (factor <= 0) // invalid lightness factor
1835 return *this;
1836 else if (factor < 100) // makes color darker
1837 return darker(10000 / factor);
1839 QColor hsv = toHsv();
1840 int s = hsv.ct.ahsv.saturation;
1841 int v = hsv.ct.ahsv.value;
1843 v = (factor*v)/100;
1844 if (v > USHRT_MAX) {
1845 // overflow... adjust saturation
1846 s -= v - USHRT_MAX;
1847 if (s < 0)
1848 s = 0;
1849 v = USHRT_MAX;
1852 hsv.ct.ahsv.saturation = s;
1853 hsv.ct.ahsv.value = v;
1855 // convert back to same color spec as original color
1856 return hsv.convertTo(cspec);
1860 \fn QColor QColor::darker(int factor) const
1861 \since 4.3
1863 Returns a darker (or lighter) color, but does not change this object.
1865 If the \a factor is greater than 100, this functions returns a darker
1866 color. Setting \a factor to 300 returns a color that has one-third the
1867 brightness. If the \a factor is less than 100, the return color is lighter,
1868 but we recommend using the lighter() function for this purpose. If the
1869 \a factor is 0 or negative, the return value is unspecified.
1871 The function converts the current RGB color to HSV, divides the value (V)
1872 component by \a factor and converts the color back to RGB.
1874 \sa lighter(), isValid()
1878 \obsolete
1880 Use darker(\a factor) instead.
1882 QColor QColor::dark(int factor) const
1884 if (factor <= 0) // invalid darkness factor
1885 return *this;
1886 else if (factor < 100) // makes color lighter
1887 return lighter(10000 / factor);
1889 QColor hsv = toHsv();
1890 hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
1892 // convert back to same color spec as original color
1893 return hsv.convertTo(cspec);
1897 Assigns a copy of \a color to this color, and returns a reference to it.
1899 QColor &QColor::operator=(const QColor &color)
1901 cspec = color.cspec;
1902 ct.argb = color.ct.argb;
1903 return *this;
1906 /*! \overload
1907 Assigns a copy of \a color and returns a reference to this color.
1909 QColor &QColor::operator=(Qt::GlobalColor color)
1911 return operator=(QColor(color));
1915 Returns true if this color has the same RGB and alpha values as \a color;
1916 otherwise returns false.
1918 bool QColor::operator==(const QColor &color) const
1920 return (cspec == color.cspec
1921 && ct.argb.alpha == color.ct.argb.alpha
1922 && ((cspec == QColor::Hsv
1923 && ((ct.argb.red % 36000) == (color.ct.argb.red % 36000)))
1924 || (ct.argb.red == color.ct.argb.red))
1925 && ct.argb.green == color.ct.argb.green
1926 && ct.argb.blue == color.ct.argb.blue
1927 && ct.argb.pad == color.ct.argb.pad);
1931 Returns true if this color has a different RGB and alpha values from
1932 \a color; otherwise returns false.
1934 bool QColor::operator!=(const QColor &color) const
1935 { return !operator==(color); }
1939 Returns the color as a QVariant
1941 QColor::operator QVariant() const
1943 return QVariant(QVariant::Color, this);
1946 #ifdef Q_WS_X11
1948 Returns true if setNamedColor() is allowed to look up colors in the X11
1949 color database. By default, this function returns false.
1951 \note This function is only available on the X11 platform.
1953 \sa setAllowX11ColorNames()
1955 bool QColor::allowX11ColorNames()
1957 return ::allowX11ColorNames;
1961 Allow setNamedColor() to look up colors in the X11 color database if
1962 \a enabled. By default, setNamedColor() does \e not look up colors in the
1963 X11 color database.
1965 \note This function is only available on the X11 platform.
1967 \sa setNamedColor(), allowX11ColorNames()
1969 void QColor::setAllowX11ColorNames(bool enabled)
1971 ::allowX11ColorNames = enabled;
1973 #endif
1975 /*! \internal
1977 Marks the color as invalid and sets all components to zero (alpha is set
1978 to fully opaque for compatibility with Qt 3).
1980 void QColor::invalidate()
1982 cspec = Invalid;
1983 ct.argb.alpha = USHRT_MAX;
1984 ct.argb.red = 0;
1985 ct.argb.green = 0;
1986 ct.argb.blue = 0;
1987 ct.argb.pad = 0;
1990 #ifdef QT3_SUPPORT
1993 Returns the pixel value used by the underlying window system to refer to a
1994 color.
1996 Use QColormap::pixel() instead.
1998 \oldcode
1999 QColor myColor;
2000 uint pixel = myColor.pixel(screen);
2001 \newcode
2002 QColormap cmap = QColormap::instance(screen);
2003 uint pixel = cmap.pixel(*this);
2004 \endcode
2006 uint QColor::pixel(int screen) const
2008 QColormap cmap = QColormap::instance(screen);
2009 return cmap.pixel(*this);
2012 #endif // QT3_SUPPORT
2014 /*****************************************************************************
2015 QColor stream functions
2016 *****************************************************************************/
2018 #ifndef QT_NO_DEBUG_STREAM
2019 QDebug operator<<(QDebug dbg, const QColor &c)
2021 #ifndef Q_BROKEN_DEBUG_STREAM
2022 if (!c.isValid())
2023 dbg.nospace() << "QColor(Invalid)";
2024 else if (c.spec() == QColor::Rgb)
2025 dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2026 else if (c.spec() == QColor::Hsv)
2027 dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
2028 else if (c.spec() == QColor::Cmyk)
2029 dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
2030 << c.blackF()<< ')';
2032 return dbg.space();
2033 #else
2034 qWarning("This compiler doesn't support streaming QColor to QDebug");
2035 return dbg;
2036 Q_UNUSED(c);
2037 #endif
2039 #endif
2041 #ifndef QT_NO_DATASTREAM
2043 \fn QDataStream &operator<<(QDataStream &stream, const QColor &color)
2044 \relates QColor
2046 Writes the \a color to the \a stream.
2048 \sa {Format of the QDataStream Operators}
2050 QDataStream &operator<<(QDataStream &stream, const QColor &color)
2052 if (stream.version() < 7) {
2053 if (!color.isValid())
2054 return stream << quint32(0x49000000);
2055 quint32 p = (quint32)color.rgb();
2056 if (stream.version() == 1) // Swap red and blue
2057 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2058 return stream << p;
2061 qint8 s = color.cspec;
2062 quint16 a = color.ct.argb.alpha;
2063 quint16 r = color.ct.argb.red;
2064 quint16 g = color.ct.argb.green;
2065 quint16 b = color.ct.argb.blue;
2066 quint16 p = color.ct.argb.pad;
2068 stream << s;
2069 stream << a;
2070 stream << r;
2071 stream << g;
2072 stream << b;
2073 stream << p;
2075 return stream;
2079 \fn QDataStream &operator>>(QDataStream &stream, QColor &color)
2080 \relates QColor
2082 Reads the \a color from the \a stream.
2084 \sa { Format of the QDataStream Operators}
2086 QDataStream &operator>>(QDataStream &stream, QColor &color)
2088 if (stream.version() < 7) {
2089 quint32 p;
2090 stream >> p;
2091 if (p == 0x49000000) {
2092 color.invalidate();
2093 return stream;
2095 if (stream.version() == 1) // Swap red and blue
2096 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2097 color.setRgb(p);
2098 return stream;
2101 qint8 s;
2102 quint16 a, r, g, b, p;
2103 stream >> s;
2104 stream >> a;
2105 stream >> r;
2106 stream >> g;
2107 stream >> b;
2108 stream >> p;
2110 color.cspec = QColor::Spec(s);
2111 color.ct.argb.alpha = a;
2112 color.ct.argb.red = r;
2113 color.ct.argb.green = g;
2114 color.ct.argb.blue = b;
2115 color.ct.argb.pad = p;
2117 return stream;
2119 #endif // QT_NO_DATASTREAM
2122 /*****************************************************************************
2123 QColor global functions (documentation only)
2124 *****************************************************************************/
2127 \fn int qRed(QRgb rgb)
2128 \relates QColor
2130 Returns the red component of the ARGB quadruplet \a rgb.
2132 \sa qRgb(), QColor::red()
2136 \fn int qGreen(QRgb rgb)
2137 \relates QColor
2139 Returns the green component of the ARGB quadruplet \a rgb.
2141 \sa qRgb(), QColor::green()
2145 \fn int qBlue(QRgb rgb)
2146 \relates QColor
2148 Returns the blue component of the ARGB quadruplet \a rgb.
2150 \sa qRgb(), QColor::blue()
2154 \fn int qAlpha(QRgb rgba)
2155 \relates QColor
2157 Returns the alpha component of the ARGB quadruplet \a rgba.
2159 \sa qRgb(), QColor::alpha()
2163 \fn QRgb qRgb(int r, int g, int b)
2164 \relates QColor
2166 Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}).
2168 \sa qRgba(), qRed(), qGreen(), qBlue()
2172 \fn QRgb qRgba(int r, int g, int b, int a)
2173 \relates QColor
2175 Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}).
2177 \sa qRgb(), qRed(), qGreen(), qBlue()
2181 \fn int qGray(int r, int g, int b)
2182 \relates QColor
2184 Returns a gray value (0 to 255) from the (\a r, \a g, \a b)
2185 triplet.
2187 The gray value is calculated using the formula (\a r * 11 + \a g * 16 +
2188 \a b * 5)/32.
2192 \fn int qGray(QRgb rgb)
2193 \overload
2194 \relates QColor
2196 Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb.
2198 The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32;
2199 the alpha-channel is ignored.
2203 \fn QColor::QColor(int x, int y, int z, Spec colorSpec)
2205 Use one of the other QColor constructors, or one of the static convenience
2206 functions, instead.
2210 \fn QColor::rgb(int *r, int *g, int *b) const
2212 Use getRgb() instead.
2216 \fn QColor::hsv(int *h, int *s, int *v) const
2218 Use getHsv() instead.
2222 \fn QColor QColor::convertTo(Spec colorSpec) const
2224 Creates a copy of \e this color in the format specified by \a colorSpec.
2226 \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()
2230 \typedef QRgb
2231 \relates QColor
2233 An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
2235 The type also holds a value for the alpha-channel. The default alpha
2236 channel is \c ff, i.e opaque. For more information, see the
2237 \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section.
2239 \sa QColor::rgb(), QColor::rgba()
2242 /*! \fn void QColormap::initialize()
2243 \internal
2246 /*! \fn void QColormap::cleanup()
2247 \internal
2250 QT_END_NAMESPACE