Skip to content

Commit

Permalink
Fix ExtendedRgb and Rgb encoding comparisons
Browse files Browse the repository at this point in the history
ExtendedRgb should be treated as Rgb as it can be an automatic upgrade.

Change-Id: I2942a1067ed5cacb2f60f303f467887cb44c36dd
Reviewed-by: Eirik Aavitsland <[email protected]>
(cherry picked from commit d80a98d)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
  • Loading branch information
Allan Sandfeld Jensen authored and Qt Cherry-pick Bot committed May 28, 2020
1 parent 6e695c0 commit cb34e43
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/gui/painting/qcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2945,6 +2945,8 @@ QColor &QColor::operator=(Qt::GlobalColor color) noexcept
Returns \c true if this color has the same color specification and component values as \a color;
otherwise returns \c false.
ExtendedRgb and Rgb specifications are considered matching in this context.
\sa spec()
*/
bool QColor::operator==(const QColor &color) const noexcept
Expand All @@ -2958,6 +2960,12 @@ bool QColor::operator==(const QColor &color) const noexcept
|| ct.ahsl.lightness == USHRT_MAX
|| color.ct.ahsl.lightness == USHRT_MAX)
&& (qAbs(ct.ahsl.lightness - color.ct.ahsl.lightness)) < 50);
} else if ((cspec == ExtendedRgb || color.cspec == ExtendedRgb) &&
(cspec == color.cspec || cspec == Rgb || color.cspec == Rgb)) {
return qFuzzyCompare(alphaF(), color.alphaF())
&& qFuzzyCompare(redF(), color.redF())
&& qFuzzyCompare(greenF(), color.greenF())
&& qFuzzyCompare(blueF(), color.blueF());
} else {
return (cspec == color.cspec
&& ct.argb.alpha == color.ct.argb.alpha
Expand All @@ -2974,6 +2982,8 @@ bool QColor::operator==(const QColor &color) const noexcept
Returns \c true if this color has different color specification or component values from
\a color; otherwise returns \c false.
ExtendedRgb and Rgb specifications are considered matching in this context.
\sa spec()
*/
bool QColor::operator!=(const QColor &color) const noexcept
Expand Down
20 changes: 20 additions & 0 deletions tests/auto/gui/painting/qcolor/tst_qcolor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ private slots:

void achromaticHslHue();

void equality();

void premultiply();
void unpremultiply_sse4();
void qrgba64();
Expand Down Expand Up @@ -1645,6 +1647,24 @@ void tst_QColor::achromaticHslHue()
QCOMPARE(hsl.hslHue(), -1);
}

void tst_QColor::equality()
{
QColor red = Qt::red;
QColor black = Qt::black;

QCOMPARE(red, red);
QCOMPARE(black, black);
QVERIFY(red != black);

// Encodings must match
QVERIFY(red != red.toHsv());
QVERIFY(black.toHsl() != black);

// Except for ExtendedRgb and Rgb, as it can be an automatic upgrade.
QCOMPARE(red, red.toExtendedRgb());
QCOMPARE(black.toExtendedRgb(), black);
}

void tst_QColor::premultiply()
{
// Tests that qPremultiply(qUnpremultiply(x)) returns x.
Expand Down

0 comments on commit cb34e43

Please sign in to comment.