~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksplash/ksplashx/qcolor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
10
**
 
11
** The original Qt license header follows:
 
12
** 
 
13
**
 
14
** Implementation of QColor class
 
15
**
 
16
** Created : 940112
 
17
**
 
18
** Copyright (C) 1992-2002 Trolltech AS.  All rights reserved.
 
19
**
 
20
** This file is part of the kernel module of the Qt GUI Toolkit.
 
21
**
 
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.
 
25
**
 
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.
 
30
**
 
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.
 
34
**
 
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.
 
37
**
 
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.
 
42
**
 
43
** Contact info@trolltech.com if any conditions of this licensing are
 
44
** not clear to you.
 
45
**
 
46
**********************************************************************/
 
47
 
 
48
#include "qcolor.h"
 
49
#include "qnamespace.h"
 
50
#include "qdatastream.h"
 
51
 
 
52
#include <stdio.h>
 
53
#include <string.h>
 
54
 
 
55
/*!
 
56
    \class QColor qcolor.h
 
57
    \brief The QColor class provides colors based on RGB or HSV values.
 
58
 
 
59
    \ingroup images
 
60
    \ingroup graphics
 
61
    \ingroup appearance
 
62
 
 
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).
 
67
 
 
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.
 
72
 
 
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.
 
78
 
 
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).
 
84
 
 
85
    \img qt-colors.png Qt Colors
 
86
 
 
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).
 
92
 
 
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:
 
96
 
 
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
 
108
 
 
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().
 
116
 
 
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,
 
120
 
 
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.
 
124
 
 
125
    \section1 HSV Colors
 
126
 
 
127
    Because many people don't know the HSV color model very well, we'll
 
128
    cover it briefly here.
 
129
 
 
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.
 
135
 
 
136
    HSV, like RGB, has three components:
 
137
 
 
138
    \list
 
139
 
 
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.
 
144
 
 
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.
 
148
 
 
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.
 
151
 
 
152
    \endlist
 
153
 
 
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.
 
158
 
 
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.
 
162
 
 
163
    \sa QPalette, QColorGroup, QApplication::setColorSpec(),
 
164
    \link http://www.inforamp.net/~poynton/Poynton-color.html Color FAQ\endlink
 
165
*/
 
166
 
 
167
/*****************************************************************************
 
168
  Global colors
 
169
 *****************************************************************************/
 
170
 
 
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
 
178
 
 
179
static QColor stdcol[19];
 
180
 
 
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];
 
200
 
 
201
 
 
202
/*****************************************************************************
 
203
  QColor member functions
 
204
 *****************************************************************************/
 
205
 
 
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;
 
209
 
 
210
 
 
211
QColor* QColor::globalColors()
 
212
{
 
213
    return stdcol;
 
214
}
 
215
 
 
216
 
 
217
/*!
 
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.
 
222
 
 
223
    Example:
 
224
    \code
 
225
        QColor myColor = red;              // will initialize red etc.
 
226
 
 
227
        int main( int argc, char **argc )
 
228
        {
 
229
        }
 
230
    \endcode
 
231
*/
 
232
 
 
233
void QColor::initGlobalColors()
 
234
{
 
235
    globals_init = true;
 
236
 
 
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 );
 
249
 
 
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
 
281
}
 
282
 
 
283
/*!
 
284
    \enum QColor::Spec
 
285
 
 
286
    The type of color specified, either RGB or HSV, e.g. in the
 
287
    \c{QColor::QColor( x, y, z, colorSpec)} constructor.
 
288
 
 
289
    \value Rgb
 
290
    \value Hsv
 
291
*/
 
292
 
 
293
 
 
294
/*!
 
295
    \fn QColor::QColor()
 
296
 
 
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.
 
300
 
 
301
    The alpha value of an invalid color is unspecified.
 
302
 
 
303
    \sa isValid()
 
304
*/
 
305
 
 
306
 
 
307
/*!
 
308
    \fn QColor::QColor( int r, int g, int b )
 
309
 
 
310
    Constructs a color with the RGB value \a r, \a g, \a b, in the
 
311
    same way as setRgb().
 
312
 
 
313
    The color is left invalid if any or the arguments are illegal.
 
314
 
 
315
    \sa setRgb()
 
316
*/
 
317
 
 
318
 
 
319
/*!
 
320
    Constructs a color with the RGB value \a rgb and a custom pixel
 
321
    value \a pixel.
 
322
 
 
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.
 
327
*/
 
328
 
 
329
QColor::QColor( QRgb rgb, uint pixel )
 
