cid#1607383 silence Overflowed constant
[LibreOffice.git] / basegfx / test / BColorTest.cxx
blob647078f338daf04d28b7af25c4792fd1e96293d1
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cppunit/TestAssert.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
24 #include <basegfx/color/bcolor.hxx>
25 #include <basegfx/color/bcolortools.hxx>
27 namespace basegfx
29 class bcolor : public CppUnit::TestFixture
31 BColor maWhite;
32 BColor maBlack;
33 BColor maRed;
34 BColor maGreen;
35 BColor maBlue;
36 BColor maYellow;
37 BColor maMagenta;
38 BColor maCyan;
40 public:
41 bcolor()
42 : maWhite(1, 1, 1)
43 , maBlack(0, 0, 0)
44 , maRed(1, 0, 0)
45 , maGreen(0, 1, 0)
46 , maBlue(0, 0, 1)
47 , maYellow(1, 1, 0)
48 , maMagenta(1, 0, 1)
49 , maCyan(0, 1, 1)
53 // insert your test code here.
54 void hslTest()
56 CPPUNIT_ASSERT_EQUAL_MESSAGE("white", BColor(0, 0, 1), utils::rgb2hsl(maWhite));
57 CPPUNIT_ASSERT_EQUAL_MESSAGE("black", BColor(0, 0, 0), utils::rgb2hsl(maBlack));
58 CPPUNIT_ASSERT_EQUAL_MESSAGE("red", BColor(0, 1, 0.5), utils::rgb2hsl(maRed));
59 CPPUNIT_ASSERT_EQUAL_MESSAGE("green", BColor(120, 1, 0.5), utils::rgb2hsl(maGreen));
60 CPPUNIT_ASSERT_EQUAL_MESSAGE("blue", BColor(240, 1, 0.5), utils::rgb2hsl(maBlue));
61 CPPUNIT_ASSERT_EQUAL_MESSAGE("yellow", BColor(60, 1, 0.5), utils::rgb2hsl(maYellow));
62 CPPUNIT_ASSERT_EQUAL_MESSAGE("magenta", BColor(300, 1, 0.5), utils::rgb2hsl(maMagenta));
63 CPPUNIT_ASSERT_EQUAL_MESSAGE("cyan", BColor(180, 1, 0.5), utils::rgb2hsl(maCyan));
64 CPPUNIT_ASSERT_EQUAL_MESSAGE("third hue case", BColor(210, 1, 0.5),
65 utils::rgb2hsl(BColor(0, 0.5, 1)));
67 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip white", maWhite,
68 utils::hsl2rgb(utils::rgb2hsl(maWhite)));
69 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip black", maBlack,
70 utils::hsl2rgb(utils::rgb2hsl(maBlack)));
71 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip red", maRed, utils::hsl2rgb(utils::rgb2hsl(maRed)));
72 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip green", maGreen,
73 utils::hsl2rgb(utils::rgb2hsl(maGreen)));
74 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip blue", maBlue,
75 utils::hsl2rgb(utils::rgb2hsl(maBlue)));
76 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip yellow", maYellow,
77 utils::hsl2rgb(utils::rgb2hsl(maYellow)));
78 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip magenta", maMagenta,
79 utils::hsl2rgb(utils::rgb2hsl(maMagenta)));
80 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip cyan", maCyan,
81 utils::hsl2rgb(utils::rgb2hsl(maCyan)));
83 CPPUNIT_ASSERT_EQUAL_MESSAGE("grey10", BColor(0, 0, .1), utils::rgb2hsl(maWhite * .1));
84 CPPUNIT_ASSERT_EQUAL_MESSAGE("grey90", BColor(0, 0, .9), utils::rgb2hsl(maWhite * .9));
85 CPPUNIT_ASSERT_EQUAL_MESSAGE("red/2", BColor(0, 1, 0.25), utils::rgb2hsl(maRed * .5));
86 CPPUNIT_ASSERT_EQUAL_MESSAGE("green/2", BColor(120, 1, 0.25), utils::rgb2hsl(maGreen * .5));
87 CPPUNIT_ASSERT_EQUAL_MESSAGE("blue/2", BColor(240, 1, 0.25), utils::rgb2hsl(maBlue * .5));
88 CPPUNIT_ASSERT_EQUAL_MESSAGE("yellow/2", BColor(60, 1, 0.25),
89 utils::rgb2hsl(maYellow * .5));
90 CPPUNIT_ASSERT_EQUAL_MESSAGE("magenta/2", BColor(300, 1, 0.25),
91 utils::rgb2hsl(maMagenta * .5));
92 CPPUNIT_ASSERT_EQUAL_MESSAGE("cyan/2", BColor(180, 1, 0.25), utils::rgb2hsl(maCyan * .5));
94 CPPUNIT_ASSERT_EQUAL_MESSAGE("pastel", BColor(0, .5, .5),
95 utils::rgb2hsl(BColor(.75, .25, .25)));
98 // insert your test code here.
99 void hsvTest()
101 CPPUNIT_ASSERT_EQUAL_MESSAGE("white", BColor(0, 0, 1), utils::rgb2hsv(maWhite));
102 CPPUNIT_ASSERT_EQUAL_MESSAGE("black", BColor(0, 0, 0), utils::rgb2hsv(maBlack));
103 CPPUNIT_ASSERT_EQUAL_MESSAGE("red", BColor(0, 1, 1), utils::rgb2hsv(maRed));
104 CPPUNIT_ASSERT_EQUAL_MESSAGE("green", BColor(120, 1, 1), utils::rgb2hsv(maGreen));
105 CPPUNIT_ASSERT_EQUAL_MESSAGE("blue", BColor(240, 1, 1), utils::rgb2hsv(maBlue));
106 CPPUNIT_ASSERT_EQUAL_MESSAGE("yellow", BColor(60, 1, 1), utils::rgb2hsv(maYellow));
107 CPPUNIT_ASSERT_EQUAL_MESSAGE("magenta", BColor(300, 1, 1), utils::rgb2hsv(maMagenta));
108 CPPUNIT_ASSERT_EQUAL_MESSAGE("cyan", BColor(180, 1, 1), utils::rgb2hsv(maCyan));
110 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip white", maWhite,
111 utils::hsv2rgb(utils::rgb2hsv(maWhite)));
112 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip black", maBlack,
113 utils::hsv2rgb(utils::rgb2hsv(maBlack)));
114 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip red", maRed, utils::hsv2rgb(utils::rgb2hsv(maRed)));
115 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip green", maGreen,
116 utils::hsv2rgb(utils::rgb2hsv(maGreen)));
117 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip blue", maBlue,
118 utils::hsv2rgb(utils::rgb2hsv(maBlue)));
119 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip yellow", maYellow,
120 utils::hsv2rgb(utils::rgb2hsv(maYellow)));
121 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip magenta", maMagenta,
122 utils::hsv2rgb(utils::rgb2hsv(maMagenta)));
123 CPPUNIT_ASSERT_EQUAL_MESSAGE("roundtrip cyan", maCyan,
124 utils::hsv2rgb(utils::rgb2hsv(maCyan)));
126 CPPUNIT_ASSERT_EQUAL_MESSAGE("grey10", BColor(0, 0, .1), utils::rgb2hsv(maWhite * .1));
127 CPPUNIT_ASSERT_EQUAL_MESSAGE("grey90", BColor(0, 0, .9), utils::rgb2hsv(maWhite * .9));
128 CPPUNIT_ASSERT_EQUAL_MESSAGE("red/2", BColor(0, 1, 0.5), utils::rgb2hsv(maRed * .5));
129 CPPUNIT_ASSERT_EQUAL_MESSAGE("green/2", BColor(120, 1, 0.5), utils::rgb2hsv(maGreen * .5));
130 CPPUNIT_ASSERT_EQUAL_MESSAGE("blue/2", BColor(240, 1, 0.5), utils::rgb2hsv(maBlue * .5));
131 CPPUNIT_ASSERT_EQUAL_MESSAGE("yellow/2", BColor(60, 1, 0.5), utils::rgb2hsv(maYellow * .5));
132 CPPUNIT_ASSERT_EQUAL_MESSAGE("magenta/2", BColor(300, 1, 0.5),
133 utils::rgb2hsv(maMagenta * .5));
134 CPPUNIT_ASSERT_EQUAL_MESSAGE("cyan/2", BColor(180, 1, 0.5), utils::rgb2hsv(maCyan * .5));
136 CPPUNIT_ASSERT_EQUAL_MESSAGE("pastel", BColor(0, .5, .5),
137 utils::rgb2hsv(BColor(.5, .25, .25)));
140 CPPUNIT_TEST_SUITE(bcolor);
141 CPPUNIT_TEST(hslTest);
142 CPPUNIT_TEST(hsvTest);
143 CPPUNIT_TEST_SUITE_END();
146 } // namespace basegfx
148 CPPUNIT_TEST_SUITE_REGISTRATION(basegfx::bcolor);
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */