6.0.0-rc4Source

QColor Class

The QColor class provides colors based on RGB values. More…

Header#include <QColor>
CMaketarget_link_libraries(app PRIVATE Nt::Gui)
ModuleNt GUI C++ Classes

List of all members, including inherited members

Public Types

enumSpec
enumNameFormat

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)
boolisValid() const
Specspec() const
QStringname(NameFormat format = HexRgb) const
voidsetNamedColor(const QString &name)
intred() const
intgreen() const
intblue() const
intalpha() const
voidsetRed(int red)
voidsetGreen(int green)
voidsetBlue(int blue)
voidsetAlpha(int alpha)
qrealredF() const
qrealgreenF() const
qrealblueF() const
qrealalphaF() const
voidsetRedF(qreal red)
voidsetGreenF(qreal green)
voidsetBlueF(qreal blue)
voidsetAlphaF(qreal alpha)
voidgetRgb(int *r, int *g, int *b, int *a = nullptr) const
voidsetRgb(int r, int g, int b, int a = 255)
voidsetRgb(QRgb rgb)
voidsetRgba(QRgb rgba)
QRgbrgb() const
QRgbrgba() const
inthue() const
intsaturation() const
intvalue() const
QColorlighter(int factor = 150) const
QColordarker(int factor = 200) const

Static Public Members

boolisValidColor(const QString &name)
QColorfromString(const QString &name)
QColorfromRgb(int r, int g, int b, int a = 255)
QColorfromRgb(QRgb rgb)
QColorfromRgba(QRgb rgba)
QColorfromRgbF(qreal r, qreal g, qreal b, qreal a = 1.0)
QColorfromHsv(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().

ConstantDescription
QColor::HexRgb#RRGGBB.
QColor::HexArgb#AARRGGBB.

enum QColor::Spec

This enum describes how a color is specified.

ConstantDescription
QColor::InvalidThe color is not valid.
QColor::RgbThe color is specified by red, green and blue.
QColor::HsvAccepted 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().

[static] 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.

[static] QColor QColor::fromRgb(int r, int g, int b, int a = 255)

Returns the color with the components r, g, b and a.

[static] QColor QColor::fromRgb(QRgb rgb)

This is an overloaded function.

Returns the opaque color for rgb.

[static] QColor QColor::fromRgba(QRgb rgba)

Returns the color for rgba, including its alpha byte.

[static] 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.

[static] 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.

[static] 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.

See also setRed() and redF().

qreal QColor::redF() const

Returns the red component from 0.0 to 1.0.

See also red() and setRedF().

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.

See also hue() and value().

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