Qt Literacy-QColor Theory Use Summary

I. Overview

QColor is an attribute class in Qt that depicts colors. This class is in the Qt GUI module, so you can know that this class is related to GUI. Such as displaying its background color, etc., this class will be used when drawing.

In the color theory system: a color is usually specified in terms of values ​​consisting of RGB (red, green, blue), but can also be specified in terms of HSV (hue, saturation, and value) and CMYK (cyan, magenta, yellow, and black) value to specify. Additionally, colors can be specified using color names. The color name can be any color name from SVG 1.0.

Insert image description here

There are different color systems in different application scenarios. When this class is used in many interfaces, the RGB color system is basically used. The parameters passed are also three values, and the most commonly used one is this constructor.

1. Constructor

The QColor constructor creates colors based on RGB values. To create a QColor based on HSV or CMYK values, use the toHsv() and toCmyk() functions respectively. These functions return a copy of the color in the desired format. Additionally, the static fromRgb(), fromHsv(), and fromCmyk() functions create colors based on specified values.

Additionally, colors can be converted to any of the three formats using the convertTo() function (which returns a copy of the color in the desired format), or using any of the setRgb(), setHsv(), and setCmyk() functions. Change the format of this color. The spec() function tells us how to specify the color.

You can set a color by passing an RGB string (such as "#112233" ) or an ARGB string (such as "#ff112233" ) or a color name (such as "blue" ) to the setNamedColor() function. Color names are taken from SVG 1.0 color names.

The function of the function is to return the name of the color in the format of "#RRGGBB". Colors can also be set using setRgb(), setHsv() and setCmyk(). To get a lighter or darker color, use the lighter() and deeper() functions respectively.

SVG 1.0 link

The following are the corresponding key names of the colors
Insert image description here

Constructor

QColor(const QColor &color)
QColor(QLatin1String name)
QColor(const char *name)
QColor(const QString &name)
QColor(QRgba64 rgba64)
QColor(QRgb color)
QColor(int r, int g, int b, int a = ...)
QColor(Qt::GlobalColor color)

static function

QColor fromCmyk(int c, int m, int y, int k, int a = 255)
QColor fromCmykF(qreal c, qreal m, qreal y, qreal k, qreal a = 1.0)
QColor fromHsl(int h, int s, int l, int a = 255)
QColor fromHslF(qreal h, qreal s, qreal l, qreal a = 1.0)
QColor fromHsv(int h, int s, int v, int a = 255)
QColor fromHsvF(qreal h, qreal s, qreal v, qreal a = 1.0)
QColor fromRgb(QRgb rgb)
QColor fromRgb(int r, int g, int b, int a = 255)
QColor fromRgba64(ushort r, ushort g, ushort b, ushort a = USHRT_MAX)
QColor fromRgba64(QRgba64 rgba64)
QColor fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0)
QColor fromRgba(QRgb rgba)

2. Legality verification

The isValid() function indicates whether the QColor is valid. For example, RGB colors outside the RGB value range are illegal. For performance reasons, QColor usually ignores illegal colors, so the results of using invalid colors are undefined.

3. Composition value retrieval

Color composition values ​​can be retrieved individually, for example using red(), hue() and cyan(). You can also use the getRgb(), getHsv(), and getCmyk() functions to retrieve the value of a color component value in one go. Using the RGB color model, you can also access color composition values ​​using RGB().

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

QColor is platform and device independent. The QColormap class maps colors to hardware.

2. Integer and floating point precision

QColor supports floating point precision and provides floating point versions of all color component value functions, such as getRgbF(), ​​hueF(), and frommykf(). Note that because the component values ​​are stored using 16-bit integers, there may be small deviations between the value set using (for example) setRgbF() and the value returned by the getRgbF() function, due to rounding.

Integer-based functions accept values ​​in the range 0-255 (except hue(), whose value must be in the range 0-359), and floating-point functions accept values ​​in the range 0.0 - 1.0.

3. Alpha-Blended Alpha blending map

QColor also supports alpha blending outlines and fills. The color's alpha channel specifies the transparency effect, with 0 representing a fully transparent color and 255 representing a fully opaque color. For example:

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

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

The code above produces the following output:
Insert image description here

