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 testGetColorError();
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
));
55 CPPUNIT_ASSERT_EQUAL(int(0x11BBCCDD), int(aColor
.mValue
));
58 CPPUNIT_ASSERT_EQUAL(int(0x1122CCDD), int(aColor
.mValue
));
61 CPPUNIT_ASSERT_EQUAL(int(0x112233DD), int(aColor
.mValue
));
64 CPPUNIT_ASSERT_EQUAL(int(0x11223344), int(aColor
.mValue
));
66 aColor
.SetTransparency(0x77);
67 CPPUNIT_ASSERT_EQUAL(int(0x77223344), int(aColor
.mValue
));
70 CPPUNIT_ASSERT_EQUAL(int(0x77883344), int(aColor
.mValue
));
72 aColor
.SetGreen(0x99);
73 CPPUNIT_ASSERT_EQUAL(int(0x77889944), int(aColor
.mValue
));
76 CPPUNIT_ASSERT_EQUAL(int(0x778899AA), int(aColor
.mValue
));
79 void Test::test_asRGBColor()
83 CPPUNIT_ASSERT_EQUAL(OUString("000000"), aColor
.AsRGBHexString());
86 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor
.AsRGBHexString());
89 CPPUNIT_ASSERT_EQUAL(OUString("800000"), aColor
.AsRGBHexString());
91 aColor
= COL_TRANSPARENT
;
92 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), aColor
.AsRGBHexString());
95 CPPUNIT_ASSERT_EQUAL(OUString("000080"), aColor
.AsRGBHexString());
98 aColor
.SetGreen(0x34);
100 CPPUNIT_ASSERT_EQUAL(OUString("123456"), aColor
.AsRGBHexString());
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())
111 aColor
.ApplyTintOrShade(nTintShade
);
112 return aColor
.AsRGBHexString();
115 void Test::test_ApplyTintOrShade()
120 CPPUNIT_ASSERT_EQUAL(OUString("0d0d0d"), createTintShade(0x00, 0x00, 0x00, "000000", 500));
122 CPPUNIT_ASSERT_EQUAL(OUString("262626"), createTintShade(0x00, 0x00, 0x00, "000000", 1500));
124 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x00, 0x00, 0x00, "000000", 2500));
126 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x00, 0x00, 0x00, "000000", 5000));
128 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x00, 0x00, 0x00, "000000", 10000));
133 CPPUNIT_ASSERT_EQUAL(OUString("f2f2f2"), createTintShade(0xff, 0xff, 0xff, "ffffff", -500));
135 CPPUNIT_ASSERT_EQUAL(OUString("d9d9d9"), createTintShade(0xff, 0xff, 0xff, "ffffff", -1500));
137 CPPUNIT_ASSERT_EQUAL(OUString("bfbfbf"), createTintShade(0xff, 0xff, 0xff, "ffffff", -2500));
139 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0xff, 0xff, 0xff, "ffffff", -5000));
141 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0xff, 0xff, 0xff, "ffffff", -10000));
146 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x80, 0x80, 0x80, "808080", 0));
149 CPPUNIT_ASSERT_EQUAL(OUString("a0a0a0"), createTintShade(0x80, 0x80, 0x80, "808080", 2500));
151 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
152 // disable for now - a rounding error happens on come platforms...
154 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x80, 0x80, 0x80, "808080", 10000));
157 CPPUNIT_ASSERT_EQUAL(OUString("606060"), createTintShade(0x80, 0x80, 0x80, "808080", -2500));
159 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x80, 0x80, 0x80, "808080", -5000));
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);
185 CPPUNIT_ASSERT_EQUAL(Color(0x00, 0xFF, 0x77).AsRGBHexString(), aColor
.AsRGBHexString());
187 // Alpha should be unaffected
188 aColor
= Color(0x22, 0xFF, 0x00, 0x88);
190 CPPUNIT_ASSERT_EQUAL(Color(0x22, 0x00, 0xFF, 0x77).AsRGBHexString(), aColor
.AsRGBHexString());
193 void Test::testBColor()
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: */