Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / tools / qa / cppunit / test_color.cxx
blob97faa2d9cf3b2438155f62dd0be8f8743e842bec
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 <cppunit/plugin/TestPlugIn.h>
15 #include <tools/color.hxx>
16 #include <tools/stream.hxx>
18 namespace
21 class Test: public CppUnit::TestFixture
23 public:
24 void test_asRGBColor();
25 void test_readAndWriteStream();
26 void test_ApplyTintOrShade();
28 CPPUNIT_TEST_SUITE(Test);
29 CPPUNIT_TEST(test_asRGBColor);
30 CPPUNIT_TEST(test_readAndWriteStream);
31 CPPUNIT_TEST(test_ApplyTintOrShade);
32 CPPUNIT_TEST_SUITE_END();
35 void Test::test_asRGBColor()
37 Color aColor;
38 aColor = COL_BLACK;
39 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("000000"));
41 aColor = COL_WHITE;
42 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
44 aColor = COL_RED;
45 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("800000"));
47 aColor = COL_TRANSPARENT;
48 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
50 aColor = COL_BLUE;
51 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("000080"));
53 aColor.SetRed(0x12);
54 aColor.SetGreen(0x34);
55 aColor.SetBlue(0x56);
56 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("123456"));
58 aColor = COL_AUTO;
59 CPPUNIT_ASSERT_EQUAL(aColor.AsRGBHexString(), OUString("ffffff"));
62 void Test::test_readAndWriteStream()
64 SvMemoryStream aStream;
65 Color aWriteColor(0x12, 0x34, 0x56);
66 Color aReadColor;
68 WriteColor(aStream, aWriteColor);
70 aStream.Seek(STREAM_SEEK_TO_BEGIN);
72 ReadColor(aStream, aReadColor);
74 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x12), aReadColor.GetRed());
75 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x34), aReadColor.GetGreen());
76 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x56), aReadColor.GetBlue());
79 OUString createTintShade(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB, OUString const & sReference, sal_Int16 nTintShade)
81 Color aColor(nR, nG, nB);
82 if (sReference != aColor.AsRGBHexString())
83 return OUString();
84 aColor.ApplyTintOrShade(nTintShade);
85 return aColor.AsRGBHexString();
88 void Test::test_ApplyTintOrShade()
90 // BLACK reference
92 // 5% tint
93 CPPUNIT_ASSERT_EQUAL(OUString("0d0d0d"), createTintShade(0x00, 0x00, 0x00, "000000", 500));
94 // 15% tint
95 CPPUNIT_ASSERT_EQUAL(OUString("262626"), createTintShade(0x00, 0x00, 0x00, "000000", 1500));
96 // 25% tint
97 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x00, 0x00, 0x00, "000000", 2500));
98 // 50% tint
99 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x00, 0x00, 0x00, "000000", 5000));
100 // 100% tint
101 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x00, 0x00, 0x00, "000000", 10000));
103 // WHITE reference
105 // 5% shade
106 CPPUNIT_ASSERT_EQUAL(OUString("f2f2f2"), createTintShade(0xff, 0xff, 0xff, "ffffff", -500));
107 // 15% shade
108 CPPUNIT_ASSERT_EQUAL(OUString("d9d9d9"), createTintShade(0xff, 0xff, 0xff, "ffffff", -1500));
109 // 25% shade
110 CPPUNIT_ASSERT_EQUAL(OUString("bfbfbf"), createTintShade(0xff, 0xff, 0xff, "ffffff", -2500));
111 // 50% shade
112 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0xff, 0xff, 0xff, "ffffff", -5000));
113 // 100% shade
114 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0xff, 0xff, 0xff, "ffffff", -10000));
116 // GREY reference
118 // 0% - no change
119 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x80, 0x80, 0x80, "808080", 0));
121 // 25% tint
122 CPPUNIT_ASSERT_EQUAL(OUString("a0a0a0"), createTintShade(0x80, 0x80, 0x80, "808080", 2500));
123 // 50% tint
124 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
125 // disable for now - a rounding error happens on come platforms..
126 // 100% tint
127 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x80, 0x80, 0x80, "808080", 10000));
129 // 25% shade
130 CPPUNIT_ASSERT_EQUAL(OUString("606060"), createTintShade(0x80, 0x80, 0x80, "808080", -2500));
131 // 50% shade
132 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x80, 0x80, 0x80, "808080", -5000));
133 // 100% shade
134 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0x80, 0x80, 0x80, "808080", -10000));
138 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */