QColor Class Reference
The QColor class provides colors based on RGB.
More...
#include <qcolor.h>
List of all member functions.
Public Members
enum Spec { Rgb, Hsv }
-
QColor ( int r, int g, int b )Â
QColor ( int x, int y, int z, Spec )Â
QColor ( QRgb rgb, uint pixel=0xffffffff )Â
QColor ( const QString & name )Â
QColor ( const char * name )Â
QColor ( const QColor & )Â
-
-
-
-
-
voidÂ
rgb ( int * r, int * g, int * b ) const
-
voidÂ
setRgb ( int r, int g, int b )Â
voidÂ
setRgb ( QRgb rgb )Â
-
-
-
voidÂ
hsv ( int * h, int * s, int * v ) const
void getHsv ( int & h, int & s, int & v ) const (obsolete)
voidÂ
setHsv ( int h, int s, int v )Â
QColorÂ
light ( int f = 150 ) const
QColorÂ
dark ( int f = 200 ) const
boolÂ
operator== ( const QColor & c ) const
boolÂ
operator!= ( const QColor & c ) const
-
-
Static Public Members
Related Functions
(Note that these are not member functions.)
- QDataStream & operator>> (QDataStream & s, QColor & c)
- int qGreen (QRgb rgb)
- int qRed (QRgb rgb)
- int qAlpha (QRgb rgba)
- int qGray (int r, int g, int b)
- QRgb qRgba (int r, int g, int b, int a)
- int qGray (qRgb rgb)
- QDataStream & operator<< (QDataStream & s, const QColor & c)
- int qBlue (QRgb rgb)
- QRgb qRgb (int r, int g, int b)
Detailed Description
The QColor class provides colors based on RGB.
A color is normally specified in terms of RGB (red,green and blue)
components, but it is also possible to specify HSV (hue,saturation
and value) or set a color name (the names are copied from from the
X11 color database).
In addition to the RGB value, a QColor also has a pixel value. This
value is used by the underlying window system to refer to a color. It
can be thought of as an index into the display hardware's color table.
There are 19 predefined QColor objects: black, white, darkGray, gray, lightGray, red, green, blue, cyan, magenta, yellow, darkRed, darkGreen, darkBlue, darkCyan, darkMagenta, darkYellow, color0
and color1.
The colors color0
(zero pixel value) and color1
(non-zero
pixel value) are special colors for drawing in bitmaps.
The QColor class has an efficient, dynamic color allocation
strategy. A color is normally allocated the first time it is used
(lazy allocation), that is, whenever the pixel() function is called:
- Is the pixel value valid? If it is, just return it, otherwise,
allocate a pixel value.
- Check an internal hash table to see if we allocated an equal RGB
value earlier. If we did, set the pixel value and return.
- Try to allocate the RGB value. If we succeed, we get a pixel value
which we save in the internal table with the RGB value.
Return the pixel value.
- The color could not be allocated. Find the closest matching
color and save it in the internal table.
Since many people don't know the HSV color model very well, we'll
cover it briefly here.
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 traditional human perception of color. For example,
the relationships "stronger than", "darker than", "the opposite of"
are easily expressed in HSV, but are much harder to express in RGB.
HSV, like RGB, has three components. They are:
- H, for
hue, is either 0-360 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.
- S, for saturation, is 0-255 and the bigger it is, the
stronger the color is. Grayish colors have saturation near 0, very
strong colors have saturation near 255.
- V, for value, is 0-255
and represents lightness or brightness of the color. 0 is black,
255 is far from black as possible.
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.
See also QPalette, QColorGroup, QApplication::setColorSpec() and Color FAQ.
Examples:
progress/progress.cpp
picture/picture.cpp
i18n/main.cpp
drawlines/connect.cpp
desktop/desktop.cpp
grapher/grapher.cpp
drawdemo/drawdemo.cpp
Member Function Documentation
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.
See also isValid().
QColor::QColor ( QRgb rgb, uint pixel=0xffffffff )
Constructs a color with a RGB value and a custom pixel value.
If the pixel = 0xffffffff, then the color uses the RGB value in a
standard way. If pixel is something else, then the pixel value will
be set directly to pixel (skips the normal allocation procedure).
QColor::QColor ( const QString & name )
Constructs a named color in the same way as setNamedColor().
See also setNamedColor().
QColor::QColor ( const QColor & c )
Constructs a color that is a copy of c.
QColor::QColor ( const char * name )
Constructs a named color in the same way as setNamedColor().
See also setNamedColor().
QColor::QColor ( int r, int g, int b )
Constructs a color with the RGB value (r,g,b). r, g and b must be in the range 0..255.
See also setRgb().
QColor::QColor ( int x, int y, int z, Spec colorSpec )
Constructs a color with the RGB or HSV value (x,y,z).
The (x,y,z) triplet defines an RGB value if colorSpec == QColor::Rgb.
x (red), y (green) and z (blue) must be in the
range 0..255.
The (x,y,z) triplet defines a HSV value if colorSpec == QColor::Hsv.
x (hue) must be in the range -1..360 (-1 means
achromatic), and y (saturation) and z (value) must be in the range
0..255.
See also setRgb() and setHsv().
uint QColor::alloc ()
Allocates the RGB color and returns the pixel value.
Allocating a color means to obtain a pixel value from the RGB
specification. The pixel value is an index into the global color
table, but should be considered an arbitrary platform-dependent value.
The pixel() function calls alloc() if necessary, so in general you
don't need to call this function.
See also setLazyAlloc() and enterAllocContext().
int QColor::blue () const
Returns the B (blue) component of the RGB value.
void QColor::cleanup () [static]
Internal clean up required for QColor.
This function is called from the QApplication destructor.
See also initialize().
int QColor::currentAllocContext () [static]
Returns the current color allocation context.
The default context is 0.
See also enterAllocContext() and leaveAllocContext().
QColor QColor::dark ( int factor = 200 ) const
Returns a darker (or lighter) color.
Returns a darker color if factor is greater than 100.
Setting factor to 300 returns a color that has
one third the brightness.
Returns a lighter color if factor is less than 100, equal to
light(10000 / factor).
This function converts the current RGB color to HSV, divides V by
factor and converts back to RGB.
See also light().
Examples:
desktop/desktop.cpp
void QColor::destroyAllocContext ( int context ) [static]
Destroys a color allocation context, context.
This function deallocates all colors that were allocated in the
specified context.
If context == -1, it frees up all colors
that the application has allocated.
If context == -2, it frees up all colors
that the application has allocated, except those in the
default context.
The function does nothing for true color displays.
See also enterAllocContext() and alloc().
int QColor::enterAllocContext () [static]
Enters a color allocation context and returns a nonzero unique identifier.
Color allocation contexts are useful for programs that need to
allocate many colors and throw them away later, like image viewers.
The allocation context functions work for true color displays as
well as colormap display, except that QColor::destroyAllocContext()
does nothing for true color.
Example:
QPixmap loadPixmap( QString fileName )
{
static int alloc_context = 0;
if ( alloc_context )
QColor::destroyAllocContext( alloc_context );
alloc_context = QColor::enterAllocContext();
QPixmap pm( fileName );
QColor::leaveAllocContext();
return pm;
}
The example code loads a pixmap from file. It frees up all colors
that were allocated the last time loadPixmap() was called.
The initial/default context is 0. Qt keeps a list of colors
associated with their allocation contexts. You can call
destroyAllocContext() to get rid of all colors that were allocated
in a specific context.
Calling enterAllocContext() enters an allocation context. The
allocation context lasts until you call leaveAllocContext(). QColor
has an internal stack of allocation contexts. Each call to
enterAllocContex() must have a corresponding leaveAllocContext().
// context 0 active
int c1 = QColor::enterAllocContext(); // enter context c1
// context c1 active
int c2 = QColor::enterAllocContext(); // enter context c2
// context c2 active
QColor::leaveAllocContext(); // leave context c2
// context c1 active
QColor::leaveAllocContext(); // leave context c1
// context 0 active
// Now, free all colors that were allocated in context c2
QColor::destroyAllocContext( c2 );
You may also want to set the application's color specification.
See QApplication::setColorSpec() for more information.
See also leaveAllocContext(), currentAllocContext(), destroyAllocContext() and QApplication::setColorSpec().
void QColor::getHsv ( int & h, int & s, int & v ) const
This function is obsolete. It is provided to keep old source working, and will probably be removed in a future version of Qt. We strongly advise against using it in new code.
int QColor::green () const
Returns the G (green) component of the RGB value.
void QColor::hsv ( int * h, int * s, int * v ) const
Returns the current RGB value as HSV.
Arguments:
- *h, hue.
- *s, saturation.
- *v, value.
The hue defines the color. Its range is 0..359 if the color is chromatic
and -1 if the color is achromatic. The saturation and value both vary
between 0 and 255 inclusive.
See also setHsv() and rgb().
void QColor::initialize () [static]
Internal initialization required for QColor.
This function is called from the QApplication constructor.
See also cleanup().
bool QColor::isDirty () const
Returns TRUE if the color is dirty, i.e. lazy allocation is enabled and
an RGB/HSV value has been set but not allocated.
See also setLazyAlloc(), alloc() and pixel().
bool QColor::isValid () const
Returns FALSE if the color is invalid, i.e. it was constructed using the
default constructor.
bool QColor::lazyAlloc () [static]
Returns TRUE if lazy color allocation is enabled (on-demand allocation),
or FALSE if it is disabled (immediate allocation).
See also setLazyAlloc().
void QColor::leaveAllocContext () [static]
Leaves a color allocation context.
See enterAllocContext() for a detailed explanation.
See also enterAllocContext() and currentAllocContext().
QColor QColor::light ( int factor = 150 ) const
Returns a lighter (or darker) color.
Returns a lighter color if factor is greater than 100.
Setting factor to 150 returns a color that is 50% brighter.
Returns a darker color if factor is less than 100, equal to
dark(10000 / factor).
This function converts the current RGB color to HSV, multiplies V with
factor and converts back to RGB.
See also dark().
Examples:
desktop/desktop.cpp
int QColor::maxColors () [static]
Returns the maximum number of colors supported by the underlying window
system.
QString QColor::name () const
Returns the name of the color in the format #RRGGBB.
See also setNamedColor().
int QColor::numBitPlanes () [static]
Returns the number of color bit planes for the underlying window system.
The returned values is equal to the default pixmap depth;
See also QPixmap::defaultDepth().
bool QColor::operator!= ( const QColor & c ) const
Returns TRUE if this color has different RGB value from c,
or FALSE if they have equal RGB values.
QColor & QColor::operator= ( const QColor & c )
Assigns a copy of the color and
returns a reference to this color.
bool QColor::operator== ( const QColor & c ) const
Returns TRUE if this color has the same RGB value as c,
or FALSE if they have different RGB values.
uint QColor::pixel () const
Returns the pixel value.
This value is used by the underlying window system to refer to a color.
It can be thought of as an index into the display hardware's color table,
but the value is an arbitrary 32-bit value.
See also setLazyAlloc() and alloc().
int QColor::red () const
Returns the R (red) component of the RGB value.
QRgb QColor::rgb () const
Returns the RGB value.
The return type QRgb is equivalent to unsigned int.
See also setRgb(), hsv(), qRed(), qBlue() and qGreen().
void QColor::rgb ( int * r, int * g, int * b ) const
Returns the red, green and blue components of the RGB value in
*r, *g and *b. The value range for a component is 0..255.
See also setRgb() and hsv().
void QColor::setHsv ( int h, int s, int v )
Sets a HSV color value.
Arguments:
- h, hue (-1,0..360). -1 means achromatic.
- s, saturation (0..255).
- v, value (0..255).
See also hsv() and setRgb().
Examples:
progress/progress.cpp
grapher/grapher.cpp
drawdemo/drawdemo.cpp
void QColor::setLazyAlloc ( bool enable ) [static]
Enables or disables lazy color allocation.
If lazy allocation is enabled, colors are allocated the first time they
are used (upon calling the pixel() function). If lazy allocation is
disabled, colors are allocated when they are constructed or when either
setRgb() or setHsv() is called.
Lazy color allocation is enabled by default.
See also lazyAlloc(), pixel() and alloc().
void QColor::setNamedColor ( const QString & name )
Sets the RGB value to that of the named color.
The color name may be in one of five formats:
- #RGB (each of R, G and B is a single hex digit)
- #RRGGBB
- #RRRGGGBBB
- #RRRRGGGGBBBB
- A named from the X color database (rgb.txt), e.g.
"steelblue" or "gainsboro". These color names also work
under Qt for Windows.
void QColor::setRgb ( QRgb rgb )
Sets the RGB value to rgb.
The type QRgb is equivalent to unsigned int.
See also rgb() and setHsv().
void QColor::setRgb ( int r, int g, int b )
Sets the RGB value to (r,g,b). r, g and b must be in the range 0..255.
See also rgb() and setHsv().
Related Functions
Reads a color object from the stream.
See also Format of the QDataStream operators
int qGreen (QRgb rgb)
Returns the green component of the RGB triplet rgb.
See also qRgb() and QColor::green().
Examples:
qmag/qmag.cpp
int qRed (QRgb rgb)
Returns the red component of the RGB triplet rgb.
See also qRgb() and QColor::red().
Examples:
qmag/qmag.cpp
int qAlpha (QRgb rgba)
Returns the alpha component of the RGBA quadruplet rgb.
int qGray (int r, int g, int b)
Returns a gray value 0..255 from the (r,g,b) triplet.
The gray value is calculated using the formula:
(r*11 + g*16 + b*5)/32
QRgb qRgba (int r, int g, int b, int a)
Returns the RGBA quadruplet (r,g,b,a).
The return type QRgb is equivalent to unsigned int.
See also qRgb(), qRed(), qGreen() and qBlue().
int qGray (qRgb rgb)
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Writes a color object to the stream.
See also Format of the QDataStream operators
int qBlue (QRgb rgb)
Returns the blue component of the RGB triplet rgb.
See also qRgb() and QColor::blue().
Examples:
qmag/qmag.cpp
QRgb qRgb (int r, int g, int b)
Returns the RGB triplet (r,g,b).
The return type QRgb is equivalent to unsigned int.
See also qRgba(), qRed(), qGreen() and qBlue().
Examples:
desktop/desktop.cpp
Search the documentation, FAQ, qt-interest archive and more (uses
www.trolltech.com):
This file is part of the Qt toolkit,
copyright © 1995-2005
Trolltech, all rights reserved.