root/tags/0.4.1/tests/color.py

Revision 112, 3.9 kB (checked in by lgs, 4 years ago)

Remove whitespace

Line 
1# Copyright (c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#
3# This file is part of PyCha.
4#
5# PyCha is free software: you can redistribute it and/or modify
6# it under the terms of the GNU Lesser General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# PyCha is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public License
16# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19
20import pycha.color
21
22class ColorTests(unittest.TestCase):
23
24    def test_clamp(self):
25        self.assertEqual(pycha.color.clamp(0, 1, 2), 1)
26        self.assertEqual(pycha.color.clamp(0, 1, -1), 0)
27        self.assertEqual(pycha.color.clamp(0, 1, 0.5), 0.5)
28        self.assertEqual(pycha.color.clamp(0, 1, 1), 1)
29        self.assertEqual(pycha.color.clamp(0, 1, 0), 0)
30
31    def test_hex2rgb(self):
32        color = pycha.color.hex2rgb('#ff0000')
33        self.assert_(isinstance(color, tuple))
34        self.assertAlmostEqual(1, color[0])
35        self.assertAlmostEqual(0, color[1])
36        self.assertAlmostEqual(0, color[2])
37
38        color2 = pycha.color.hex2rgb(color)
39        self.assertEqual(color, color2)
40
41        color = pycha.color.hex2rgb('#000fff000', digits=3)
42        self.assert_(isinstance(color, tuple))
43        self.assertEqual(0, color[0])
44        self.assertEqual(1, color[1])
45        self.assertEqual(0, color[2])
46
47        color = pycha.color.hex2rgb('#00000000ffff', digits=4)
48        self.assert_(isinstance(color, tuple))
49        self.assertEqual(0, color[0])
50        self.assertEqual(0, color[1])
51        self.assertEqual(1, color[2])
52
53    def test_lighten(self):
54        r, g, b = (1.0, 1.0, 0.0)
55        r2, g2, b2 = pycha.color.lighten(r, g, b, 0.1)
56        self.assertEqual((r2, g2, b2), (1.0, 1.0, 0.1))
57
58        r3, g3, b3 = pycha.color.lighten(r2, g2, b2, 0.5)
59        self.assertEqual((r3, g3, b3), (1.0, 1.0, 0.6))
60
61    def _assertColors(self, c1, c2, precission):
62        for i in range(3):
63            self.assertAlmostEqual(c1[i], c2[i], precission)
64
65    def test_generateColorscheme(self):
66        keys = ('k1', 'k2', 'k3', 'k4')
67        color = '#ff0000'
68        scheme = pycha.color.generateColorscheme(color, keys)
69
70        self._assertColors(scheme['k1'], (1, 0, 0), 3)
71        self._assertColors(scheme['k2'], (1, 0.125, 0.125), 3)
72        self._assertColors(scheme['k3'], (1, 0.250, 0.250), 3)
73        self._assertColors(scheme['k4'], (1, 0.375, 0.375), 3)
74
75    def test_defaultColorScheme(self):
76        keys = ('k1', 'k2', 'k3', 'k4')
77        scheme1 = pycha.color.defaultColorscheme(keys)
78        color = pycha.color.DEFAULT_COLOR
79        scheme2 = pycha.color.generateColorscheme(color, keys)
80        self.assertEqual(scheme1, scheme2)
81
82    def test_colorScheme(self):
83        colors = ('red', 'green', 'blue', 'grey', 'black', 'darkcyan')
84        for color in colors:
85            self.assert_(pycha.color.colorSchemes.has_key(color))
86
87    def test_autoLighting(self):
88        """This test ensures that the colors don't get to white too fast.
89
90        See bug #8.
91        """
92        # we have a lot of keys
93        n = 50
94        keys = range(n)
95        color = '#ff0000'
96        scheme = pycha.color.generateColorscheme(color, keys)
97
98        # ensure that the last color is not completely white
99        color = scheme[n-1]
100        self.assertAlmostEqual(color[0], 1.0, 4) # the red component was already 1
101        self.assertNotAlmostEqual(color[1], 1.0, 4)
102        self.assertNotAlmostEqual(color[2], 1.0, 4)
103
104def test_suite():
105    return unittest.TestSuite((
106        unittest.makeSuite(ColorTests),
107    ))
108
109if __name__ == '__main__':
110    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.