CopperSpice API
1.9.1
|
The QColor class provides colors based on RGB, HSV or CMYK values. More...
Public Types | |
enum | Spec |
Public Methods | |
QColor () | |
QColor (const char *name) | |
QColor (const QColor &other) | |
QColor (const QString &name) | |
QColor (int r, int g, int b, int a=255) | |
QColor (QColor &&other) | |
QColor (QRgb rgb) | |
QColor (QRgba64 rgba64) | |
QColor (Qt::GlobalColor color) | |
QColor (Spec spec) | |
int | alpha () const |
qreal | alphaF () const |
int | black () const |
qreal | blackF () const |
int | blue () const |
qreal | blueF () const |
QColor | convertTo (Spec colorSpec) const |
int | cyan () const |
qreal | cyanF () const |
QColor | dark (int factor=200) const |
QColor | darker (int factor=200) const |
void | getCmyk (int *c, int *m, int *y, int *k, int *a=nullptr) |
void | getCmykF (qreal *c, qreal *m, qreal *y, qreal *k, qreal *a=nullptr) |
void | getHsl (int *h, int *s, int *l, int *a=nullptr) const |
void | getHslF (qreal *h, qreal *s, qreal *l, qreal *a=nullptr) const |
void | getHsv (int *h, int *s, int *v, int *a=nullptr) const |
void | getHsvF (qreal *h, qreal *s, qreal *v, qreal *a=nullptr) const |
void | getRgb (int *r, int *g, int *b, int *a=nullptr) const |
void | getRgbF (qreal *r, qreal *g, qreal *b, qreal *a=nullptr) const |
int | green () const |
qreal | greenF () const |
int | hslHue () const |
qreal | hslHueF () const |
int | hslSaturation () const |
qreal | hslSaturationF () const |
int | hsvHue () const |
qreal | hsvHueF () const |
int | hsvSaturation () const |
qreal | hsvSaturationF () const |
int | hue () const |
qreal | hueF () const |
bool | isValid () const |
QColor | light (int factor=150) const |
QColor | lighter (int factor=150) const |
int | lightness () const |
qreal | lightnessF () const |
int | magenta () const |
qreal | magentaF () const |
QString | name (NameFormat format=HexRgb) const |
operator QVariant () const | |
bool | operator!= (const QColor &color) const |
QColor & | operator= (Qt::GlobalColor color) |
bool | operator== (const QColor &color) const |
int | red () const |
qreal | redF () const |
QRgb | rgb () const |
QRgb | rgba () const |
QRgba64 | rgba64 () const |
int | saturation () const |
qreal | saturationF () const |
void | setAlpha (int alpha) |
void | setAlphaF (qreal alpha) |
void | setBlue (int blue) |
void | setBlueF (qreal blue) |
void | setCmyk (int c, int m, int y, int k, int a=255) |
void | setCmykF (qreal c, qreal m, qreal y, qreal k, qreal a=1.0) |
void | setGreen (int green) |
void | setGreenF (qreal green) |
void | setHsl (int h, int s, int l, int a=255) |
void | setHslF (qreal h, qreal s, qreal l, qreal a=1.0) |
void | setHsv (int h, int s, int v, int a=255) |
void | setHsvF (qreal h, qreal s, qreal v, qreal a=1.0) |
void | setNamedColor (const QString &name) |
void | setRed (int red) |
void | setRedF (qreal red) |
void | setRgb (int r, int g, int b, int a=255) |
void | setRgb (QRgb rgb) |
void | setRgba (QRgb rgba) |
void | setRgba64 (QRgba64 rgba) |
void | setRgbF (qreal r, qreal g, qreal b, qreal a=1.0) |
Spec | spec () const |
QColor | toCmyk () const |
QColor | toHsl () const |
QColor | toHsv () const |
QColor | toRgb () const |
int | value () const |
qreal | valueF () const |
int | yellow () const |
qreal | yellowF () const |
Static Public Methods | |
static QStringList | colorNames () |
static QColor | fromCmyk (int c, int m, int y, int k, int a=255) |
static QColor | fromCmykF (qreal c, qreal m, qreal y, qreal k, qreal a=1.0) |
static QColor | fromHsl (int h, int s, int l, int a=255) |
static QColor | fromHslF (qreal h, qreal s, qreal l, qreal a=1.0) |
static QColor | fromHsv (int h, int s, int v, int a=255) |
static QColor | fromHsvF (qreal h, qreal s, qreal v, qreal a=1.0) |
static QColor | fromRgb (int r, int g, int b, int a=255) |
static QColor | fromRgb (QRgb rgb) |
static QColor | fromRgba (QRgb rgba) |
static QColor | fromRgba64 (QRgba64 rgba) |
static QColor | fromRgba64 (ushort r, ushort g, ushort b, ushort a=USHRT_MAX) |
static QColor | fromRgbF (qreal r, qreal g, qreal b, qreal a=1.0) |
static bool | isValidColor (const QString &name) |
Friends | |
QDataStream & | operator<< (QDataStream &stream, const QColor &color) |
QDataStream & | operator>> (QDataStream &stream, QColor &color) |
class | QColormap |
Related Functions | |
These are not member functions | |
int | qAlpha (QRgb rgba) |
int | qBlue (QRgb rgb) |
int | qGray (int r, int g, int b) |
int | qGray (QRgb rgb) |
int | qGreen (QRgb rgb) |
int | qRed (QRgb rgb) |
typedef | QRgb |
QRgb | qRgb (int r, int g, int b) |
QRgb | qRgba (int r, int g, int b, int a) |
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() methods 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().
QRgb is a typedef for an unsigned int representing the RGB value triplet (r, g, b). It can hold a value for the alpha channel. For more information refer to 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 refer to the Paint System documentation.
QColor supports floating point precision and provides floating point versions of color methods like, getRgbF(), hueF(), and fromCmykF(). Since the components are stored using 16-bit integers there might be minor deviations between the values set using setRgbF() and the values returned by the getRgbF() due to rounding.
While the integer based methods 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.
QColor also support alpha-blended outlining and filling. The alpha channel of a color specifies the transparency effect. The value of 0 represents a fully transparent color while 255 represents a fully opaque color.
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() methods 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() methods.
There are 20 predefined QColors described by the Qt::GlobalColor enum, including black, white, primary and secondary colors, darker versions of these colors and three shades of gray. QColor also recognizes a variety of color names; the static colorNames() method returns a QStringList color names that QColor knows about.
Additionally, the Qt::color0, Qt::color1 and Qt::transparent colors are used for special purposes.
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).
Qt::transparent is used to indicate a transparent pixel. When painting with this value, a pixel value will be used that is appropriate for the underlying pixel format in use.
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 where as a grayish light red could have H about 0 (say 350-359 or 0-10), S about 50-100, and S=255.
For achromatic color the hue value returned is -1. If you pass a hue value that is too large, it will reset into range. Hue 360 or 720 is treated as 0, hue 540 is treated as 180.
In addition to the standard HSV model there is also an alpha-channel feature Alpha-Blended Drawing.
HSL is similar to HSV. Instead of value parameter from HSV, HSL has the lightness parameter. The lightness parameter goes from black to color and from color to white. If you go outside at the night its black or dark gray. At day its colorful but if you look in a really strong light a things they are going to white and wash out.
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 color 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 there is also an alpha-channel to feature Alpha-Blended Drawing.
enum QColor::Spec |
The type of color specified, either RGB, HSV, CMYK or HSL.
Constant | Value |
---|---|
QColor::Rgb | 1 |
QColor::Hsv | 2 |
QColor::Cmyk | 3 |
QColor::Hsl | 4 |
QColor::Invalid | 0 |
QColor::QColor | ( | const QColor & | other | ) |
Copy constructs a new QColor from other.
QColor::QColor | ( | QColor && | other | ) |
Move constructs a new QColor other.
|
inline |
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.
QColor::QColor | ( | Qt::GlobalColor | color | ) |
Constructs a new QColor with a color value of color.
|
inline |
QColor::QColor | ( | QRgb | rgb | ) |
|
inline |
Constructs a named color in the same way as setNamedColor() using the given name. The color is invalid if the name can not be parsed.
|
inline |
Constructs a named color in the same way as setNamedColor() using the given name. The color is invalid if name can not be parsed.
QColor::QColor | ( | Spec | spec | ) |
Constructs a color with the given spec enum value.
int QColor::alpha | ( | ) | const |
Returns the alpha color component of this color.
qreal QColor::alphaF | ( | ) | const |
Returns the alpha color component of this color.
int QColor::black | ( | ) | const |
Returns the black color component of this color.
qreal QColor::blackF | ( | ) | const |
Returns the black color component of this color.
int QColor::blue | ( | ) | const |
qreal QColor::blueF | ( | ) | const |
Returns the blue color component of this color.
|
static |
Returns a QStringList containing the color names CopperSpice knows about.
QColor QColor::convertTo | ( | Spec | colorSpec | ) | const |
int QColor::cyan | ( | ) | const |
Returns the cyan color component of this color.
qreal QColor::cyanF | ( | ) | const |
Returns the cyan color component of this color.
|
deprecated |
|
inline |
Returns a darker (or lighter) color, but does not change this object.
If the factor is greater than 100, this methods returns a darker color. Setting factor to 300 returns a color that has one-third the brightness. If the factor is less than 100, the return color is lighter, but we recommend using the lighter() method for this purpose. If the factor is 0 or negative, the return value is unspecified.
The method converts the current RGB color to HSV, divides the value (V) component by factor and converts the color back to RGB.
|
static |
Returns a QColor constructed from the given CMYK color values: c (cyan), m (magenta), y (yellow), k (black), and a (alpha channel). All the values must be in the range 0-255.
Returns a QColor constructed from the given CMYK color values: c (cyan), m (magenta), y (yellow), k (black), and a (alpha channel).
All the values must be in the range 0.0 to 1.0.
|
static |
Returns a QColor constructed from the HSV color values, h (hue), s (saturation), l (lightness), and a (alpha channel).
The value of s, l, and a must all be in the range 0-255; the value of h must be in the range 0-359.
|
static |
Returns a QColor constructed from the HSV color values, h (hue), s (saturation), v (value), and a (alpha channel).
The values of s, v, and a must all be in the range 0-255; the value of h must be in the range 0-359.
Returns a QColor constructed from the HSV color values, h (hue), s (saturation), v (value), and a (alpha channel). All the values must be in the range 0.0 to 1.0.
|
static |
Static method that returns a QColor constructed from the RGB color values, r (red), g (green), b (blue), and a (alpha channel). All values must be in the range 0-255.
|
static |
Static method that returns a QColor constructed from the given QRgb value rgb.
The alpha component of rgb is ignored (i.e. it is automatically set to 255), use the fromRgba() method to include the alpha channel specified by the given QRgb value.
|
static |
|
static |
Returns a QColor constructed from the RGBA64 color values, r (red), g (green), b (blue), and a (alpha channel).
void QColor::getCmyk | ( | int * | c, |
int * | m, | ||
int * | y, | ||
int * | k, | ||
int * | a = nullptr |
||
) |
Sets the contents pointed to by c, m, y, k, and a, to the cyan, magenta, yellow, black, and alpha channel components of the color's CMYK value.
These components can be retrieved individually using the cyanF(), magentaF(), yellowF(), blackF() and alphaF() methods.
void QColor::getHsl | ( | int * | h, |
int * | s, | ||
int * | l, | ||
int * | a = nullptr |
||
) | const |
Sets the contents pointed to by h, s, l, and a, to the hue, saturation, lightness, and alpha channel components of the color's HSL value.
These components can be retrieved individually using the hslHue(), hslSaturation(), lightness() and alpha() methods.
Sets the contents pointed to by h, s, l, and a, to the hue, saturation, lightness, and alpha channel components of the color's HSL value. These components can be retrieved individually using the hslHueF(), hslSaturationF(), lightnessF() and alphaF() methods.
void QColor::getHsv | ( | int * | h, |
int * | s, | ||
int * | v, | ||
int * | a = nullptr |
||
) | const |
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha channel components of the color's HSV value. These components can be retrieved individually using the hue(), saturation(), value() and alpha() methods.
Sets the contents pointed to by h, s, v, and a, to the hue, saturation, value, and alpha channel (transparency) components of the color's HSV value. These components can be retrieved individually using the hueF(), saturationF(), valueF() and alphaF() methods.
void QColor::getRgb | ( | int * | r, |
int * | g, | ||
int * | b, | ||
int * | a = nullptr |
||
) | const |
int QColor::green | ( | ) | const |
Returns the green color component of this color.
qreal QColor::greenF | ( | ) | const |
Returns the green color component of this color.
int QColor::hslHue | ( | ) | const |
qreal QColor::hslHueF | ( | ) | const |
int QColor::hslSaturation | ( | ) | const |
Returns the saturation color component of this color.
qreal QColor::hslSaturationF | ( | ) | const |
Returns the saturation color component of this color.
int QColor::hsvHue | ( | ) | const |
Returns the hue color component of this color.
qreal QColor::hsvHueF | ( | ) | const |
Returns the hue color component of this color.
int QColor::hsvSaturation | ( | ) | const |
Returns the saturation color component of this color.
qreal QColor::hsvSaturationF | ( | ) | const |
Returns the saturation color component of this color.
int QColor::hue | ( | ) | const |
Returns the hue color component of this color.
The color is implicitly converted to HSV.
qreal QColor::hueF | ( | ) | const |
Returns the hue color component of this color.
The color is implicitly converted to HSV.
|
inline |
Returns true if the color is valid, otherwise returns false.
|
static |
Returns true if the name is a valid color name and can be used to construct a valid QColor object, otherwise returns false.
It uses the same algorithm used in setNamedColor().
|
deprecated |
|
inline |
Returns a lighter (or darker) color, but does not change this object.
If the factor is greater than 100, this methods returns a lighter color. Setting factor to 150 returns a color that is 50% brighter. If the factor is less than 100, the return color is darker, but we recommend using the darker() method for this purpose. If the factor is 0 or negative, the return value is unspecified.
The method converts the current RGB color to HSV, multiplies the value (V) component by factor and converts the color back to RGB.
int QColor::lightness | ( | ) | const |
Returns the lightness color component of this color.
qreal QColor::lightnessF | ( | ) | const |
int QColor::magenta | ( | ) | const |
Returns the magenta color component of this color.
qreal QColor::magentaF | ( | ) | const |
Returns the magenta color component of this color.
QString QColor::name | ( | NameFormat | format = HexRgb | ) | const |
Returns the name of the color in the specified format. The default format is "\#RRGGBB". For example, a "\#" character followed by three two-digit hexadecimal numbers.
bool QColor::operator!= | ( | const QColor & | color | ) | const |
Returns true if this color has a different RGB and alpha values from color, otherwise returns false.
QColor & QColor::operator= | ( | Qt::GlobalColor | color | ) |
Assigns the given color to this QColor and returns a reference to this object.
bool QColor::operator== | ( | const QColor & | color | ) | const |
Returns true if this QColor has the same RGB and alpha values as the given color, otherwise returns false.
int QColor::red | ( | ) | const |
qreal QColor::redF | ( | ) | const |
QRgb QColor::rgb | ( | ) | const |
QRgb QColor::rgba | ( | ) | const |
QRgba64 QColor::rgba64 | ( | ) | const |
Returns the RGB64 value of the color, including its alpha. For an invalid color, the alpha value of the returned color is unspecified.
int QColor::saturation | ( | ) | const |
Returns the saturation color component of this color.
The color is implicitly converted to HSV.
qreal QColor::saturationF | ( | ) | const |
Returns the saturation color component of this color.
The color is implicitly converted to HSV.
void QColor::setAlpha | ( | int | alpha | ) |
Sets the alpha of this color to alpha. Integer alpha is specified in the range 0-255.
void QColor::setAlphaF | ( | qreal | alpha | ) |
Sets the alpha of this color to alpha. qreal alpha is specified in the range 0.0 to 1.0.
void QColor::setBlue | ( | int | blue | ) |
void QColor::setBlueF | ( | qreal | blue | ) |
void QColor::setCmyk | ( | int | c, |
int | m, | ||
int | y, | ||
int | k, | ||
int | a = 255 |
||
) |
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha channel). All the values must be in the range 0-255.
Sets the color to CMYK values, c (cyan), m (magenta), y (yellow), k (black), and a (alpha channel). All values must be in the range 0.0 to 1.0.
void QColor::setGreen | ( | int | green | ) |
void QColor::setGreenF | ( | qreal | green | ) |
void QColor::setHsl | ( | int | h, |
int | s, | ||
int | l, | ||
int | a = 255 |
||
) |
void QColor::setHsv | ( | int | h, |
int | s, | ||
int | v, | ||
int | a = 255 |
||
) |
Sets a HSV color value. The values for h, s, v, and a are the hue, saturation, value, and alpha component of the HSL color. The saturation, value, and alpha channel values must be in the range 0-255, and the hue value must be greater than -1.
Sets a HSV color value. The values for h, s, v, and a are the hue, saturation, value, and alpha component of the HSL color. All the values must be in the range 0.0 to 1.0.
void QColor::setNamedColor | ( | const QString & | name | ) |
Sets the RGB value of this QColor to name, which may be in one of the following formats. The color is invalid if name can not be parsed.
void QColor::setRed | ( | int | red | ) |
void QColor::setRedF | ( | qreal | red | ) |
void QColor::setRgb | ( | int | r, |
int | g, | ||
int | b, | ||
int | a = 255 |
||
) |
void QColor::setRgb | ( | QRgb | rgb | ) |
Sets the RGB value to rgb. The alpha value is set to opaque.
void QColor::setRgba | ( | QRgb | rgba | ) |
void QColor::setRgba64 | ( | QRgba64 | rgba | ) |
Sets the RGB value to rgba, including its alpha.
|
inline |
Returns how the color was specified.
QColor QColor::toCmyk | ( | ) | const |
Creates and returns a CMYK QColor based on this color.
QColor QColor::toHsl | ( | ) | const |
Creates and returns an HSL QColor based on this color.
QColor QColor::toHsv | ( | ) | const |
Creates and returns an HSV QColor based on this color.
QColor QColor::toRgb | ( | ) | const |
Create and returns an RGB QColor based on this color.
int QColor::value | ( | ) | const |
Returns the value color component of this color.
qreal QColor::valueF | ( | ) | const |
Returns the value color component of this color.
int QColor::yellow | ( | ) | const |
Returns the yellow color component of this color.
qreal QColor::yellowF | ( | ) | const |
Returns the yellow color component of this color.
|
friend |
Writes the color to the stream and returns a reference to the stream.
Refer to Serializing Data Types for additional information.
|
friend |
Reads the color from the stream and returns a reference to the stream.
Refer to Serializing Data Types for additional information.
|
related |
Returns the alpha component of the ARGB quadruplet rgba.
|
related |
Returns the blue component of the ARGB quadruplet rgb.
|
related |
Returns a gray value (0 to 255) from the (r, g, b) triplet.
The gray value is calculated using the formula (r * 11 + g * 16 + b * 5)/32.
|
related |
Returns a gray value (0 to 255) from the given ARGB quadruplet rgb.
The gray value is calculated using the formula (R * 11 + G * 16 + B * 5)/32; the alpha channel is ignored.
|
related |
Returns the green component of the ARGB quadruplet rgb.
|
related |
Returns the red component of the ARGB quadruplet rgb.
|
related |
An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int. The type also holds a value for the alpha channel. The default alpha channel is ff
is opaque. For more information refer to the Alpha-Blended Drawing section.
|
related |