/*!
\class QColor
- \brief The QColor class provides colors based on RGB, HSV or HSL values.
+ \brief The QColor class provides colors based on RGB or HSV values.
\ingroup painting
\ingroup appearance
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 HSL (hue, saturation,
- and lightness) 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.
-
- The QColor constructor creates the color based on RGB values. To
- create a QColor based on either HSV or HSL values, use the
- toHsv() and toHsl() functions respectively. These functions
- return a copy of the color using the desired format. In addition
- the static fromRgb(), fromHsv() and fromHsl() functions create
+ of HSV (hue, saturation and value) 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.
+
+ The QColor constructor creates the color based on RGB values. To
+ create a QColor based on HSV values, use the toHsv() function.
+ That function return a copy of the color using the desired format.
+ In addition the static fromRgb() and fromHsv() 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 setHsl() functions altering \e
- this color's format. The spec() function tells how the color was
- specified.
+ any of the setRgb() and setHsv() functions altering \e 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
- setHsl(). To get a lighter or darker color use the lighter() and
- darker() functions respectively.
+ "#RRGGBB". Colors can also be set using setRgb() and setHsv(). 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
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
- getHsl() functions. Using the RGB color model, the color
- components can in addition be accessed with rgb().
+ also be retrieved in one go using the getRgb() and getHsv()
+ 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
QColor supports floating point precision and provides floating
point versions of all the color components functions,
- e.g. getRgbF(), hueF() and fromHslF(). 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.
+ e.g. getRgbF() and hueF(). 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),
alpha-channel to feature \l {QColor#Alpha-Blended
Drawing}{alpha-blended drawing}.
- \section1 The HSL Color Model
-
- 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.
-
\sa QPalette, QBrush, QApplication::setColorSpec()
*/
/*!
\enum QColor::Spec
- The type of color specified, either RGB, HSV or HSL.
+ The type of color specified, either RGB or HSV.
\value Rgb
\value Hsv
- \value Hsl
\value Invalid
- \sa spec(), convertTo()
+ \sa spec(), toRgb(), toHsv()
*/
/*!
Returns how the color was specified.
- \sa Spec, convertTo()
+ \sa Spec
*/
QColor::QColor(QRgb color)
{
- cspec = Rgb;
+ cspec = QColor::Rgb;
ct.argb.alpha = 0xffff;
ct.argb.red = qRed(color) * 0x101;
ct.argb.green = qGreen(color) * 0x101;
QColor::QColor(Spec spec)
{
switch (spec) {
- case Invalid:
- invalidate();
- break;
- case Rgb:
- setRgb(0, 0, 0);
- break;
- case Hsv:
- setHsv(0, 0, 0);
- break;
- case Hsl:
- setHsl(0, 0, 0, 0);
- break;
+ case QColor::Invalid: {
+ invalidate();
+ break;
+ }
+ case QColor::Rgb: {
+ setRgb(0, 0, 0);
+ break;
+ }
+ case QColor::Hsv: {
+ setHsv(0, 0, 0);
+ break;
+ }
}
}
if (!h || !s || !v)
return;
- if (cspec != Invalid && cspec != Hsv) {
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv) {
toHsv().getHsvF(h, s, v, a);
return;
}
if (!h || !s || !v)
return;
- if (cspec != Invalid && cspec != Hsv) {
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv) {
toHsv().getHsv(h, s, v, a);
return;
}
return;
}
- cspec = Hsv;
+ cspec = QColor::Hsv;
ct.ahsv.alpha = qRound(a * USHRT_MAX);
ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
ct.ahsv.saturation = qRound(s * USHRT_MAX);
return;
}
- cspec = Hsv;
+ cspec = QColor::Hsv;
ct.ahsv.alpha = a * 0x101;
ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
ct.ahsv.saturation = s * 0x101;
}
/*!
- \since 4.6
-
- Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
- saturation, lightness, and alpha-channel (transparency) components of the
- color's HSL value.
-
- These components can be retrieved individually using the hueHslF(),
- saturationHslF(), lightnessF() and alphaF() functions.
-
- \sa setHsl()
-*/
-void QColor::getHslF(qreal *h, qreal *s, qreal *l, qreal *a) const
-{
- if (!h || !s || !l)
- return;
-
- if (cspec != Invalid && cspec != Hsl) {
- toHsl().getHslF(h, s, l, a);
- return;
- }
-
- *h = ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
- *s = ct.ahsl.saturation / qreal(USHRT_MAX);
- *l = ct.ahsl.lightness / qreal(USHRT_MAX);
-
- if (a)
- *a = ct.ahsl.alpha / qreal(USHRT_MAX);
-}
-
-/*!
- \since 4.6
-
- Sets the contents pointed to by \a h, \a s, \a l, and \a a, to the hue,
- saturation, lightness, and alpha-channel (transparency) components of the
- color's HSL value.
-
- These components can be retrieved individually using the hueHsl(),
- saturationHsl(), lightness() and alpha() functions.
-
- \sa setHsl()
-*/
-void QColor::getHsl(int *h, int *s, int *l, int *a) const
-{
- if (!h || !s || !l)
- return;
-
- if (cspec != Invalid && cspec != Hsl) {
- toHsl().getHsl(h, s, l, a);
- return;
- }
-
- *h = ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
- *s = ct.ahsl.saturation >> 8;
- *l = ct.ahsl.lightness >> 8;
-
- if (a)
- *a = ct.ahsl.alpha >> 8;
-}
-
-/*!
- \since 4.6
-
- Sets a HSL color lightness; \a h is the hue, \a s is the saturation, \a l is
- the lightness and \a a is the alpha component of the HSL color.
-
- All the values must be in the range 0.0-1.0.
-
- \sa getHslF(), setHsl()
-*/
-void QColor::setHslF(qreal h, qreal s, qreal l, qreal a)
-{
- if (Q_UNLIKELY(((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
- || (s < qreal(0.0) || s > qreal(1.0))
- || (l < qreal(0.0) || l > qreal(1.0))
- || (a < qreal(0.0) || a > qreal(1.0)))) {
- qWarning("QColor::setHsvF: HSV parameters out of range");
- invalidate();
- return;
- }
-
- cspec = Hsl;
- ct.ahsl.alpha = qRound(a * USHRT_MAX);
- ct.ahsl.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
- ct.ahsl.saturation = qRound(s * USHRT_MAX);
- ct.ahsl.lightness = qRound(l * USHRT_MAX);
-}
-
-/*!
- \since 4.6
-
- Sets a HSL color value; \a h is the hue, \a s is the saturation, \a l is
- the lightness and \a a is the 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.
-
- \sa getHsl(), setHslF()
-*/
-void QColor::setHsl(int h, int s, int l, int a)
-{
- if (Q_UNLIKELY(h < -1 || (uint)s > 255 || (uint)l > 255 || (uint)a > 255)) {
- qWarning("QColor::setHsv: HSV parameters out of range");
- invalidate();
- return;
- }
-
- cspec = Hsl;
- ct.ahsl.alpha = a * 0x101;
- ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
- ct.ahsl.saturation = s * 0x101;
- ct.ahsl.lightness = l * 0x101;
-}
-
-/*!
Sets the contents pointed to by \a r, \a g, \a b, and \a a, to the red,
green, blue, and alpha-channel (transparency) components of the color's
RGB value.
if (!r || !g || !b)
return;
- if (cspec != Invalid && cspec != Rgb) {
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb) {
toRgb().getRgbF(r, g, b, a);
return;
}
if (!r || !g || !b)
return;
- if (cspec != Invalid && cspec != Rgb) {
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb) {
toRgb().getRgb(r, g, b, a);
return;
}
return;
}
- cspec = Rgb;
+ cspec = QColor::Rgb;
ct.argb.alpha = qRound(a * USHRT_MAX);
ct.argb.red = qRound(r * USHRT_MAX);
ct.argb.green = qRound(g * USHRT_MAX);
return;
}
- cspec = Rgb;
+ cspec = QColor::Rgb;
ct.argb.alpha = a * 0x101;
ct.argb.red = r * 0x101;
ct.argb.green = g * 0x101;
QRgb QColor::rgba() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().rgba();
return qRgba(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8, ct.argb.alpha >> 8);
}
*/
void QColor::setRgba(QRgb rgba)
{
- cspec = Rgb;
+ cspec = QColor::Rgb;
ct.argb.alpha = qAlpha(rgba) * 0x101;
ct.argb.red = qRed(rgba) * 0x101;
ct.argb.green = qGreen(rgba) * 0x101;
*/
QRgb QColor::rgb() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().rgb();
return qRgb(ct.argb.red >> 8, ct.argb.green >> 8, ct.argb.blue >> 8);
}
*/
void QColor::setRgb(QRgb rgb)
{
- cspec = Rgb;
+ cspec = QColor::Rgb;
ct.argb.alpha = 0xffff;
ct.argb.red = qRed(rgb) * 0x101;
ct.argb.green = qGreen(rgb) * 0x101;
*/
int QColor::red() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().red();
return ct.argb.red >> 8;
}
void QColor::setRed(int red)
{
QCOLOR_INT_RANGE_CHECK("QColor::setRed", red);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgb(red, green(), blue(), alpha());
else
ct.argb.red = red * 0x101;
*/
int QColor::green() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().green();
return ct.argb.green >> 8;
}
void QColor::setGreen(int green)
{
QCOLOR_INT_RANGE_CHECK("QColor::setGreen", green);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgb(red(), green, blue(), alpha());
else
ct.argb.green = green * 0x101;
*/
int QColor::blue() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().blue();
return ct.argb.blue >> 8;
}
void QColor::setBlue(int blue)
{
QCOLOR_INT_RANGE_CHECK("QColor::setBlue", blue);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgb(red(), green(), blue, alpha());
else
ct.argb.blue = blue * 0x101;
*/
qreal QColor::redF() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().redF();
return ct.argb.red / qreal(USHRT_MAX);
}
void QColor::setRedF(qreal red)
{
QCOLOR_REAL_RANGE_CHECK("QColor::setRedF", red);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgbF(red, greenF(), blueF(), alphaF());
else
ct.argb.red = qRound(red * USHRT_MAX);
*/
qreal QColor::greenF() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().greenF();
return ct.argb.green / qreal(USHRT_MAX);
}
void QColor::setGreenF(qreal green)
{
QCOLOR_REAL_RANGE_CHECK("QColor::setGreenF", green);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgbF(redF(), green, blueF(), alphaF());
else
ct.argb.green = qRound(green * USHRT_MAX);
*/
qreal QColor::blueF() const
{
- if (cspec != Invalid && cspec != Rgb)
+ if (cspec != QColor::Invalid && cspec != QColor::Rgb)
return toRgb().blueF();
return ct.argb.blue / qreal(USHRT_MAX);
}
void QColor::setBlueF(qreal blue)
{
QCOLOR_REAL_RANGE_CHECK("QColor::setBlueF", blue);
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
setRgbF(redF(), greenF(), blue, alphaF());
else
ct.argb.blue = qRound(blue * USHRT_MAX);
*/
int QColor::hsvHue() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().hue();
return ct.ahsv.hue == USHRT_MAX ? -1 : ct.ahsv.hue / 100;
}
*/
int QColor::hsvSaturation() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().saturation();
return ct.ahsv.saturation >> 8;
}
*/
int QColor::value() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().value();
return ct.ahsv.value >> 8;
}
*/
qreal QColor::hsvHueF() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().hueF();
return ct.ahsv.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsv.hue / qreal(36000.0);
}
*/
qreal QColor::hsvSaturationF() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().saturationF();
return ct.ahsv.saturation / qreal(USHRT_MAX);
}
*/
qreal QColor::valueF() const
{
- if (cspec != Invalid && cspec != Hsv)
+ if (cspec != QColor::Invalid && cspec != QColor::Hsv)
return toHsv().valueF();
return ct.ahsv.value / qreal(USHRT_MAX);
}
/*!
- \since 4.6
-
- Returns the hue color component of this color.
-
- \sa getHslF(), getHsl()
-*/
-int QColor::hslHue() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().hslHue();
- return ct.ahsl.hue == USHRT_MAX ? -1 : ct.ahsl.hue / 100;
-}
-
-/*!
- \since 4.6
-
- Returns the saturation color component of this color.
-
- \sa saturationF(), getHsv(), {QColor#The HSV Color Model}{The HSV Color
- Model}
-*/
-int QColor::hslSaturation() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().hslSaturation();
- return ct.ahsl.saturation >> 8;
-}
-
-/*!
- \since 4.6
-
- Returns the lightness color component of this color.
-
- \sa lightnessF(), getHsl()
-*/
-int QColor::lightness() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().lightness();
- return ct.ahsl.lightness >> 8;
-}
-
-/*!
- \since 4.6
-
- Returns the hue color component of this color.
-
- \sa hue(), getHslF()
-*/
-qreal QColor::hslHueF() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().hslHueF();
- return ct.ahsl.hue == USHRT_MAX ? qreal(-1.0) : ct.ahsl.hue / qreal(36000.0);
-}
-
-/*!
- \since 4.6
-
- Returns the saturation color component of this color.
-
- \sa saturationF() getHslF()
-*/
-qreal QColor::hslSaturationF() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().hslSaturationF();
- return ct.ahsl.saturation / qreal(USHRT_MAX);
-}
-
-/*!
- \since 4.6
-
- Returns the lightness color component of this color.
-
- \sa value() getHslF()
-*/
-qreal QColor::lightnessF() const
-{
- if (cspec != Invalid && cspec != Hsl)
- return toHsl().lightnessF();
- return ct.ahsl.lightness / qreal(USHRT_MAX);
-}
-
-/*!
Create and returns an RGB QColor based on this color.
- \sa fromRgb(), convertTo(), isValid()
+ \sa fromRgb(), toHsv(), isValid()
*/
QColor QColor::toRgb() const
{
- if (!isValid() || cspec == Rgb)
+ if (!isValid() || cspec == QColor::Rgb) {
return *this;
+ }
QColor color;
- color.cspec = Rgb;
+ color.cspec = QColor::Rgb;
color.ct.argb.alpha = ct.argb.alpha;
switch (cspec) {
- case Hsv: {
+ case QColor::Hsv: {
if (ct.ahsv.saturation == 0 || ct.ahsv.hue == USHRT_MAX) {
// achromatic case
color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsv.value;
}
break;
}
- case Hsl: {
- if (ct.ahsl.saturation == 0 || ct.ahsl.hue == USHRT_MAX) {
- // achromatic case
- color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = ct.ahsl.lightness;
- } else if (ct.ahsl.lightness == 0) {
- // lightness 0
- color.ct.argb.red = color.ct.argb.green = color.ct.argb.blue = 0;
- } else {
- // chromatic case
- const qreal h = ct.ahsl.hue == 36000 ? 0 : ct.ahsl.hue / qreal(36000.);
- const qreal s = ct.ahsl.saturation / qreal(USHRT_MAX);
- const qreal l = ct.ahsl.lightness / qreal(USHRT_MAX);
-
- qreal temp2;
- if (l < qreal(0.5))
- temp2 = l * (qreal(1.0) + s);
- else
- temp2 = l + s - (l * s);
-
- const qreal temp1 = (qreal(2.0) * l) - temp2;
- qreal temp3[3] = { h + (qreal(1.0) / qreal(3.0)),
- h,
- h - (qreal(1.0) / qreal(3.0)) };
-
- for (int i = 0; i != 3; ++i) {
- if (temp3[i] < qreal(0.0))
- temp3[i] += qreal(1.0);
- else if (temp3[i] > qreal(1.0))
- temp3[i] -= qreal(1.0);
-
- const qreal sixtemp3 = temp3[i] * qreal(6.0);
- if (sixtemp3 < qreal(1.0))
- color.ct.array[i+1] = qRound((temp1 + (temp2 - temp1) * sixtemp3) * USHRT_MAX);
- else if ((temp3[i] * qreal(2.0)) < qreal(1.0))
- color.ct.array[i+1] = qRound(temp2 * USHRT_MAX);
- else if ((temp3[i] * qreal(3.0)) < qreal(2.0))
- color.ct.array[i+1] = qRound((temp1 + (temp2 -temp1) * (qreal(2.0) /qreal(3.0) - temp3[i]) * qreal(6.0)) * USHRT_MAX);
- else
- color.ct.array[i+1] = qRound(temp1 * USHRT_MAX);
- }
- color.ct.argb.red = color.ct.argb.red == 1 ? 0 : color.ct.argb.red;
- color.ct.argb.green = color.ct.argb.green == 1 ? 0 : color.ct.argb.green;
- color.ct.argb.blue = color.ct.argb.blue == 1 ? 0 : color.ct.argb.blue;
- }
- break;
- }
default:
break;
}
/*!
Creates and returns an HSV QColor based on this color.
- \sa fromHsv(), convertTo(), isValid(), {QColor#The HSV Color
+ \sa fromHsv(), toRgb(), isValid(), {QColor#The HSV Color
Model}{The HSV Color Model}
*/
QColor QColor::toHsv() const
{
- if (!isValid() || cspec == Hsv)
+ if (!isValid() || cspec == QColor::Hsv)
return *this;
- if (cspec != Rgb)
+ if (cspec != QColor::Rgb)
return toRgb().toHsv();
QColor color;
- color.cspec = Hsv;
+ color.cspec = QColor::Hsv;
color.ct.ahsv.alpha = ct.argb.alpha;
const qreal r = ct.argb.red / qreal(USHRT_MAX);
}
/*!
- Creates and returns an HSL QColor based on this color.
-
- \sa fromHsl(), convertTo(), isValid()
-*/
-QColor QColor::toHsl() const
-{
- if (!isValid() || cspec == Hsl)
- return *this;
-
- if (cspec != Rgb)
- return toRgb().toHsl();
-
- QColor color;
- color.cspec = Hsl;
- color.ct.ahsl.alpha = ct.argb.alpha;
-
- const qreal r = ct.argb.red / qreal(USHRT_MAX);
- const qreal g = ct.argb.green / qreal(USHRT_MAX);
- const qreal b = ct.argb.blue / qreal(USHRT_MAX);
- const qreal max = Q_MAX_3(r, g, b);
- const qreal min = Q_MIN_3(r, g, b);
- const qreal delta = max - min;
- const qreal delta2 = max + min;
- const qreal lightness = qreal(0.5) * delta2;
- color.ct.ahsl.lightness = qRound(lightness * USHRT_MAX);
- if (qFuzzyIsNull(delta)) {
- // achromatic case, hue is undefined
- color.ct.ahsl.hue = USHRT_MAX;
- color.ct.ahsl.saturation = 0;
- } else {
- // chromatic case
- qreal hue = 0;
- if (lightness < qreal(0.5))
- color.ct.ahsl.saturation = qRound((delta / delta2) * USHRT_MAX);
- else
- color.ct.ahsl.saturation = qRound((delta / (qreal(2.0) - delta2)) * USHRT_MAX);
- if (qFuzzyCompare(r, max)) {
- hue = ((g - b) /delta);
- } else if (qFuzzyCompare(g, max)) {
- hue = (qreal(2.0) + (b - r) / delta);
- } else if (qFuzzyCompare(b, max)) {
- hue = (qreal(4.0) + (r - g) / delta);
- } else {
- Q_ASSERT_X(false, "QColor::toHsv", "internal error");
- }
- hue *= qreal(60.0);
- if (hue < qreal(0.0))
- hue += qreal(360.0);
- color.ct.ahsl.hue = qRound(hue * 100);
- }
-
- return color;
-}
-
-QColor QColor::convertTo(QColor::Spec colorSpec) const
-{
- if (colorSpec == cspec)
- return *this;
- switch (colorSpec) {
- case Rgb:
- return toRgb();
- case Hsv:
- return toHsv();
- case Hsl:
- return toHsl();
- case Invalid:
- break;
- }
- return QColor(); // must be invalid
-}
-
-
-/*!
Static convenience function that returns a QColor constructed from the
given QRgb value \a rgb.
}
QColor color;
- color.cspec = Rgb;
+ color.cspec = QColor::Rgb;
color.ct.argb.alpha = a * 0x101;
color.ct.argb.red = r * 0x101;
color.ct.argb.green = g * 0x101;
}
QColor color;
- color.cspec = Rgb;
+ color.cspec = QColor::Rgb;
color.ct.argb.alpha = qRound(a * USHRT_MAX);
color.ct.argb.red = qRound(r * USHRT_MAX);
color.ct.argb.green = qRound(g * USHRT_MAX);
}
QColor color;
- color.cspec = Hsv;
+ color.cspec = QColor::Hsv;
color.ct.ahsv.alpha = a * 0x101;
color.ct.ahsv.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
color.ct.ahsv.saturation = s * 0x101;
}
QColor color;
- color.cspec = Hsv;
+ color.cspec = QColor::Hsv;
color.ct.ahsv.alpha = qRound(a * USHRT_MAX);
color.ct.ahsv.hue = h == qreal(-1.0) ? USHRT_MAX : qRound(h * 36000);
color.ct.ahsv.saturation = qRound(s * USHRT_MAX);
}
/*!
- \since 4.6
-
- Static convenience function that returns a QColor constructed from the HSV
- color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
- (alpha-channel, i.e. transparency).
-
- The value of \a s, \a l, and \a a must all be in the range 0-255; the value
- of \a h must be in the range 0-359.
-
- \sa toHsl(), fromHslF(), isValid()
-*/
-QColor QColor::fromHsl(int h, int s, int l, int a)
-{
- if (Q_UNLIKELY(((h < 0 || h >= 360) && h != -1)
- || s < 0 || s > 255
- || l < 0 || l > 255
- || a < 0 || a > 255)) {
- qWarning("QColor::fromHsv: HSV parameters out of range");
- return QColor();
- }
-
- QColor color;
- color.cspec = Hsl;
- color.ct.ahsl.alpha = a * 0x101;
- color.ct.ahsl.hue = h == -1 ? USHRT_MAX : (h % 360) * 100;
- color.ct.ahsl.saturation = s * 0x101;
- color.ct.ahsl.lightness = l * 0x101;
- return color;
-}
-
-/*!
- \overload
- \since 4.6
-
- Static convenience function that returns a QColor constructed from the HSV
- color values, \a h (hue), \a s (saturation), \a l (lightness), and \a a
- (alpha-channel, i.e. transparency).
-
- All the values must be in the range 0.0-1.0.
-
- \sa toHsl(), fromHsl(), isValid()
-*/
-QColor QColor::fromHslF(qreal h, qreal s, qreal l, qreal a)
-{
- if (Q_UNLIKELY(((h < qreal(0.0) || h > qreal(1.0)) && h != qreal(-1.0))
- || (s < qreal(0.0) || s > qreal(1.0))
- || (l < qreal(0.0) || l > qreal(1.0))
- || (a < qreal(0.0) || a > qreal(1.0)))) {
- qWarning("QColor::fromHsvF: HSV parameters out of range");
- return QColor();
- }
-
- QColor color;
- color.cspec = Hsl;
- color.ct.ahsl.alpha = qRound(a * USHRT_MAX);
- color.ct.ahsl.hue = (h == qreal(-1.0)) ? USHRT_MAX : qRound(h * 36000);
- if (color.ct.ahsl.hue == 36000)
- color.ct.ahsl.hue = 0;
- color.ct.ahsl.saturation = qRound(s * USHRT_MAX);
- color.ct.ahsl.lightness = qRound(l * USHRT_MAX);
- return color;
-}
-
-/*!
\fn QColor QColor::lighter(int factor) const
\since 4.3
*/
QColor QColor::light(int factor) const
{
- if (factor <= 0) // invalid lightness factor
+ if (cspec == QColor::Invalid) {
return *this;
- else if (factor < 100) // makes color darker
+ } else if (factor <= 0) {
+ // invalid lightness factor
+ return *this;
+ } else if (factor < 100) {
+ // makes color darker
return darker(10000 / factor);
+ }
QColor hsv = toHsv();
int s = hsv.ct.ahsv.saturation;
hsv.ct.ahsv.value = v;
// convert back to same color spec as original color
- return hsv.convertTo(cspec);
+ switch (cspec) {
+ case QColor::Rgb: {
+ return hsv.toRgb();
+ }
+ case QColor::Hsv: {
+ return hsv;
+ }
+ default: {
+ Q_ASSERT(false);
+ return QColor();
+ }
+ }
+ Q_UNREACHABLE();
}
/*!
*/
QColor QColor::dark(int factor) const
{
- if (factor <= 0) // invalid darkness factor
+ if (cspec == QColor::Invalid) {
return *this;
- else if (factor < 100) // makes color lighter
+ } else if (factor <= 0) {
+ // invalid darkness factor
+ return *this;
+ } else if (factor < 100) {
+ // makes color lighter
return lighter(10000 / factor);
+ }
QColor hsv = toHsv();
hsv.ct.ahsv.value = (hsv.ct.ahsv.value * 100) / factor;
// convert back to same color spec as original color
- return hsv.convertTo(cspec);
+ switch (cspec) {
+ case QColor::Rgb: {
+ return hsv.toRgb();
+ }
+ case QColor::Hsv: {
+ return hsv;
+ }
+ default: {
+ Q_ASSERT(false);
+ return QColor();
+ }
+ }
+ Q_UNREACHABLE();
}
/*!
*/
bool QColor::operator==(const QColor &color) const
{
- if (cspec == Hsl && cspec == color.cspec) {
- return (ct.argb.alpha == color.ct.argb.alpha
- && ((((ct.ahsl.hue % 36000) == (color.ct.ahsl.hue % 36000)))
- || (ct.ahsl.hue == color.ct.ahsl.hue))
- && (qAbs(ct.ahsl.saturation - color.ct.ahsl.saturation) < 50
- || ct.ahsl.lightness == 0
- || color.ct.ahsl.lightness == 0
- || ct.ahsl.lightness == USHRT_MAX
- || color.ct.ahsl.lightness == USHRT_MAX)
- && (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
- } else {
- return (cspec == color.cspec
- && ct.argb.alpha == color.ct.argb.alpha
- && (((cspec == QColor::Hsv)
- && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
- || (ct.ahsv.hue == color.ct.ahsv.hue))
- && ct.argb.green == color.ct.argb.green
- && ct.argb.blue == color.ct.argb.blue);
- }
+ return (cspec == color.cspec
+ && ct.argb.alpha == color.ct.argb.alpha
+ && (((cspec == QColor::Hsv)
+ && ((ct.ahsv.hue % 36000) == (color.ct.ahsv.hue % 36000)))
+ || (ct.ahsv.hue == color.ct.ahsv.hue))
+ && ct.argb.green == color.ct.argb.green
+ && ct.argb.blue == color.ct.argb.blue);
}
/*!
*/
void QColor::invalidate()
{
- cspec = Invalid;
+ cspec = QColor::Invalid;
ct.argb.alpha = USHRT_MAX;
ct.argb.red = 0;
ct.argb.green = 0;
dbg.nospace() << "QColor(ARGB " << c.alphaF() << ", " << c.redF() << ", " << c.greenF() << ", " << c.blueF() << ')';
else if (c.spec() == QColor::Hsv)
dbg.nospace() << "QColor(AHSV " << c.alphaF() << ", " << c.hueF() << ", " << c.saturationF() << ", " << c.valueF() << ')';
- else if (c.spec() == QColor::Hsl)
- dbg.nospace() << "QColor(AHSL " << c.alphaF() << ", " << c.hslHueF() << ", " << c.hslSaturationF() << ", " << c.lightnessF() << ')';
return dbg.space();
#else
*/
/*!
- \fn QColor QColor::convertTo(Spec colorSpec) const
-
- Creates a copy of \e this color in the format specified by \a colorSpec.
-
- \sa spec(), toHsv(), toRgb(), isValid()
-*/
-
-/*!
\typedef QRgb
\relates QColor
void getHsv();
void setHsv();
- void hueHsl();
- void saturationHsl();
- void lightness();
-
- void getHsl();
- void setHsl();
-
void toRgb_data();
void toRgb();
void toRgbNonDestructive();
void toHsv();
void toHsvNonDestructive();
- void toHsl_data();
- void toHsl();
- void toHslNonDestructive();
-
- void convertTo();
-
void fromRgb();
void fromHsv();
- void fromHsl();
void light();
void dark();
void specConstructor_data();
void specConstructor();
- void achromaticHslHue();
-
#ifdef Q_WS_X11
void allowX11ColorNames();
void setallowX11ColorNames();
QColor hsv = QColor::fromHsv(0, 0, 0);
QCOMPARE(hsv.spec(), QColor::Hsv);
- QColor hsl = QColor::fromHsl(0, 0, 0, 0);
- QCOMPARE(hsl.spec(), QColor::Hsl);
-
}
void tst_QColor::alpha()
}
}
-void tst_QColor::hueHsl()
-{ DEPENDS_ON(setHsl()); }
-
-void tst_QColor::saturationHsl()
-{ DEPENDS_ON(setHsl()); }
-
-void tst_QColor::lightness()
-{ DEPENDS_ON(setHsl()); }
-
-void tst_QColor::getHsl()
-{ DEPENDS_ON(setHsl()); }
-
-void tst_QColor::setHsl()
-{
- QColor color;
-
- for (int A = 0; A <= USHRT_MAX; ++A) {
- {
- // 0-255
- int a = A >> 8;
- color.setHsl(0, 0, 0, a);
- QCOMPARE(color.alpha(), a);
-
- int h, s, l, a2;
- color.getHsv(&h, &s, &l, &a2);
- QCOMPARE(a2, a);
- }
-
- {
- // 0.0-1.0
- qreal a = A / qreal(USHRT_MAX);
- color.setHslF(0.0, 0.0, 0.0, a); QCOMPARE(color.alphaF(), a);
-
- qreal h, s, l, a2;
- color.getHslF(&h, &s, &l, &a2);
- QCOMPARE(a2, a);
- }
- }
-
- for (int H = 0; H < 36000; ++H) {
- {
- // 0-255
- int h = H / 100;
-
- color.setHsl(h, 0, 0, 0);
- QCOMPARE(color.hslHue(), h);
-
- int h2, s, l, a;
- color.getHsl(&h2, &s, &l, &a);
- QCOMPARE(h2, h);
- }
-
- {
- // 0.0-1.0
- qreal h = H / 36000.0;
- color.setHslF(h, 0.0, 0.0, 0.0);
- QCOMPARE(color.hslHueF(), h);
-
- qreal h2, s, l, a;
- color.getHslF(&h2, &s, &l, &a);
- QCOMPARE(h2, h);
- }
- }
-
- for (int S = 0; S <= USHRT_MAX; ++S) {
- {
- // 0-255
- int s = S >> 8;
- color.setHsl(0, s, 0, 0);
- QCOMPARE(color.hslSaturation(), s);
-
- int h, s2, l, a;
- color.getHsl(&h, &s2, &l, &a);
- QCOMPARE(s2, s);
- }
-
- {
- // 0.0-1.0
- qreal s = S / qreal(USHRT_MAX);
- color.setHslF(0.0, s, 0.0, 0.0);
- QCOMPARE(color.hslSaturationF(), s);
-
- qreal h, s2, l, a;
- color.getHslF(&h, &s2, &l, &a);
- QCOMPARE(s2, s);
- }
- }
-
- for (int L = 0; L <= USHRT_MAX; ++L) {
- {
- // 0-255
- int l = L >> 8;
- color.setHsl(0, 0, l, 0);
- QCOMPARE(color.lightness(), l);
-
- int h, s, l2, a;
- color.getHsl(&h, &s, &l2, &a);
- QCOMPARE(l2, l);
- }
-
- {
- // 0.0-1.0
- qreal l = L / qreal(USHRT_MAX);
- color.setHslF(0.0, 0.0, l, 0.0);
- QCOMPARE(color.lightnessF(), l);
-
- qreal h, s, l2, a;
- color.getHslF(&h, &s, &l2, &a);
- QCOMPARE(l2, l);
- }
- }
-}
-
void tst_QColor::toRgb_data()
{
QTest::addColumn<QColor>("expectedColor");
QTest::addColumn<QColor>("hsvColor");
- QTest::addColumn<QColor>("hslColor");
QTest::newRow("black")
<< QColor::fromRgbF(0.0, 0.0, 0.0)
- << QColor::fromHsvF(-1.0, 0.0, 0.0)
- << QColor::fromHslF(-1.0, 0.0, 0.0);
+ << QColor::fromHsvF(-1.0, 0.0, 0.0);
QTest::newRow("white")
<< QColor::fromRgbF(1.0, 1.0, 1.0)
- << QColor::fromHsvF(-1.0, 0.0, 1.0)
- << QColor::fromHslF(-1.0, 0.0, 1.0);
+ << QColor::fromHsvF(-1.0, 0.0, 1.0);
QTest::newRow("red")
<< QColor::fromRgbF(1.0, 0.0, 0.0)
- << QColor::fromHsvF(0.0, 1.0, 1.0)
- << QColor::fromHslF(0.0, 1.0, 0.5, 1.0);
+ << QColor::fromHsvF(0.0, 1.0, 1.0);
QTest::newRow("green")
<< QColor::fromRgbF(0.0, 1.0, 0.0)
- << QColor::fromHsvF(0.33333, 1.0, 1.0)
- << QColor::fromHslF(0.33333, 1.0, 0.5);
+ << QColor::fromHsvF(0.33333, 1.0, 1.0);
QTest::newRow("blue")
<< QColor::fromRgbF(0.0, 0.0, 1.0)
- << QColor::fromHsvF(0.66667, 1.0, 1.0)
- << QColor::fromHslF(0.66667, 1.0, 0.5);
+ << QColor::fromHsvF(0.66667, 1.0, 1.0);
QTest::newRow("cyan")
<< QColor::fromRgbF(0.0, 1.0, 1.0)
- << QColor::fromHsvF(0.5, 1.0, 1.0)
- << QColor::fromHslF(0.5, 1.0, 0.5);
+ << QColor::fromHsvF(0.5, 1.0, 1.0);
QTest::newRow("magenta")
<< QColor::fromRgbF(1.0, 0.0, 1.0)
- << QColor::fromHsvF(0.83333, 1.0, 1.0)
- << QColor::fromHslF(0.83333, 1.0, 0.5);
+ << QColor::fromHsvF(0.83333, 1.0, 1.0);
QTest::newRow("yellow")
<< QColor::fromRgbF(1.0, 1.0, 0.0)
- << QColor::fromHsvF(0.16667, 1.0, 1.0)
- << QColor::fromHslF(0.16667, 1.0, 0.5);
+ << QColor::fromHsvF(0.16667, 1.0, 1.0);
QTest::newRow("gray")
<< QColor::fromRgbF(0.6431375, 0.6431375, 0.6431375)
- << QColor::fromHsvF(-1.0, 0.0, 0.6431375)
- << QColor::fromHslF(-1.0, 0.0, 0.6431375);
+ << QColor::fromHsvF(-1.0, 0.0, 0.6431375);
// ### add colors using the 0-255 functions
}
QFETCH(QColor, expectedColor);
QFETCH(QColor, hsvColor);
- QFETCH(QColor, hslColor);
QCOMPARE(hsvColor.toRgb(), expectedColor);
- QCOMPARE(hslColor.toRgb(), expectedColor);
}
{
QTest::addColumn<QColor>("expectedColor");
QTest::addColumn<QColor>("rgbColor");
- QTest::addColumn<QColor>("hslColor");
QTest::newRow("data0")
<< QColor::fromHsv(300, 255, 255)
- << QColor(255, 0, 255)
- << QColor::fromHslF(300./360., 1., 0.5, 1.0);
+ << QColor(255, 0, 255);
QTest::newRow("data1")
<< QColor::fromHsvF(1., 1., 1., 1.)
- << QColor(255, 0, 0, 255)
- << QColor::fromHsvF(1., 1., 1., 1.);
+ << QColor(255, 0, 0, 255);
}
void tst_QColor::toRgbNonDestructive()
QFETCH(QColor, expectedColor);
QFETCH(QColor, rgbColor);
- QFETCH(QColor, hslColor);
QCOMPARE(rgbColor.toHsv(), expectedColor);
- QCOMPARE(hslColor.toHsv(), expectedColor);
}
void tst_QColor::toHsvNonDestructive()
QCOMPARE(aColor, aColor.toHsv());
}
-void tst_QColor::toHsl_data()
-{
- QTest::addColumn<QColor>("expectedColor");
- QTest::addColumn<QColor>("hsvColor");
- QTest::addColumn<QColor>("rgbColor");
-
-
- QTest::newRow("data0")
- << QColor::fromHslF(300./360., 1., 0.5, 1.0)
- << QColor::fromHsv(300, 255, 255)
- << QColor(255, 0, 255);
-
- QTest::newRow("data1")
- << QColor::fromHslF(1., 1., 0.5, 1.0)
- << QColor::fromHsvF(1., 1., 1., 1.)
- << QColor(255, 0, 0, 255);
-}
-
-void tst_QColor::toHsl()
-{
- // invalid should remain invalid
- QVERIFY(!QColor().toHsl().isValid());
-
- QFETCH(QColor, expectedColor);
- QFETCH(QColor, rgbColor);
- QFETCH(QColor, hsvColor);
-
- QCOMPARE(rgbColor.toHsl(), expectedColor);
- QCOMPARE(hsvColor.toHsl(), expectedColor);
-
-}
-
-
-void tst_QColor::toHslNonDestructive()
-{
- QColor aColor = QColor::fromHslF(0.11, 0.22, 0.33, 0.44);
- QCOMPARE(aColor, aColor.toHsl());
-}
-
-
-void tst_QColor::convertTo()
-{
- QColor color(Qt::black);
-
- QColor rgb = color.convertTo(QColor::Rgb);
- QVERIFY(rgb.spec() == QColor::Rgb);
-
- QColor hsv = color.convertTo(QColor::Hsv);
- QVERIFY(hsv.spec() == QColor::Hsv);
-
- QColor hsl = color.convertTo(QColor::Hsl);
- QVERIFY(hsl.spec() == QColor::Hsl);
-
- QColor invalid = color.convertTo(QColor::Invalid);
- QVERIFY(invalid.spec() == QColor::Invalid);
-
- DEPENDS_ON(toRgb());
- DEPENDS_ON(toHsv());
- DEPENDS_ON(toHsl());
-}
-
void tst_QColor::fromRgb()
{ DEPENDS_ON(convertTo()); }
void tst_QColor::fromHsv()
{ DEPENDS_ON(convertTo()); }
-void tst_QColor::fromHsl()
-{ DEPENDS_ON(convertTo()); }
-
void tst_QColor::light()
{
QColor gray(Qt::gray);
QCOMPARE(color.spec(), spec);
}
-void tst_QColor::achromaticHslHue()
-{
- QColor color = Qt::black;
-
- QColor hsl = color.toHsl();
- QCOMPARE(hsl.hslHue(), -1);
-}
-
#ifdef Q_WS_X11
void tst_QColor::allowX11ColorNames()
{