QColor Class
The QColor class provides colors based on RGB values. More…
| Header | #include <QColor> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Gui) |
| Module | Nt GUI C++ Classes |
List of all members, including inherited members
Public Types
| enum | Spec |
| enum | NameFormat |
Public Functions
| QColor() | |
| QColor(Qt::GlobalColor color) | |
| QColor(int r, int g, int b, int a = 255) | |
| QColor(QRgb rgb) | |
| QColor(const QString &name) | |
| QColor(const char *name) | |
| bool | isValid() const |
| Spec | spec() const |
| QString | name(NameFormat format = HexRgb) const |
| void | setNamedColor(const QString &name) |
| int | red() const |
| int | green() const |
| int | blue() const |
| int | alpha() const |
| void | setRed(int red) |
| void | setGreen(int green) |
| void | setBlue(int blue) |
| void | setAlpha(int alpha) |
| qreal | redF() const |
| qreal | greenF() const |
| qreal | blueF() const |
| qreal | alphaF() const |
| void | setRedF(qreal red) |
| void | setGreenF(qreal green) |
| void | setBlueF(qreal blue) |
| void | setAlphaF(qreal alpha) |
| void | getRgb(int *r, int *g, int *b, int *a = nullptr) const |
| void | setRgb(int r, int g, int b, int a = 255) |
| void | setRgb(QRgb rgb) |
| void | setRgba(QRgb rgba) |
| QRgb | rgb() const |
| QRgb | rgba() const |
| int | hue() const |
| int | saturation() const |
| int | value() const |
| QColor | lighter(int factor = 150) const |
| QColor | darker(int factor = 200) const |
Static Public Members
| bool | isValidColor(const QString &name) |
| QColor | fromString(const QString &name) |
| QColor | fromRgb(int r, int g, int b, int a = 255) |
| QColor | fromRgb(QRgb rgb) |
| QColor | fromRgba(QRgb rgba) |
| QColor | fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0) |
| QColor | fromHsv(int h, int s, int v, int a = 255) |
Detailed Description
A color is a red, green, blue and alpha value, each from 0 to 255. It can be built from components, a QRgb, a Qt::GlobalColor or a name such as "#ff8000" or "steelblue":
QColor orange(255, 128, 0);
QColor translucent = QColor::fromRgb(0, 0, 255, 128);
QColor named("#336699");
A default-constructed QColor is invalid; isValid() returns false.
See also QBrush, QPen and QImage.
Differences from Qt
Only the RGB color specification exists: there is no CMYK, HSL or extended RGB, and each component has 8 bits, so the floating-point accessors are quantized to 1/255. Color names are the #rgb, #rrggbb and #aarrggbb forms and a short list of the CSS basic colors, not the full SVG list. There are no QVariant or QDataStream conversions.
Member Type Documentation
enum QColor::NameFormat
This enum selects the form of name().
| Constant | Description |
|---|---|
QColor::HexRgb | #RRGGBB. |
QColor::HexArgb | #AARRGGBB. |
enum QColor::Spec
This enum describes how a color is specified.
| Constant | Description |
|---|---|
QColor::Invalid | The color is not valid. |
QColor::Rgb | The color is specified by red, green and blue. |
QColor::Hsv | Accepted for compatibility; colors are always reported as Rgb. |
Member Function Documentation
QColor::QColor()
Constructs an invalid color.
See also isValid().
QColor::QColor(Qt::GlobalColor color)
Constructs a color for the predefined color.
QColor::QColor(int r, int g, int b, int a = 255)
Constructs a color with the components r, g, b and a, each clamped to the range 0 to 255.
QColor::QColor(QRgb rgb)
Constructs an opaque color from rgb. The alpha byte of rgb is ignored; use fromRgba() to keep it.
QColor::QColor(const QString &name)
Constructs a color from name, which is a hex form or a color name. The color is invalid if the name is not recognized.
See also setNamedColor().
QColor::QColor(const char *name)
This is an overloaded function.
Constructs a color from the Latin-1 string name.
int QColor::alpha() const
Returns the alpha component, from 0 (transparent) to 255 (opaque).
See also setAlpha() and alphaF().
qreal QColor::alphaF() const
Returns the alpha component from 0.0 to 1.0.
See also alpha() and setAlphaF().
int QColor::blue() const
Returns the blue component, from 0 to 255.
See also setBlue() and blueF().
qreal QColor::blueF() const
Returns the blue component from 0.0 to 1.0.
See also blue() and setBlueF().
QColor QColor::darker(int factor = 200) const
Returns a darker color. A factor of 200 halves the value; 100 or less returns the color unchanged.
See also lighter().
QColor QColor::fromHsv(int h, int s, int v, int a = 255)
Returns the color with hue h (0 to 359), saturation s, value v and alpha a (each 0 to 255). A negative h gives a gray.
QColor QColor::fromRgb(int r, int g, int b, int a = 255)
Returns the color with the components r, g, b and a.
QColor QColor::fromRgb(QRgb rgb)
This is an overloaded function.
Returns the opaque color for rgb.
QColor QColor::fromRgba(QRgb rgba)
Returns the color for rgba, including its alpha byte.
QColor QColor::fromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0)
Returns the color with the components r, g, b and a, each from 0.0 to 1.0.
QColor QColor::fromString(const QString &name)
Returns the color called name, or an invalid color.
See also setNamedColor().
void QColor::getRgb(int *r, int *g, int *b, int *a = nullptr) const
Stores the components in r, g, b and, if not null, a.
See also setRgb().
int QColor::green() const
Returns the green component, from 0 to 255.
See also setGreen() and greenF().
qreal QColor::greenF() const
Returns the green component from 0.0 to 1.0.
See also green() and setGreenF().
int QColor::hue() const
Returns the hue, from 0 to 359, or -1 for a gray.
See also saturation() and value().
bool QColor::isValid() const
Returns true if the color is valid; otherwise returns false.
bool QColor::isValidColor(const QString &name)
Returns true if name is a color name that setNamedColor() accepts.
QColor QColor::lighter(int factor = 150) const
Returns a lighter color. A factor of 150 makes the value 50% higher; 100 or less returns the color unchanged. Black becomes dark gray.
See also darker().
QString QColor::name(NameFormat format = HexRgb) const
Returns the name of the color in the given format: #RRGGBB for HexRgb and #AARRGGBB for HexArgb. An invalid color gives an empty string.
See also setNamedColor().
int QColor::red() const
Returns the red component, from 0 to 255.
qreal QColor::redF() const
Returns the red component from 0.0 to 1.0.
QRgb QColor::rgb() const
Returns the color as an opaque QRgb; the alpha component is dropped.
See also rgba().
QRgb QColor::rgba() const
Returns the color as a QRgb that includes the alpha component.
See also rgb().
int QColor::saturation() const
Returns the HSV saturation, from 0 to 255.
void QColor::setAlpha(int alpha)
Sets the alpha component to alpha, clamped to 0 to 255. The color becomes valid.
See also alpha().
void QColor::setAlphaF(qreal alpha)
Sets the alpha component to alpha, from 0.0 to 1.0.
See also alphaF().
void QColor::setBlue(int blue)
Sets the blue component to blue, clamped to 0 to 255. The color becomes valid.
See also blue().
void QColor::setBlueF(qreal blue)
Sets the blue component to blue, from 0.0 to 1.0.
See also blueF().
void QColor::setGreen(int green)
Sets the green component to green, clamped to 0 to 255. The color becomes valid.
See also green().
void QColor::setGreenF(qreal green)
Sets the green component to green, from 0.0 to 1.0.
See also greenF().
void QColor::setNamedColor(const QString &name)
Sets the color to the one called name: #rgb, #rrggbb, #aarrggbb or a color name, in any case. The color becomes invalid if name is not recognized.
See also isValidColor() and name().
void QColor::setRed(int red)
Sets the red component to red, clamped to 0 to 255. The color becomes valid.
See also red().
void QColor::setRedF(qreal red)
Sets the red component to red, from 0.0 to 1.0.
See also redF().
void QColor::setRgb(int r, int g, int b, int a = 255)
Sets the color to the components r, g, b and a.
See also getRgb().
void QColor::setRgb(QRgb rgb)
This is an overloaded function.
Sets the color to rgb, with full opacity.
void QColor::setRgba(QRgb rgba)
Sets the color to rgba, including its alpha byte.
See also rgba().
Spec QColor::spec() const
Returns Rgb for a valid color and Invalid otherwise.
int QColor::value() const
Returns the HSV value, from 0 to 255.
See also hue() and saturation().