cid#1606940 Check of thread-shared field evades lock acquisition
[LibreOffice.git] / tools / qa / cppunit / test_color.cxx
blob4e2046c58e7192c88b22f00a1c8c243cfc64cfeb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
16 namespace
19 class Test: public CppUnit::TestFixture
21 public:
22 void testVariables();
23 void test_asRGBColor();
24 void test_ApplyTintOrShade();
25 void test_ApplyLumModOff();
26 void testGetColorError();
27 void testInvert();
28 void testBColor();
29 void testLuminance();
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));
61 aColor.SetRed(0x22);
62 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x1122CCDD), sal_uInt32(aColor));
64 aColor.SetGreen(0x33);
65 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x112233DD), sal_uInt32(aColor));
67 aColor.SetBlue(0x44);
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));
73 aColor.SetRed(0x88);
74 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x77883344), sal_uInt32(aColor));
76 aColor.SetGreen(0x99);
77 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x77889944), sal_uInt32(aColor));
79 aColor.SetBlue(0xAA);
80 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x778899AA), sal_uInt32(aColor));
83 void Test::test_asRGBColor()
85 Color aColor;
86 aColor = COL_BLACK;
87 CPPUNIT_ASSERT_EQUAL(u"000000"_ustr, aColor.AsRGBHexString());
89 aColor = COL_WHITE;
90 CPPUNIT_ASSERT_EQUAL(u"ffffff"_ustr, aColor.AsRGBHexString());
92 aColor = COL_RED;
93 CPPUNIT_ASSERT_EQUAL(u"800000"_ustr, aColor.AsRGBHexString());
95 aColor = COL_TRANSPARENT;
96 CPPUNIT_ASSERT_EQUAL(u"ffffff"_ustr, aColor.AsRGBHexString());
98 aColor = COL_BLUE;
99 CPPUNIT_ASSERT_EQUAL(u"000080"_ustr, aColor.AsRGBHexString());
101 aColor.SetRed(0x12);
102 aColor.SetGreen(0x34);
103 aColor.SetBlue(0x56);
104 CPPUNIT_ASSERT_EQUAL(u"123456"_ustr, aColor.AsRGBHexString());
106 aColor = COL_AUTO;
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())
114 return OUString();
115 aColor.ApplyTintOrShade(nTintShade);
116 return aColor.AsRGBHexString();
119 void Test::test_ApplyTintOrShade()
121 // BLACK reference
123 // 5% tint
124 CPPUNIT_ASSERT_EQUAL(u"0d0d0d"_ustr, createTintShade(0x00, 0x00, 0x00, u"000000", 500));
125 // 15% tint
126 CPPUNIT_ASSERT_EQUAL(u"262626"_ustr, createTintShade(0x00, 0x00, 0x00, u"000000", 1500));
127 // 25% tint
128 CPPUNIT_ASSERT_EQUAL(u"404040"_ustr, createTintShade(0x00, 0x00, 0x00, u"000000", 2500));
129 // 50% tint
130 CPPUNIT_ASSERT_EQUAL(u"808080"_ustr, createTintShade(0x00, 0x00, 0x00, u"000000", 5000));
131 // 100% tint
132 CPPUNIT_ASSERT_EQUAL(u"ffffff"_ustr, createTintShade(0x00, 0x00, 0x00, u"000000", 10000));
134 // WHITE reference
136 // 5% shade
137 CPPUNIT_ASSERT_EQUAL(u"f2f2f2"_ustr, createTintShade(0xff, 0xff, 0xff, u"ffffff", -500));
138 // 15% shade
139 CPPUNIT_ASSERT_EQUAL(u"d9d9d9"_ustr, createTintShade(0xff, 0xff, 0xff, u"ffffff", -1500));
140 // 25% shade
141 CPPUNIT_ASSERT_EQUAL(u"bfbfbf"_ustr, createTintShade(0xff, 0xff, 0xff, u"ffffff", -2500));
142 // 50% shade
143 CPPUNIT_ASSERT_EQUAL(u"808080"_ustr, createTintShade(0xff, 0xff, 0xff, u"ffffff", -5000));
144 // 100% shade
145 CPPUNIT_ASSERT_EQUAL(u"000000"_ustr, createTintShade(0xff, 0xff, 0xff, u"ffffff", -10000));
147 // GREY reference
149 // 0% - no change
150 CPPUNIT_ASSERT_EQUAL(u"808080"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", 0));
152 // 25% tint
153 CPPUNIT_ASSERT_EQUAL(u"a0a0a0"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", 2500));
154 // 50% tint
155 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
156 // disable for now - a rounding error happens on come platforms...
157 // 100% tint
158 CPPUNIT_ASSERT_EQUAL(u"ffffff"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", 10000));
160 // 25% shade
161 CPPUNIT_ASSERT_EQUAL(u"606060"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", -2500));
162 // 50% shade
163 CPPUNIT_ASSERT_EQUAL(u"404040"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", -5000));
164 // 100% shade
165 CPPUNIT_ASSERT_EQUAL(u"000000"_ustr, createTintShade(0x80, 0x80, 0x80, u"808080", -10000));
168 void Test::test_ApplyLumModOff()
170 // Kind of blue.
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);
199 aColor.Invert();
200 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0xFF, 0x77).AsRGBHexString(), aColor.AsRGBHexString());
202 // Alpha should be unaffected
203 aColor = Color(ColorTransparency, 0x22, 0xFF, 0x00, 0x88);
204 aColor.Invert();
205 CPPUNIT_ASSERT_EQUAL(Color(ColorTransparency, 0x22, 0x00, 0xFF, 0x77).AsRGBHexString(), aColor.AsRGBHexString());
208 void Test::testBColor()
210 Color aColor;
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: */