330
{
 
331
    if ( pixel == 0xffffffff ) {
 
332
        setRgb( rgb );
 
333
    } else {
 
334
        d.argb = rgb;
 
335
        setPixel( pixel );
 
336
    }
 
337
}
 
338
 
 
339
void QColor::setPixel( uint pixel )
 
340
{
 
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;
 
351
    }
 
352
}
 
353
 
 
354
 
 
355
/*!
 
356
    Constructs a color with the RGB or HSV value \a x, \a y, \a z.
 
357
 
 
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.
 
361
 
 
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.
 
366
 
 
367
    \sa setRgb(), setHsv()
 
368
*/
 
369
 
 
370
QColor::QColor( int x, int y, int z, Spec colorSpec )
 
371
{
 
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 );
 
378
}
 
379
 
 
380
 
 
381
/*!
 
382
    Constructs a named color in the same way as setNamedColor() using
 
383
    name \a name.
 
384
 
 
385
    The color is left invalid if \a name cannot be parsed.
 
386
 
 
387
    \sa setNamedColor()
 
388
*/
 
389
 
 
390
#if 0
 
391
QColor::QColor( const QString& name )
 
392
{
 
393
    setNamedColor( name );
 
394
}
 
395
#endif
 
396
 
 
397
/*!
 
398
    Constructs a named color in the same way as setNamedColor() using
 
399
    name \a name.
 
400
 
 
401
    The color is left invalid if \a name cannot be parsed.
 
402
 
 
403
    \sa setNamedColor()
 
404
*/
 
405
 
 
406
QColor::QColor( const char *name )
 
407
{
 
408
#if 0
 
409
    setNamedColor( QString(name) );
 
410
#else
 
411
    setNamedColor( name );
 
412
#endif
 
413
}
 
414
 
 
415
 
 
416
/*!
 
417
    Constructs a color that is a copy of \a c.
 
418
*/
 
419
 
 
420
QColor::QColor( const QColor &c )
 
421
{
 
422
    if ( !globals_init )
 
423
        initGlobalColors();
 
424
    d.argb = c.d.argb;
 
425
    d.d32.pix = c.d.d32.pix;
 
426
}
 
427
 
 
428
 
 
429
/*!
 
430
    Assigns a copy of the color \a c and returns a reference to this
 
431
    color.
 
432
*/
 
433
 
 
434
QColor &QColor::operator=( const QColor &c )
 
435
{
 
436
    if ( !globals_init )
 
437
        initGlobalColors();
 
438
    d.argb = c.d.argb;
 
439
    d.d32.pix = c.d.d32.pix;
 
440
    return *this;
 
441
}
 
442
 
 
443
 
 
444
/*!
 
445
    \fn bool QColor::isValid() const
 
446
 
 
447
    Returns false if the color is invalid, i.e. it was constructed using the
 
448
    default constructor; otherwise returns true.
 
449
*/
 
450
 
 
451
/*!
 
452
    \internal
 
453
*/
 
454
bool QColor::isDirty() const
 
455
{
 
456
    if ( colormodel == d8 ) {
 
457
        return d.d8.dirty;
 
458
    } else {
 
459
        return d.d32.probablyDirty();
 
460
    }
 
461
}
 
462
 
 
463
/*!
 
464
    Returns the name of the color in the format "#RRGGBB", i.e. a "#"
 
465
    character followed by three two-digit hexadecimal numbers.
 
466
 
 
467
    \sa setNamedColor()
 
468
*/
 
469
 
 
470
#if 0
 
471
QString QColor::name() const
 
472
{
 
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
 
482
}
 
483
#endif
 
484
 
 
485
#if 0
 
486
static int hex2int( QChar hexchar )
 
487
{
 
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;
 
498
}
 
499
#else
 
500
static int hex2int( char hexchar )
 
501
{
 
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;
 
512
}
 
513
#endif
 
514
 
 
515
/*!
 
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
 
527
 
 
528
    The color is invalid if \a name cannot be parsed.
 
529
*/
 
530
 
 
531
#if 0
 
532
void QColor::setNamedColor( const QString &name )
 
533
#else
 
534
void QColor::setNamedColor( const char* name )
 
535
#endif
 
536
{
 
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;
 
547
        }
 
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;
 
575
        }
 
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 );
 
585
        }
 
586
    } else {
 
587
        setSystemNamedColor( name );
 
588
    }
 
589
}
 
590
 
 
591
#undef max
 
592
#undef min
 
593
 
 
594
/*!
 
595
  \fn void QColor::getHsv( int &h, int &s, int &v ) const
 
596
  \obsolete
 
597
*/
 
