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 "cppunit/plugin/TestPlugIn.h"
15 #include <tools/color.hxx>
16 #include <tools/stream.hxx>
21 class Test
: public CppUnit::TestFixture
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()
39 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("000000"));
42 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("ffffff"));
45 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("800000"));
47 aColor
= COL_TRANSPARENT
;
48 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("ffffff"));
51 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("000080"));
54 aColor
.SetGreen(0x34);
56 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("123456"));
59 CPPUNIT_ASSERT_EQUAL(aColor
.AsRGBHexString(), OUString("ffffff"));
62 void Test::test_readAndWriteStream()
65 SvMemoryStream aStream
;
66 Color
aWriteColor(0x12, 0x34, 0x56);
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())
86 aColor
.ApplyTintOrShade(nTintShade
);
87 return aColor
.AsRGBHexString();
90 void Test::test_ApplyTintOrShade()
95 CPPUNIT_ASSERT_EQUAL(OUString("0d0d0d"), createTintShade(0x00, 0x00, 0x00, "000000", 500));
97 CPPUNIT_ASSERT_EQUAL(OUString("262626"), createTintShade(0x00, 0x00, 0x00, "000000", 1500));
99 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x00, 0x00, 0x00, "000000", 2500));
101 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x00, 0x00, 0x00, "000000", 5000));
103 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x00, 0x00, 0x00, "000000", 10000));
108 CPPUNIT_ASSERT_EQUAL(OUString("f2f2f2"), createTintShade(0xff, 0xff, 0xff, "ffffff", -500));
110 CPPUNIT_ASSERT_EQUAL(OUString("d9d9d9"), createTintShade(0xff, 0xff, 0xff, "ffffff", -1500));
112 CPPUNIT_ASSERT_EQUAL(OUString("bfbfbf"), createTintShade(0xff, 0xff, 0xff, "ffffff", -2500));
114 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0xff, 0xff, 0xff, "ffffff", -5000));
116 CPPUNIT_ASSERT_EQUAL(OUString("000000"), createTintShade(0xff, 0xff, 0xff, "ffffff", -10000));
121 CPPUNIT_ASSERT_EQUAL(OUString("808080"), createTintShade(0x80, 0x80, 0x80, "808080", 0));
124 CPPUNIT_ASSERT_EQUAL(OUString("a0a0a0"), createTintShade(0x80, 0x80, 0x80, "808080", 2500));
126 //CPPUNIT_ASSERT_EQUAL(OUString("c0c0c0"), createTintShade(0x80, 0x80, 0x80, "808080", 5000));
127 // disable for now - a rounding error happens on come platforms..
129 CPPUNIT_ASSERT_EQUAL(OUString("ffffff"), createTintShade(0x80, 0x80, 0x80, "808080", 10000));
132 CPPUNIT_ASSERT_EQUAL(OUString("606060"), createTintShade(0x80, 0x80, 0x80, "808080", -2500));
134 CPPUNIT_ASSERT_EQUAL(OUString("404040"), createTintShade(0x80, 0x80, 0x80, "808080", -5000));
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: */