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.


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.

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().

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::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,\e g,b).

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 04:27, 1995/05/20 by the webmaster at Troll Tech