598
 
 
599
/*!  \fn void QColor::getHsv( int *h, int *s, int *v ) const
 
600
 
 
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.
 
604
 
 
605
    The hue (which \a h points to) is set to -1 if the color is
 
606
    achromatic.
 
607
 
 
608
    \warning Colors are stored internally as RGB values, so getHSv()
 
609
    may return slightly different values to those set by setHsv().
 
610
 
 
611
    \sa setHsv(), rgb()
 
612
*/
 
613
 
 
614
/*! \obsolete Use getHsv() instead.
 
615
 */
 
616
void QColor::hsv( int *h, int *s, int *v ) const
 
617
{
 
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;
 
628
    }
 
629
    if ( (uint)b > max ) {
 
630
        max = b;
 
631
        whatmax = 2;
 
632
    }
 
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;
 
661
        }
 
662
    }
 
663
}
 
664
 
 
665
 
 
666
/*!
 
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.
 
669
 
 
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.
 
672
 
 
673
    \warning Colors are stored internally as RGB values, so getHSv()
 
674
    may return slightly different values to those set by setHsv().
 
675
 
 
676
    \sa hsv(), setRgb()
 
677
*/
 
678
 
 
679
void QColor::setHsv( int h, int s, int v )
 
680
{
 
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;
 
686
    }
 
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;
 
703
            }
 
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;
 
710
            }
 
711
        }
 
712
    }
 
713
    setRgb( r, g, b );
 
714
}
 
715
 
 
716
 
 
717
/*!
 
718
    \fn QRgb QColor::rgb() const
 
719
 
 
720
    Returns the RGB value.
 
721
 
 
722
    The return type \e QRgb is equivalent to \c unsigned \c int.
 
723
 
 
724
    For an invalid color, the alpha value of the returned color is
 
725
    unspecified.
 
726
 
 
727
    \sa setRgb(), hsv(), qRed(), qBlue(), qGreen(), isValid()
 
728
*/
 
729
 
 
730
/*! \fn void QColor::getRgb( int *r, int *g, int *b ) const
 
731
 
 
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.
 
735
 
 
736
    \sa rgb(), setRgb(), getHsv()
 
737
*/
 
738
 
 
739
/*! \obsolete Use getRgb() instead */
 
740
void QColor::rgb( int *r, int *g, int *b ) const
 
741
{
 
742
    *r = qRed(d.argb);
 
743
    *g = qGreen(d.argb);
 
744
    *b = qBlue(d.argb);
 
745
}
 
746
 
 
747
 
 
748
/*!
 
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.
 
752
 
 
753
    \sa rgb(), setHsv()
 
754
*/
 
755
 
 
756
void QColor::setRgb( int r, int g, int b )
 
757
{
 
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;
 
763
    }
 
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;
 
771
    }
 
772
}
 
773
 
 
774
 
 
775
/*!
 
776
    \overload
 
777
    Sets the RGB value to \a rgb.
 
778
 
 
779
    The type \e QRgb is equivalent to \c unsigned \c int.
 
780
 
 
781
    \sa rgb(), setHsv()
 
782
*/
 
783
 
 
784
void QColor::setRgb( QRgb rgb )
 
785
{
 
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;
 
793
    }
 
794
}
 
795
 
 
796
/*!
 
797
    \fn int QColor::red() const
 
798
 
 
799
    Returns the R (red) component of the RGB value.
 
800
*/
 
801
 
 
802
 
 
803
/*!
 
804
    \fn int QColor::green() const
 
805
 
 
806
    Returns the G (green) component of the RGB value.
 
807
*/
 
808
 
 
809
/*!
 
810
    \fn int QColor::blue() const
 
811
 
 
812
    Returns the B (blue) component of the RGB value.
 
813
*/
 
814
 
 
815
 
 
816
/*!
 
817
    Returns a lighter (or darker) color, but does not change this
 
818
    object.
 
819
 
 
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.
 
822
 
 
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.
 
826
 
 
827
    (This function converts the current RGB color to HSV, multiplies V
 
828
    by \a factor, and converts the result back to RGB.)
 
829
 
 
830
    \sa dark()
 
831
*/
 
832
 
 
833
QColor QColor::light( int factor ) const
 
834
{
 
835
    if ( factor <= 0 )                          // invalid lightness factor
 
836
        return *this;
 
837
    else if ( factor < 100 )                    // makes color darker
 
838
        return dark( 10000/factor );
 
839
 
 
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;
 
848
    }
 
849
    QColor c;
 
850
    c.setHsv( h, s, v );
 
851
    return c;
 
852
}
 
853
 
 
854
 
 
855
/*!
 
856
    Returns a darker (or lighter) color, but does not change this
 
857
    object.
 
858
 
 
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.
 
862
 
 
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.
 
866
 
 
867
    (This function converts the current RGB color to HSV, divides V by
 
868
    \a factor and converts back to RGB.)
 
869
 
 
870
    \sa light()
 
871
*/
 
