source: trunk/src/gui/painting/qcolor.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 75.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
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.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
55static bool allowX11ColorNames = false;
56
57#endif
58
59#include <math.h>
60#include <stdio.h>
61#include <limits.h>
62
63QT_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 \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
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 */
343QColor::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
418QColor::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
438QColor::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
502QString 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
533void QColor::setNamedColor(const QString &name)
534{
535 if (!setColorFromString(name))
536 qWarning("QColor::setNamedColor: Unknown color name '%s'", name.toLatin1().constData());
537}
538
539/*!
540 \since 4.7
541
542 Returns true if the \a name is a valid color name and can
543 be used to construct a valid QColor object, otherwise returns
544 false.
545
546 It uses the same algorithm used in setNamedColor().
547
548 \sa setNamedColor()
549*/
550bool QColor::isValidColor(const QString &name)
551{
552 return !name.isEmpty() && QColor().setColorFromString(name);
553}
554
555bool QColor::setColorFromString(const QString &name)
556{
557 if (name.isEmpty()) {
558 invalidate();
559 return true;
560 }
561
562 if (name.startsWith(QLatin1Char('#'))) {
563 QRgb rgb;
564 if (qt_get_hex_rgb(name.constData(), name.length(), &rgb)) {
565 setRgb(rgb);
566 return true;
567 } else {
568 invalidate();
569 return false;
570 }
571 }
572
573#ifndef QT_NO_COLORNAMES
574 QRgb rgb;
575 if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
576 setRgba(rgb);
577 return true;
578 } else
579#endif
580 {
581#ifdef Q_WS_X11
582 XColor result;
583 if (allowX11ColorNames()
584 && QApplication::instance()
585 && QX11Info::display()
586 && XParseColor(QX11Info::display(), QX11Info::appColormap(), name.toLatin1().constData(), &result)) {
587 setRgb(result.red >> 8, result.green >> 8, result.blue >> 8);
588 return true;
589 } else
590#endif
591 {
592 invalidate();
593 return false;
594 }
595 }
596}
597
598/*!
599 Returns a QStringList containing the color names Qt knows about.
600
601 \sa {QColor#Predefined Colors}{Predefined Colors}
602*/
603QStringList QColor::colorNames()
604{
605#ifndef QT_NO_COLORNAMES
606 return qt_get_colornames();
607#else
608 return QStringList();
609#endif
610}
611
612/*!
613 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
614 saturation, value, and alpha-channel (transparency) components of the
615 color's HSV value.
616
617 These components can be retrieved individually using the hueF(),
618 saturationF(), valueF() and alphaF() functions.
619
620 \sa setHsv() {QColor#The HSV Color Model}{The HSV Color Model}
621*/
622void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
623{
624 if (!h || !s || !v)
625 return;
626
627 if (cspec != Invalid && cspec != Hsv) {
628 toHsv().getHsvF(h, s, v, a);
629 return;
630 }
631
632 *h = ct.ahsv.hue == USHRT_MAX ? -1.0 : ct.ahsv.hue / 36000.0;
633 *s = ct.ahsv.saturation / qreal(USHRT_MAX);
634 *v = ct.ahsv.value / qreal(USHRT_MAX);
635
636 if (a)
637 *a = ct.ahsv.alpha / qreal(USHRT_MAX);
638}
639
640/*!
641 Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
642 saturation, value, and alpha-channel (transparency) components of the
643 color's HSV value.
644
645 These components can be retrieved individually using the hue(),
646 saturation(), value() and alpha() functions.
647
648 \sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
649*/
650void QColor::getHsv(int *h, int *s, int *v, int *a) const
651{
652 if (!h || !s || !v)
653 return;
654
655 if (cspec != Invalid && cspec != Hsv) {
656 toHsv().getHsv(h, s, v, a);
657 return;
658 }
659
660 *h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
661 *s = ct.ahsv.saturation >> 8;
662 *v = ct.ahsv.value >> 8;
663
664 if (a)
665 *a = ct.ahsv.alpha >> 8;
666}
667
668/*!
669 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
670 the value and \a a is the alpha component of the HSV color.
671
672 All the values must be in the range 0.0-1.0.
673
674 \sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV
675 Color Model}
676*/
677void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a)
678{
679 if (((h < 0.0 || h > 1.0) && h != -1.0)
680 || (s < 0.0 || s > 1.0)
681 || (v < 0.0 || v > 1.0)
682 || (a < 0.0 || a > 1.0)) {
683 qWarning("QColor::setHsvF: HSV parameters out of range");
684 return;
685 }
686
687 cspec = Hsv;
688 ct.ahsv.alpha = qRound(a * USHRT_MAX);
689 ct.ahsv.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000);
690 ct.ahsv.saturation = qRound(s * USHRT_MAX);
691 ct.ahsv.value = qRound(v * USHRT_MAX);
692 ct.ahsv.pad = 0;
693}
694
695/*!
696 Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
697 the value and \a a is the alpha component of the HSV color.
698
699 The saturation, value and alpha-channel values must be in the range 0-255,
700 and the hue value must be greater than -1.
701
702 \sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV
703 Color Model}
704*/
705void QColor::setHsv(int h, int s, int v, int a)
706{
707 if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
708 qWarning("QColor::setHsv: HSV parameters out of range");
709 invalidate();
710 return;
711 }
712
713 cspec = Hsv;
714 ct.ahsv.alpha = a * 0x101;
715 ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
716 ct.ahsv.saturation = s * 0x101;
717 ct.ahsv.value = v * 0x101;
718 ct.ahsv.pad = 0;
719}
720
721/*!
722 \since 4.6
723
724 Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
725 saturation, lightness, and alpha-channel (transparency) components of the
726 color's HSL value.
727
728 These components can be retrieved individually using the hueHslF(),
729 saturationHslF(), lightnessF() and alphaF() functions.
730
731 \sa setHsl()
732*/
733void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
734{
735 if (!h || !s || !l)
736 return;
737
738 if (cspec != Invalid && cspec != Hsl) {
739 toHsl().getHslF(h, s, l, a);
740 return;
741 }
742
743 *h = ct.ahsl.hue == USHRT_MAX ? -1.0 : ct.ahsl.hue / 36000.0;
744 *s = ct.ahsl.saturation / qreal(USHRT_MAX);
745 *l = ct.ahsl.lightness / qreal(USHRT_MAX);
746
747 if (a)
748 *a = ct.ahsl.alpha / qreal(USHRT_MAX);
749}
750
751/*!
752 \since 4.6
753
754 Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
755 saturation, lightness, and alpha-channel (transparency) components of the
756 color's HSL value.
757
758 These components can be retrieved individually using the hueHsl(),
759 saturationHsl(), lightness() and alpha() functions.
760
761 \sa setHsl()
762*/
763void QColor::getHsl(int *h, int *s, int *l, int *a) const
764{
765 if (!h || !s || !l)
766 return;
767
768 if (cspec != Invalid && cspec != Hsl) {
769 toHsl().getHsl(h, s, l, a);
770 return;
771 }
772
773 *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
774 *s = ct.ahsl.saturation >> 8;
775 *l = ct.ahsl.lightness >> 8;
776
777 if (a)
778 *a = ct.ahsl.alpha >> 8;
779}
780
781/*!
782 \since 4.6
783
784 Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
785 the lightness and \a a is the alpha component of the HSL color.
786
787 All the values must be in the range 0.0-1.0.
788
789 \sa getHslF(), setHsl()
790*/
791void QColor::setHslF(qreal h, qreal s, qreal l, qreal a)
792{
793 if (((h < 0.0 || h > 1.0) && h != -1.0)
794 || (s < 0.0 || s > 1.0)
795 || (l < 0.0 || l > 1.0)
796 || (a < 0.0 || a > 1.0)) {
797 qWarning("QColor::setHsvF: HSV parameters out of range");
798 return;
799 }
800
801 cspec = Hsl;
802 ct.ahsl.alpha = qRound(a * USHRT_MAX);
803 ct.ahsl.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000);
804 ct.ahsl.saturation = qRound(s * USHRT_MAX);
805 ct.ahsl.lightness = qRound(l * USHRT_MAX);
806 ct.ahsl.pad = 0;
807}
808
809/*!
810 \since 4.6
811
812 Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
813 the lightness and \a a is the alpha component of the HSL color.
814
815 The saturation, value and alpha-channel values must be in the range 0-255,
816 and the hue value must be greater than -1.
817
818 \sa getHsl(), setHslF()
819*/
820void QColor::setHsl(int h, int s, int l, int a)
821{
822 if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
823 qWarning("QColor::setHsv: HSV parameters out of range");
824 invalidate();
825 return;
826 }
827
828 cspec = Hsl;
829 ct.ahsl.alpha = a * 0x101;
830 ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
831 ct.ahsl.saturation = s * 0x101;
832 ct.ahsl.lightness = l * 0x101;
833 ct.ahsl.pad = 0;
834}
835
836/*!
837 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
838 green, blue, and alpha-channel (transparency) components of the color's
839 RGB value.
840
841 These components can be retrieved individually using the redF(), greenF(),
842 blueF() and alphaF() functions.
843
844 \sa rgb(), setRgb()
845*/
846void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
847{
848 if (!r || !g || !b)
849 return;
850
851 if (cspec != Invalid && cspec != Rgb) {
852 toRgb().getRgbF(r, g, b, a);
853 return;
854 }
855
856 *r = ct.argb.red / qreal(USHRT_MAX);
857 *g = ct.argb.green / qreal(USHRT_MAX);
858 *b = ct.argb.blue / qreal(USHRT_MAX);
859
860 if (a)
861 *a = ct.argb.alpha / qreal(USHRT_MAX);
862
863}
864
865/*!
866 Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
867 green, blue, and alpha-channel (transparency) components of the color's
868 RGB value.
869
870 These components can be retrieved individually using the red(), green(),
871 blue() and alpha() functions.
872
873 \sa rgb(), setRgb()
874*/
875void QColor::getRgb(int *r, int *g, int *b, int *a) const
876{
877 if (!r || !g || !b)
878 return;
879
880 if (cspec != Invalid && cspec != Rgb) {
881 toRgb().getRgb(r, g, b, a);
882 return;
883 }
884
885 *r = ct.argb.red >> 8;
886 *g = ct.argb.green >> 8;
887 *b = ct.argb.blue >> 8;
888
889 if (a)
890 *a = ct.argb.alpha >> 8;
891}
892
893/*!
894 \obsolete
895 \fn void QColor::getRgba(int *r, int *g, int *b, int *a) const
896
897 Use getRgb() instead.
898*/
899
900/*!
901 \fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
902
903 Sets the color channels of this color to \a r (red), \a g (green),
904 \a b (blue) and \a a (alpha, transparency).
905
906 All values must be in the range 0.0-1.0.
907
908 \sa rgb(), getRgbF(), setRgb()
909*/
910void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
911{
912 if (r < 0.0 || r > 1.0
913 || g < 0.0 || g > 1.0
914 || b < 0.0 || b > 1.0
915 || a < 0.0 || a > 1.0) {
916 qWarning("QColor::setRgbF: RGB parameters out of range");
917 invalidate();
918 return;
919 }
920
921 cspec = Rgb;
922 ct.argb.alpha = qRound(a * USHRT_MAX);
923 ct.argb.red = qRound(r * USHRT_MAX);
924 ct.argb.green = qRound(g * USHRT_MAX);
925 ct.argb.blue = qRound(b * USHRT_MAX);
926 ct.argb.pad = 0;
927}
928
929/*!
930 Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
931
932 All the values must be in the range 0-255.
933
934 \sa rgb(), getRgb(), setRgbF()
935*/
936void QColor::setRgb(int r, int g, int b, int a)
937{
938 if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
939 qWarning("QColor::setRgb: RGB parameters out of range");
940 invalidate();
941 return;
942 }
943
944 cspec = Rgb;
945 ct.argb.alpha = a * 0x101;
946 ct.argb.red = r * 0x101;
947 ct.argb.green = g * 0x101;
948 ct.argb.blue = b * 0x101;
949 ct.argb.pad = 0;
950}
951
952/*!
953 \obsolete
954 \fn void QColor::setRgba(int r, int g, int b, int a)
955
956 Use setRgb() instead.
957*/
958
959/*!
960 \fn QRgb QColor::rgba() const
961
962 Returns the RGB value of the color, including its alpha.
963
964 For an invalid color, the alpha value of the returned color is unspecified.
965
966 \sa setRgba(), rgb()
967*/
968
969QRgb QColor::rgba() const
970{
971 if (cspec != Invalid && cspec != Rgb)
972 return toRgb().rgba();
973 return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
974}
975
976/*!
977 Sets the RGB value to \a rgba, including its alpha.
978
979 \sa rgba(), rgb()
980*/
981void QColor::setRgba(QRgb rgba)
982{
983 cspec = Rgb;
984 ct.argb.alpha = qAlpha(rgba) * 0x101;
985 ct.argb.red = qRed(rgba) * 0x101;
986 ct.argb.green = qGreen(rgba) * 0x101;
987 ct.argb.blue = qBlue(rgba) * 0x101;
988 ct.argb.pad = 0;
989}
990
991/*!
992 \fn QRgb QColor::rgb() const
993
994 Returns the RGB value of the color. The alpha value is opaque.
995
996 \sa getRgb(), rgba()
997*/
998QRgb QColor::rgb() const
999{
1000 if (cspec != Invalid && cspec != Rgb)
1001 return toRgb().rgb();
1002 return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
1003}
1004
1005/*!
1006 \overload
1007
1008 Sets the RGB value to \a rgb. The alpha value is set to opaque.
1009*/
1010void QColor::setRgb(QRgb rgb)
1011{
1012 cspec = Rgb;
1013 ct.argb.alpha = 0xffff;
1014 ct.argb.red = qRed(rgb) * 0x101;
1015 ct.argb.green = qGreen(rgb) * 0x101;
1016 ct.argb.blue = qBlue(rgb) * 0x101;
1017 ct.argb.pad = 0;
1018}
1019
1020/*!
1021 Returns the alpha color component of this color.
1022
1023 \sa setAlpha(), alphaF(), {QColor#Alpha-Blended
1024 Drawing}{Alpha-Blended Drawing}
1025*/
1026int QColor::alpha() const
1027{ return ct.argb.alpha >> 8; }
1028
1029
1030/*!
1031 Sets the alpha of this color to \a alpha. Integer alpha is specified in the
1032 range 0-255.
1033
1034 \sa alpha(), alphaF(), {QColor#Alpha-Blended
1035 Drawing}{Alpha-Blended Drawing}
1036*/
1037
1038void QColor::setAlpha(int alpha)
1039{
1040 QCOLOR_INT_RANGE_CHECK("QColor::setAlpha", alpha);
1041 ct.argb.alpha = alpha * 0x101;
1042}
1043
1044/*!
1045 Returns the alpha color component of this color.
1046
1047 \sa setAlphaF(), alpha(), {QColor#Alpha-Blended
1048 Drawing}{Alpha-Blended Drawing}
1049*/
1050qreal QColor::alphaF() const
1051{ return ct.argb.alpha / qreal(USHRT_MAX); }
1052
1053/*!
1054 Sets the alpha of this color to \a alpha. qreal alpha is specified in the
1055 range 0.0-1.0.
1056
1057 \sa alphaF(), alpha(), {QColor#Alpha-Blended
1058 Drawing}{Alpha-Blended Drawing}
1059
1060*/
1061void QColor::setAlphaF(qreal alpha)
1062{
1063 QCOLOR_REAL_RANGE_CHECK("QColor::setAlphaF", alpha);
1064 qreal tmp = alpha * USHRT_MAX;
1065 ct.argb.alpha = qRound(tmp);
1066}
1067
1068
1069/*!
1070 Returns the red color component of this color.
1071
1072 \sa setRed(), redF(), getRgb()
1073*/
1074int QColor::red() const
1075{
1076 if (cspec != Invalid && cspec != Rgb)
1077 return toRgb().red();
1078 return ct.argb.red >> 8;
1079}
1080
1081/*!
1082 Sets the red color component of this color to \a red. Integer components
1083 are specified in the range 0-255.
1084
1085 \sa red(), redF(), setRgb()
1086*/
1087void QColor::setRed(int red)
1088{
1089 QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
1090 if (cspec != Rgb)
1091 setRgb(red, green(), blue(), alpha());
1092 else
1093 ct.argb.red = red * 0x101;
1094}
1095
1096/*!
1097 Returns the green color component of this color.
1098
1099 \sa setGreen(), greenF(), getRgb()
1100*/
1101int QColor::green() const
1102{
1103 if (cspec != Invalid && cspec != Rgb)
1104 return toRgb().green();
1105 return ct.argb.green >> 8;
1106}
1107
1108/*!
1109 Sets the green color component of this color to \a green. Integer
1110 components are specified in the range 0-255.
1111
1112 \sa green(), greenF(), setRgb()
1113*/
1114void QColor::setGreen(int green)
1115{
1116 QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
1117 if (cspec != Rgb)
1118 setRgb(red(), green, blue(), alpha());
1119 else
1120 ct.argb.green = green * 0x101;
1121}
1122
1123
1124/*!
1125 Returns the blue color component of this color.
1126
1127 \sa setBlue(), blueF(), getRgb()
1128*/
1129int QColor::blue() const
1130{
1131 if (cspec != Invalid && cspec != Rgb)
1132 return toRgb().blue();
1133 return ct.argb.blue >> 8;
1134}
1135
1136
1137/*!
1138 Sets the blue color component of this color to \a blue. Integer components
1139 are specified in the range 0-255.
1140
1141 \sa blue(), blueF(), setRgb()
1142*/
1143void QColor::setBlue(int blue)
1144{
1145 QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
1146 if (cspec != Rgb)
1147 setRgb(red(), green(), blue, alpha());
1148 else
1149 ct.argb.blue = blue * 0x101;
1150}
1151
1152/*!
1153 Returns the red color component of this color.
1154
1155 \sa setRedF(), red(), getRgbF()
1156*/
1157qreal QColor::redF() const
1158{
1159 if (cspec != Invalid && cspec != Rgb)
1160 return toRgb().redF();
1161 return ct.argb.red / qreal(USHRT_MAX);
1162}
1163
1164
1165/*!
1166 Sets the red color component of this color to \a red. Float components
1167 are specified in the range 0.0-1.0.
1168
1169 \sa redF(), red(), setRgbF()
1170*/
1171void QColor::setRedF(qreal red)
1172{
1173 QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red);
1174 if (cspec != Rgb)
1175 setRgbF(red, greenF(), blueF(), alphaF());
1176 else
1177 ct.argb.red = qRound(red * USHRT_MAX);
1178}
1179
1180/*!
1181 Returns the green color component of this color.
1182
1183 \sa setGreenF(), green(), getRgbF()
1184*/
1185qreal QColor::greenF() const
1186{
1187 if (cspec != Invalid && cspec != Rgb)
1188 return toRgb().greenF();
1189 return ct.argb.green / qreal(USHRT_MAX);
1190}
1191
1192
1193/*!
1194 Sets the green color component of this color to \a green. Float components
1195 are specified in the range 0.0-1.0.
1196
1197 \sa greenF(), green(), setRgbF()
1198*/
1199void QColor::setGreenF(qreal green)
1200{
1201 QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green);
1202 if (cspec != Rgb)
1203 setRgbF(redF(), green, blueF(), alphaF());
1204 else
1205 ct.argb.green = qRound(green * USHRT_MAX);
1206}
1207
1208/*!
1209 Returns the blue color component of this color.
1210
1211 \sa setBlueF(), blue(), getRgbF()
1212*/
1213qreal QColor::blueF() const
1214{
1215 if (cspec != Invalid && cspec != Rgb)
1216 return toRgb().blueF();
1217 return ct.argb.blue / qreal(USHRT_MAX);
1218}
1219
1220/*!
1221 Sets the blue color component of this color to \a blue. Float components
1222 are specified in the range 0.0-1.0.
1223
1224 \sa blueF(), blue(), setRgbF()
1225*/
1226void QColor::setBlueF(qreal blue)
1227{
1228 QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue);
1229 if (cspec != Rgb)
1230 setRgbF(redF(), greenF(), blue, alphaF());
1231 else
1232 ct.argb.blue = qRound(blue * USHRT_MAX);
1233}
1234
1235/*!
1236 Returns the hue color component of this color.
1237
1238 The color is implicitly converted to HSV.
1239
1240 \sa hsvHue(), hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1241 Model}
1242*/
1243
1244int QColor::hue() const
1245{
1246 return hsvHue();
1247}
1248
1249/*!
1250 Returns the hue color component of this color.
1251
1252 \sa hueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1253 Model}
1254*/
1255int QColor::hsvHue() const
1256{
1257 if (cspec != Invalid && cspec != Hsv)
1258 return toHsv().hue();
1259 return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
1260}
1261
1262/*!
1263 Returns the saturation color component of this color.
1264
1265 The color is implicitly converted to HSV.
1266
1267 \sa hsvSaturation(), saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1268 Model}
1269*/
1270
1271int QColor::saturation() const
1272{
1273 return hsvSaturation();
1274}
1275
1276/*!
1277 Returns the saturation color component of this color.
1278
1279 \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1280 Model}
1281*/
1282int QColor::hsvSaturation() const
1283{
1284 if (cspec != Invalid && cspec != Hsv)
1285 return toHsv().saturation();
1286 return ct.ahsv.saturation >> 8;
1287}
1288
1289/*!
1290 Returns the value color component of this color.
1291
1292 \sa valueF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1293 Model}
1294*/
1295int QColor::value() const
1296{
1297 if (cspec != Invalid && cspec != Hsv)
1298 return toHsv().value();
1299 return ct.ahsv.value >> 8;
1300}
1301
1302/*!
1303 Returns the hue color component of this color.
1304
1305 The color is implicitly converted to HSV.
1306
1307 \sa hsvHueF(), hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1308 Model}
1309*/
1310qreal QColor::hueF() const
1311{
1312 return hsvHueF();
1313}
1314
1315/*!
1316 Returns the hue color component of this color.
1317
1318 \sa hue(), getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1319 Model}
1320*/
1321qreal QColor::hsvHueF() const
1322{
1323 if (cspec != Invalid && cspec != Hsv)
1324 return toHsv().hueF();
1325 return ct.ahsv.hue == USHRT_MAX ? -1.0 : ct.ahsv.hue / 36000.0;
1326}
1327
1328/*!
1329 Returns the saturation color component of this color.
1330
1331 The color is implicitly converted to HSV.
1332
1333 \sa hsvSaturationF(), saturation() getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1334 Model}
1335*/
1336qreal QColor::saturationF() const
1337{
1338 return hsvSaturationF();
1339}
1340
1341/*!
1342 Returns the saturation color component of this color.
1343
1344 \sa saturation() getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1345 Model}
1346*/
1347qreal QColor::hsvSaturationF() const
1348{
1349 if (cspec != Invalid && cspec != Hsv)
1350 return toHsv().saturationF();
1351 return ct.ahsv.saturation / qreal(USHRT_MAX);
1352}
1353
1354/*!
1355 Returns the value color component of this color.
1356
1357 \sa value() getHsvF(), {QColor#The HSV Color Model}{The HSV Color
1358 Model}
1359*/
1360qreal QColor::valueF() const
1361{
1362 if (cspec != Invalid && cspec != Hsv)
1363 return toHsv().valueF();
1364 return ct.ahsv.value / qreal(USHRT_MAX);
1365}
1366
1367/*!
1368 \since 4.6
1369
1370 Returns the hue color component of this color.
1371
1372 \sa getHslF(), getHsl()
1373*/
1374int QColor::hslHue() const
1375{
1376 if (cspec != Invalid && cspec != Hsl)
1377 return toHsl().hslHue();
1378 return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
1379}
1380
1381/*!
1382 \since 4.6
1383
1384 Returns the saturation color component of this color.
1385
1386 \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
1387 Model}
1388*/
1389int QColor::hslSaturation() const
1390{
1391 if (cspec != Invalid && cspec != Hsl)
1392 return toHsl().hslSaturation();
1393 return ct.ahsl.saturation >> 8;
1394}
1395
1396/*!
1397 \since 4.6
1398
1399 Returns the lightness color component of this color.
1400
1401 \sa lightnessF(), getHsl()
1402*/
1403int QColor::lightness() const
1404{
1405 if (cspec != Invalid && cspec != Hsl)
1406 return toHsl().lightness();
1407 return ct.ahsl.lightness >> 8;
1408}
1409
1410/*!
1411 \since 4.6
1412
1413 Returns the hue color component of this color.
1414
1415 \sa hue(), getHslF()
1416*/
1417qreal QColor::hslHueF() const
1418{
1419 if (cspec != Invalid && cspec != Hsl)
1420 return toHsl().hslHueF();
1421 return ct.ahsl.hue == USHRT_MAX ? -1.0 : ct.ahsl.hue / 36000.0;
1422}
1423
1424/*!
1425 \since 4.6
1426
1427 Returns the saturation color component of this color.
1428
1429 \sa saturationF() getHslF()
1430*/
1431qreal QColor::hslSaturationF() const
1432{
1433 if (cspec != Invalid && cspec != Hsl)
1434 return toHsl().hslSaturationF();
1435 return ct.ahsl.saturation / qreal(USHRT_MAX);
1436}
1437
1438/*!
1439 \since 4.6
1440
1441 Returns the lightness color component of this color.
1442
1443 \sa value() getHslF()
1444*/
1445qreal QColor::lightnessF() const
1446{
1447 if (cspec != Invalid && cspec != Hsl)
1448 return toHsl().lightnessF();
1449 return ct.ahsl.lightness / qreal(USHRT_MAX);
1450}
1451
1452/*!
1453 Returns the cyan color component of this color.
1454
1455 \sa cyanF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1456 Color Model}
1457*/
1458int QColor::cyan() const
1459{
1460 if (cspec != Invalid && cspec != Cmyk)
1461 return toCmyk().cyan();
1462 return ct.acmyk.cyan >> 8;
1463}
1464
1465/*!
1466 Returns the magenta color component of this color.
1467
1468 \sa magentaF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1469 Color Model}
1470*/
1471int QColor::magenta() const
1472{
1473 if (cspec != Invalid && cspec != Cmyk)
1474 return toCmyk().magenta();
1475 return ct.acmyk.magenta >> 8;
1476}
1477
1478/*!
1479 Returns the yellow color component of this color.
1480
1481 \sa yellowF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1482 Color Model}
1483*/
1484int QColor::yellow() const
1485{
1486 if (cspec != Invalid && cspec != Cmyk)
1487 return toCmyk().yellow();
1488 return ct.acmyk.yellow >> 8;
1489}
1490
1491/*!
1492 Returns the black color component of this color.
1493
1494 \sa blackF(), getCmyk(), {QColor#The CMYK Color Model}{The CMYK
1495 Color Model}
1496
1497*/
1498int QColor::black() const
1499{
1500 if (cspec != Invalid && cspec != Cmyk)
1501 return toCmyk().black();
1502 return ct.acmyk.black >> 8;
1503}
1504
1505/*!
1506 Returns the cyan color component of this color.
1507
1508 \sa cyan(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1509 Color Model}
1510*/
1511qreal QColor::cyanF() const
1512{
1513 if (cspec != Invalid && cspec != Cmyk)
1514 return toCmyk().cyanF();
1515 return ct.acmyk.cyan / qreal(USHRT_MAX);
1516}
1517
1518/*!
1519 Returns the magenta color component of this color.
1520
1521 \sa magenta(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1522 Color Model}
1523*/
1524qreal QColor::magentaF() const
1525{
1526 if (cspec != Invalid && cspec != Cmyk)
1527 return toCmyk().magentaF();
1528 return ct.acmyk.magenta / qreal(USHRT_MAX);
1529}
1530
1531/*!
1532 Returns the yellow color component of this color.
1533
1534 \sa yellow(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1535 Color Model}
1536*/
1537qreal QColor::yellowF() const
1538{
1539 if (cspec != Invalid && cspec != Cmyk)
1540 return toCmyk().yellowF();
1541 return ct.acmyk.yellow / qreal(USHRT_MAX);
1542}
1543
1544/*!
1545 Returns the black color component of this color.
1546
1547 \sa black(), getCmykF(), {QColor#The CMYK Color Model}{The CMYK
1548 Color Model}
1549*/
1550qreal QColor::blackF() const
1551{
1552 if (cspec != Invalid && cspec != Cmyk)
1553 return toCmyk().blackF();
1554 return ct.acmyk.black / qreal(USHRT_MAX);
1555}
1556
1557/*!
1558 Create and returns an RGB QColor based on this color.
1559
1560 \sa fromRgb(), convertTo(), isValid()
1561*/
1562QColor QColor::toRgb() const
1563{
1564 if (!isValid() || cspec == Rgb)
1565 return *this;
1566
1567 QColor color;
1568 color.cspec = Rgb;
1569 color.ct.argb.alpha = ct.argb.alpha;
1570 color.ct.argb.pad = 0;
1571
1572 switch (cspec) {
1573 case Hsv:
1574 {
1575 if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
1576 // achromatic case
1577 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
1578 break;
1579 }
1580
1581 // chromatic case
1582 const qreal h = ct.ahsv.hue == 36000 ? 0 : ct.ahsv.hue / 6000.;
1583 const qreal s = ct.ahsv.saturation / qreal(USHRT_MAX);
1584 const qreal v = ct.ahsv.value / qreal(USHRT_MAX);
1585 const int i = int(h);
1586 const qreal f = h - i;
1587 const qreal p = v * (1.0 - s);
1588
1589 if (i & 1) {
1590 const qreal q = v * (1.0 - (s * f));
1591
1592 switch (i) {
1593 case 1:
1594 color.ct.argb.red = qRound(q * USHRT_MAX);
1595 color.ct.argb.green = qRound(v * USHRT_MAX);
1596 color.ct.argb.blue = qRound(p * USHRT_MAX);
1597 break;
1598 case 3:
1599 color.ct.argb.red = qRound(p * USHRT_MAX);
1600 color.ct.argb.green = qRound(q * USHRT_MAX);
1601 color.ct.argb.blue = qRound(v * USHRT_MAX);
1602 break;
1603 case 5:
1604 color.ct.argb.red = qRound(v * USHRT_MAX);
1605 color.ct.argb.green = qRound(p * USHRT_MAX);
1606 color.ct.argb.blue = qRound(q * USHRT_MAX);
1607 break;
1608 }
1609 } else {
1610 const qreal t = v * (1.0 - (s * (1.0 - f)));
1611
1612 switch (i) {
1613 case 0:
1614 color.ct.argb.red = qRound(v * USHRT_MAX);
1615 color.ct.argb.green = qRound(t * USHRT_MAX);
1616 color.ct.argb.blue = qRound(p * USHRT_MAX);
1617 break;
1618 case 2:
1619 color.ct.argb.red = qRound(p * USHRT_MAX);
1620 color.ct.argb.green = qRound(v * USHRT_MAX);
1621 color.ct.argb.blue = qRound(t * USHRT_MAX);
1622 break;
1623 case 4:
1624 color.ct.argb.red = qRound(t * USHRT_MAX);
1625 color.ct.argb.green = qRound(p * USHRT_MAX);
1626 color.ct.argb.blue = qRound(v * USHRT_MAX);
1627 break;
1628 }
1629 }
1630 break;
1631 }
1632 case Hsl:
1633 {
1634 if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
1635 // achromatic case
1636 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
1637 } else if (ct.ahsl.lightness == 0) {
1638 // lightness 0
1639 color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
1640 } else {
1641 // chromatic case
1642 const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / 36000.;
1643 const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX);
1644 const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX);
1645
1646 qreal temp2;
1647 if (l < qreal(0.5))
1648 temp2 = l * (qreal(1.0) + s);
1649 else
1650 temp2 = l + s - (l * s);
1651
1652 const qreal temp1 = (qreal(2.0) * l) - temp2;
1653 qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)),
1654 h,
1655 h - (qreal(1.0) / qreal(3.0)) };
1656
1657 for (int i = 0; i != 3; ++i) {
1658 if (temp3[i] < qreal(0.0))
1659 temp3[i] += qreal(1.0);
1660 else if (temp3[i] > qreal(1.0))
1661 temp3[i] -= qreal(1.0);
1662
1663 const qreal sixtemp3 = temp3[i] * qreal(6.0);
1664 if (sixtemp3 < qreal(1.0))
1665 color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
1666 else if ((temp3[i] * qreal(2.0)) < qreal(1.0))
1667 color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
1668 else if ((temp3[i] * qreal(3.0)) < qreal(2.0))
1669 color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX);
1670 else
1671 color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
1672 }
1673 color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
1674 color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
1675 color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
1676 }
1677 break;
1678 }
1679 case Cmyk:
1680 {
1681 const qreal c = ct.acmyk.cyan / qreal(USHRT_MAX);
1682 const qreal m = ct.acmyk.magenta / qreal(USHRT_MAX);
1683 const qreal y = ct.acmyk.yellow / qreal(USHRT_MAX);
1684 const qreal k = ct.acmyk.black / qreal(USHRT_MAX);
1685
1686 color.ct.argb.red = qRound((1.0 - (c * (1.0 - k) + k)) * USHRT_MAX);
1687 color.ct.argb.green = qRound((1.0 - (m * (1.0 - k) + k)) * USHRT_MAX);
1688 color.ct.argb.blue = qRound((1.0 - (y * (1.0 - k) + k)) * USHRT_MAX);
1689 break;
1690 }
1691 default:
1692 break;
1693 }
1694
1695 return color;
1696}
1697
1698
1699#define Q_MAX_3(a, b, c) ( ( a > b && a > c) ? a : (b > c ? b : c) )
1700#define Q_MIN_3(a, b, c) ( ( a < b && a < c) ? a : (b < c ? b : c) )
1701
1702
1703/*!
1704 Creates and returns an HSV QColor based on this color.
1705
1706 \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color
1707 Model}{The HSV Color Model}
1708*/
1709QColor QColor::toHsv() const
1710{
1711 if (!isValid() || cspec == Hsv)
1712 return *this;
1713
1714 if (cspec != Rgb)
1715 return toRgb().toHsv();
1716
1717 QColor color;
1718 color.cspec = Hsv;
1719 color.ct.ahsv.alpha = ct.argb.alpha;
1720 color.ct.ahsv.pad = 0;
1721
1722 const qreal r = ct.argb.red / qreal(USHRT_MAX);
1723 const qreal g = ct.argb.green / qreal(USHRT_MAX);
1724 const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1725 const qreal max = Q_MAX_3(r, g, b);
1726 const qreal min = Q_MIN_3(r, g, b);
1727 const qreal delta = max - min;
1728 color.ct.ahsv.value = qRound(max * USHRT_MAX);
1729 if (qFuzzyIsNull(delta)) {
1730 // achromatic case, hue is undefined
1731 color.ct.ahsv.hue = USHRT_MAX;
1732 color.ct.ahsv.saturation = 0;
1733 } else {
1734 // chromatic case
1735 qreal hue = 0;
1736 color.ct.ahsv.saturation = qRound((delta / max) * USHRT_MAX);
1737 if (qFuzzyCompare(r, max)) {
1738 hue = ((g - b) /delta);
1739 } else if (qFuzzyCompare(g, max)) {
1740 hue = (2.0 + (b - r) / delta);
1741 } else if (qFuzzyCompare(b, max)) {
1742 hue = (4.0 + (r - g) / delta);
1743 } else {
1744 Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1745 }
1746 hue *= 60.0;
1747 if (hue < 0.0)
1748 hue += 360.0;
1749 color.ct.ahsv.hue = qRound(hue * 100);
1750 }
1751
1752 return color;
1753}
1754
1755/*!
1756 Creates and returns an HSL QColor based on this color.
1757
1758 \sa fromHsl(), convertTo(), isValid()
1759*/
1760QColor QColor::toHsl() const
1761{
1762 if (!isValid() || cspec == Hsl)
1763 return *this;
1764
1765 if (cspec != Rgb)
1766 return toRgb().toHsl();
1767
1768 QColor color;
1769 color.cspec = Hsl;
1770 color.ct.ahsl.alpha = ct.argb.alpha;
1771 color.ct.ahsl.pad = 0;
1772
1773 const qreal r = ct.argb.red / qreal(USHRT_MAX);
1774 const qreal g = ct.argb.green / qreal(USHRT_MAX);
1775 const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1776 const qreal max = Q_MAX_3(r, g, b);
1777 const qreal min = Q_MIN_3(r, g, b);
1778 const qreal delta = max - min;
1779 const qreal delta2 = max + min;
1780 const qreal lightness = qreal(0.5) * delta2;
1781 color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
1782 if (qFuzzyIsNull(delta)) {
1783 // achromatic case, hue is undefined
1784 color.ct.ahsl.hue = 0;
1785 color.ct.ahsl.saturation = 0;
1786 } else {
1787 // chromatic case
1788 qreal hue = 0;
1789 if (lightness < qreal(0.5))
1790 color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
1791 else
1792 color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX);
1793 if (qFuzzyCompare(r, max)) {
1794 hue = ((g - b) /delta);
1795 } else if (qFuzzyCompare(g, max)) {
1796 hue = (2.0 + (b - r) / delta);
1797 } else if (qFuzzyCompare(b, max)) {
1798 hue = (4.0 + (r - g) / delta);
1799 } else {
1800 Q_ASSERT_X(false, "QColor::toHsv", "internal error");
1801 }
1802 hue *= 60.0;
1803 if (hue < 0.0)
1804 hue += 360.0;
1805 color.ct.ahsl.hue = qRound(hue * 100);
1806 }
1807
1808 return color;
1809}
1810
1811/*!
1812 Creates and returns a CMYK QColor based on this color.
1813
1814 \sa fromCmyk(), convertTo(), isValid(), {QColor#The CMYK Color
1815 Model}{The CMYK Color Model}
1816*/
1817QColor QColor::toCmyk() const
1818{
1819 if (!isValid() || cspec == Cmyk)
1820 return *this;
1821 if (cspec != Rgb)
1822 return toRgb().toCmyk();
1823
1824 QColor color;
1825 color.cspec = Cmyk;
1826 color.ct.acmyk.alpha = ct.argb.alpha;
1827
1828 // rgb -> cmy
1829 const qreal r = ct.argb.red / qreal(USHRT_MAX);
1830 const qreal g = ct.argb.green / qreal(USHRT_MAX);
1831 const qreal b = ct.argb.blue / qreal(USHRT_MAX);
1832 qreal c = 1.0 - r;
1833 qreal m = 1.0 - g;
1834 qreal y = 1.0 - b;
1835
1836 // cmy -> cmyk
1837 const qreal k = qMin(c, qMin(m, y));
1838
1839 if (!qFuzzyIsNull(k - 1)) {
1840 c = (c - k) / (1.0 - k);
1841 m = (m - k) / (1.0 - k);
1842 y = (y - k) / (1.0 - k);
1843 }
1844
1845 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
1846 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
1847 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
1848 color.ct.acmyk.black = qRound(k * USHRT_MAX);
1849
1850 return color;
1851}
1852
1853QColor QColor::convertTo(QColor::Spec colorSpec) const
1854{
1855 if (colorSpec == cspec)
1856 return *this;
1857 switch (colorSpec) {
1858 case Rgb:
1859 return toRgb();
1860 case Hsv:
1861 return toHsv();
1862 case Cmyk:
1863 return toCmyk();
1864 case Hsl:
1865 return toHsl();
1866 case Invalid:
1867 break;
1868 }
1869 return QColor(); // must be invalid
1870}
1871
1872
1873/*!
1874 Static convenience function that returns a QColor constructed from the
1875 given QRgb value \a rgb.
1876
1877 The alpha component of \a rgb is ignored (i.e. it is automatically set to
1878 255), use the fromRgba() function to include the alpha-channel specified by
1879 the given QRgb value.
1880
1881 \sa fromRgba(), fromRgbF(), toRgb(), isValid()
1882*/
1883
1884QColor QColor::fromRgb(QRgb rgb)
1885{
1886 return fromRgb(qRed(rgb), qGreen(rgb), qBlue(rgb));
1887}
1888
1889
1890/*!
1891 Static convenience function that returns a QColor constructed from the
1892 given QRgb value \a rgba.
1893
1894 Unlike the fromRgb() function, the alpha-channel specified by the given
1895 QRgb value is included.
1896
1897 \sa fromRgb(), isValid()
1898*/
1899
1900QColor QColor::fromRgba(QRgb rgba)
1901{
1902 return fromRgb(qRed(rgba), qGreen(rgba), qBlue(rgba), qAlpha(rgba));
1903}
1904
1905/*!
1906 Static convenience function that returns a QColor constructed from the RGB
1907 color values, \a r (red), \a g (green), \a b (blue), and \a a
1908 (alpha-channel, i.e. transparency).
1909
1910 All the values must be in the range 0-255.
1911
1912 \sa toRgb(), fromRgbF(), isValid()
1913*/
1914QColor QColor::fromRgb(int r, int g, int b, int a)
1915{
1916 if (r < 0 || r > 255
1917 || g < 0 || g > 255
1918 || b < 0 || b > 255
1919 || a < 0 || a > 255) {
1920 qWarning("QColor::fromRgb: RGB parameters out of range");
1921 return QColor();
1922 }
1923
1924 QColor color;
1925 color.cspec = Rgb;
1926 color.ct.argb.alpha = a * 0x101;
1927 color.ct.argb.red = r * 0x101;
1928 color.ct.argb.green = g * 0x101;
1929 color.ct.argb.blue = b * 0x101;
1930 color.ct.argb.pad = 0;
1931 return color;
1932}
1933
1934/*!
1935 Static convenience function that returns a QColor constructed from the RGB
1936 color values, \a r (red), \a g (green), \a b (blue), and \a a
1937 (alpha-channel, i.e. transparency).
1938
1939 All the values must be in the range 0.0-1.0.
1940
1941 \sa fromRgb(), toRgb(), isValid()
1942*/
1943QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a)
1944{
1945 if (r < 0.0 || r > 1.0
1946 || g < 0.0 || g > 1.0
1947 || b < 0.0 || b > 1.0
1948 || a < 0.0 || a > 1.0) {
1949 qWarning("QColor::fromRgbF: RGB parameters out of range");
1950 return QColor();
1951 }
1952
1953 QColor color;
1954 color.cspec = Rgb;
1955 color.ct.argb.alpha = qRound(a * USHRT_MAX);
1956 color.ct.argb.red = qRound(r * USHRT_MAX);
1957 color.ct.argb.green = qRound(g * USHRT_MAX);
1958 color.ct.argb.blue = qRound(b * USHRT_MAX);
1959 color.ct.argb.pad = 0;
1960 return color;
1961}
1962
1963/*!
1964 Static convenience function that returns a QColor constructed from the HSV
1965 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1966 (alpha-channel, i.e. transparency).
1967
1968 The value of \a s, \a v, and \a a must all be in the range 0-255; the value
1969 of \a h must be in the range 0-359.
1970
1971 \sa toHsv(), fromHsvF(), isValid(), {QColor#The HSV Color
1972 Model}{The HSV Color Model}
1973*/
1974QColor QColor::fromHsv(int h, int s, int v, int a)
1975{
1976 if (((h < 0 || h >= 360) && h != -1)
1977 || s < 0 || s > 255
1978 || v < 0 || v > 255
1979 || a < 0 || a > 255) {
1980 qWarning("QColor::fromHsv: HSV parameters out of range");
1981 return QColor();
1982 }
1983
1984 QColor color;
1985 color.cspec = Hsv;
1986 color.ct.ahsv.alpha = a * 0x101;
1987 color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
1988 color.ct.ahsv.saturation = s * 0x101;
1989 color.ct.ahsv.value = v * 0x101;
1990 color.ct.ahsv.pad = 0;
1991 return color;
1992}
1993
1994/*!
1995 \overload
1996
1997 Static convenience function that returns a QColor constructed from the HSV
1998 color values, \a h (hue), \a s (saturation), \a v (value), and \a a
1999 (alpha-channel, i.e. transparency).
2000
2001 All the values must be in the range 0.0-1.0.
2002
2003 \sa toHsv(), fromHsv(), isValid(), {QColor#The HSV Color
2004 Model}{The HSV Color Model}
2005*/
2006QColor QColor::fromHsvF(qreal h, qreal s, qreal v, qreal a)
2007{
2008 if (((h < 0.0 || h > 1.0) && h != -1.0)
2009 || (s < 0.0 || s > 1.0)
2010 || (v < 0.0 || v > 1.0)
2011 || (a < 0.0 || a > 1.0)) {
2012 qWarning("QColor::fromHsvF: HSV parameters out of range");
2013 return QColor();
2014 }
2015
2016 QColor color;
2017 color.cspec = Hsv;
2018 color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
2019 color.ct.ahsv.hue = h == -1.0 ? USHRT_MAX : qRound(h * 36000);
2020 color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
2021 color.ct.ahsv.value = qRound(v * USHRT_MAX);
2022 color.ct.ahsv.pad = 0;
2023 return color;
2024}
2025
2026/*!
2027 \since 4.6
2028
2029 Static convenience function that returns a QColor constructed from the HSV
2030 color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2031 (alpha-channel, i.e. transparency).
2032
2033 The value of \a s, \a l, and \a a must all be in the range 0-255; the value
2034 of \a h must be in the range 0-359.
2035
2036 \sa toHsl(), fromHslF(), isValid()
2037*/
2038QColor QColor::fromHsl(int h, int s, int l, int a)
2039{
2040 if (((h < 0 || h >= 360) && h != -1)
2041 || s < 0 || s > 255
2042 || l < 0 || l > 255
2043 || a < 0 || a > 255) {
2044 qWarning("QColor::fromHsv: HSV parameters out of range");
2045 return QColor();
2046 }
2047
2048 QColor color;
2049 color.cspec = Hsl;
2050 color.ct.ahsl.alpha = a * 0x101;
2051 color.ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
2052 color.ct.ahsl.saturation = s * 0x101;
2053 color.ct.ahsl.lightness = l * 0x101;
2054 color.ct.ahsl.pad = 0;
2055 return color;
2056}
2057
2058/*!
2059 \overload
2060 \since 4.6
2061
2062 Static convenience function that returns a QColor constructed from the HSV
2063 color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
2064 (alpha-channel, i.e. transparency).
2065
2066 All the values must be in the range 0.0-1.0.
2067
2068 \sa toHsl(), fromHsl(), isValid()
2069*/
2070QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a)
2071{
2072 if (((h < 0.0 || h > 1.0) && h != -1.0)
2073 || (s < 0.0 || s > 1.0)
2074 || (l < 0.0 || l > 1.0)
2075 || (a < 0.0 || a > 1.0)) {
2076 qWarning("QColor::fromHsvF: HSV parameters out of range");
2077 return QColor();
2078 }
2079
2080 QColor color;
2081 color.cspec = Hsl;
2082 color.ct.ahsl.alpha = qRound(a * USHRT_MAX);
2083 color.ct.ahsl.hue = (h == -1.0) ? USHRT_MAX : qRound(h * 36000);
2084 if (color.ct.ahsl.hue == 36000)
2085 color.ct.ahsl.hue = 0;
2086 color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
2087 color.ct.ahsl.lightness = qRound(l * USHRT_MAX);
2088 color.ct.ahsl.pad = 0;
2089 return color;
2090}
2091
2092
2093/*!
2094 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2095 cyan, magenta, yellow, black, and alpha-channel (transparency) components
2096 of the color's CMYK value.
2097
2098 These components can be retrieved individually using the cyan(), magenta(),
2099 yellow(), black() and alpha() functions.
2100
2101 \sa setCmyk(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2102*/
2103void QColor::getCmyk(int *c, int *m, int *y, int *k, int *a)
2104{
2105 if (!c || !m || !y || !k)
2106 return;
2107
2108 if (cspec != Invalid && cspec != Cmyk) {
2109 toCmyk().getCmyk(c, m, y, k, a);
2110 return;
2111 }
2112
2113 *c = ct.acmyk.cyan >> 8;
2114 *m = ct.acmyk.magenta >> 8;
2115 *y = ct.acmyk.yellow >> 8;
2116 *k = ct.acmyk.black >> 8;
2117
2118 if (a)
2119 *a = ct.acmyk.alpha >> 8;
2120}
2121
2122/*!
2123 Sets the contents pointed to by \a c, \a m, \a y, \a k, and \a a, to the
2124 cyan, magenta, yellow, black, and alpha-channel (transparency) components
2125 of the color's CMYK value.
2126
2127 These components can be retrieved individually using the cyanF(),
2128 magentaF(), yellowF(), blackF() and alphaF() functions.
2129
2130 \sa setCmykF(), {QColor#The CMYK Color Model}{The CMYK Color Model}
2131*/
2132void QColor::getCmykF(qreal *c, qreal *m, qreal *y, qreal *k, qreal *a)
2133{
2134 if (!c || !m || !y || !k)
2135 return;
2136
2137 if (cspec != Invalid && cspec != Cmyk) {
2138 toCmyk().getCmykF(c, m, y, k, a);
2139 return;
2140 }
2141
2142 *c = ct.acmyk.cyan / qreal(USHRT_MAX);
2143 *m = ct.acmyk.magenta / qreal(USHRT_MAX);
2144 *y = ct.acmyk.yellow / qreal(USHRT_MAX);
2145 *k = ct.acmyk.black / qreal(USHRT_MAX);
2146
2147 if (a)
2148 *a = ct.acmyk.alpha / qreal(USHRT_MAX);
2149}
2150
2151/*!
2152 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2153 \a k (black), and \a a (alpha-channel, i.e. transparency).
2154
2155 All the values must be in the range 0-255.
2156
2157 \sa getCmyk(), setCmykF(), {QColor#The CMYK Color Model}{The
2158 CMYK Color Model}
2159*/
2160void QColor::setCmyk(int c, int m, int y, int k, int a)
2161{
2162 if (c < 0 || c > 255
2163 || m < 0 || m > 255
2164 || y < 0 || y > 255
2165 || k < 0 || k > 255
2166 || a < 0 || a > 255) {
2167 qWarning("QColor::setCmyk: CMYK parameters out of range");
2168 return;
2169 }
2170
2171 cspec = Cmyk;
2172 ct.acmyk.alpha = a * 0x101;
2173 ct.acmyk.cyan = c * 0x101;
2174 ct.acmyk.magenta = m * 0x101;
2175 ct.acmyk.yellow = y * 0x101;
2176 ct.acmyk.black = k * 0x101;
2177}
2178
2179/*!
2180 \overload
2181
2182 Sets the color to CMYK values, \a c (cyan), \a m (magenta), \a y (yellow),
2183 \a k (black), and \a a (alpha-channel, i.e. transparency).
2184
2185 All the values must be in the range 0.0-1.0.
2186
2187 \sa getCmykF() setCmyk(), {QColor#The CMYK Color Model}{The CMYK
2188 Color Model}
2189*/
2190void QColor::setCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2191{
2192 if (c < 0.0 || c > 1.0
2193 || m < 0.0 || m > 1.0
2194 || y < 0.0 || y > 1.0
2195 || k < 0.0 || k > 1.0
2196 || a < 0.0 || a > 1.0) {
2197 qWarning("QColor::setCmykF: CMYK parameters out of range");
2198 return;
2199 }
2200
2201 cspec = Cmyk;
2202 ct.acmyk.alpha = qRound(a * USHRT_MAX);
2203 ct.acmyk.cyan = qRound(c * USHRT_MAX);
2204 ct.acmyk.magenta = qRound(m * USHRT_MAX);
2205 ct.acmyk.yellow = qRound(y * USHRT_MAX);
2206 ct.acmyk.black = qRound(k * USHRT_MAX);
2207}
2208
2209/*!
2210 Static convenience function that returns a QColor constructed from the
2211 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2212 (black), and \a a (alpha-channel, i.e. transparency).
2213
2214 All the values must be in the range 0-255.
2215
2216 \sa toCmyk(), fromCmykF(), isValid(), {QColor#The CMYK Color Model}{The CMYK
2217 Color Model}
2218*/
2219QColor QColor::fromCmyk(int c, int m, int y, int k, int a)
2220{
2221 if (c < 0 || c > 255
2222 || m < 0 || m > 255
2223 || y < 0 || y > 255
2224 || k < 0 || k > 255
2225 || a < 0 || a > 255) {
2226 qWarning("QColor::fromCmyk: CMYK parameters out of range");
2227 return QColor();
2228 }
2229
2230 QColor color;
2231 color.cspec = Cmyk;
2232 color.ct.acmyk.alpha = a * 0x101;
2233 color.ct.acmyk.cyan = c * 0x101;
2234 color.ct.acmyk.magenta = m * 0x101;
2235 color.ct.acmyk.yellow = y * 0x101;
2236 color.ct.acmyk.black = k * 0x101;
2237 return color;
2238}
2239
2240/*!
2241 \overload
2242
2243 Static convenience function that returns a QColor constructed from the
2244 given CMYK color values: \a c (cyan), \a m (magenta), \a y (yellow), \a k
2245 (black), and \a a (alpha-channel, i.e. transparency).
2246
2247 All the values must be in the range 0.0-1.0.
2248
2249 \sa toCmyk(), fromCmyk(), isValid(), {QColor#The CMYK Color
2250 Model}{The CMYK Color Model}
2251*/
2252QColor QColor::fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a)
2253{
2254 if (c < 0.0 || c > 1.0
2255 || m < 0.0 || m > 1.0
2256 || y < 0.0 || y > 1.0
2257 || k < 0.0 || k > 1.0
2258 || a < 0.0 || a > 1.0) {
2259 qWarning("QColor::fromCmykF: CMYK parameters out of range");
2260 return QColor();
2261 }
2262
2263 QColor color;
2264 color.cspec = Cmyk;
2265 color.ct.acmyk.alpha = qRound(a * USHRT_MAX);
2266 color.ct.acmyk.cyan = qRound(c * USHRT_MAX);
2267 color.ct.acmyk.magenta = qRound(m * USHRT_MAX);
2268 color.ct.acmyk.yellow = qRound(y * USHRT_MAX);
2269 color.ct.acmyk.black = qRound(k * USHRT_MAX);
2270 return color;
2271}
2272
2273/*!
2274 \fn QColor QColor::lighter(int factor) const
2275 \since 4.3
2276
2277 Returns a lighter (or darker) color, but does not change this object.
2278
2279 If the \a factor is greater than 100, this functions returns a lighter
2280 color. Setting \a factor to 150 returns a color that is 50% brighter. If
2281 the \a factor is less than 100, the return color is darker, but we
2282 recommend using the darker() function for this purpose. If the \a factor
2283 is 0 or negative, the return value is unspecified.
2284
2285 The function converts the current RGB color to HSV, multiplies the value
2286 (V) component by \a factor and converts the color back to RGB.
2287
2288 \sa darker(), isValid()
2289*/
2290
2291/*!
2292 \obsolete
2293
2294 Use lighter(\a factor) instead.
2295*/
2296QColor QColor::light(int factor) const
2297{
2298 if (factor <= 0) // invalid lightness factor
2299 return *this;
2300 else if (factor < 100) // makes color darker
2301 return darker(10000 / factor);
2302
2303 QColor hsv = toHsv();
2304 int s = hsv.ct.ahsv.saturation;
2305 int v = hsv.ct.ahsv.value;
2306
2307 v = (factor*v)/100;
2308 if (v > USHRT_MAX) {
2309 // overflow... adjust saturation
2310 s -= v - USHRT_MAX;
2311 if (s < 0)
2312 s = 0;
2313 v = USHRT_MAX;
2314 }
2315
2316 hsv.ct.ahsv.saturation = s;
2317 hsv.ct.ahsv.value = v;
2318
2319 // convert back to same color spec as original color
2320 return hsv.convertTo(cspec);
2321}
2322
2323/*!
2324 \fn QColor QColor::darker(int factor) const
2325 \since 4.3
2326
2327 Returns a darker (or lighter) color, but does not change this object.
2328
2329 If the \a factor is greater than 100, this functions returns a darker
2330 color. Setting \a factor to 300 returns a color that has one-third the
2331 brightness. If the \a factor is less than 100, the return color is lighter,
2332 but we recommend using the lighter() function for this purpose. If the
2333 \a factor is 0 or negative, the return value is unspecified.
2334
2335 The function converts the current RGB color to HSV, divides the value (V)
2336 component by \a factor and converts the color back to RGB.
2337
2338 \sa lighter(), isValid()
2339*/
2340
2341/*!
2342 \obsolete
2343
2344 Use darker(\a factor) instead.
2345*/
2346QColor QColor::dark(int factor) const
2347{
2348 if (factor <= 0) // invalid darkness factor
2349 return *this;
2350 else if (factor < 100) // makes color lighter
2351 return lighter(10000 / factor);
2352
2353 QColor hsv = toHsv();
2354 hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
2355
2356 // convert back to same color spec as original color
2357 return hsv.convertTo(cspec);
2358}
2359
2360/*!
2361 Assigns a copy of \a color to this color, and returns a reference to it.
2362*/
2363QColor &QColor::operator=(const QColor &color)
2364{
2365 cspec = color.cspec;
2366 ct.argb = color.ct.argb;
2367 return *this;
2368}
2369
2370/*! \overload
2371 Assigns a copy of \a color and returns a reference to this color.
2372 */
2373QColor &QColor::operator=(Qt::GlobalColor color)
2374{
2375 return operator=(QColor(color));
2376}
2377
2378/*!
2379 Returns true if this color has the same RGB and alpha values as \a color;
2380 otherwise returns false.
2381*/
2382bool QColor::operator==(const QColor &color) const
2383{
2384 if (cspec == Hsl && cspec == color.cspec) {
2385 return (ct.argb.alpha == color.ct.argb.alpha
2386 && ((((ct.ahsl.hue % 36000) == (color.ct.ahsl.hue % 36000)))
2387 || (ct.ahsl.hue == color.ct.ahsl.hue))
2388 && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50
2389 || ct.ahsl.lightness == 0
2390 || color.ct.ahsl.lightness == 0
2391 || ct.ahsl.lightness == USHRT_MAX
2392 || color.ct.ahsl.lightness == USHRT_MAX)
2393 && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
2394 } else {
2395 return (cspec == color.cspec
2396 && ct.argb.alpha == color.ct.argb.alpha
2397 && (((cspec == QColor::Hsv)
2398 && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
2399 || (ct.ahsv.hue == color.ct.ahsv.hue))
2400 && ct.argb.green == color.ct.argb.green
2401 && ct.argb.blue == color.ct.argb.blue
2402 && ct.argb.pad == color.ct.argb.pad);
2403 }
2404}
2405
2406/*!
2407 Returns true if this color has a different RGB and alpha values from
2408 \a color; otherwise returns false.
2409*/
2410bool QColor::operator!=(const QColor &color) const
2411{ return !operator==(color); }
2412
2413
2414/*!
2415 Returns the color as a QVariant
2416*/
2417QColor::operator QVariant() const
2418{
2419 return QVariant(QVariant::Color, this);
2420}
2421
2422#ifdef Q_WS_X11
2423/*!
2424 Returns true if setNamedColor() is allowed to look up colors in the X11
2425 color database. By default, this function returns false.
2426
2427 \note This function is only available on the X11 platform.
2428
2429 \sa setAllowX11ColorNames()
2430*/
2431bool QColor::allowX11ColorNames()
2432{
2433 return ::allowX11ColorNames;
2434}
2435
2436/*!
2437 Allow setNamedColor() to look up colors in the X11 color database if
2438 \a enabled. By default, setNamedColor() does \e not look up colors in the
2439 X11 color database.
2440
2441 \note This function is only available on the X11 platform.
2442
2443 \sa setNamedColor(), allowX11ColorNames()
2444*/
2445void QColor::setAllowX11ColorNames(bool enabled)
2446{
2447 ::allowX11ColorNames = enabled;
2448}
2449#endif
2450
2451/*! \internal
2452
2453 Marks the color as invalid and sets all components to zero (alpha is set
2454 to fully opaque for compatibility with Qt 3).
2455*/
2456void QColor::invalidate()
2457{
2458 cspec = Invalid;
2459 ct.argb.alpha = USHRT_MAX;
2460 ct.argb.red = 0;
2461 ct.argb.green = 0;
2462 ct.argb.blue = 0;
2463 ct.argb.pad = 0;
2464}
2465
2466#ifdef QT3_SUPPORT
2467
2468/*!
2469 Returns the pixel value used by the underlying window system to refer to a
2470 color.
2471
2472 Use QColormap::pixel() instead.
2473
2474 \oldcode
2475 QColor myColor;
2476 uint pixel = myColor.pixel(screen);
2477 \newcode
2478 QColormap cmap = QColormap::instance(screen);
2479 uint pixel = cmap.pixel(*this);
2480 \endcode
2481*/
2482uint QColor::pixel(int screen) const
2483{
2484 QColormap cmap = QColormap::instance(screen);
2485 return cmap.pixel(*this);
2486}
2487
2488#endif // QT3_SUPPORT
2489
2490/*****************************************************************************
2491 QColor stream functions
2492 *****************************************************************************/
2493
2494#ifndef QT_NO_DEBUG_STREAM
2495QDebug operator<<(QDebug dbg, const QColor &c)
2496{
2497#ifndef Q_BROKEN_DEBUG_STREAM
2498 if (!c.isValid())
2499 dbg.nospace() << "QColor(Invalid)";
2500 else if (c.spec() == QColor::Rgb)
2501 dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
2502 else if (c.spec() == QColor::Hsv)
2503 dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
2504 else if (c.spec() == QColor::Cmyk)
2505 dbg.nospace() << "QColor(ACMYK " << c.alphaF() << ", " << c.cyanF() << ", " << c.magentaF() << ", " << c.yellowF() << ", "
2506 << c.blackF()<< ')';
2507 else if (c.spec() == QColor::Hsl)
2508 dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
2509
2510 return dbg.space();
2511#else
2512 qWarning("This compiler doesn't support streaming QColor to QDebug");
2513 return dbg;
2514 Q_UNUSED(c);
2515#endif
2516}
2517#endif
2518
2519#ifndef QT_NO_DATASTREAM
2520/*!
2521 \fn QDataStream &operator<<(QDataStream &stream, const QColor &color)
2522 \relates QColor
2523
2524 Writes the \a color to the \a stream.
2525
2526 \sa {Serializing Qt Data Types}
2527*/
2528QDataStream &operator<<(QDataStream &stream, const QColor &color)
2529{
2530 if (stream.version() < 7) {
2531 if (!color.isValid())
2532 return stream << quint32(0x49000000);
2533 quint32 p = (quint32)color.rgb();
2534 if (stream.version() == 1) // Swap red and blue
2535 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2536 return stream << p;
2537 }
2538
2539 qint8 s = color.cspec;
2540 quint16 a = color.ct.argb.alpha;
2541 quint16 r = color.ct.argb.red;
2542 quint16 g = color.ct.argb.green;
2543 quint16 b = color.ct.argb.blue;
2544 quint16 p = color.ct.argb.pad;
2545
2546 stream << s;
2547 stream << a;
2548 stream << r;
2549 stream << g;
2550 stream << b;
2551 stream << p;
2552
2553 return stream;
2554}
2555
2556/*!
2557 \fn QDataStream &operator>>(QDataStream &stream, QColor &color)
2558 \relates QColor
2559
2560 Reads the \a color from the \a stream.
2561
2562 \sa {Serializing Qt Data Types}
2563*/
2564QDataStream &operator>>(QDataStream &stream, QColor &color)
2565{
2566 if (stream.version() < 7) {
2567 quint32 p;
2568 stream >> p;
2569 if (p == 0x49000000) {
2570 color.invalidate();
2571 return stream;
2572 }
2573 if (stream.version() == 1) // Swap red and blue
2574 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
2575 color.setRgb(p);
2576 return stream;
2577 }
2578
2579 qint8 s;
2580 quint16 a, r, g, b, p;
2581 stream >> s;
2582 stream >> a;
2583 stream >> r;
2584 stream >> g;
2585 stream >> b;
2586 stream >> p;
2587
2588 color.cspec = QColor::Spec(s);
2589 color.ct.argb.alpha = a;
2590 color.ct.argb.red = r;
2591 color.ct.argb.green = g;
2592 color.ct.argb.blue = b;
2593 color.ct.argb.pad = p;
2594
2595 return stream;
2596}
2597#endif // QT_NO_DATASTREAM
2598
2599
2600/*****************************************************************************
2601 QColor global functions (documentation only)
2602 *****************************************************************************/
2603
2604/*!
2605 \fn int qRed(QRgb rgb)
2606 \relates QColor
2607
2608 Returns the red component of the ARGB quadruplet \a rgb.
2609
2610 \sa qRgb(), QColor::red()
2611*/
2612
2613/*!
2614 \fn int qGreen(QRgb rgb)
2615 \relates QColor
2616
2617 Returns the green component of the ARGB quadruplet \a rgb.
2618
2619 \sa qRgb(), QColor::green()
2620*/
2621
2622/*!
2623 \fn int qBlue(QRgb rgb)
2624 \relates QColor
2625
2626 Returns the blue component of the ARGB quadruplet \a rgb.
2627
2628 \sa qRgb(), QColor::blue()
2629*/
2630
2631/*!
2632 \fn int qAlpha(QRgb rgba)
2633 \relates QColor
2634
2635 Returns the alpha component of the ARGB quadruplet \a rgba.
2636
2637 \sa qRgb(), QColor::alpha()
2638*/
2639
2640/*!
2641 \fn QRgb qRgb(int r, int g, int b)
2642 \relates QColor
2643
2644 Returns the ARGB quadruplet (255, \a{r}, \a{g}, \a{b}).
2645
2646 \sa qRgba(), qRed(), qGreen(), qBlue()
2647*/
2648
2649/*!
2650 \fn QRgb qRgba(int r, int g, int b, int a)
2651 \relates QColor
2652
2653 Returns the ARGB quadruplet (\a{a}, \a{r}, \a{g}, \a{b}).
2654
2655 \sa qRgb(), qRed(), qGreen(), qBlue()
2656*/
2657
2658/*!
2659 \fn int qGray(int r, int g, int b)
2660 \relates QColor
2661
2662 Returns a gray value (0 to 255) from the (\a r, \a g, \a b)
2663 triplet.
2664
2665 The gray value is calculated using the formula (\a r * 11 + \a g * 16 +
2666 \a b * 5)/32.
2667*/
2668
2669/*!
2670 \fn int qGray(QRgb rgb)
2671 \overload
2672 \relates QColor
2673
2674 Returns a gray value (0 to 255) from the given ARGB quadruplet \a rgb.
2675
2676 The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32;
2677 the alpha-channel is ignored.
2678*/
2679
2680/*!
2681 \fn QColor::QColor(int x, int y, int z, Spec colorSpec)
2682
2683 Use one of the other QColor constructors, or one of the static convenience
2684 functions, instead.
2685*/
2686
2687/*!
2688 \fn QColor::rgb(int *r, int *g, int *b) const
2689
2690 Use getRgb() instead.
2691*/
2692
2693/*!
2694 \fn QColor::hsv(int *h, int *s, int *v) const
2695
2696 Use getHsv() instead.
2697*/
2698
2699/*!
2700 \fn QColor QColor::convertTo(Spec colorSpec) const
2701
2702 Creates a copy of \e this color in the format specified by \a colorSpec.
2703
2704 \sa spec(), toCmyk(), toHsv(), toRgb(), isValid()
2705*/
2706
2707/*!
2708 \typedef QRgb
2709 \relates QColor
2710
2711 An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
2712
2713 The type also holds a value for the alpha-channel. The default alpha
2714 channel is \c ff, i.e opaque. For more information, see the
2715 \l{QColor#Alpha-Blended Drawing}{Alpha-Blended Drawing} section.
2716
2717 \sa QColor::rgb(), QColor::rgba()
2718*/
2719
2720QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.