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 TQColor 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.
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, TQColor mostly disregards illegal colors. The result of using an invalid color is unspecified and will usually be surprising.
There are 19 predefined TQColor 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 TQt namespace (ie. \fCQt::red\fR).
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).
The TQColor 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:
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.
Qt returns a hue value of -1 for achromatic colors. If you pass a too-big hue value, TQt forces it into range. Hue 360 or 720 is treated as 0; hue 540 is treated as 180.
The arguments are an RGB value if \fIcolorSpec\fR is TQColor::Rgb. \fIx\fR (red), \fIy\fR (green), and \fIz\fR (blue). All of them must be in the range 0-255.
The arguments are an HSV value if \fIcolorSpec\fR is TQColor::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.
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.
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.
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.)
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.
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 TQColor::destroyAllocContext() does nothing for true color.
The initial/default context is 0. TQt 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(). TQColor has an internal stack of allocation contexts. Each call to enterAllocContex() must have a corresponding leaveAllocContext().
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().
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.
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.)
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.
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.
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.