-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathqcolor.cpp
2604 lines (2121 loc) · 73.8 KB
/
qcolor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qcolor.h"
#include "qcolor_p.h"
#include "qnamespace.h"
#include "qdatastream.h"
#include "qvariant.h"
#include "qdebug.h"
#include <math.h>
#include <stdio.h>
#include <limits.h>
QT_BEGIN_NAMESPACE
/*!
\class QColor
\brief The QColor class provides colors based on RGB, HSV or CMYK values.
\ingroup painting
\ingroup appearance
\inmodule QtGui
A color is normally specified in terms of RGB (red, green, and
blue) components, but it is also possible to specify it in terms
of HSV (hue, saturation, and value) and CMYK (cyan, magenta,
yellow and black) components. In addition a color can be specified
using a color name. The color name can be any of the SVG 1.0 color
names.
\table
\header
\li RGB \li HSV \li CMYK
\row
\li \inlineimage qcolor-rgb.png
\li \inlineimage qcolor-hsv.png
\li \inlineimage qcolor-cmyk.png
\endtable
The QColor constructor creates the color based on RGB values. To
create a QColor based on either HSV or CMYK values, use the
toHsv() and toCmyk() functions respectively. These functions
return a copy of the color using the desired format. In addition
the static fromRgb(), fromHsv() and fromCmyk() functions create
colors from the specified values. Alternatively, a color can be
converted to any of the three formats using the convertTo()
function (returning a copy of the color in the desired format), or
any of the setRgb(), setHsv() and setCmyk() functions altering \e
this color's format. The spec() function tells how the color was
specified.
A color can be set by passing an RGB string (such as "#112233"),
or an ARGB string (such as "#ff112233") or a color name (such as "blue"),
to the setNamedColor() function.
The color names are taken from the SVG 1.0 color names. The name()
function returns the name of the color in the format
"#RRGGBB". Colors can also be set using setRgb(), setHsv() and
setCmyk(). To get a lighter or darker color use the lighter() and
darker() functions respectively.
The isValid() function indicates whether a QColor is legal at
all. For example, a RGB color with RGB values out of range is
illegal. For performance reasons, QColor mostly disregards illegal
colors, and for that reason, the result of using an invalid color
is undefined.
The color components can be retrieved individually, e.g with
red(), hue() and cyan(). The values of the color components can
also be retrieved in one go using the getRgb(), getHsv() and
getCmyk() functions. Using the RGB color model, the color
components can in addition be accessed with rgb().
There are several related non-members: QRgb is a typdef for an
unsigned int representing the RGB value triplet (r, g, b). Note
that it also can hold a value for the alpha-channel (for more
information, see the \l {QColor#Alpha-Blended
Drawing}{Alpha-Blended Drawing} section). The qRed(), qBlue() and
qGreen() functions return the respective component of the given
QRgb value, while the qRgb() and qRgba() functions create and
return the QRgb triplet based on the given component
values. Finally, the qAlpha() function returns the alpha component
of the provided QRgb, and the qGray() function calculates and
return a gray value based on the given value.
QColor is platform and device independent. The QColormap class
maps the color to the hardware.
For more information about painting in general, see the \l{Paint
System} documentation.
\tableofcontents
\section1 Integer vs. Floating Point Precision
QColor supports floating point precision and provides floating
point versions of all the color components functions,
e.g. getRgbF(), hueF() and fromCmykF(). Note that since the
components are stored using 16-bit integers, there might be minor
deviations between the values set using, for example, setRgbF()
and the values returned by the getRgbF() function due to rounding.
While the integer based functions take values in the range 0-255
(except hue() which must have values within the range 0-359),
the floating point functions accept values in the range 0.0 - 1.0.
\section1 Alpha-Blended Drawing
QColor also support alpha-blended outlining and filling. The
alpha channel of a color specifies the transparency effect, 0
represents a fully transparent color, while 255 represents a fully
opaque color. For example:
\snippet code/src_gui_painting_qcolor.cpp 0
The code above produces the following output:
\image alphafill.png
The alpha channel of a color can be retrieved and set using the
alpha() and setAlpha() functions if its value is an integer, and
alphaF() and setAlphaF() if its value is qreal (double). By
default, the alpha-channel is set to 255 (opaque). To retrieve and
set \e all the RGB color components (including the alpha-channel)
in one go, use the rgba() and setRgba() functions.
\section1 Predefined Colors
There are 20 predefined QColors described by the Qt::GlobalColor enum,
including black, white, primary and secondary colors, darker versions
of these colors and three shades of gray. QColor also recognizes a
variety of color names; the static colorNames() function returns a
QStringList color names that QColor knows about.
\image qt-colors.png Qt Colors
Additionally, the Qt::color0, Qt::color1 and Qt::transparent colors
are used for special purposes.
Qt::color0 (zero pixel value) and Qt::color1 (non-zero pixel value)
are special colors for drawing in QBitmaps. Painting with Qt::color0
sets the bitmap bits to 0 (transparent; i.e., background), and painting
with Qt::color1 sets the bits to 1 (opaque; i.e., foreground).
Qt::transparent is used to indicate a transparent pixel. When painting
with this value, a pixel value will be used that is appropriate for the
underlying pixel format in use.
\section1 The HSV Color Model
The RGB model is hardware-oriented. Its representation is close to
what most monitors show. In contrast, HSV represents color in a way
more suited to the human perception of color. For example, the
relationships "stronger than", "darker than", and "the opposite of"
are easily expressed in HSV but are much harder to express in RGB.
HSV, like RGB, has three components:
\list
\li H, for hue, is in the range 0 to 359 if the color is chromatic (not
gray), or meaningless if it is gray. It represents degrees on the
color wheel familiar to most people. Red is 0 (degrees), green is
120, and blue is 240.
\inlineimage qcolor-hue.png
\li S, for saturation, is in the range 0 to 255, and the bigger it is,
the stronger the color is. Grayish colors have saturation near 0; very
strong colors have saturation near 255.
\inlineimage qcolor-saturation.png
\li V, for value, is in the range 0 to 255 and represents lightness or
brightness of the color. 0 is black; 255 is as far from black as
possible.
\inlineimage qcolor-value.png
\endlist
Here are some examples: pure red is H=0, S=255, V=255; a dark red,
moving slightly towards the magenta, could be H=350 (equivalent to
-10), S=255, V=180; a grayish light red could have H about 0 (say
350-359 or 0-10), S about 50-100, and S=255.
Qt returns a hue value of -1 for achromatic colors. If you pass a
hue value that is too large, Qt forces it into range. Hue 360 or 720 is
treated as 0; hue 540 is treated as 180.
In addition to the standard HSV model, Qt provides an
alpha-channel to feature \l {QColor#Alpha-Blended
Drawing}{alpha-blended drawing}.
\section1 The HSL Color Model
HSL is similar to HSV. Instead of value parameter from HSV,
HSL has the lightness parameter.
The lightness parameter goes from black to color and from color to white.
If you go outside at the night its black or dark gray. At day its colorful but
if you look in a really strong light a things they are going to white and
wash out.
\section1 The CMYK Color Model
While the RGB and HSV color models are used for display on
computer monitors, the CMYK model is used in the four-color
printing process of printing presses and some hard-copy
devices.
CMYK has four components, all in the range 0-255: cyan (C),
magenta (M), yellow (Y) and black (K). Cyan, magenta and yellow
are called subtractive colors; the CMYK color model creates color
by starting with a white surface and then subtracting color by
applying the appropriate components. While combining cyan, magenta
and yellow gives the color black, subtracting one or more will
yield any other color. When combined in various percentages, these
three colors can create the entire spectrum of colors.
Mixing 100 percent of cyan, magenta and yellow \e does produce
black, but the result is unsatisfactory since it wastes ink,
increases drying time, and gives a muddy colour when printing. For
that reason, black is added in professional printing to provide a
solid black tone; hence the term 'four color process'.
In addition to the standard CMYK model, Qt provides an
alpha-channel to feature \l {QColor#Alpha-Blended
Drawing}{alpha-blended drawing}.
\sa QPalette, QBrush, QApplication::setColorSpec()
*/
#define QCOLOR_INT_RANGE_CHECK(fn, var) \
do { \
if (var < 0 || var > 255) { \
qWarning(#fn": invalid value %d", var); \
var = qMax(0, qMin(var, 255)); \
} \
} while (0)
#define QCOLOR_REAL_RANGE_CHECK(fn, var) \
do { \
if (var < qreal(0.0) || var > qreal(1.0)) { \
qWarning(#fn": invalid value %g", var); \
var = qMax(qreal(0.0), qMin(var, qreal(1.0))); \
} \
} while (0)
/*****************************************************************************
QColor member functions
*****************************************************************************/
/*!
\enum QColor::Spec
The type of color specified, either RGB, HSV, CMYK or HSL.
\value Rgb
\value Hsv
\value Cmyk
\value Hsl
\value Invalid
\sa spec(), convertTo()
*/
/*!
\enum QColor::NameFormat
How to format the output of the name() function
\value HexRgb #RRGGBB A "#" character followed by three two-digit hexadecimal numbers (i.e. \c{#RRGGBB}).
\value HexArgb #AARRGGBB A "#" character followed by four two-digit hexadecimal numbers (i.e. \c{#AARRGGBB}).
\sa name()
*/
/*!
\fn Spec QColor::spec() const
Returns how the color was specified.
\sa Spec, convertTo()
*/
/*!
\fn QColor::QColor()
Constructs an invalid color with the RGB value (0, 0, 0). An
invalid color is a color that is not properly set up for the
underlying window system.
The alpha value of an invalid color is unspecified.
\sa isValid()
*/
/*!
\overload
Constructs a new color with a color value of \a color.
\sa isValid(), {QColor#Predefined Colors}{Predefined Colors}
*/
QColor::QColor(Qt::GlobalColor color)
{
#define QRGB(r, g, b) \
QRgb(((0xffu << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff)))
#define QRGBA(r, g, b, a) \
QRgb(((a & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff))
static const QRgb global_colors[] = {
QRGB(255, 255, 255), // Qt::color0
QRGB( 0, 0, 0), // Qt::color1
QRGB( 0, 0, 0), // black
QRGB(255, 255, 255), // white
/*
* From the "The Palette Manager: How and Why" by Ron Gery,
* March 23, 1992, archived on MSDN:
*
* The Windows system palette is broken up into two
* sections, one with fixed colors and one with colors
* that can be changed by applications. The system palette
* predefines 20 entries; these colors are known as the
* static or reserved colors and consist of the 16 colors
* found in the Windows version 3.0 VGA driver and 4
* additional colors chosen for their visual appeal. The
* DEFAULT_PALETTE stock object is, as the name implies,
* the default palette selected into a device context (DC)
* and consists of these static colors. Applications can
* set the remaining 236 colors using the Palette Manager.
*
* The 20 reserved entries have indices in [0,9] and
* [246,255]. We reuse 17 of them.
*/
QRGB(128, 128, 128), // index 248 medium gray
QRGB(160, 160, 164), // index 247 light gray
QRGB(192, 192, 192), // index 7 light gray
QRGB(255, 0, 0), // index 249 red
QRGB( 0, 255, 0), // index 250 green
QRGB( 0, 0, 255), // index 252 blue
QRGB( 0, 255, 255), // index 254 cyan
QRGB(255, 0, 255), // index 253 magenta
QRGB(255, 255, 0), // index 251 yellow
QRGB(128, 0, 0), // index 1 dark red
QRGB( 0, 128, 0), // index 2 dark green
QRGB( 0, 0, 128), // index 4 dark blue
QRGB( 0, 128, 128), // index 6 dark cyan
QRGB(128, 0, 128), // index 5 dark magenta
QRGB(128, 128, 0), // index 3 dark yellow
QRGBA(0, 0, 0, 0) // transparent
};
#undef QRGB
#undef QRGBA
setRgb(qRed(global_colors[color]),
qGreen(global_colors[color]),
qBlue(global_colors[color]),
qAlpha(global_colors[color]));
}
/*!
\fn QColor::QColor(int r, int g, int b, int a = 255)
Constructs a color with the RGB value \a r, \a g, \a b, and the
alpha-channel (transparency) value of \a a.
The color is left invalid if any of the arguments are invalid.
\sa setRgba(), isValid()
*/
/*!
Constructs a color with the value \a color. The alpha component is
ignored and set to solid.
\sa fromRgb(), isValid()
*/
QColor::QColor(QRgb color)
{
cspec = Rgb;
ct.argb.alpha = 0xffff;
ct.argb.red = qRed(color) * 0x101;
ct.argb.green = qGreen(color) * 0x101;
ct.argb.blue = qBlue(color) * 0x101;
ct.argb.pad = 0;
}
/*!
\internal
Constructs a color with the given \a spec.
This function is primarly present to avoid that QColor::Invalid
becomes a valid color by accident.
*/
QColor::QColor(Spec spec)
{
switch (spec) {
case Invalid:
invalidate();
break;
case Rgb:
setRgb(0, 0, 0);
break;
case Hsv:
setHsv(0, 0, 0);
break;
case Cmyk:
setCmyk(0, 0, 0, 0);
break;
case Hsl:
setHsl(0, 0, 0, 0);
break;
}
}
/*!
\fn QColor::QColor(const QString &name)
Constructs a named color in the same way as setNamedColor() using
the given \a name.
The color is left invalid if the \a name cannot be parsed.
\sa setNamedColor(), name(), isValid()
*/
/*!
\fn QColor::QColor(const char *name)
Constructs a named color in the same way as setNamedColor() using
the given \a name.
The color is left invalid if the \a name cannot be parsed.
\sa setNamedColor(), name(), isValid()
*/
/*!
\fn QColor::QColor(const QColor &color)
Constructs a color that is a copy of \a color.
\sa isValid()
*/
/*!
\fn bool QColor::isValid() const
Returns \c true if the color is valid; otherwise returns \c false.
*/
/*!
Returns the name of the color in the format "#RRGGBB"; i.e. a "#"
character followed by three two-digit hexadecimal numbers.
\sa setNamedColor()
*/
QString QColor::name() const
{
return name(HexRgb);
}
/*!
\since 5.2
Returns the name of the color in the specified \a format.
\sa setNamedColor(), NameFormat
*/
QString QColor::name(NameFormat format) const
{
QString s;
switch (format) {
case HexRgb:
s.sprintf("#%02x%02x%02x", red(), green(), blue());
break;
case HexArgb:
s.sprintf("#%02x%02x%02x%02x", alpha(), red(), green(), blue());
break;
}
return s;
}
/*!
Sets the RGB value of this QColor to \a name, which may be in one
of these formats:
\list
\li #RGB (each of R, G, and B is a single hex digit)
\li #RRGGBB
\li #AARRGGBB (Since 5.2)
\li #RRRGGGBBB
\li #RRRRGGGGBBBB
\li A name from the list of colors defined in the list of \l{http://www.w3.org/TR/SVG/types.html#ColorKeywords}{SVG color keyword names}
provided by the World Wide Web Consortium; for example, "steelblue" or "gainsboro".
These color names work on all platforms. Note that these color names are \e not the
same as defined by the Qt::GlobalColor enums, e.g. "green" and Qt::green does not
refer to the same color.
\li \c transparent - representing the absence of a color.
\endlist
The color is invalid if \a name cannot be parsed.
\sa QColor(), name(), isValid()
*/
void QColor::setNamedColor(const QString &name)
{
setColorFromString(name);
}
/*!
\since 4.7
Returns \c true if the \a name is a valid color name and can
be used to construct a valid QColor object, otherwise returns
false.
It uses the same algorithm used in setNamedColor().
\sa setNamedColor()
*/
bool QColor::isValidColor(const QString &name)
{
return !name.isEmpty() && QColor().setColorFromString(name);
}
bool QColor::setColorFromString(const QString &name)
{
if (name.isEmpty()) {
invalidate();
return true;
}
if (name.startsWith(QLatin1Char('#'))) {
QRgb rgba;
if (qt_get_hex_rgb(name.constData(), name.length(), &rgba)) {
setRgba(rgba);
return true;
} else {
invalidate();
return false;
}
}
#ifndef QT_NO_COLORNAMES
QRgb rgb;
if (qt_get_named_rgb(name.constData(), name.length(), &rgb)) {
setRgba(rgb);
return true;
} else
#endif
{
invalidate();
return false;
}
}
/*!
Returns a QStringList containing the color names Qt knows about.
\sa {QColor#Predefined Colors}{Predefined Colors}
*/
QStringList QColor::colorNames()
{
#ifndef QT_NO_COLORNAMES
return qt_get_colornames();
#else
return QStringList();
#endif
}
/*!
Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
saturation, value, and alpha-channel (transparency) components of the
color's HSV value.
These components can be retrieved individually using the hueF(),
saturationF(), valueF() and alphaF() functions.
\sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
*/
void QColor::getHsvF(qreal *h, qreal *s, qreal *v, qreal *a) const
{
if (!h || !s || !v)
return;
if (cspec != Invalid && cspec != Hsv) {
toHsv().getHsvF(h, s, v, a);
return;
}
*h = ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
*s = ct.ahsv.saturation / qreal(USHRT_MAX);
*v = ct.ahsv.value / qreal(USHRT_MAX);
if (a)
*a = ct.ahsv.alpha / qreal(USHRT_MAX);
}
/*!
Sets the contents pointed to by \a h, \a s, \a v, and \a a, to the hue,
saturation, value, and alpha-channel (transparency) components of the
color's HSV value.
These components can be retrieved individually using the hue(),
saturation(), value() and alpha() functions.
\sa setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
*/
void QColor::getHsv(int *h, int *s, int *v, int *a) const
{
if (!h || !s || !v)
return;
if (cspec != Invalid && cspec != Hsv) {
toHsv().getHsv(h, s, v, a);
return;
}
*h = ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
*s = ct.ahsv.saturation >> 8;
*v = ct.ahsv.value >> 8;
if (a)
*a = ct.ahsv.alpha >> 8;
}
/*!
Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
the value and \a a is the alpha component of the HSV color.
All the values must be in the range 0.0-1.0.
\sa getHsvF(), setHsv(), {QColor#The HSV Color Model}{The HSV Color Model}
*/
void QColor::setHsvF(qreal h, qreal s, qreal v, qreal a)
{
if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
|| (s < qreal(0.0) || s > qreal(1.0))
|| (v < qreal(0.0) || v > qreal(1.0))
|| (a < qreal(0.0) || a > qreal(1.0))) {
qWarning("QColor::setHsvF: HSV parameters out of range");
return;
}
cspec = Hsv;
ct.ahsv.alpha = qRound(a * USHRT_MAX);
ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
ct.ahsv.saturation = qRound(s * USHRT_MAX);
ct.ahsv.value = qRound(v * USHRT_MAX);
ct.ahsv.pad = 0;
}
/*!
Sets a HSV color value; \a h is the hue, \a s is the saturation, \a v is
the value and \a a is the alpha component of the HSV color.
The saturation, value and alpha-channel values must be in the range 0-255,
and the hue value must be greater than -1.
\sa getHsv(), setHsvF(), {QColor#The HSV Color Model}{The HSV Color Model}
*/
void QColor::setHsv(int h, int s, int v, int a)
{
if (h < -1 || (uint)s > 255 || (uint)v > 255 || (uint)a > 255) {
qWarning("QColor::setHsv: HSV parameters out of range");
invalidate();
return;
}
cspec = Hsv;
ct.ahsv.alpha = a * 0x101;
ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
ct.ahsv.saturation = s * 0x101;
ct.ahsv.value = v * 0x101;
ct.ahsv.pad = 0;
}
/*!
\since 4.6
Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
saturation, lightness, and alpha-channel (transparency) components of the
color's HSL value.
These components can be retrieved individually using the hueHslF(),
saturationHslF(), lightnessF() and alphaF() functions.
\sa setHsl()
*/
void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
{
if (!h || !s || !l)
return;
if (cspec != Invalid && cspec != Hsl) {
toHsl().getHslF(h, s, l, a);
return;
}
*h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
*s = ct.ahsl.saturation / qreal(USHRT_MAX);
*l = ct.ahsl.lightness / qreal(USHRT_MAX);
if (a)
*a = ct.ahsl.alpha / qreal(USHRT_MAX);
}
/*!
\since 4.6
Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
saturation, lightness, and alpha-channel (transparency) components of the
color's HSL value.
These components can be retrieved individually using the hueHsl(),
saturationHsl(), lightness() and alpha() functions.
\sa setHsl()
*/
void QColor::getHsl(int *h, int *s, int *l, int *a) const
{
if (!h || !s || !l)
return;
if (cspec != Invalid && cspec != Hsl) {
toHsl().getHsl(h, s, l, a);
return;
}
*h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
*s = ct.ahsl.saturation >> 8;
*l = ct.ahsl.lightness >> 8;
if (a)
*a = ct.ahsl.alpha >> 8;
}
/*!
\since 4.6
Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
the lightness and \a a is the alpha component of the HSL color.
All the values must be in the range 0.0-1.0.
\sa getHslF(), setHsl()
*/
void QColor::setHslF(qreal h, qreal s, qreal l, qreal a)
{
if (((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
|| (s < qreal(0.0) || s > qreal(1.0))
|| (l < qreal(0.0) || l > qreal(1.0))
|| (a < qreal(0.0) || a > qreal(1.0))) {
qWarning("QColor::setHsvF: HSV parameters out of range");
return;
}
cspec = Hsl;
ct.ahsl.alpha = qRound(a * USHRT_MAX);
ct.ahsl.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
ct.ahsl.saturation = qRound(s * USHRT_MAX);
ct.ahsl.lightness = qRound(l * USHRT_MAX);
ct.ahsl.pad = 0;
}
/*!
\since 4.6
Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
the lightness and \a a is the alpha component of the HSL color.
The saturation, value and alpha-channel values must be in the range 0-255,
and the hue value must be greater than -1.
\sa getHsl(), setHslF()
*/
void QColor::setHsl(int h, int s, int l, int a)
{
if (h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255) {
qWarning("QColor::setHsv: HSV parameters out of range");
invalidate();
return;
}
cspec = Hsl;
ct.ahsl.alpha = a * 0x101;
ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
ct.ahsl.saturation = s * 0x101;
ct.ahsl.lightness = l * 0x101;
ct.ahsl.pad = 0;
}
/*!
Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
green, blue, and alpha-channel (transparency) components of the color's
RGB value.
These components can be retrieved individually using the redF(), greenF(),
blueF() and alphaF() functions.
\sa rgb(), setRgb()
*/
void QColor::getRgbF(qreal *r, qreal *g, qreal *b, qreal *a) const
{
if (!r || !g || !b)
return;
if (cspec != Invalid && cspec != Rgb) {
toRgb().getRgbF(r, g, b, a);
return;
}
*r = ct.argb.red / qreal(USHRT_MAX);
*g = ct.argb.green / qreal(USHRT_MAX);
*b = ct.argb.blue / qreal(USHRT_MAX);
if (a)
*a = ct.argb.alpha / qreal(USHRT_MAX);
}
/*!
Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
green, blue, and alpha-channel (transparency) components of the color's
RGB value.
These components can be retrieved individually using the red(), green(),
blue() and alpha() functions.
\sa rgb(), setRgb()
*/
void QColor::getRgb(int *r, int *g, int *b, int *a) const
{
if (!r || !g || !b)
return;
if (cspec != Invalid && cspec != Rgb) {
toRgb().getRgb(r, g, b, a);
return;
}
*r = ct.argb.red >> 8;
*g = ct.argb.green >> 8;
*b = ct.argb.blue >> 8;
if (a)
*a = ct.argb.alpha >> 8;
}
/*!
\fn void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
Sets the color channels of this color to \a r (red), \a g (green),
\a b (blue) and \a a (alpha, transparency).
All values must be in the range 0.0-1.0.
\sa rgb(), getRgbF(), setRgb()
*/
void QColor::setRgbF(qreal r, qreal g, qreal b, qreal a)
{
if (r < qreal(0.0) || r > qreal(1.0)
|| g < qreal(0.0) || g > qreal(1.0)
|| b < qreal(0.0) || b > qreal(1.0)
|| a < qreal(0.0) || a > qreal(1.0)) {
qWarning("QColor::setRgbF: RGB parameters out of range");
invalidate();
return;
}
cspec = Rgb;
ct.argb.alpha = qRound(a * USHRT_MAX);
ct.argb.red = qRound(r * USHRT_MAX);
ct.argb.green = qRound(g * USHRT_MAX);
ct.argb.blue = qRound(b * USHRT_MAX);
ct.argb.pad = 0;
}
/*!
Sets the RGB value to \a r, \a g, \a b and the alpha value to \a a.
All the values must be in the range 0-255.
\sa rgb(), getRgb(), setRgbF()
*/
void QColor::setRgb(int r, int g, int b, int a)
{
if ((uint)r > 255 || (uint)g > 255 || (uint)b > 255 || (uint)a > 255) {
qWarning("QColor::setRgb: RGB parameters out of range");
invalidate();
return;
}
cspec = Rgb;
ct.argb.alpha = a * 0x101;
ct.argb.red = r * 0x101;
ct.argb.green = g * 0x101;
ct.argb.blue = b * 0x101;
ct.argb.pad = 0;
}
/*!
\fn QRgb QColor::rgba() const
Returns the RGB value of the color, including its alpha.
For an invalid color, the alpha value of the returned color is unspecified.
\sa setRgba(), rgb()
*/
QRgb QColor::rgba() const
{
if (cspec != Invalid && cspec != Rgb)
return toRgb().rgba();
return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
}
/*!
Sets the RGB value to \a rgba, including its alpha.
\sa rgba(), rgb()
*/
void QColor::setRgba(QRgb rgba)
{
cspec = Rgb;
ct.argb.alpha = qAlpha(rgba) * 0x101;
ct.argb.red = qRed(rgba) * 0x101;
ct.argb.green = qGreen(rgba) * 0x101;
ct.argb.blue = qBlue(rgba) * 0x101;
ct.argb.pad = 0;
}
/*!
\fn QRgb QColor::rgb() const
Returns the RGB value of the color. The alpha value is opaque.
\sa getRgb(), rgba()
*/
QRgb QColor::rgb() const
{
if (cspec != Invalid && cspec != Rgb)
return toRgb().rgb();
return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
}
/*!
\overload
Sets the RGB value to \a rgb. The alpha value is set to opaque.
*/
void QColor::setRgb(QRgb rgb)