1#include "kcolorutilstest.h"
2
3#include <QtTestGui>
4
5#include <kcolorutils.h>
6#include "../colors/kcolorspaces_p.h" // private header
7#include "../colors/kcolorspaces.cpp" // private implementation
8
9void tst_KColorUtils::testOverlay()
10{
11 QColor color1(10, 10, 100);
12 QColor color2(10, 10, 160);
13 QColor blended = KColorUtils::overlayColors(color1, color2);
14 QCOMPARE(blended, color2); // no transparency.
15
16 QColor previous;
17 // check that when altering the alpha of color2 to be less and less transparent this
18 // means we are moving more and more towards color2
19 for (int i = 10; i <= 255; i += 10) {
20 color2.setAlpha(i);
21 blended = KColorUtils::overlayColors(color1, color2);
22 if (previous.isValid()) {
23 QCOMPARE(previous.red(), 10);
24 QCOMPARE(previous.green(), 10);
25 QVERIFY(previous.blue() <= blended.blue());
26 }
27 previous = blended;
28 }
29
30 // only the alpha of color 2 alters the output
31 color2.setAlpha(255); //opaque
32 color1.setAlpha(80); //opaque
33 blended = KColorUtils::overlayColors(color2, color2);
34 QCOMPARE(blended.red(), color2.red());
35 QCOMPARE(blended.green(), color2.green());
36 QCOMPARE(blended.blue(), color2.blue());
37
38 // merge from itself to itself gives; TADA; itself again ;)
39 color2.setAlpha(127);
40 blended = KColorUtils::overlayColors(color2, color2);
41 QCOMPARE(blended.red(), color2.red());
42 QCOMPARE(blended.green(), color2.green());
43 QCOMPARE(blended.blue(), color2.blue());
44}
45
46#define compareColors(c1, c2) \
47 if ( c1 != c2 ) { \
48 fprintf( stderr, "%08x != expected value %08x\n", c1.rgb(), c2.rgb() ); \
49 QCOMPARE( c1, c2 ); \
50 } (void)0
51
52void tst_KColorUtils::testMix()
53{
54 int r, g, b, k;
55 for (r = 0; r < 52; r++) {
56 for (g = 0; g < 52; g++) {
57 for (b = 0; b < 52; b++) {
58 QColor color(r * 5, g * 5, b * 5);
59 // Test blend-to-black spectrum
60 for (k = 5; k >= 0; k--) {
61 QColor result = KColorUtils::mix(Qt::black, color, k * 0.2);
62 compareColors(result, QColor(r * k, g * k, b * k));
63 }
64 // Test blend-to-white spectrum
65 for (k = 5; k >= 0; k--) {
66 int n = 51 * (5 - k);
67 QColor result = KColorUtils::mix(Qt::white, color, k * 0.2);
68 compareColors(result, QColor(n + r * k, n + g * k, n + b * k));
69 }
70 // Test blend-to-self across a couple bias values
71 for (k = 5; k >= 0; k--) {
72 QColor result = KColorUtils::mix(color, color, k * 0.2);
73 compareColors(result, color);
74 }
75 }
76 }
77 }
78}
79
80void tst_KColorUtils::testHCY()
81{
82 int r, g, b;
83 for (r = 0; r < 256; r += 5) {
84 for (g = 0; g < 256; g += 5) {
85 for (b = 0; b < 256; b += 5) {
86 QColor color(r, g, b);
87 KColorSpaces::KHCY hcy(color);
88 compareColors(hcy.qColor(), color);
89 }
90 }
91 }
92}
93
94void tst_KColorUtils::testContrast()
95{
96 QCOMPARE(KColorUtils::contrastRatio(Qt::black, Qt::white), qreal(21.0));
97 QCOMPARE(KColorUtils::contrastRatio(Qt::white, Qt::black), qreal(21.0));
98 QCOMPARE(KColorUtils::contrastRatio(Qt::black, Qt::black), qreal(1.0));
99 QCOMPARE(KColorUtils::contrastRatio(Qt::white, Qt::white), qreal(1.0));
100
101 // TODO better tests :-)
102}
103
104void checkIsGray(const QColor &color, int line)
105{
106 KColorSpaces::KHCY hcy(color);
107 if (hcy.c != qreal(0.0)) {
108 fprintf(stderr, "%08x has chroma %g, expected gray!\n", color.rgb(), hcy.c);
109 }
110 QTest::qCompare(hcy.c, qreal(0.0), "hcy.c", "0.0", __FILE__, line);
111}
112
113void tst_KColorUtils::testShading()
114{
115 const QColor testGray(128, 128, 128); // Qt::gray isn't pure gray!
116
117 // Test that KHCY gets chroma correct for white/black, grays
118 checkIsGray(Qt::white, __LINE__);
119 checkIsGray(testGray, __LINE__);
120 checkIsGray(Qt::black, __LINE__);
121
122 // Test that lighten/darken/shade don't change chroma for grays
123 checkIsGray(KColorUtils::shade(Qt::white, -0.1), __LINE__);
124 checkIsGray(KColorUtils::shade(Qt::black, 0.1), __LINE__);
125 checkIsGray(KColorUtils::darken(Qt::white, 0.1), __LINE__);
126 checkIsGray(KColorUtils::darken(testGray, 0.1), __LINE__);
127 checkIsGray(KColorUtils::darken(testGray, -0.1), __LINE__);
128 checkIsGray(KColorUtils::darken(Qt::black, -0.1), __LINE__);
129 checkIsGray(KColorUtils::lighten(Qt::black, 0.1), __LINE__);
130 checkIsGray(KColorUtils::lighten(testGray, 0.1), __LINE__);
131 checkIsGray(KColorUtils::lighten(testGray, -0.1), __LINE__);
132 checkIsGray(KColorUtils::lighten(Qt::white, -0.1), __LINE__);
133}
134
135QTEST_MAIN(tst_KColorUtils)
136