If its value is an integer, use the alphaF() and setAlphaF() functions, if its value is a qreal (double), use the alphaF() and setAlphaF() functions. By default, the alpha-channel is set to 255 (opaque). To retrieve and set all RGB color component values ​​(including the alpha channel) at once, use the rgba() and setRgba() functions.

4. Predefined colors

There are 20 predefined QColor objects in the QColorConstants namespace, including black, white, primary and secondary colors, darker versions of these colors, and three grays. Additionally, the QColorConstants::Svg namespace defines QColor objects for standard Svg color keyword names.
Insert image description here

QColorConstants::Color0, QColorConstants::Color1 and QColorConstants::Transparent colors are used for special purposes.

QColorConstants::Color0 (zero pixel value) and QColorConstants::Color1 (non-zero pixel value) are special colors drawn in qbitmap. Use QColorConstants::Color0 to set the bitmap bits to 0 (transparent; for example, background), and use c{QColorConstants::Color1} to set the bitmap bits to 1 (opaque; foreground, for example).

QColorConstants: Transparent is used to represent transparent pixels. When drawing with this value, the pixel value appropriate for the underlying pixel format used will be used.

For historical reasons, these 20 predefined colors are also available in the Qt::GlobalColor enumeration.

Finally, QColor recognizes various color names (as strings); the static colorNames() function returns a QStringList of color names that QColor knows about.

5. Extended RGB color model

The extended RGB color model, also known as scRGB color space, is the same as the RGB color model except that it allows values ​​below 0.0 and above 1.0. This allows you to represent colors outside the range of the RGB color space but still use the same values ​​for colors within the RGB color space.

6. HSV color model

The RGB model is hardware-oriented. It performs close to what most monitors show. In contrast, HSV represents color in a way that is more suitable for human perception of color. For example, the relationships of "stronger", "dark" and "opposite" are easy to express in HSV, but difficult to express in RGB.
HSV, like RGB, has three components:

  • H represents hue, which ranges from 0 to 359 if the color is colored (not gray), meaningless if it is gray. It represents the degrees on the color wheel that most people are familiar with. Red is 0 degrees, green is 120 degrees, and blue is 240 degrees.
    Insert image description here

  • S represents saturation, ranging from 0 to 255, the larger it is, the stronger the color. Gray has a color saturation close to 0; very intense colors have a saturation close to 255.
    Insert image description here

  • V represents a value, ranging from 0 to 255, indicating the brightness or brightness of the color. 0 is black; 255 is as far away from black as possible.
    Insert image description here

Here are some examples: pure red is H=0, S=255, V=255; dark red, slightly moved toward magenta, might be H=350 (equivalent to -10), S=255, V=180; light red The H value of gray-red is about 0 (for example, 350-359 or 0-10), and the S value is about 50-100, S=255.

For achromatic colors, Qt returns a hue value of -1. If you pass a hue value that is too large, Qt will force it into range. Hue 360 ​​or 720 is considered 0; Hue 540 is considered 180.

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

7. HSL color model

HSL is similar to HSV, but instead of the Value parameter, HSL specifies a brightness parameter that maps to the brightness of the color somewhat differently.
Similarly, for the same color, the saturation value of HSL is usually different from the saturation value of HSV. hslSaturation() provides the HSL saturation value of a color, while saturation() and hsvSaturation() provide the HSV saturation value.
Hue values ​​are defined the same in HSL and HSV.

8. 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 on printing presses and some hard copy equipment.
CMYK has four components, all in the range 0-255: cyan©, magenta (M), yellow (Y), and black (K). Cyan, magenta, and yellow are known as subtractive colors; the CMYK color model creates colors by starting from a white surface and then subtracting the colors by applying the appropriate composition values. Combining cyan, magenta, and yellow gives you black, while subtracting one or more gives you other colors. When combined in different percentages, these three colors can create the entire color spectrum.
Mixing 100% cyan, magenta, and yellow does produce black, but the result is unsatisfactory because it wastes ink, increases drying time, and gives a muddy color when printed . Therefore, in professional printing, black is added to provide a pure black tone; hence the term "four-color process".
In addition to the standard CMYK model, Qt also provides an alpha channel to implement alpha blended drawing.

Guess you like

Origin blog.csdn.net/qq_43680827/article/details/133969770