add more spacing
[personal-kdebase.git] / workspace / ksplash / ksplashx / qcolor.cpp
blobd8bce0620b54a1a3a89e051527741c291e735212
1 /****************************************************************************
2 **
3 ** This file is based on sources of the Qt GUI Toolkit, used under the terms
4 ** of the GNU General Public License version 2 (see the original copyright
5 ** notice below).
6 ** All further contributions to this file are (and are required to be)
7 ** licensed under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
11 ** The original Qt license header follows:
12 **
14 ** Implementation of QColor class
16 ** Created : 940112
18 ** Copyright (C) 1992-2002 Trolltech AS. All rights reserved.
20 ** This file is part of the kernel module of the Qt GUI Toolkit.
22 ** This file may be distributed under the terms of the Q Public License
23 ** as defined by Trolltech AS of Norway and appearing in the file
24 ** LICENSE.QPL included in the packaging of this file.
26 ** This file may be distributed and/or modified under the terms of the
27 ** GNU General Public License version 2 as published by the Free Software
28 ** Foundation and appearing in the file LICENSE.GPL included in the
29 ** packaging of this file.
31 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
32 ** licenses may use this file in accordance with the Qt Commercial License
33 ** Agreement provided with the Software.
35 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
36 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
38 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
39 ** information about Qt Commercial License Agreements.
40 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
41 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
43 ** Contact info@trolltech.com if any conditions of this licensing are
44 ** not clear to you.
46 **********************************************************************/
48 #include "qcolor.h"
49 #include "qnamespace.h"
50 #include "qdatastream.h"
52 #include <stdio.h>
53 #include <string.h>
55 /*!
56 \class QColor qcolor.h
57 \brief The QColor class provides colors based on RGB or HSV values.
59 \ingroup images
60 \ingroup graphics
61 \ingroup appearance
63 A color is normally specified in terms of RGB (red, green and blue)
64 components, but it is also possible to specify HSV (hue, saturation
65 and value) or set a color name (the names are copied from from the
66 X11 color database).
68 In addition to the RGB value, a QColor also has a pixel value and a
69 validity. The pixel value is used by the underlying window system
70 to refer to a color. It can be thought of as an index into the
71 display hardware's color table.
73 The validity (isValid()) indicates whether the color is legal at
74 all. For example, a RGB color with RGB values out of range is
75 illegal. For performance reasons, QColor mostly disregards illegal
76 colors. The result of using an invalid color is unspecified and
77 will usually be surprising.
79 There are 19 predefined QColor objects: \c white, \c black, \c
80 red, \c darkRed, \c green, \c darkGreen, \c blue, \c darkBlue, \c
81 cyan, \c darkCyan, \c magenta, \c darkMagenta, \c yellow, \c
82 darkYellow, \c gray, \c darkGray, \c lightGray, \c color0 and \c
83 color1, accessible as members of the Qt namespace (ie. \c Qt::red).
85 \img qt-colors.png Qt Colors
87 The colors \c color0 (zero pixel value) and \c color1 (non-zero
88 pixel value) are special colors for drawing in \link QBitmap
89 bitmaps\endlink. Painting with \c color0 sets the bitmap bits to 0
90 (transparent, i.e. background), and painting with \c color1 sets the
91 bits to 1 (opaque, i.e. foreground).
93 The QColor class has an efficient, dynamic color allocation
94 strategy. A color is normally allocated the first time it is used
95 (lazy allocation), that is, whenever the pixel() function is called:
97 \list 1
98 \i Is the pixel value valid? If it is, just return it; otherwise,
99 allocate a pixel value.
100 \i Check an internal hash table to see if we allocated an equal RGB
101 value earlier. If we did, set the pixel value and return.
102 \i Try to allocate the RGB value. If we succeed, we get a pixel value
103 that we save in the internal table with the RGB value.
104 Return the pixel value.
105 \i The color could not be allocated. Find the closest matching
106 color and save it in the internal table.
107 \endlist
109 A color can be set by passing setNamedColor() an RGB string like
110 "#112233", or a color name, e.g. "blue". The names are taken from
111 X11's rgb.txt database but can also be used under Windows. To get
112 a lighter or darker color use light() and dark() respectively.
113 Colors can also be set using setRgb() and setHsv(). The color
114 components can be accessed in one go with rgb() and hsv(), or
115 individually with red(), green() and blue().
117 Use maxColors() and numBitPlanes() to determine the maximum number
118 of colors and the number of bit planes supported by the underlying
119 window system,
121 If you need to allocate many colors temporarily, for example in an
122 image viewer application, enterAllocContext(), leaveAllocContext() and
123 destroyAllocContext() will prove useful.
125 \section1 HSV Colors
127 Because many people don't know the HSV color model very well, we'll
128 cover it briefly here.
130 The RGB model is hardware-oriented. Its representation is close to
131 what most monitors show. In contrast, HSV represents color in a way
132 more suited to the human perception of color. For example, the
133 relationships "stronger than", "darker than" and "the opposite of"
134 are easily expressed in HSV but are much harder to express in RGB.
136 HSV, like RGB, has three components:
138 \list
140 \i H, for hue, is either 0-359 if the color is chromatic (not
141 gray), or meaningless if it is gray. It represents degrees on the
142 color wheel familiar to most people. Red is 0 (degrees), green is
143 120 and blue is 240.
145 \i S, for saturation, is 0-255, and the bigger it is, the
146 stronger the color is. Grayish colors have saturation near 0; very
147 strong colors have saturation near 255.
149 \i V, for value, is 0-255 and represents lightness or brightness
150 of the color. 0 is black; 255 is as far from black as possible.
152 \endlist
154 Here are some examples: Pure red is H=0, S=255, V=255. A dark red,
155 moving slightly towards the magenta, could be H=350 (equivalent to
156 -10), S=255, V=180. A grayish light red could have H about 0 (say
157 350-359 or 0-10), S about 50-100, and S=255.
159 Qt returns a hue value of -1 for achromatic colors. If you pass a
160 too-big hue value, Qt forces it into range. Hue 360 or 720 is
161 treated as 0; hue 540 is treated as 180.
163 \sa QPalette, QColorGroup, QApplication::setColorSpec(),
164 \link http://www.inforamp.net/~poynton/Poynton-color.html Color FAQ\endlink
167 /*****************************************************************************
168 Global colors
169 *****************************************************************************/
171 #if defined(Q_WS_WIN)
172 #define COLOR0_PIX 0x00ffffff
173 #define COLOR1_PIX 0
174 #else
175 #define COLOR0_PIX 0
176 #define COLOR1_PIX 1
177 #endif
179 static QColor stdcol[19];
181 QT_STATIC_CONST_IMPL QColor & Qt::color0 = stdcol[0];
182 QT_STATIC_CONST_IMPL QColor & Qt::color1 = stdcol[1];
183 QT_STATIC_CONST_IMPL QColor & Qt::black = stdcol[2];
184 QT_STATIC_CONST_IMPL QColor & Qt::white = stdcol[3];
185 QT_STATIC_CONST_IMPL QColor & Qt::darkGray = stdcol[4];
186 QT_STATIC_CONST_IMPL QColor & Qt::gray = stdcol[5];
187 QT_STATIC_CONST_IMPL QColor & Qt::lightGray = stdcol[6];
188 QT_STATIC_CONST_IMPL QColor & Qt::red = stdcol[7];
189 QT_STATIC_CONST_IMPL QColor & Qt::green = stdcol[8];
190 QT_STATIC_CONST_IMPL QColor & Qt::blue = stdcol[9];
191 QT_STATIC_CONST_IMPL QColor & Qt::cyan = stdcol[10];
192 QT_STATIC_CONST_IMPL QColor & Qt::magenta = stdcol[11];
193 QT_STATIC_CONST_IMPL QColor & Qt::yellow = stdcol[12];
194 QT_STATIC_CONST_IMPL QColor & Qt::darkRed = stdcol[13];
195 QT_STATIC_CONST_IMPL QColor & Qt::darkGreen = stdcol[14];
196 QT_STATIC_CONST_IMPL QColor & Qt::darkBlue = stdcol[15];
197 QT_STATIC_CONST_IMPL QColor & Qt::darkCyan = stdcol[16];
198 QT_STATIC_CONST_IMPL QColor & Qt::darkMagenta = stdcol[17];
199 QT_STATIC_CONST_IMPL QColor & Qt::darkYellow = stdcol[18];
202 /*****************************************************************************
203 QColor member functions
204 *****************************************************************************/
206 bool QColor::color_init = false; // color system not initialized
207 bool QColor::globals_init = false; // global color not initialized
208 QColor::ColorModel QColor::colormodel = d32;
211 QColor* QColor::globalColors()
213 return stdcol;
218 Initializes the global colors. This function is called if a global
219 color variable is initialized before the constructors for our
220 global color objects are executed. Without this mechanism,
221 assigning a color might assign an uninitialized value.
223 Example:
224 \code
225 QColor myColor = red; // will initialize red etc.
227 int main( int argc, char **argc )
230 \endcode
233 void QColor::initGlobalColors()
235 globals_init = true;
237 #ifdef Q_WS_X11
238 // HACK: we need a way to recognize color0 and color1 uniquely, so
239 // that we can use color0 and color1 with fixed pixel values on
240 // all screens
241 stdcol[ 0].d.argb = qRgba(255, 255, 255, 1);
242 stdcol[ 1].d.argb = qRgba( 0, 0, 0, 1);
243 #else
244 stdcol[ 0].d.argb = qRgb(255,255,255);
245 stdcol[ 1].d.argb = 0;
246 #endif // Q_WS_X11
247 stdcol[ 0].setPixel( COLOR0_PIX );
248 stdcol[ 1].setPixel( COLOR1_PIX );
250 // From the "The Palette Manager: How and Why" by Ron Gery, March 23,
251 // 1992, archived on MSDN:
252 // The Windows system palette is broken up into two sections,
253 // one with fixed colors and one with colors that can be changed
254 // by applications. The system palette predefines 20 entries;
255 // these colors are known as the static or reserved colors and
256 // consist of the 16 colors found in the Windows version 3.0 VGA
257 // driver and 4 additional colors chosen for their visual appeal.
258 // The DEFAULT_PALETTE stock object is, as the name implies, the
259 // default palette selected into a device context (DC) and consists
260 // of these static colors. Applications can set the remaining 236
261 // colors using the Palette Manager.
262 // The 20 reserved entries have indices in [0,9] and [246,255]. We
263 // reuse 17 of them.
264 stdcol[ 2].setRgb( 0, 0, 0 ); // index 0 black
265 stdcol[ 3].setRgb( 255, 255, 255 ); // index 255 white
266 stdcol[ 4].setRgb( 128, 128, 128 ); // index 248 medium gray
267 stdcol[ 5].setRgb( 160, 160, 164 ); // index 247 light gray
268 stdcol[ 6].setRgb( 192, 192, 192 ); // index 7 light gray
269 stdcol[ 7].setRgb( 255, 0, 0 ); // index 249 red
270 stdcol[ 8].setRgb( 0, 255, 0 ); // index 250 green
271 stdcol[ 9].setRgb( 0, 0, 255 ); // index 252 blue
272 stdcol[10].setRgb( 0, 255, 255 ); // index 254 cyan
273 stdcol[11].setRgb( 255, 0, 255 ); // index 253 magenta
274 stdcol[12].setRgb( 255, 255, 0 ); // index 251 yellow
275 stdcol[13].setRgb( 128, 0, 0 ); // index 1 dark red
276 stdcol[14].setRgb( 0, 128, 0 ); // index 2 dark green
277 stdcol[15].setRgb( 0, 0, 128 ); // index 4 dark blue
278 stdcol[16].setRgb( 0, 128, 128 ); // index 6 dark cyan
279 stdcol[17].setRgb( 128, 0, 128 ); // index 5 dark magenta
280 stdcol[18].setRgb( 128, 128, 0 ); // index 3 dark yellow
284 \enum QColor::Spec
286 The type of color specified, either RGB or HSV, e.g. in the
287 \c{QColor::QColor( x, y, z, colorSpec)} constructor.
289 \value Rgb
290 \value Hsv
295 \fn QColor::QColor()
297 Constructs an invalid color with the RGB value (0, 0, 0). An
298 invalid color is a color that is not properly set up for the
299 underlying window system.
301 The alpha value of an invalid color is unspecified.
303 \sa isValid()
308 \fn QColor::QColor( int r, int g, int b )
310 Constructs a color with the RGB value \a r, \a g, \a b, in the
311 same way as setRgb().
313 The color is left invalid if any or the arguments are illegal.
315 \sa setRgb()
320 Constructs a color with the RGB value \a rgb and a custom pixel
321 value \a pixel.
323 If \a pixel == 0xffffffff (the default), then the color uses the
324 RGB value in a standard way. If \a pixel is something else, then
325 the pixel value is set directly to \a pixel, skipping the normal
326 allocation procedure.
329 QColor::QColor( QRgb rgb, uint pixel )
331 if ( pixel == 0xffffffff ) {
332 setRgb( rgb );
333 } else {
334 d.argb = rgb;
335 setPixel( pixel );
339 void QColor::setPixel( uint pixel )
341 switch ( colormodel ) {
342 case d8:
343 d.d8.direct = true;
344 d.d8.invalid = false;
345 d.d8.dirty = false;
346 d.d8.pix = pixel;
347 break;
348 case d32:
349 d.d32.pix = pixel;
350 break;
356 Constructs a color with the RGB or HSV value \a x, \a y, \a z.
358 The arguments are an RGB value if \a colorSpec is QColor::Rgb. \a
359 x (red), \a y (green), and \a z (blue). All of them must be in the
360 range 0-255.
362 The arguments are an HSV value if \a colorSpec is QColor::Hsv. \a
363 x (hue) must be -1 for achromatic colors and 0-359 for chromatic
364 colors; \a y (saturation) and \a z (value) must both be in the
365 range 0-255.
367 \sa setRgb(), setHsv()
370 QColor::QColor( int x, int y, int z, Spec colorSpec )
372 d.d32.argb = Invalid;
373 d.d32.pix = Dirt;
374 if ( colorSpec == Hsv )
375 setHsv( x, y, z );
376 else
377 setRgb( x, y, z );
382 Constructs a named color in the same way as setNamedColor() using
383 name \a name.
385 The color is left invalid if \a name cannot be parsed.
387 \sa setNamedColor()
390 #if 0
391 QColor::QColor( const QString& name )
393 setNamedColor( name );
395 #endif
398 Constructs a named color in the same way as setNamedColor() using
399 name \a name.
401 The color is left invalid if \a name cannot be parsed.
403 \sa setNamedColor()
406 QColor::QColor( const char *name )
408 #if 0
409 setNamedColor( QString(name) );
410 #else
411 setNamedColor( name );
412 #endif
417 Constructs a color that is a copy of \a c.
420 QColor::QColor( const QColor &c )
422 if ( !globals_init )
423 initGlobalColors();
424 d.argb = c.d.argb;
425 d.d32.pix = c.d.d32.pix;
430 Assigns a copy of the color \a c and returns a reference to this
431 color.
434 QColor &QColor::operator=( const QColor &c )
436 if ( !globals_init )
437 initGlobalColors();
438 d.argb = c.d.argb;
439 d.d32.pix = c.d.d32.pix;
440 return *this;
445 \fn bool QColor::isValid() const
447 Returns false if the color is invalid, i.e. it was constructed using the
448 default constructor; otherwise returns true.
452 \internal
454 bool QColor::isDirty() const
456 if ( colormodel == d8 ) {
457 return d.d8.dirty;
458 } else {
459 return d.d32.probablyDirty();
464 Returns the name of the color in the format "#RRGGBB", i.e. a "#"
465 character followed by three two-digit hexadecimal numbers.
467 \sa setNamedColor()
470 #if 0
471 QString QColor::name() const
473 #ifndef QT_NO_SPRINTF
474 QString s;
475 s.sprintf( "#%02x%02x%02x", red(), green(), blue() );
476 return s;
477 #else
478 char s[20];
479 sprintf( s, "#%02x%02x%02x", red(), green(), blue() );
480 return QString(s);
481 #endif
483 #endif
485 #if 0
486 static int hex2int( QChar hexchar )
488 int v;
489 if ( hexchar.isDigit() )
490 v = hexchar.digitValue();
491 else if ( hexchar >= 'A' && hexchar <= 'F' )
492 v = hexchar.cell() - 'A' + 10;
493 else if ( hexchar >= 'a' && hexchar <= 'f' )
494 v = hexchar.cell() - 'a' + 10;
495 else
496 v = -1;
497 return v;
499 #else
500 static int hex2int( char hexchar )
502 int v;
503 if ( hexchar >= '0' && hexchar <= '9' )
504 v = hexchar - '0';
505 else if ( hexchar >= 'A' && hexchar <= 'F' )
506 v = hexchar - 'A' + 10;
507 else if ( hexchar >= 'a' && hexchar <= 'f' )
508 v = hexchar - 'a' + 10;
509 else
510 v = -1;
511 return v;
513 #endif
516 Sets the RGB value to \a name, which may be in one of these
517 formats:
518 \list
519 \i #RGB (each of R, G and B is a single hex digit)
520 \i #RRGGBB
521 \i #RRRGGGBBB
522 \i #RRRRGGGGBBBB
523 \i A name from the X color database (rgb.txt) (e.g.
524 "steelblue" or "gainsboro"). These color names also work
525 under Windows.
526 \endlist
528 The color is invalid if \a name cannot be parsed.
531 #if 0
532 void QColor::setNamedColor( const QString &name )
533 #else
534 void QColor::setNamedColor( const char* name )
535 #endif
537 #if 0
538 if ( name.isEmpty() ) {
539 #else
540 if ( name == NULL || strlen( name ) == 0 ) {
541 #endif
542 d.argb = 0;
543 if ( colormodel == d8 ) {
544 d.d8.invalid = true;
545 } else {
546 d.d32.argb = Invalid;
548 } else if ( name[0] == '#' ) {
549 #if 0
550 const QChar *p = name.unicode()+1;
551 int len = name.length()-1;
552 #else
553 const char *p = name+1;
554 int len = strlen(name)-1;
555 #endif
556 int r, g, b;
557 if ( len == 12 ) {
558 r = (hex2int(p[0]) << 4) + hex2int(p[1]);
559 g = (hex2int(p[4]) << 4) + hex2int(p[5]);
560 b = (hex2int(p[8]) << 4) + hex2int(p[9]);
561 } else if ( len == 9 ) {
562 r = (hex2int(p[0]) << 4) + hex2int(p[1]);
563 g = (hex2int(p[3]) << 4) + hex2int(p[4]);
564 b = (hex2int(p[6]) << 4) + hex2int(p[7]);
565 } else if ( len == 6 ) {
566 r = (hex2int(p[0]) << 4) + hex2int(p[1]);
567 g = (hex2int(p[2]) << 4) + hex2int(p[3]);
568 b = (hex2int(p[4]) << 4) + hex2int(p[5]);
569 } else if ( len == 3 ) {
570 r = (hex2int(p[0]) << 4) + hex2int(p[0]);
571 g = (hex2int(p[1]) << 4) + hex2int(p[1]);
572 b = (hex2int(p[2]) << 4) + hex2int(p[2]);
573 } else {
574 r = g = b = -1;
576 if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) {
577 d.d32.argb = Invalid;
578 d.d32.pix = Dirt;
579 #if defined(QT_CHECK_RANGE)
580 qWarning( "QColor::setNamedColor: could not parse color '%s'",
581 name.local8Bit().data() );
582 #endif
583 } else {
584 setRgb( r, g, b );
586 } else {
587 setSystemNamedColor( name );
591 #undef max
592 #undef min
595 \fn void QColor::getHsv( int &h, int &s, int &v ) const
596 \obsolete
599 /*! \fn void QColor::getHsv( int *h, int *s, int *v ) const
601 Returns the current RGB value as HSV. The contents of the \a h, \a
602 s and \a v pointers are set to the HSV values. If any of the three
603 pointers are null, the function does nothing.
605 The hue (which \a h points to) is set to -1 if the color is
606 achromatic.
608 \warning Colors are stored internally as RGB values, so getHSv()
609 may return slightly different values to those set by setHsv().
611 \sa setHsv(), rgb()
614 /*! \obsolete Use getHsv() instead.
616 void QColor::hsv( int *h, int *s, int *v ) const
618 if ( !h || !s || !v )
619 return;
620 int r = qRed(d.argb);
621 int g = qGreen(d.argb);
622 int b = qBlue(d.argb);
623 uint max = r; // maximum RGB component
624 int whatmax = 0; // r=>0, g=>1, b=>2
625 if ( (uint)g > max ) {
626 max = g;
627 whatmax = 1;
629 if ( (uint)b > max ) {
630 max = b;
631 whatmax = 2;
633 uint min = r; // find minimum value
634 if ( (uint)g < min ) min = g;
635 if ( (uint)b < min ) min = b;
636 int delta = max-min;
637 *v = max; // calc value
638 *s = max ? (510*delta+max)/(2*max) : 0;
639 if ( *s == 0 ) {
640 *h = -1; // undefined hue
641 } else {
642 switch ( whatmax ) {
643 case 0: // red is max component
644 if ( g >= b )
645 *h = (120*(g-b)+delta)/(2*delta);
646 else
647 *h = (120*(g-b+delta)+delta)/(2*delta) + 300;
648 break;
649 case 1: // green is max component
650 if ( b > r )
651 *h = 120 + (120*(b-r)+delta)/(2*delta);
652 else
653 *h = 60 + (120*(b-r+delta)+delta)/(2*delta);
654 break;
655 case 2: // blue is max component
656 if ( r > g )
657 *h = 240 + (120*(r-g)+delta)/(2*delta);
658 else
659 *h = 180 + (120*(r-g+delta)+delta)/(2*delta);
660 break;
667 Sets a HSV color value. \a h is the hue, \a s is the saturation
668 and \a v is the value of the HSV color.
670 If \a s or \a v are not in the range 0-255, or \a h is < -1, the
671 color is not changed.
673 \warning Colors are stored internally as RGB values, so getHSv()
674 may return slightly different values to those set by setHsv().
676 \sa hsv(), setRgb()
679 void QColor::setHsv( int h, int s, int v )
681 if ( h < -1 || (uint)s > 255 || (uint)v > 255 ) {
682 #if defined(QT_CHECK_RANGE)
683 qWarning( "QColor::setHsv: HSV parameters out of range" );
684 #endif
685 return;
687 int r=v, g=v, b=v;
688 if ( s == 0 || h == -1 ) { // achromatic case
689 // Ignore
690 } else { // chromatic case
691 if ( (uint)h >= 360 )
692 h %= 360;
693 uint f = h%60;
694 h /= 60;
695 uint p = (uint)(2*v*(255-s)+255)/510;
696 uint q, t;
697 if ( h&1 ) {
698 q = (uint)(2*v*(15300-s*f)+15300)/30600;
699 switch( h ) {
700 case 1: r=(int)q; g=(int)v, b=(int)p; break;
701 case 3: r=(int)p; g=(int)q, b=(int)v; break;
702 case 5: r=(int)v; g=(int)p, b=(int)q; break;
704 } else {
705 t = (uint)(2*v*(15300-(s*(60-f)))+15300)/30600;
706 switch( h ) {
707 case 0: r=(int)v; g=(int)t, b=(int)p; break;
708 case 2: r=(int)p; g=(int)v, b=(int)t; break;
709 case 4: r=(int)t; g=(int)p, b=(int)v; break;
713 setRgb( r, g, b );
718 \fn QRgb QColor::rgb() const
720 Returns the RGB value.
722 The return type \e QRgb is equivalent to \c unsigned \c int.
724 For an invalid color, the alpha value of the returned color is
725 unspecified.
727 \sa setRgb(), hsv(), qRed(), qBlue(), qGreen(), isValid()
730 /*! \fn void QColor::getRgb( int *r, int *g, int *b ) const
732 Sets the contents pointed to by \a r, \a g and \a b to the red,
733 green and blue components of the RGB value respectively. The value
734 range for a component is 0..255.
736 \sa rgb(), setRgb(), getHsv()
739 /*! \obsolete Use getRgb() instead */
740 void QColor::rgb( int *r, int *g, int *b ) const
742 *r = qRed(d.argb);
743 *g = qGreen(d.argb);
744 *b = qBlue(d.argb);
749 Sets the RGB value to \a r, \a g, \a b. The arguments, \a r, \a g
750 and \a b must all be in the range 0..255. If any of them are
751 outside the legal range, the color is not changed.
753 \sa rgb(), setHsv()
756 void QColor::setRgb( int r, int g, int b )
758 if ( (uint)r > 255 || (uint)g > 255 || (uint)b > 255 ) {
759 #if defined(QT_CHECK_RANGE)
760 qWarning( "QColor::setRgb: RGB parameter(s) out of range" );
761 #endif
762 return;
764 d.argb = qRgb( r, g, b );
765 if ( colormodel == d8 ) {
766 d.d8.invalid = false;
767 d.d8.direct = false;
768 d.d8.dirty = true;
769 } else {
770 d.d32.pix = Dirt;
776 \overload
777 Sets the RGB value to \a rgb.
779 The type \e QRgb is equivalent to \c unsigned \c int.
781 \sa rgb(), setHsv()
784 void QColor::setRgb( QRgb rgb )
786 d.argb = rgb;
787 if ( colormodel == d8 ) {
788 d.d8.invalid = false;
789 d.d8.direct = false;
790 d.d8.dirty = true;
791 } else {
792 d.d32.pix = Dirt;
797 \fn int QColor::red() const
799 Returns the R (red) component of the RGB value.
804 \fn int QColor::green() const
806 Returns the G (green) component of the RGB value.
810 \fn int QColor::blue() const
812 Returns the B (blue) component of the RGB value.
817 Returns a lighter (or darker) color, but does not change this
818 object.
820 Returns a lighter color if \a factor is greater than 100. Setting
821 \a factor to 150 returns a color that is 50% brighter.
823 Returns a darker color if \a factor is less than 100. We recommend
824 using dark() for this purpose. If \a factor is 0 or negative, the
825 return value is unspecified.
827 (This function converts the current RGB color to HSV, multiplies V
828 by \a factor, and converts the result back to RGB.)
830 \sa dark()
833 QColor QColor::light( int factor ) const
835 if ( factor <= 0 ) // invalid lightness factor
836 return *this;
837 else if ( factor < 100 ) // makes color darker
838 return dark( 10000/factor );
840 int h, s, v;
841 hsv( &h, &s, &v );
842 v = (factor*v)/100;
843 if ( v > 255 ) { // overflow
844 s -= v-255; // adjust saturation
845 if ( s < 0 )
846 s = 0;
847 v = 255;
849 QColor c;
850 c.setHsv( h, s, v );
851 return c;
856 Returns a darker (or lighter) color, but does not change this
857 object.
859 Returns a darker color if \a factor is greater than 100. Setting
860 \a factor to 300 returns a color that has one-third the
861 brightness.
863 Returns a lighter color if \a factor is less than 100. We
864 recommend using lighter() for this purpose. If \a factor is 0 or
865 negative, the return value is unspecified.
867 (This function converts the current RGB color to HSV, divides V by
868 \a factor and converts back to RGB.)
870 \sa light()
873 QColor QColor::dark( int factor ) const
875 if ( factor <= 0 ) // invalid darkness factor
876 return *this;
877 else if ( factor < 100 ) // makes color lighter
878 return light( 10000/factor );
879 int h, s, v;
880 hsv( &h, &s, &v );
881 v = (v*100)/factor;
882 QColor c;
883 c.setHsv( h, s, v );
884 return c;
889 \fn bool QColor::operator==( const QColor &c ) const
891 Returns true if this color has the same RGB value as \a c;
892 otherwise returns false.
896 \fn bool QColor::operator!=( const QColor &c ) const
897 Returns true if this color has a different RGB value from \a c;
898 otherwise returns false.
902 Returns the pixel value.
904 This value is used by the underlying window system to refer to a
905 color. It can be thought of as an index into the display
906 hardware's color table, but the value is an arbitrary 32-bit
907 value.
909 \sa alloc()
911 uint QColor::pixel() const
913 if ( isDirty() )
914 return ((QColor*)this)->alloc();
915 else if ( colormodel == d8 )
916 #ifdef Q_WS_WIN
917 // since d.d8.pix is uchar we have to use the PALETTEINDEX
918 // macro to get the respective palette entry index.
919 return (0x01000000 | (int)(short)(d.d8.pix));
920 #else
921 return d.d8.pix;
922 #endif
923 else
924 return d.d32.pix;
928 \fn QStringList QColor::colorNames()
929 Returns a QStringList containing the color names Qt knows about.
932 /*****************************************************************************
933 QColor stream functions
934 *****************************************************************************/
935 #ifndef QT_NO_DATASTREAM
937 \relates QColor
938 Writes a color object, \a c to the stream, \a s.
940 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
943 QDataStream &operator<<( QDataStream &s, const QColor &c )
945 quint32 p = (quint32)c.rgb();
946 if ( s.version() == 1 ) // Swap red and blue
947 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
948 return s << p;
952 \relates QColor
953 Reads a color object, \a c, from the stream, \a s.
955 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
958 QDataStream &operator>>( QDataStream &s, QColor &c )
960 quint32 p;
961 s >> p;
962 if ( s.version() == 1 ) // Swap red and blue
963 p = ((p << 16) & 0xff0000) | ((p >> 16) & 0xff) | (p & 0xff00ff00);
964 c.setRgb( p );
965 return s;
967 #endif
969 /*****************************************************************************
970 QColor global functions (documentation only)
971 *****************************************************************************/
974 \fn int qRed( QRgb rgb )
975 \relates QColor
977 Returns the red component of the RGB triplet \a rgb.
978 \sa qRgb(), QColor::red()
982 \fn int qGreen( QRgb rgb )
983 \relates QColor
985 Returns the green component of the RGB triplet \a rgb.
986 \sa qRgb(), QColor::green()
990 \fn int qBlue( QRgb rgb )
991 \relates QColor
993 Returns the blue component of the RGB triplet \a rgb.
994 \sa qRgb(), QColor::blue()
998 \fn int qAlpha( QRgb rgba )
999 \relates QColor
1001 Returns the alpha component of the RGBA quadruplet \a rgba.
1005 \fn QRgb qRgb( int r, int g, int b )
1006 \relates QColor
1008 Returns the RGB triplet \a (r,g,b).
1010 The return type QRgb is equivalent to \c unsigned \c int.
1012 \sa qRgba(), qRed(), qGreen(), qBlue()
1016 \fn QRgb qRgba( int r, int g, int b, int a )
1017 \relates QColor
1019 Returns the RGBA quadruplet \a (r,g,b,a).
1021 The return type QRgba is equivalent to \c unsigned \c int.
1023 \sa qRgb(), qRed(), qGreen(), qBlue()
1027 \fn int qGray( int r, int g, int b )
1028 \relates QColor
1030 Returns a gray value 0..255 from the (\a r, \a g, \a b) triplet.
1032 The gray value is calculated using the formula (r*11 + g*16 +
1033 b*5)/32.
1037 \overload int qGray( qRgb rgb )
1038 \relates QColor
1040 Returns a gray value 0..255 from the given \a rgb colour.