1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/types.h>
11 #include <cppunit/TestAssert.h>
12 #include <cppunit/TestFixture.h>
13 #include <cppunit/extensions/HelperMacros.h>
14 #include <tools/color.hxx>
19 class Test
: public CppUnit::TestFixture
23 void test_asRGBColor();
24 void test_ApplyTintOrShade();
25 void test_ApplyLumModOff();
26 void testGetColorError();
31 CPPUNIT_TEST_SUITE(Test
);
32 CPPUNIT_TEST(testVariables
);
33 CPPUNIT_TEST(test_asRGBColor
);
34 CPPUNIT_TEST(test_ApplyTintOrShade
);
35 CPPUNIT_TEST(test_ApplyLumModOff
);
36 CPPUNIT_TEST(testGetColorError
);
37 CPPUNIT_TEST(testInvert
);
38 CPPUNIT_TEST(testBColor
);
39 CPPUNIT_TEST(testLuminance
);
40 CPPUNIT_TEST_SUITE_END();
43 void Test::testVariables()
45 Color
aColor(0x44, 0x88, 0xAA);
46 CPPUNIT_ASSERT_EQUAL(int(0x00), int(255 - aColor
.GetAlpha()));
47 CPPUNIT_ASSERT_EQUAL(int(0x44), int(aColor
.GetRed()));
48 CPPUNIT_ASSERT_EQUAL(int(0x88), int(aColor
.GetGreen()));
49 CPPUNIT_ASSERT_EQUAL(int(0xAA), int(aColor
.GetBlue()));
50 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x004488AA), sal_uInt32(aColor
));
52 aColor
= Color(ColorTransparency
, 0xAABBCCDD);
53 CPPUNIT_ASSERT_EQUAL(int(0xAA), int(255 - aColor
.GetAlpha()));
54 CPPUNIT_ASSERT_EQUAL(int(0xBB), int(aColor
.GetRed()));
55 CPPUNIT_ASSERT_EQUAL(int(0xCC), int(aColor
.GetGreen()));
56 CPPUNIT_ASSERT_EQUAL(int(0xDD), int(aColor
.GetBlue()));
58 aColor
.SetAlpha(255 - 0x11);
59 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x11BBCCDD), sal_uInt32(aColor
));
62 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x1122CCDD), sal_uInt32(aColor
));
64 aColor
.SetGreen(0x33);
65 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x112233DD), sal_uInt32(aColor
));
68 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x11223344), sal_uInt32(aColor
));
70 aColor
.SetAlpha(255 - 0x77);
71 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x77223344), sal_uInt32(aColor
));
74 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x77883344), sal_uInt32(aColor
));
76 aColor
.SetGreen(0x99);
77 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x77889944), sal_uInt32(aColor
));
80 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x778899AA), sal_uInt32(aColor
));
83 void Test::test_asRGBColor()
87 CPPUNIT_ASSERT_EQUAL(u
"000000"_ustr
, aColor
.AsRGBHexString());
90 CPPUNIT_ASSERT_EQUAL(u
"ffffff"_ustr
, aColor
.AsRGBHexString());
93 CPPUNIT_ASSERT_EQUAL(u
"800000"_ustr
, aColor
.AsRGBHexString());
95 aColor
= COL_TRANSPARENT
;
96 CPPUNIT_ASSERT_EQUAL(u
"ffffff"_ustr
, aColor
.AsRGBHexString());
99 CPPUNIT_ASSERT_EQUAL(u
"000080"_ustr
, aColor
.AsRGBHexString());
102 aColor
.SetGreen(0x34);
103 aColor
.SetBlue(0x56);
104 CPPUNIT_ASSERT_EQUAL(u
"123456"_ustr
, aColor
.AsRGBHexString());
107 CPPUNIT_ASSERT_EQUAL(u
"ffffff"_ustr
, aColor
.AsRGBHexString());
110 OUString
createTintShade(sal_uInt8 nR
, sal_uInt8 nG
, sal_uInt8 nB
, std::u16string_view sReference
, sal_Int16 nTintShade
)
112 Color
aColor(nR
, nG
, nB
);
113 if (sReference
!= aColor
.AsRGBHexString())
115 aColor
.ApplyTintOrShade(nTintShade
);
116 return aColor
.AsRGBHexString();
119 void Test::test_ApplyTintOrShade()
124 CPPUNIT_ASSERT_EQUAL(u
"0d0d0d"_ustr
, createTintShade(0x00, 0x00, 0x00, u
"000000", 500));
126 CPPUNIT_ASSERT_EQUAL(u
"262626"_ustr
, createTintShade(0x00, 0x00, 0x00, u
"000000", 1500));
128 CPPUNIT_ASSERT_EQUAL(u
"404040"_ustr
, createTintShade(0x00, 0x00, 0x00, u
"000000", 2500));
130 CPPUNIT_ASSERT_EQUAL(u
"808080"_ustr
, createTintShade(0x00, 0x00, 0x00, u
"000000", 5000));
132 CPPUNIT_ASSERT_EQUAL(u
"ffffff"_ustr
, createTintShade(0x00, 0x00, 0x00, u
"000000", 10000));
137 CPPUNIT_ASSERT_EQUAL(u
"f2f2f2"_ustr
, createTintShade(0xff, 0xff, 0xff, u
"ffffff", -500));
139 CPPUNIT_ASSERT_EQUAL(u
"d9d9d9"_ustr
, createTintShade(0xff, 0xff, 0xff, u
"ffffff", -1500));
141 CPPUNIT_ASSERT_EQUAL(u
"bfbfbf"_ustr
, createTintShade(0xff, 0xff, 0xff, u
"ffffff", -2500));
143 CPPUNIT_ASSERT_EQUAL(u
"808080"_ustr
, createTintShade(0xff, 0xff, 0xff, u
"ffffff", -5000));
145 CPPUNIT_ASSERT_EQUAL(u
"000000"_ustr
, createTintShade(0xff, 0xff, 0xff, u
"ffffff", -10000));
150 CPPUNIT_ASSERT_EQUAL(u
"808080"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", 0));
153 CPPUNIT_ASSERT_EQUAL(u
"a0a0a0"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", 2500));
155 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
156 // disable for now - a rounding error happens on come platforms...
158 CPPUNIT_ASSERT_EQUAL(u
"ffffff"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", 10000));
161 CPPUNIT_ASSERT_EQUAL(u
"606060"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", -2500));
163 CPPUNIT_ASSERT_EQUAL(u
"404040"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", -5000));
165 CPPUNIT_ASSERT_EQUAL(u
"000000"_ustr
, createTintShade(0x80, 0x80, 0x80, u
"808080", -10000));
168 void Test::test_ApplyLumModOff()
171 Color
aColor(0x44, 0x72, 0xC4);
173 // PowerPoint calls this "Lighter 40%".
174 aColor
.ApplyLumModOff(6000, 4000);
176 CPPUNIT_ASSERT_EQUAL(u
"8faadc"_ustr
, aColor
.AsRGBHexString());
179 void Test::testGetColorError()
181 CPPUNIT_ASSERT_EQUAL(sal_uInt16(0), Color(0xAA, 0xBB, 0xCC).GetColorError(Color(0xAA, 0xBB, 0xCC)));
183 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB0, 0xC0)));
184 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB1, 0xC0)));
185 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB0, 0xC1)));
187 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC0)));
188 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB1, 0xC1)));
189 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB0, 0xC1)));
191 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
192 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
193 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
196 void Test::testInvert()
198 Color
aColor(0xFF, 0x00, 0x88);
200 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0xFF, 0x77).AsRGBHexString(), aColor
.AsRGBHexString());
202 // Alpha should be unaffected
203 aColor
= Color(ColorTransparency
, 0x22, 0xFF, 0x00, 0x88);
205 CPPUNIT_ASSERT_EQUAL(Color(ColorTransparency
, 0x22, 0x00, 0xFF, 0x77).AsRGBHexString(), aColor
.AsRGBHexString());
208 void Test::testBColor()
212 aColor
= Color(basegfx::BColor(0.0, 0.0, 0.0));
214 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0x00, 0x00).AsRGBHexString(), aColor
.AsRGBHexString());
215 CPPUNIT_ASSERT_EQUAL(0.0, aColor
.getBColor().getRed());
216 CPPUNIT_ASSERT_EQUAL(0.0, aColor
.getBColor().getGreen());
217 CPPUNIT_ASSERT_EQUAL(0.0, aColor
.getBColor().getBlue());
219 aColor
= Color(basegfx::BColor(1.0, 1.0, 1.0));
221 CPPUNIT_ASSERT_EQUAL(Color(0xFF, 0xFF, 0xFF).AsRGBHexString(), aColor
.AsRGBHexString());
222 CPPUNIT_ASSERT_EQUAL(1.0, aColor
.getBColor().getRed());
223 CPPUNIT_ASSERT_EQUAL(1.0, aColor
.getBColor().getGreen());
224 CPPUNIT_ASSERT_EQUAL(1.0, aColor
.getBColor().getBlue());
226 aColor
= Color(basegfx::BColor(0.5, 0.25, 0.125));
228 CPPUNIT_ASSERT_EQUAL(Color(0x80, 0x40, 0x20).AsRGBHexString(), aColor
.AsRGBHexString());
229 // FP error is rather big, but that's normal
230 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.500, aColor
.getBColor().getRed(), 1E-2);
231 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.250, aColor
.getBColor().getGreen(), 1E-2);
232 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.125, aColor
.getBColor().getBlue(), 1E-2);
236 void Test::testLuminance()
238 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0), COL_BLACK
.GetLuminance());
239 CPPUNIT_ASSERT_EQUAL(sal_uInt8(255), COL_WHITE
.GetLuminance());
240 CPPUNIT_ASSERT_EQUAL(sal_uInt8(128), Color(128, 128, 128).GetLuminance());
241 CPPUNIT_ASSERT(COL_WHITE
.IsBright());
242 CPPUNIT_ASSERT(COL_BLACK
.IsDark());
243 CPPUNIT_ASSERT(Color(249, 250, 251).IsBright());
244 CPPUNIT_ASSERT(Color(9, 10, 11).IsDark());
245 CPPUNIT_ASSERT(COL_WHITE
.GetLuminance() > COL_BLACK
.GetLuminance());
248 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */