'\" t .TH QColor 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*- .\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the .\" license file included in the distribution for a complete license .\" statement. .\" .ad l .nh .SH NAME QColor \- Colors based on RGB or HSV values .SH SYNOPSIS \fC#include \fR .PP .SS "Public Members" .in +1c .ti -1c .BI "enum \fBSpec\fR { Rgb, Hsv }" .br .ti -1c .BI "\fBQColor\fR ()" .br .ti -1c .BI "\fBQColor\fR ( int r, int g, int b )" .br .ti -1c .BI "\fBQColor\fR ( int x, int y, int z, Spec colorSpec )" .br .ti -1c .BI "\fBQColor\fR ( QRgb rgb, uint pixel = 0xffffffff )" .br .ti -1c .BI "\fBQColor\fR ( const QString & name )" .br .ti -1c .BI "\fBQColor\fR ( const char * name )" .br .ti -1c .BI "\fBQColor\fR ( const QColor & c )" .br .ti -1c .BI "QColor & \fBoperator=\fR ( const QColor & c )" .br .ti -1c .BI "bool \fBisValid\fR () const" .br .ti -1c .BI "QString \fBname\fR () const" .br .ti -1c .BI "void \fBsetNamedColor\fR ( const QString & name )" .br .ti -1c .BI "QRgb \fBrgb\fR () const" .br .ti -1c .BI "void \fBsetRgb\fR ( int r, int g, int b )" .br .ti -1c .BI "void \fBsetRgb\fR ( QRgb rgb )" .br .ti -1c .BI "void \fBgetRgb\fR ( int * r, int * g, int * b ) const" .br .ti -1c .BI "void rgb ( int * r, int * g, int * b ) const \fI(obsolete)\fR" .br .ti -1c .BI "int \fBred\fR () const" .br .ti -1c .BI "int \fBgreen\fR () const" .br .ti -1c .BI "int \fBblue\fR () const" .br .ti -1c .BI "void \fBsetHsv\fR ( int h, int s, int v )" .br .ti -1c .BI "void \fBgetHsv\fR ( int * h, int * s, int * v ) const" .br .ti -1c .BI "void hsv ( int * h, int * s, int * v ) const \fI(obsolete)\fR" .br .ti -1c .BI "void getHsv ( int & h, int & s, int & v ) const \fI(obsolete)\fR" .br .ti -1c .BI "QColor \fBlight\fR ( int factor = 150 ) const" .br .ti -1c .BI "QColor \fBdark\fR ( int factor = 200 ) const" .br .ti -1c .BI "bool \fBoperator==\fR ( const QColor & c ) const" .br .ti -1c .BI "bool \fBoperator!=\fR ( const QColor & c ) const" .br .ti -1c .BI "uint \fBalloc\fR ()" .br .ti -1c .BI "uint \fBpixel\fR () const" .br .ti -1c .BI "uint \fBpixel\fR ( int screen ) const" .br .in -1c .SS "Static Public Members" .in +1c .ti -1c .BI "int \fBmaxColors\fR ()" .br .ti -1c .BI "int \fBnumBitPlanes\fR ()" .br .ti -1c .BI "int \fBenterAllocContext\fR ()" .br .ti -1c .BI "void \fBleaveAllocContext\fR ()" .br .ti -1c .BI "int \fBcurrentAllocContext\fR ()" .br .ti -1c .BI "void \fBdestroyAllocContext\fR ( int context )" .br .ti -1c .BI "void \fBinitialize\fR ()" .br .ti -1c .BI "void \fBcleanup\fR ()" .br .ti -1c .BI "QStringList \fBcolorNames\fR ()" .br .in -1c .SH RELATED FUNCTION DOCUMENTATION .in +1c .ti -1c .BI "QDataStream & \fBoperator<<\fR ( QDataStream & s, const QColor & c )" .br .ti -1c .BI "QDataStream & \fBoperator>>\fR ( QDataStream & s, QColor & c )" .br .ti -1c .BI "int \fBqRed\fR ( QRgb rgb )" .br .ti -1c .BI "int \fBqGreen\fR ( QRgb rgb )" .br .ti -1c .BI "int \fBqBlue\fR ( QRgb rgb )" .br .ti -1c .BI "int \fBqAlpha\fR ( QRgb rgba )" .br .ti -1c .BI "QRgb \fBqRgb\fR ( int r, int g, int b )" .br .ti -1c .BI "QRgb \fBqRgba\fR ( int r, int g, int b, int a )" .br .ti -1c .BI "int \fBqGray\fR ( int r, int g, int b )" .br .ti -1c .BI "int \fBqGray\fR ( tqRgb rgb )" .br .in -1c .SH DESCRIPTION The QColor class provides colors based on RGB or HSV values. .PP 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). .PP In addition to the RGB value, a QColor also has a pixel value and a validity. The pixel 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. .PP The validity (isValid()) indicates whether the color 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. The result of using an invalid color is unspecified and will usually be surprising. .PP There are 19 predefined QColor objects: \fCwhite\fR, \fCblack\fR, \fCred\fR, \fCdarkRed\fR, \fCgreen\fR, \fCdarkGreen\fR, \fCblue\fR, \fCdarkBlue\fR, \fCcyan\fR, \fCdarkCyan\fR, \fCmagenta\fR, \fCdarkMagenta\fR, \fCyellow\fR, \fCdarkYellow\fR, \fCgray\fR, \fCdarkGray\fR, \fClightGray\fR, \fCcolor0\fR and \fCcolor1\fR, accessible as members of the Qt namespace (ie. \fCQt::red\fR). .PP
.ce 1 .B "[Image Omitted]" .PP
.PP The colors \fCcolor0\fR (zero pixel value) and \fCcolor1\fR (non-zero pixel value) are special colors for drawing in bitmaps. Painting with \fCcolor0\fR sets the bitmap bits to 0 (transparent, i.e. background), and painting with \fCcolor1\fR sets the bits to 1 (opaque, i.e. foreground). .PP 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. The following steps are taken to allocate a color. If, at any point, a suitable color is found then the appropriate pixel value is returned and the subsequent steps are not taken: .PP
    .IP 1 Is the pixel value valid? If it is, just return it; otherwise, allocate a pixel value. .IP 2 Check an internal hash table to see if we allocated an equal RGB value earlier. If we did, set the corresponding pixel value for the color and return it. .IP 3 Try to allocate the RGB value. If we succeed, we get a pixel value that we save in the internal table with the RGB value. Return the pixel value. .IP 4 The color could not be allocated. Find the closest matching color, save it in the internal table, and return it. .PP A color can be set by passing setNamedColor() an RGB string like" #112233", or a color name, e.g. "blue". The names are taken from X11's rgb.txt database but can also be used under Windows. To get a lighter or darker color use light() and dark() respectively. Colors can also be set using setRgb() and setHsv(). The color components can be accessed in one go with rgb() and hsv(), or individually with red(), green() and blue(). .PP Use maxColors() and numBitPlanes() to determine the maximum number of colors and the number of bit planes supported by the underlying window system, .PP If you need to allocate many colors temporarily, for example in an image viewer application, enterAllocContext(), leaveAllocContext() and destroyAllocContext() will prove useful. .SH "HSV Colors" Because many people don't know the HSV color model very well, we'll cover it briefly here. .PP 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. .PP HSV, like RGB, has three components: .IP .TP H, for hue, is either 0-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. .IP .TP 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. .IP .TP V, for value, is 0-255 and represents lightness or brightness of the color. 0 is black; 255 is as far from black as possible. .IP .PP 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. .PP Qt returns a hue value of -1 for achromatic colors. If you pass a too-big hue value, Qt forces it into range. Hue 360 or 720 is treated as 0; hue 540 is treated as 180. .PP See also QPalette, QColorGroup, QApplication::setColorSpec(), Color FAQ, Widget Appearance and Style, Graphics Classes, and Image Processing Classes. .SS "Member Type Documentation" .SH "QColor::Spec" The type of color specified, either RGB or HSV, e.g. in the \fCQColor::QColor( x, y, z, colorSpec)\fR constructor. .TP \fCQColor::Rgb\fR .TP \fCQColor::Hsv\fR .SH MEMBER FUNCTION DOCUMENTATION .SH "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. .PP The alpha value of an invalid color is unspecified. .PP See also isValid(). .SH "QColor::QColor ( int r, int g, int b )" Constructs a color with the RGB value \fIr\fR, \fIg\fR, \fIb\fR, in the same way as setRgb(). .PP The color is left invalid if any or the arguments are illegal. .PP See also setRgb(). .SH "QColor::QColor ( int x, int y, int z, Spec colorSpec )" Constructs a color with the RGB or HSV value \fIx\fR, \fIy\fR, \fIz\fR. .PP The arguments are an RGB value if \fIcolorSpec\fR is QColor::Rgb. \fIx\fR (red), \fIy\fR (green), and \fIz\fR (blue). All of them must be in the range 0-255. .PP The arguments are an HSV value if \fIcolorSpec\fR is QColor::Hsv. \fIx\fR (hue) must be -1 for achromatic colors and 0-359 for chromatic colors; \fIy\fR (saturation) and \fIz\fR (value) must both be in the range 0-255. .PP See also setRgb() and setHsv(). .SH "QColor::QColor ( QRgb rgb, uint pixel = 0xffffffff )" Constructs a color with the RGB value \fIrgb\fR and a custom pixel value \fIpixel\fR. .PP If \fIpixel\fR == 0xffffffff (the default), then the color uses the RGB value in a standard way. If \fIpixel\fR is something else, then the pixel value is set directly to \fIpixel\fR, skipping the normal allocation procedure. .SH "QColor::QColor ( const QString & name )" Constructs a named color in the same way as setNamedColor() using name \fIname\fR. .PP The color is left invalid if \fIname\fR cannot be parsed. .PP See also setNamedColor(). .SH "QColor::QColor ( const char * name )" Constructs a named color in the same way as setNamedColor() using name \fIname\fR. .PP The color is left invalid if \fIname\fR cannot be parsed. .PP See also setNamedColor(). .SH "QColor::QColor ( const QColor & c )" Constructs a color that is a copy of \fIc\fR. .SH "uint QColor::alloc ()" Allocates the RGB color and returns the pixel value. .PP 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. .PP The pixel() function calls alloc() if necessary, so in general you don't need to call this function. .PP See also enterAllocContext(). .SH "int QColor::blue () const" Returns the B (blue) component of the RGB value. .SH "void QColor::cleanup ()\fC [static]\fR" Internal clean up required for QColor. This function is called from the QApplication destructor. .PP See also initialize(). .SH "QStringList QColor::colorNames ()\fC [static]\fR" Returns a QStringList containing the color names Qt knows about. .SH "int QColor::currentAllocContext ()\fC [static]\fR" Returns the current color allocation context. .PP The default context is 0. .PP See also enterAllocContext() and leaveAllocContext(). .SH "QColor QColor::dark ( int factor = 200 ) const" Returns a darker (or lighter) color, but does not change this object. .PP Returns a darker color if \fIfactor\fR is greater than 100. Setting \fIfactor\fR to 300 returns a color that has one-third the brightness. .PP Returns a lighter color if \fIfactor\fR is less than 100. We recommend using lighter() for this purpose. If \fIfactor\fR is 0 or negative, the return value is unspecified. .PP (This function converts the current RGB color to HSV, divides V by \fIfactor\fR and converts back to RGB.) .PP See also light(). .PP Examples: .)l desktop/desktop.cpp and themes/wood.cpp. .SH "void QColor::destroyAllocContext ( int context )\fC [static]\fR" Destroys a color allocation context, \fIcontext\fR. .PP This function deallocates all colors that were allocated in the specified \fIcontext\fR. If \fIcontext\fR == -1, it frees up all colors that the application has allocated. If \fIcontext\fR == -2, it frees up all colors that the application has allocated, except those in the default context. .PP The function does nothing for true color displays. .PP See also enterAllocContext() and alloc(). .PP Example: showimg/showimg.cpp. .SH "int QColor::enterAllocContext ()\fC [static]\fR" Enters a color allocation context and returns a non-zero unique identifier. .PP 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 for colormap displays, except that QColor::destroyAllocContext() does nothing for true color. .PP Example: .PP .nf .br QPixmap loadPixmap( QString fileName ) .br { .br static int alloc_context = 0; .br if ( alloc_context ) .br QColor::destroyAllocContext( alloc_context ); .br alloc_context = QColor::enterAllocContext(); .br QPixmap pm( fileName ); .br QColor::leaveAllocContext(); .br return pm; .br } .br .fi .PP The example code loads a pixmap from file. It frees up all colors that were allocated the last time loadPixmap() was called. .PP 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. .PP 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(). .PP .nf .br // context 0 active .br int c1 = QColor::enterAllocContext(); // enter context c1 .br // context c1 active .br int c2 = QColor::enterAllocContext(); // enter context c2 .br // context c2 active .br QColor::leaveAllocContext(); // leave context c2 .br // context c1 active .br QColor::leaveAllocContext(); // leave context c1 .br // context 0 active .br // Now, free all colors that were allocated in context c2 .br QColor::destroyAllocContext( c2 ); .br .fi .PP You may also want to set the application's color specification. See QApplication::setColorSpec() for more information. .PP See also leaveAllocContext(), currentAllocContext(), destroyAllocContext(), and QApplication::setColorSpec(). .PP Example: showimg/showimg.cpp. .SH "void QColor::getHsv ( int * h, int * s, int * v ) const" Returns the current RGB value as HSV. The contents of the \fIh\fR, \fIs\fR and \fIv\fR pointers are set to the HSV values. If any of the three pointers are null, the function does nothing. .PP The hue (which \fIh\fR points to) is set to -1 if the color is achromatic. .PP \fBWarning:\fR Colors are stored internally as RGB values, so getHSv() may return slightly different values to those set by setHsv(). .PP See also setHsv() and rgb(). .SH "void QColor::getHsv ( int & h, int & s, int & v ) const" \fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code. .SH "void QColor::getRgb ( int * r, int * g, int * b ) const" Sets the contents pointed to by \fIr\fR, \fIg\fR and \fIb\fR to the red, green and blue components of the RGB value respectively. The value range for a component is 0..255. .PP See also rgb(), setRgb(), and getHsv(). .SH "int QColor::green () const" Returns the G (green) component of the RGB value. .SH "void QColor::hsv ( int * h, int * s, int * v ) const" \fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code. Use getHsv() instead. .PP Example: themes/metal.cpp. .SH "void QColor::initialize ()\fC [static]\fR" Internal initialization required for QColor. This function is called from the QApplication constructor. .PP See also cleanup(). .SH "bool QColor::isValid () const" Returns FALSE if the color is invalid, i.e. it was constructed using the default constructor; otherwise returns TRUE. .PP Examples: .)l chart/element.cpp, chart/setdataform.cpp, and scribble/scribble.cpp. .SH "void QColor::leaveAllocContext ()\fC [static]\fR" Leaves a color allocation context. .PP See enterAllocContext() for a detailed explanation. .PP See also enterAllocContext() and currentAllocContext(). .PP Example: showimg/showimg.cpp. .SH "QColor QColor::light ( int factor = 150 ) const" Returns a lighter (or darker) color, but does not change this object. .PP Returns a lighter color if \fIfactor\fR is greater than 100. Setting \fIfactor\fR to 150 returns a color that is 50% brighter. .PP Returns a darker color if \fIfactor\fR is less than 100. We recommend using dark() for this purpose. If \fIfactor\fR is 0 or negative, the return value is unspecified. .PP (This function converts the current RGB color to HSV, multiplies V by \fIfactor\fR, and converts the result back to RGB.) .PP See also dark(). .PP Examples: .)l desktop/desktop.cpp and themes/wood.cpp. .SH "int QColor::maxColors ()\fC [static]\fR" Returns the maximum number of colors supported by the underlying window system if the window system uses a palette. .PP Otherwise returns -1. Use numBitPlanes() to calculate the available colors in that case. .SH "QString QColor::name () const" Returns the name of the color in the format "#RRGGBB", i.e. a "#" character followed by three two-digit hexadecimal numbers. .PP See also setNamedColor(). .PP Example: chart/setdataform.cpp. .SH "int QColor::numBitPlanes ()\fC [static]\fR" Returns the number of color bit planes for the underlying window system. .PP The returned value is equal to the default pixmap depth. .PP See also QPixmap::defaultDepth(). .SH "bool QColor::operator!= ( const QColor & c ) const" Returns TRUE if this color has a different RGB value from \fIc\fR; otherwise returns FALSE. .SH "QColor & QColor::operator= ( const QColor & c )" Assigns a copy of the color \fIc\fR and returns a reference to this color. .SH "bool QColor::operator== ( const QColor & c ) const" Returns TRUE if this color has the same RGB value as \fIc\fR; otherwise returns FALSE. .SH "uint QColor::pixel () const" Returns the pixel value. .PP 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. .PP See also alloc(). .SH "uint QColor::pixel ( int screen ) const" This is an overloaded member function, provided for convenience. It behaves essentially like the above function. .PP Returns the pixel value for screen \fIscreen\fR. .PP 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. .PP See also alloc(). .SH "int QColor::red () const" Returns the R (red) component of the RGB value. .SH "QRgb QColor::rgb () const" Returns the RGB value. .PP The return type \fIQRgb\fR is equivalent to \fCunsigned\fR \fCint\fR. .PP For an invalid color, the alpha value of the returned color is unspecified. .PP See also setRgb(), hsv(), tqRed(), tqBlue(), tqGreen(), and isValid(). .SH "void QColor::rgb ( int * r, int * g, int * b ) const" \fBThis function is obsolete.\fR It is provided to keep old source working. We strongly advise against using it in new code. Use getRgb() instead .SH "void QColor::setHsv ( int h, int s, int v )" Sets a HSV color value. \fIh\fR is the hue, \fIs\fR is the saturation and \fIv\fR is the value of the HSV color. .PP If \fIs\fR or \fIv\fR are not in the range 0-255, or \fIh\fR is < -1, the color is not changed. .PP \fBWarning:\fR Colors are stored internally as RGB values, so getHSv() may return slightly different values to those set by setHsv(). .PP See also hsv() and setRgb(). .PP Examples: .)l drawdemo/drawdemo.cpp, grapher/grapher.cpp, and progress/progress.cpp. .SH "void QColor::setNamedColor ( const QString & name )" Sets the RGB value to \fIname\fR, which may be in one of these formats: .TP #RGB (each of R, G and B is a single hex digit) .TP #RRGGBB .TP #RRRGGGBBB .TP #RRRRGGGGBBBB .TP A name from the X color database (rgb.txt) (e.g." steelblue" or "gainsboro"). These color names also work under Windows. .PP The color is invalid if \fIname\fR cannot be parsed. .SH "void QColor::setRgb ( int r, int g, int b )" Sets the RGB value to \fIr\fR, \fIg\fR, \fIb\fR. The arguments, \fIr\fR, \fIg\fR and \fIb\fR must all be in the range 0..255. If any of them are outside the legal range, the color is not changed. .PP See also rgb() and setHsv(). .SH "void QColor::setRgb ( QRgb rgb )" This is an overloaded member function, provided for convenience. It behaves essentially like the above function. .PP Sets the RGB value to \fIrgb\fR. .PP The type \fIQRgb\fR is equivalent to \fCunsigned\fR \fCint\fR. .PP See also rgb() and setHsv(). .SH RELATED FUNCTION DOCUMENTATION .SH "QDataStream & operator<< ( QDataStream & s, const QColor & c )" Writes a color object, \fIc\fR to the stream, \fIs\fR. .PP See also Format of the QDataStream operators. .SH "QDataStream & operator>> ( QDataStream & s, QColor & c )" Reads a color object, \fIc\fR, from the stream, \fIs\fR. .PP See also Format of the QDataStream operators. .SH "int tqAlpha ( QRgb rgba )" Returns the alpha component of the RGBA quadruplet \fIrgba\fR. .SH "int tqBlue ( QRgb rgb )" Returns the blue component of the RGB triplet \fIrgb\fR. .PP See also tqRgb() and QColor::blue(). .SH "int tqGray ( int r, int g, int b )" Returns a gray value 0..255 from the (\fIr\fR, \fIg\fR, \fIb\fR) triplet. .PP The gray value is calculated using the formula (r*11 + g*16 + b*5)/32. .SH "int tqGray ( tqRgb rgb )" This is an overloaded member function, provided for convenience. It behaves essentially like the above function. .PP Returns a gray value 0..255 from the given \fIrgb\fR colour. .SH "int tqGreen ( QRgb rgb )" Returns the green component of the RGB triplet \fIrgb\fR. .PP See also tqRgb() and QColor::green(). .SH "int tqRed ( QRgb rgb )" Returns the red component of the RGB triplet \fIrgb\fR. .PP See also tqRgb() and QColor::red(). .SH "QRgb tqRgb ( int r, int g, int b )" Returns the RGB triplet \fI(r,g,b)\fR. .PP The return type QRgb is equivalent to \fCunsigned\fR \fCint\fR. .PP See also tqRgba(), tqRed(), tqGreen(), and tqBlue(). .SH "QRgb tqRgba ( int r, int g, int b, int a )" Returns the RGBA quadruplet \fI(r,g,b,a)\fR. .PP The return type QRgba is equivalent to \fCunsigned\fR \fCint\fR. .PP See also tqRgb(), tqRed(), tqGreen(), and tqBlue(). .SH "SEE ALSO" .BR http://doc.trolltech.com/ntqcolor.html .BR http://www.trolltech.com/faq/tech.html .SH COPYRIGHT Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. .SH AUTHOR Generated automatically from the source code. .SH BUGS If you find a bug in Qt, please report it as described in .BR http://doc.trolltech.com/bughowto.html . Good bug reports help us to help you. Thank you. .P The definitive Qt documentation is provided in HTML format; it is located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. .P If you find errors in this manual page, please report them to .BR qt-bugs@trolltech.com . Please include the name of the manual page (tqcolor.3qt) and the Qt version (3.3.8).