summaryrefslogtreecommitdiffstats
path: root/tests/auto/gui/painting/qcolortransform/tst_qcolortransform.cpp
blob: c2205a02d1780fb5667bfd46012c47119b4fdb21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/****************************************************************************
**
** 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();

    void transformIsIdentity();
};

tst_QColorTransform::tst_QColorTransform()
{ }


void tst_QColorTransform::mapRGB32_data()
{
    QTest::addColumn<QColorTransform>("transform");
    QTest::addColumn<bool>("sharesRed");

    QTest::newRow("default") << QColorTransform() << true;
    QTest::newRow("sRGB to Linear") << QColorSpace(QColorSpace::SRgb).transformationToColorSpace(QColorSpace::SRgbLinear) << true;
    QTest::newRow("AdobeRGB to sRGB") << QColorSpace(QColorSpace::AdobeRgb).transformationToColorSpace(QColorSpace::SRgb) << true;
    QTest::newRow("Linear AdobeRGB to Linear sRGB")
        << QColorSpace(QColorSpace::AdobeRgb).withTransferFunction(QColorSpace::TransferFunction::Linear).transformationToColorSpace(
            QColorSpace::SRgb)
        << true;
    QTest::newRow("sRgb to AdobeRGB") << QColorSpace(QColorSpace::SRgb).transformationToColorSpace(QColorSpace::AdobeRgb) << true;
    QTest::newRow("DP3 to sRGB") << QColorSpace(QColorSpace::DisplayP3).transformationToColorSpace(QColorSpace::SRgb) << false;
    QTest::newRow("DP3 to Linear DP3")
        << QColorSpace(QColorSpace::DisplayP3).transformationToColorSpace(
            QColorSpace(QColorSpace::DisplayP3).withTransferFunction(QColorSpace::TransferFunction::Linear))
        << false;
    QTest::newRow("Linear DP3 to Linear sRGB")
        << QColorSpace(QColorSpace::DisplayP3).withTransferFunction(QColorSpace::TransferFunction::Linear).transformationToColorSpace(
            QColorSpace::SRgb)
        << false;
}

void tst_QColorTransform::mapRGB32()
{
    QFETCH(QColorTransform, transform);
    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 (transform.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 (transform.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 (transform.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, 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 (transform.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, 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 (transform.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);
}

void tst_QColorTransform::transformIsIdentity()
{
    QColorTransform ct;
    QVERIFY(ct.isIdentity());

    QColorSpace cs = QColorSpace::SRgb;
    ct = cs.transformationToColorSpace(QColorSpace::SRgb);
    QVERIFY(ct.isIdentity());

    ct = cs.transformationToColorSpace(QColorSpace::SRgbLinear);
    QVERIFY(!ct.isIdentity());

    ct = cs.withTransferFunction(QColorSpace::TransferFunction::Linear).transformationToColorSpace(QColorSpace::SRgbLinear);
    QVERIFY(ct.isIdentity());
}

QTEST_MAIN(tst_QColorTransform)
#include "tst_qcolortransform.moc"