Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The TQColor class provides colors based on RGB or HSV values. More...
#include <tqcolor.h>
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: white, black, red, darkRed, green, darkGreen, blue, darkBlue, cyan, darkCyan, magenta, darkMagenta, yellow, darkYellow, gray, darkGray, lightGray, color0 and color1, accessible as members of the TQt namespace (ie. TQt::red).
The colors color0 (zero pixel value) and color1 (non-zero pixel value) are special colors for drawing in bitmaps. Painting with color0 sets the bitmap bits to 0 (transparent, i.e. background), and painting with color1 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:
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().
Use maxColors() and numBitPlanes() to determine the maximum number of colors and the number of bit planes supported by the underlying window system,
If you need to allocate many colors temporarily, for example in an image viewer application, enterAllocContext(), leaveAllocContext() and destroyAllocContext() will prove useful.
Because 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 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.
HSV, like RGB, has three components:
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.
TQt 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.
See also TQPalette, TQColorGroup, TQApplication::setColorSpec(), Color FAQ, Widget Appearance and Style, Graphics Classes, and Image Processing Classes.
The type of color specified, either RGB or HSV, e.g. in the TQColor::TQColor( x, y, z, colorSpec) constructor.
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.
The alpha value of an invalid color is unspecified.
See also isValid().
Constructs a color with the RGB value r, g, b, in the same way as setRgb().
The color is left invalid if any or the arguments are illegal.
See also setRgb().
The arguments are an RGB value if colorSpec is TQColor::Rgb. x (red), y (green), and z (blue). All of them must be in the range 0-255.
The arguments are an HSV value if colorSpec is TQColor::Hsv. x (hue) must be -1 for achromatic colors and 0-359 for chromatic colors; y (saturation) and z (value) must both be in the range 0-255.
See also setRgb() and setHsv().
If pixel == 0xffffffff (the default), then the color uses the RGB value in a standard way. If pixel is something else, then the pixel value is set directly to pixel, skipping the normal allocation procedure.
The color is left invalid if name cannot be parsed.
See also setNamedColor().
The color is left invalid if name cannot be parsed.
See also setNamedColor().
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 enterAllocContext().
Returns the B (blue) component of the RGB value.
See also initialize().
The default context is 0.
See also enterAllocContext() and leaveAllocContext().
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. We recommend using lighter() for this purpose. If factor is 0 or negative, the return value is unspecified.
(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 and themes/wood.cpp.
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().
Example: showimg/showimg.cpp.
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.
Example:
TQPixmap loadPixmap( TQString fileName ) { static int alloc_context = 0; if ( alloc_context ) TQColor::destroyAllocContext( alloc_context ); alloc_context = TQColor::enterAllocContext(); TQPixmap pm( fileName ); TQColor::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. 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().
// context 0 active int c1 = TQColor::enterAllocContext(); // enter context c1 // context c1 active int c2 = TQColor::enterAllocContext(); // enter context c2 // context c2 active TQColor::leaveAllocContext(); // leave context c2 // context c1 active TQColor::leaveAllocContext(); // leave context c1 // context 0 active // Now, free all colors that were allocated in context c2 TQColor::destroyAllocContext( c2 );
You may also want to set the application's color specification. See TQApplication::setColorSpec() for more information.
See also leaveAllocContext(), currentAllocContext(), destroyAllocContext(), and TQApplication::setColorSpec().
Example: showimg/showimg.cpp.
Returns the current RGB value as HSV. The contents of the h, s and v pointers are set to the HSV values. If any of the three pointers are null, the function does nothing.
The hue (which h points to) is set to -1 if the color is achromatic.
Warning: 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 r, g and b to the red, green and blue components of the RGB value respectively. The value range for a component is 0..255.
See also rgb(), setRgb(), and getHsv().
Returns the G (green) component of the RGB value.
Example: themes/metal.cpp.
See also cleanup().
Returns FALSE if the color is invalid, i.e. it was constructed using the default constructor; otherwise returns TRUE.
Examples: chart/element.cpp, chart/setdataform.cpp, and scribble/scribble.cpp.
See enterAllocContext() for a detailed explanation.
See also enterAllocContext() and currentAllocContext().
Example: showimg/showimg.cpp.
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. We recommend using dark() for this purpose. If factor is 0 or negative, the return value is unspecified.
(This function converts the current RGB color to HSV, multiplies V by factor, and converts the result back to RGB.)
See also dark().
Examples: desktop/desktop.cpp and themes/wood.cpp.
Otherwise returns -1. Use numBitPlanes() to calculate the available colors in that case.
See also setNamedColor().
Example: chart/setdataform.cpp.
The returned value is equal to the default pixmap depth.
See also TQPixmap::defaultDepth().
Returns TRUE if this color has the same RGB value as c; otherwise returns FALSE.
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 alloc().
Returns the pixel value for screen screen.
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 alloc().
Returns the R (red) component of the RGB value.
Returns the RGB value.
The return type TQRgb is equivalent to unsigned int.
For an invalid color, the alpha value of the returned color is unspecified.
See also setRgb(), hsv(), tqRed(), tqBlue(), tqGreen(), and isValid().
If s or v are not in the range 0-255, or h is < -1, the color is not changed.
Warning: Colors are stored internally as RGB values, so getHSv() may return slightly different values to those set by setHsv().
Examples: drawdemo/drawdemo.cpp and progress/progress.cpp.
The color is invalid if name cannot be parsed.
Sets the RGB value to rgb.
The type TQRgb is equivalent to unsigned int.
See also Format of the TQDataStream operators.
See also Format of the TQDataStream operators.
Returns the alpha component of the RGBA quadruplet rgba.
Returns the blue component of the RGB triplet rgb.
See also tqRgb() and TQColor::blue().
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.
Returns a gray value 0..255 from the given rgb colour.
Returns the green component of the RGB triplet rgb.
See also tqRgb() and TQColor::green().
Returns the red component of the RGB triplet rgb.
See also tqRgb() and TQColor::red().
Returns the RGB triplet (r,g,b).
The return type TQRgb is equivalent to unsigned int.
See also tqRgba(), tqRed(), tqGreen(), and tqBlue().
Returns the RGBA quadruplet (r,g,b,a).
The return type TQRgba is equivalent to unsigned int.
See also tqRgb(), tqRed(), tqGreen(), and tqBlue().
This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.
Copyright © 2007 Trolltech | Trademarks | TQt 3.3.8
|