872
 
 
873
QColor QColor::dark( int factor ) const
 
874
{
 
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;
 
885
}
 
886
 
 
887
 
 
888
/*!
 
889
    \fn bool QColor::operator==( const QColor &c ) const
 
890
 
 
891
    Returns true if this color has the same RGB value as \a c;
 
892
    otherwise returns false.
 
893
*/
 
894
 
 
895
/*!
 
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.
 
899
*/
 
900
 
 
901
/*!
 
902
    Returns the pixel value.
 
903
 
 
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.
 
908
 
 
909
    \sa alloc()
 
910
*/
 
911
uint QColor::pixel() const
 
912
{
 
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;
 
925
}
 
926
 
 
927
/*!
 
928
    \fn QStringList QColor::colorNames()
 
929
    Returns a QStringList containing the color names Qt knows about.
 
930
*/
 
931
 
 
932
/*****************************************************************************
 
933
  QColor stream functions
 
934
 *****************************************************************************/
 
935
#ifndef QT_NO_DATASTREAM
 
936
/*!
 
937
    \relates QColor
 
938
    Writes a color object, \a c to the stream, \a s.
 
939
 
 
940
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
941
*/
 
942
 
 
943
QDataStream &operator<<( QDataStream &s, const QColor &c )
 
944
{
 
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;
 
949
}
 
950
 
 
951
/*!
 
952
    \relates QColor
 
953
    Reads a color object, \a c, from the stream, \a s.
 
954
 
 
955
    \sa \link datastreamformat.html Format of the QDataStream operators \endlink
 
956
*/
 
957
 
 
958
QDataStream &operator>>( QDataStream &s, QColor &c )
 
959
{
 
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;
 
966
}
 
967
#endif
 
968
 
 
969
/*****************************************************************************
 
970
  QColor global functions (documentation only)
 
971
 *****************************************************************************/
 
972
 
 
973
/*!
 
974
    \fn int qRed( QRgb rgb )
 
975
    \relates QColor
 
976
 
 
977
    Returns the red component of the RGB triplet \a rgb.
 
978
    \sa qRgb(), QColor::red()
 
979
*/
 
980
 
 
981
/*!
 
982
    \fn int qGreen( QRgb rgb )
 
983
    \relates QColor
 
984
 
 
985
    Returns the green component of the RGB triplet \a rgb.
 
986
    \sa qRgb(), QColor::green()
 
987
*/
 
988
 
 
989
/*!
 
990
    \fn int qBlue( QRgb rgb )
 
991
    \relates QColor
 
992
 
 
993
    Returns the blue component of the RGB triplet \a rgb.
 
994
    \sa qRgb(), QColor::blue()
 
995
*/
 
996
 
 
997
/*!
 
998
    \fn int qAlpha( QRgb rgba )
 
999
    \relates QColor
 
1000
 
 
1001
    Returns the alpha component of the RGBA quadruplet \a rgba.
 
1002
    */
 
1003
 
 
1004
/*!
 
1005
    \fn QRgb qRgb( int r, int g, int b )
 
1006
    \relates QColor
 
1007
 
 
1008
    Returns the RGB triplet \a (r,g,b).
 
1009
 
 
1010
    The return type QRgb is equivalent to \c unsigned \c int.
 
1011
 
 
1012
    \sa qRgba(), qRed(), qGreen(), qBlue()
 
1013
*/
 
1014
 
 
1015
/*!
 
1016
    \fn QRgb qRgba( int r, int g, int b, int a )
 
1017
    \relates QColor
 
1018
 
 
1019
    Returns the RGBA quadruplet \a (r,g,b,a).
 
1020
 
 
1021
    The return type QRgba is equivalent to \c unsigned \c int.
 
1022
 
 
1023
    \sa qRgb(), qRed(), qGreen(), qBlue()
 
1024
*/
 
1025
 
 
1026
/*!
 
1027
    \fn int qGray( int r, int g, int b )
 
1028
    \relates QColor
 
1029
 
 
1030
    Returns a gray value 0..255 from the (\a r, \a g, \a b) triplet.
 
1031
 
 
1032
    The gray value is calculated using the formula (r*11 + g*16 +
 
1033
    b*5)/32.
 
1034
*/
 
1035
 
 
1036
/*!
 
1037
    \overload int qGray( qRgb rgb )
 
1038
    \relates QColor
 
1039
 
 
1040
    Returns a gray value 0..255 from the given \a rgb colour.
 
1041
*/