QColor Class Reference


The QColor class provides colors based on RGB. (details) (complete member list)

#include <qcolor.h>

Public Members

Static Public Members

Related Functions

(Note that these are not member functions.)

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 convert from HSV (hue,saturation and value) or set a color name (X-Windows color database).

There are 19 predefined global 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 (pixel value = 0) and color1 (pixel value = 1) 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:

  1. Is the pixel value valid? If it is, just return it, otherwise, allocate a pixel value (2).
  2. Check an internal hash table to see if we allocated an equal RGB value earlier. If we did, set the pixel value and return.
  3. 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.
  4. The color could not be allocated. Find the closest matching color and save it in the internal table.

This method seems to work well even with thousands of allocated colors.

See also: QPalette.


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.

QColor::QColor (int r, int g, int b)

Constructs a color with the RGB value (r,g,b).

QColor::QColor (ulong rgb, ulong pix=0xffffffff=0xffffffff)

Constructs a color with a RGB value and a custom pixel value.

If the pix = 0xffffffff, then the color uses this RGB value in a standard way. If pix is something else, then the pixel value will be set directly to pix (skips the standard allocation procedure).

QColor::QColor (const char *name)

Constructs a named color.

See also: setNamedColor().

QColor::QColor (const QColor &c)

Constructs a color that is a copy of c.

void QColor::alloc ()

Allocates a pixel value for the current RGB value.

int QColor::blue () const

Returns the blue component of the RGB value.

QColor QColor::dark (int factor) 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(100 / factor).

This function converts the current RGB color to HSV, divides V by factor and converts back to RGB.

void QColor::getHSV (int *h, int *s, int *v) const

Returns the current RGB value as HSV.

Arguments:

The hue defines the color. It is between 0 and 360 inclusive in the chromatic case, and -1 if the color is achromatic. The saturation and value vary between 0 and 255 inclusive.

See also: setHSV().

ulong QColor::getRGB () const

Returns the RGB value.

void QColor::getRGB (int *r, int *g, int *b) const

Gets the red, green and blue components of the RGB value.

See also: setRGB().

int QColor::green () const

Returns the green component of the RGB value.

bool QColor::lazyAlloc ()

Returns TRUE if colors are allocated on-demand. The default setting is TRUE.

See also: setLazyAlloc().

QColor QColor::light (int factor) 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(100 / factor).

This function converts the current RGB color to HSV, multiplies V with factor and converts back to RGB.

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.

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.

ulong QColor::pixel () const

Returns the pixel value.

int QColor::red () const

Returns the red component of the RGB value.

void QColor::setHSV (int h, int s, int v)

Sets a HSV color value.

Arguments:

See also: getHSV().

void QColor::setLazyAlloc (bool enable)

Enables or disables lazy color allocation.

See also: lazyAlloc().

void QColor::setNamedColor (const char *name)

Sets the RGB value to that of the named color.

This function searches the X color database for the color and sets the RGB value. The color will be set to invalid if such a color does not exist.

void QColor::setRGB (int r, int g, int b)

Sets the RGB value to (r, g, b).

See also: getRGB().

void QColor::setRGB (ulong rgb)

Sets the RGB value to rgb.

Bits 0-7 = red, bits 8-15 = green, bits 16-23 = blue.

See also: getRGB().


Related Functions

QDataStream & operator<< (QDataStream &s, const QColor &c)

Writes a color object to the stream.

Format: RGB value serialized as UINT32.

QDataStream & operator>> (QDataStream &s, QColor &c)

Reads a color object from the stream.


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 16:17, 1995/06/30 by the webmaster at Troll Tech