Search code examples
c++qtqcolor

Compare the color of two QColor objects


I'm implementing tests for my Qt application using an 'internal test library'. The problem is that the library does not provide an option to compare two QColor objects.

Is that correct to use the following code to compare the color of two QColor objects?

void MyClass::compare(QColor color1, QColor color2)
{
    ASSERT_EQ(color1.red(), color2.red());
    ASSERT_EQ(color1.green(), color2.green());
    ASSERT_EQ(color1.blue(), color2.blue());
    ASSERT_EQ(color1.alpha(), color2.alpha());
}

Or rather, is it enough to compare the red, green, blue and alpha channels to consider the two colors equal?

Note: in this case, I need to know exactly what channel 'does not match', so I can't just use the == operator.


Solution

  • QColor has operator ==. Just try if(color1 == color2)...

    Documentation here. (As you can see it compares RGB and alpha.)

    http://doc.qt.io/qt-4.8/qcolor.html#operator-eq-eq