Bump version to 6.4-15
[LibreOffice.git] / tools / qa / cppunit / test_color.cxx
blob0a71c38191da3212a61d59c34f8fc2651fbc63ce
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 testGetColorError();
26 void testInvert();
27 void testBColor();
29 CPPUNIT_TEST_SUITE(Test);
30 CPPUNIT_TEST(testVariables);
31 CPPUNIT_TEST(test_asRGBColor);
32 CPPUNIT_TEST(test_ApplyTintOrShade);
33 CPPUNIT_TEST(testGetColorError);
34 CPPUNIT_TEST(testInvert);
35 CPPUNIT_TEST(testBColor);
36 CPPUNIT_TEST_SUITE_END();
39 void Test::testVariables()
41 Color aColor(0x44, 0x88, 0xAA);
42 CPPUNIT_ASSERT_EQUAL(int(0x00), int(aColor.A));
43 CPPUNIT_ASSERT_EQUAL(int(0x44), int(aColor.R));
44 CPPUNIT_ASSERT_EQUAL(int(0x88), int(aColor.G));
45 CPPUNIT_ASSERT_EQUAL(int(0xAA), int(aColor.B));
46 CPPUNIT_ASSERT_EQUAL(int(0x004488AA), int(aColor.mValue));
48 aColor.mValue = 0xAABBCCDD;
49 CPPUNIT_ASSERT_EQUAL(int(0xAA), int(aColor.A));
50 CPPUNIT_ASSERT_EQUAL(int(0xBB), int(aColor.R));
51 CPPUNIT_ASSERT_EQUAL(int(0xCC), int(aColor.G));
52 CPPUNIT_ASSERT_EQUAL(int(0xDD), int(aColor.B));
54 aColor.A = 0x11;
55 CPPUNIT_ASSERT_EQUAL(int(0x11BBCCDD), int(aColor.mValue));
57 aColor.R = 0x22;
58 CPPUNIT_ASSERT_EQUAL(int(0x1122CCDD), int(aColor.mValue));
60 aColor.G = 0x33;
61 CPPUNIT_ASSERT_EQUAL(int(0x112233DD), int(aColor.mValue));
63 aColor.B = 0x44;
64 CPPUNIT_ASSERT_EQUAL(int(0x11223344), int(aColor.mValue));
66 aColor.SetTransparency(0x77);
67 CPPUNIT_ASSERT_EQUAL(int(0x77223344), int(aColor.mValue));
69 aColor.SetRed(0x88);
70 CPPUNIT_ASSERT_EQUAL(int(0x77883344), int(aColor.mValue));
72 aColor.SetGreen(0x99);
73 CPPUNIT_ASSERT_EQUAL(int(0x77889944), int(aColor.mValue));
75 aColor.SetBlue(0xAA);
76 CPPUNIT_ASSERT_EQUAL(int(0x778899AA), int(aColor.mValue));
79 void Test::test_asRGBColor()
81 Color aColor;
82 aColor = COL_BLACK;
83 CPPUNIT_ASSERT_EQUAL(OUString("000000"), aColor.AsRGBHexString());
85 aColor = COL_WHITE;
86 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor.AsRGBHexString());
88 aColor = COL_RED;
89 CPPUNIT_ASSERT_EQUAL(OUString("800000"), aColor.AsRGBHexString());
91 aColor = COL_TRANSPARENT;
92 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor.AsRGBHexString());
94 aColor = COL_BLUE;
95 CPPUNIT_ASSERT_EQUAL(OUString("000080"), aColor.AsRGBHexString());
97 aColor.SetRed(0x12);
98 aColor.SetGreen(0x34);
99 aColor.SetBlue(0x56);
100 CPPUNIT_ASSERT_EQUAL(OUString("123456"), aColor.AsRGBHexString());
102 aColor = COL_AUTO;
103 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor.AsRGBHexString());
106 OUString createTintShade(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB, OUString const & sReference, sal_Int16 nTintShade)
108 Color aColor(nR, nG, nB);
109 if (sReference != aColor.AsRGBHexString())
110 return OUString();
111 aColor.ApplyTintOrShade(nTintShade);
112 return aColor.AsRGBHexString();
115 void Test::test_ApplyTintOrShade()
117 // BLACK reference
119 // 5% tint
120 CPPUNIT_ASSERT_EQUAL(OUString("0d0d0d"), createTintShade(0x00, 0x00, 0x00, "000000", 500));
121 // 15% tint
122 CPPUNIT_ASSERT_EQUAL(OUString("262626"), createTintShade(0x00, 0x00, 0x00, "000000", 1500));
123 // 25% tint
124 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x00, 0x00, 0x00, "000000", 2500));
125 // 50% tint
126 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x00, 0x00, 0x00, "000000", 5000));
127 // 100% tint
128 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x00, 0x00, 0x00, "000000", 10000));
130 // WHITE reference
132 // 5% shade
133 CPPUNIT_ASSERT_EQUAL(OUString("f2f2f2"), createTintShade(0xff, 0xff, 0xff, "ffffff", -500));
134 // 15% shade
135 CPPUNIT_ASSERT_EQUAL(OUString("d9d9d9"), createTintShade(0xff, 0xff, 0xff, "ffffff", -1500));
136 // 25% shade
137 CPPUNIT_ASSERT_EQUAL(OUString("bfbfbf"), createTintShade(0xff, 0xff, 0xff, "ffffff", -2500));
138 // 50% shade
139 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0xff, 0xff, 0xff, "ffffff", -5000));
140 // 100% shade
141 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0xff, 0xff, 0xff, "ffffff", -10000));
143 // GREY reference
145 // 0% - no change
146 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x80, 0x80, 0x80, "808080", 0));
148 // 25% tint
149 CPPUNIT_ASSERT_EQUAL(OUString("a0a0a0"), createTintShade(0x80, 0x80, 0x80, "808080", 2500));
150 // 50% tint
151 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
152 // disable for now - a rounding error happens on come platforms...
153 // 100% tint
154 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x80, 0x80, 0x80, "808080", 10000));
156 // 25% shade
157 CPPUNIT_ASSERT_EQUAL(OUString("606060"), createTintShade(0x80, 0x80, 0x80, "808080", -2500));
158 // 50% shade
159 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x80, 0x80, 0x80, "808080", -5000));
160 // 100% shade
161 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0x80, 0x80, 0x80, "808080", -10000));
164 void Test::testGetColorError()
166 CPPUNIT_ASSERT_EQUAL(sal_uInt16(0), Color(0xAA, 0xBB, 0xCC).GetColorError(Color(0xAA, 0xBB, 0xCC)));
168 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB0, 0xC0)));
169 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB1, 0xC0)));
170 CPPUNIT_ASSERT_EQUAL(sal_uInt16(1), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB0, 0xC1)));
172 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC0)));
173 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA0, 0xB1, 0xC1)));
174 CPPUNIT_ASSERT_EQUAL(sal_uInt16(2), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB0, 0xC1)));
176 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
177 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
178 CPPUNIT_ASSERT_EQUAL(sal_uInt16(3), Color(0xA0, 0xB0, 0xC0).GetColorError(Color(0xA1, 0xB1, 0xC1)));
181 void Test::testInvert()
183 Color aColor(0xFF, 0x00, 0x88);
184 aColor.Invert();
185 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0xFF, 0x77).AsRGBHexString(), aColor.AsRGBHexString());
187 // Alpha should be unaffected
188 aColor = Color(0x22, 0xFF, 0x00, 0x88);
189 aColor.Invert();
190 CPPUNIT_ASSERT_EQUAL(Color(0x22, 0x00, 0xFF, 0x77).AsRGBHexString(), aColor.AsRGBHexString());
193 void Test::testBColor()
195 Color aColor;
197 aColor = Color(basegfx::BColor(0.0, 0.0, 0.0));
199 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0x00, 0x00).AsRGBHexString(), aColor.AsRGBHexString());
200 CPPUNIT_ASSERT_EQUAL(0.0, aColor.getBColor().getRed());
201 CPPUNIT_ASSERT_EQUAL(0.0, aColor.getBColor().getGreen());
202 CPPUNIT_ASSERT_EQUAL(0.0, aColor.getBColor().getBlue());
204 aColor = Color(basegfx::BColor(1.0, 1.0, 1.0));
206 CPPUNIT_ASSERT_EQUAL(Color(0xFF, 0xFF, 0xFF).AsRGBHexString(), aColor.AsRGBHexString());
207 CPPUNIT_ASSERT_EQUAL(1.0, aColor.getBColor().getRed());
208 CPPUNIT_ASSERT_EQUAL(1.0, aColor.getBColor().getGreen());
209 CPPUNIT_ASSERT_EQUAL(1.0, aColor.getBColor().getBlue());
211 aColor = Color(basegfx::BColor(0.5, 0.25, 0.125));
213 CPPUNIT_ASSERT_EQUAL(Color(0x80, 0x40, 0x20).AsRGBHexString(), aColor.AsRGBHexString());
214 // FP error is rather big, but that's normal
215 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.500, aColor.getBColor().getRed(), 1E-2);
216 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.250, aColor.getBColor().getGreen(), 1E-2);
217 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.125, aColor.getBColor().getBlue(), 1E-2);
221 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */