diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-04-22 16:20:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2022-05-26 02:08:38 +0200 |
commit | f4b7401cdb809f02c56ac739ae0507c7427ae196 (patch) | |
tree | 9c8c5458e8a7c92f5c26b557338368d66d5b2881 | |
parent | fdd7227c67fd54456b8c0f2348630ef04b4d2a5e (diff) |
Add test of QColorTransform
Before it was only tested indirectly as part of the qcolorspace
tests. This also fixes a rounding for maximum RGB64 values.
Pick-to: 6.3
Change-Id: I6106e662c0f9d00c0b3a0c13213cb051ea39e14e
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
-rw-r--r-- | src/gui/painting/qcolortransform.cpp | 2 | ||||
-rw-r--r-- | tests/auto/gui/painting/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/gui/painting/qcolortransform/CMakeLists.txt | 10 | ||||
-rw-r--r-- | tests/auto/gui/painting/qcolortransform/tst_qcolortransform.cpp | 226 |
4 files changed, 238 insertions, 1 deletions
diff --git a/src/gui/painting/qcolortransform.cpp b/src/gui/painting/qcolortransform.cpp index ff7229e060..71b07f30a5 100644 --- a/src/gui/painting/qcolortransform.cpp +++ b/src/gui/painting/qcolortransform.cpp @@ -163,7 +163,7 @@ QRgba64 QColorTransform::map(QRgba64 rgba64) const c.z = d->colorSpaceOut->trc[2].applyInverse(c.z); } - return QRgba64::fromRgba64(c.x * 65535, c.y * 65535, c.z * 65535, rgba64.alpha()); + return QRgba64::fromRgba64(c.x * 65535.f + 0.5f, c.y * 65535.f + 0.5f, c.z * 65535.f + 0.5f, rgba64.alpha()); } /*! diff --git a/tests/auto/gui/painting/CMakeLists.txt b/tests/auto/gui/painting/CMakeLists.txt index 4f933139ca..9aa99ea878 100644 --- a/tests/auto/gui/painting/CMakeLists.txt +++ b/tests/auto/gui/painting/CMakeLists.txt @@ -17,6 +17,7 @@ add_subdirectory(qpolygon) # QTBUG-87669 # special case if(NOT ANDROID) add_subdirectory(qcolorspace) + add_subdirectory(qcolortransform) endif() if(QT_FEATURE_private_tests) add_subdirectory(qpathclipper) diff --git a/tests/auto/gui/painting/qcolortransform/CMakeLists.txt b/tests/auto/gui/painting/qcolortransform/CMakeLists.txt new file mode 100644 index 0000000000..008ac92905 --- /dev/null +++ b/tests/auto/gui/painting/qcolortransform/CMakeLists.txt @@ -0,0 +1,10 @@ +##################################################################### +## tst_qcolortransform Test: +##################################################################### + +qt_internal_add_test(tst_qcolortransform + SOURCES + tst_qcolortransform.cpp + PUBLIC_LIBRARIES + Qt::Gui +) diff --git a/tests/auto/gui/painting/qcolortransform/tst_qcolortransform.cpp b/tests/auto/gui/painting/qcolortransform/tst_qcolortransform.cpp new file mode 100644 index 0000000000..c09d3fc11e --- /dev/null +++ b/tests/auto/gui/painting/qcolortransform/tst_qcolortransform.cpp @@ -0,0 +1,226 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + + +#include <QTest> + +#include <qcolorspace.h> +#include <qcolortransform.h> + +class tst_QColorTransform : public QObject +{ + Q_OBJECT + +public: + tst_QColorTransform(); + +private slots: + void mapRGB32_data(); + void mapRGB32(); + void mapRGB64_data(); + void mapRGB64(); + void mapQColor_data(); + void mapQColor(); +}; + +tst_QColorTransform::tst_QColorTransform() +{ } + + +void tst_QColorTransform::mapRGB32_data() +{ + QTest::addColumn<QColorTransform>("transform"); + QTest::addColumn<bool>("isIdentity"); + QTest::addColumn<bool>("sharesRed"); + + QTest::newRow("default") << QColorTransform() << true << true; + QTest::newRow("sRGB to Linear") << QColorSpace(QColorSpace::SRgb).transformationToColorSpace(QColorSpace::SRgbLinear) << false << true; + QTest::newRow("AdobeRGB to sRGB") << QColorSpace(QColorSpace::AdobeRgb).transformationToColorSpace(QColorSpace::SRgb) << false << true; + QTest::newRow("Linear AdobeRGB to Linear sRGB") + << QColorSpace(QColorSpace::AdobeRgb).withTransferFunction(QColorSpace::TransferFunction::Linear).transformationToColorSpace( + QColorSpace::SRgb) + << false << true; + QTest::newRow("sRgb to AdobeRGB") << QColorSpace(QColorSpace::SRgb).transformationToColorSpace(QColorSpace::AdobeRgb) << false << true; + QTest::newRow("DP3 to sRGB") << QColorSpace(QColorSpace::DisplayP3).transformationToColorSpace(QColorSpace::SRgb) << false << false; + QTest::newRow("DP3 to Linear DP3") + << QColorSpace(QColorSpace::DisplayP3).transformationToColorSpace( + QColorSpace(QColorSpace::DisplayP3).withTransferFunction(QColorSpace::TransferFunction::Linear)) + << false << false; + QTest::newRow("Linear DP3 to Linear sRGB") + << QColorSpace(QColorSpace::DisplayP3).withTransferFunction(QColorSpace::TransferFunction::Linear).transformationToColorSpace( + QColorSpace::SRgb) + << false << false; +} + +void tst_QColorTransform::mapRGB32() +{ + QFETCH(QColorTransform, transform); + QFETCH(bool, isIdentity); + QFETCH(bool, sharesRed); + // Do basic sanity tests of conversions between similar sane color spaces + + QRgb testColor = qRgb(32, 64, 128); + QRgb result = transform.map(testColor); + QVERIFY(qRed(result) < qGreen(result)); + QVERIFY(qGreen(result) < qBlue(result)); + QCOMPARE(qAlpha(result), 255); + if (isIdentity) + QVERIFY(result == testColor); + else + QVERIFY(result != testColor); + + testColor = qRgb(128, 64, 32); + result = transform.map(testColor); + QVERIFY(qRed(result) > qGreen(result)); + QVERIFY(qGreen(result) > qBlue(result)); + QCOMPARE(qAlpha(result), 255); + if (isIdentity) + QVERIFY(result == testColor); + else + QVERIFY(result != testColor); + + testColor = qRgba(15, 31, 63, 128); + result = transform.map(testColor); + QVERIFY(qRed(result) < qGreen(result)); + QVERIFY(qGreen(result) < qBlue(result)); + QCOMPARE(qAlpha(result), 128); + if (isIdentity) + QVERIFY(result == testColor); + else + QVERIFY(result != testColor); + + testColor = qRgb(0, 0, 0); + result = transform.map(testColor); + QCOMPARE(qRed(result), 0); + QCOMPARE(qGreen(result), 0); + QCOMPARE(qBlue(result), 0); + QCOMPARE(qAlpha(result), 255); + + testColor = qRgb(255, 255, 255); + result = transform.map(testColor); + QCOMPARE(qRed(result), 255); + QCOMPARE(qGreen(result), 255); + QCOMPARE(qBlue(result), 255); + QCOMPARE(qAlpha(result), 255); + + testColor = qRgb(255, 255, 0); + result = transform.map(testColor); + QCOMPARE(qAlpha(result), 255); + if (sharesRed) + QCOMPARE(qRed(result), 255); + + testColor = qRgb(0, 255, 255); + result = transform.map(testColor); + QCOMPARE(qBlue(result), 255); + QCOMPARE(qAlpha(result), 255); +} + +void tst_QColorTransform::mapRGB64_data() +{ + mapRGB32_data(); +} + +void tst_QColorTransform::mapRGB64() +{ + QFETCH(QColorTransform, transform); + QFETCH(bool, isIdentity); + QFETCH(bool, sharesRed); + + QRgba64 testColor = QRgba64::fromRgba(128, 64, 32, 255); + QRgba64 result = transform.map(testColor); + QVERIFY(result.red() > result.green()); + QVERIFY(result.green() > result.blue()); + QCOMPARE(result.alpha(), 0xffff); + if (isIdentity) + QVERIFY(result == testColor); + else + QVERIFY(result != testColor); + + testColor = QRgba64::fromRgba64(0, 0, 0, 0xffff); + result = transform.map(testColor); + QCOMPARE(result, testColor); + + testColor = QRgba64::fromRgba64(0xffff, 0xffff, 0xffff, 0xffff); + result = transform.map(testColor); + QCOMPARE(result, testColor); + + testColor = QRgba64::fromRgba64(0xffff, 0xffff, 0, 0xffff); + result = transform.map(testColor); + QCOMPARE(result.alpha(), 0xffff); + if (sharesRed) + QCOMPARE(result.red(), 0xffff); + + testColor = QRgba64::fromRgba64(0, 0xffff, 0xffff, 0xffff); + result = transform.map(testColor); + QCOMPARE(result.blue(), 0xffff); + QCOMPARE(result.alpha(), 0xffff); +} + +void tst_QColorTransform::mapQColor_data() +{ + mapRGB32_data(); +} + +void tst_QColorTransform::mapQColor() +{ + QFETCH(QColorTransform, transform); + QFETCH(bool, isIdentity); + QFETCH(bool, sharesRed); + + QColor testColor(32, 64, 128); + QColor result = transform.map(testColor); + QVERIFY(result.redF() < result.greenF()); + QVERIFY(result.greenF() < result.blueF()); + QCOMPARE(result.alphaF(), 1.0f); + if (isIdentity) + QVERIFY(result == testColor); + else + QVERIFY(result != testColor); + + testColor = Qt::black; + result = transform.map(testColor); + QCOMPARE(result, testColor); + + testColor = Qt::white; + result = transform.map(testColor); + QCOMPARE(result, testColor); + + testColor = QColor(255, 255, 0); + result = transform.map(testColor); + QCOMPARE(result.alphaF(), 1); + if (sharesRed) + QVERIFY(result.redF() >= 1.0f); + + testColor = QColor(0, 255, 255); + result = transform.map(testColor); + QCOMPARE(result.alphaF(), 1.0f); + QVERIFY(result.blueF() >= 1.0f); +} + +QTEST_MAIN(tst_QColorTransform) +#include "tst_qcolortransform.moc" |