Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / tools / qa / cppunit / test_color.cxx
blob2c8726eff7faefb7eeca2da3b638a7ff54a0b659
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()
65 SvMemoryStream aStream;
66 Color aWriteColor(0x12, 0x34, 0x56);
67 Color aReadColor;
69 WriteColor(aStream, aWriteColor);
71 aStream.Seek(STREAM_SEEK_TO_BEGIN);
73 ReadColor(aStream, aReadColor);
75 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x12), aReadColor.GetRed());
76 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x34), aReadColor.GetGreen());
77 CPPUNIT_ASSERT_EQUAL(sal_uInt8(0x56), aReadColor.GetBlue());
81 OUString createTintShade(sal_uInt8 nR, sal_uInt8 nG, sal_uInt8 nB, OUString const & sReference, sal_Int16 nTintShade)
83 Color aColor(nR, nG, nB);
84 if (sReference != aColor.AsRGBHexString())
85 return OUString("");
86 aColor.ApplyTintOrShade(nTintShade);
87 return aColor.AsRGBHexString();
90 void Test::test_ApplyTintOrShade()
92 // BLACK reference
94 // 5% tint
95 CPPUNIT_ASSERT_EQUAL(OUString("0d0d0d"), createTintShade(0x00, 0x00, 0x00, "000000", 500));
96 // 15% tint
97 CPPUNIT_ASSERT_EQUAL(OUString("262626"), createTintShade(0x00, 0x00, 0x00, "000000", 1500));
98 // 25% tint
99 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x00, 0x00, 0x00, "000000", 2500));
100 // 50% tint
101 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x00, 0x00, 0x00, "000000", 5000));
102 // 100% tint
103 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x00, 0x00, 0x00, "000000", 10000));
105 // WHITE reference
107 // 5% shade
108 CPPUNIT_ASSERT_EQUAL(OUString("f2f2f2"), createTintShade(0xff, 0xff, 0xff, "ffffff", -500));
109 // 15% shade
110 CPPUNIT_ASSERT_EQUAL(OUString("d9d9d9"), createTintShade(0xff, 0xff, 0xff, "ffffff", -1500));
111 // 25% shade
112 CPPUNIT_ASSERT_EQUAL(OUString("bfbfbf"), createTintShade(0xff, 0xff, 0xff, "ffffff", -2500));
113 // 50% shade
114 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0xff, 0xff, 0xff, "ffffff", -5000));
115 // 100% shade
116 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0xff, 0xff, 0xff, "ffffff", -10000));
118 // GREY reference
120 // 0% - no change
121 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x80, 0x80, 0x80, "808080", 0));
123 // 25% tint
124 CPPUNIT_ASSERT_EQUAL(OUString("a0a0a0"), createTintShade(0x80, 0x80, 0x80, "808080", 2500));
125 // 50% tint
126 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
127 // disable for now - a rounding error happens on come platforms..
128 // 100% tint
129 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x80, 0x80, 0x80, "808080", 10000));
131 // 25% shade
132 CPPUNIT_ASSERT_EQUAL(OUString("606060"), createTintShade(0x80, 0x80, 0x80, "808080", -2500));
133 // 50% shade
134 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x80, 0x80, 0x80, "808080", -5000));
135 // 100% shade
136 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0x80, 0x80, 0x80, "808080", -10000));
140 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */