|  | Home · Overviews · Examples

# QColor Class Reference
[com.trolltech.qt.gui module]

The `QColor` class provides colors based on RGB, HSV or CMYK values. More...

## Detailed Description

The `QColor` class provides colors based on RGB, HSV or CMYK values.

A color is normally specified in terms of RGB (red, green, and blue) components, but it is also possible to specify it in terms of HSV (hue, saturation, and value) and CMYK (cyan, magenta, yellow and black) components. In addition a color can be specified using a color name. The color name can be any of the SVG 1.0 color names.

RGB | HSV | CMYK
--- | --- | ---

The `QColor` constructor creates the color based on RGB values. To create a `QColor` based on either HSV or CMYK values, use the `toHsv` and `toCmyk` functions respectively. These functions return a copy of the color using the desired format. In addition the static `fromRgb`, `fromHsv` and `fromCmyk` functions create colors from the specified values. Alternatively, a color can be converted to any of the three formats using the `convertTo` function (returning a copy of the color in the desired format), or any of the `setRgb`, `setHsv` and `setCmyk` functions altering this color's format. The `spec` function tells how the color was specified.

A color can be set by passing an RGB string (such as "#112233"), or a color name (such as "blue"), to the `setNamedColor` function. The color names are taken from the SVG 1.0 color names. The `name` function returns the name of the color in the format "#RRGGBB". Colors can also be set using `setRgb`, `setHsv` and `setCmyk`. To get a lighter or darker color use the `lighter` and `darker` functions respectively.

The `isValid` function indicates whether a `QColor` 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, and for that reason, the result of using an invalid color is undefined.

The color components can be retrieved individually, e.g with `red`, `hue` and `cyan`. The values of the color components can also be retrieved in one go using the getRgb(), getHsv() and getCmyk() functions. Using the RGB color model, the color components can in addition be accessed with `rgb`.

There are several related non-members: QRgb is a typdef for an unsigned int representing the RGB value triplet (r, g, b). Note that it also can hold a value for the alpha-channel (for more information, see the Alpha-Blended Drawing section). The qRed(), qBlue() and qGreen() functions return the respective component of the given QRgb value, while the qRgb() and qRgba() functions create and return the QRgb triplet based on the given component values. Finally, the qAlpha() function returns the alpha component of the provided QRgb, and the qGray() function calculates and return a gray value based on the given value.

`QColor` is platform and device independent. The `QColormap` class maps the color to the hardware.

For more information about painting in general, see The Paint System documentation.

- Integer vs. Floating Point Precision
- Alpha-Blended Drawing
- Predefined Colors
- The HSV Color Model
- The CMYK Color Model

### Integer vs. Floating Point Precision

`QColor` supports floating point precision and provides floating point versions of all the color components functions, e.g. getRgbF(), `hueF` and `fromCmykF`. Note that since the components are stored using 16-bit integers, there might be minor deviations between the values set using, for example, `setRgbF` and the values returned by the getRgbF() function due to rounding.

While the integer based functions take values in the range 0-255 (except `hue` which must have values within the range 0-359), the floating point functions accept values in the range 0.0 - 1.0.

### Alpha-Blended Drawing

`QColor` also support alpha-blended outlining and filling. The alpha channel of a color specifies the transparency effect, 0 represents a fully transparent color, while 255 represents a fully opaque color. For example:

        // Specfiy semi-transparent red
        painter.setBrush(QColor(255, 0, 0, 127));
        painter.drawRect(0, 0, width()/2, height());

        // Specify semi-transparend blue
        painter.setBrush(QColor(0, 0, 255, 127));
        painter.drawRect(0, 0, width(), height()/2);

The code above produces the following output:

Alpha-blended drawing is supported on Windows, Mac OS X, and on X11 systems that have the X Render extension installed.

The alpha channel of a color can be retrieved and set using the `alpha` and `setAlpha` functions if its value is an integer, and `alphaF` and `setAlphaF` if its value is qreal (double). By default, the alpha-channel is set to 255 (opaque). To retrieve and set all the RGB color components (including the alpha-channel) in one go, use the `rgba` and `setRgba` functions.

### Predefined Colors

There are 20 predefined QColors: Qt::white, Qt::black, Qt::red, Qt::darkRed, Qt::green, Qt::darkGreen, Qt::blue, Qt::darkBlue, Qt::cyan, Qt::darkCyan, Qt::magenta, Qt::darkMagenta, Qt::yellow, Qt::darkYellow, Qt::gray, Qt::darkGray, Qt::lightGray, Qt::color0, Qt::color1, and Qt::transparent.

`QColor` provides the static `colorNames` function which returns a `QStringList` containing the color names Qt knows about.

The colors Qt::color0 (zero pixel value) and Qt::color1 (non-zero pixel value) are special colors for drawing in QBitmaps. Painting with Qt::color0 sets the bitmap bits to 0 (transparent, i.e. background), and painting with Qt::color1 sets the bits to 1 (opaque, i.e. foreground).

### The HSV Color Model

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:

- H, for hue, is in the range 0 to 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.

- S, for saturation, is in the range 0 to 255, and the bigger it is, the stronger the color is. Grayish colors have saturation near 0; very strong colors have saturation near 255.

- V, for value, is in the range 0 to 255 and represents lightness or brightness of the color. 0 is black; 255 is as far from black as possible.

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 hue value that is too large, Qt forces it into range. Hue 360 or 720 is treated as 0; hue 540 is treated as 180.

In addition to the standard HSV model, Qt provides an alpha-channel to feature alpha-blended drawing.

### The CMYK Color Model

While the RGB and HSV color models are used for display on computer monitors, the CMYK model is used in the four-color printing process of printing presses and some hard-copy devices.

CMYK has four components, all in the range 0-255: cyan (C), magenta (M), yellow (Y) and black (K). Cyan, magenta and yellow are called subtractive colors; the CMYK color model creates color by starting with a white surface and then subtracting color by applying the appropriate components. While combining cyan, magenta and yellow gives the color black, subtracting one or more will yield any other color. When combined in various percentages, these three colors can create the entire spectrum of colors.

Mixing 100 percent of cyan, magenta and yellow does produce black, but the result is unsatisfactory since it wastes ink, increases drying time, and gives a muddy colour when printing. For that reason, black is added in professional printing to provide a solid black tone; hence the term 'four color process'.

In addition to the standard CMYK model, Qt provides an alpha-channel to feature alpha-blended drawing.

See also `QPalette`, `QBrush`, and `QApplication::setColorSpec`.