root/tags/0.5.0/tests/color.py

Revision 170, 5.8 kB (checked in by lgs, 3 years ago)

Implement different color schemes. Fixes #29

Line 
1# Copyright(c) 2007-2008 by Lorenzo Gil Sanchez <lorenzo.gil.sanchez@gmail.com>
2#              2009 by Yaco S.L. <lgs@yaco.es>
3#
4# This file is part of PyCha.
5#
6# PyCha is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# PyCha is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with PyCha.  If not, see <http://www.gnu.org/licenses/>.
18
19import unittest
20
21import pycha.color
22
23
24class SimpleColorScheme(pycha.color.ColorScheme):
25    pass
26
27
28class ColorTests(unittest.TestCase):
29
30    def test_clamp(self):
31        self.assertEqual(pycha.color.clamp(0, 1, 2), 1)
32        self.assertEqual(pycha.color.clamp(0, 1, -1), 0)
33        self.assertEqual(pycha.color.clamp(0, 1, 0.5), 0.5)
34        self.assertEqual(pycha.color.clamp(0, 1, 1), 1)
35        self.assertEqual(pycha.color.clamp(0, 1, 0), 0)
36
37    def test_hex2rgb(self):
38        color = pycha.color.hex2rgb('#ff0000')
39        self.assert_(isinstance(color, tuple))
40        self.assertAlmostEqual(1, color[0])
41        self.assertAlmostEqual(0, color[1])
42        self.assertAlmostEqual(0, color[2])
43
44        color2 = pycha.color.hex2rgb(color)
45        self.assertEqual(color, color2)
46
47        color = pycha.color.hex2rgb('#000fff000', digits=3)
48        self.assert_(isinstance(color, tuple))
49        self.assertEqual(0, color[0])
50        self.assertEqual(1, color[1])
51        self.assertEqual(0, color[2])
52
53        color = pycha.color.hex2rgb('#00000000ffff', digits=4)
54        self.assert_(isinstance(color, tuple))
55        self.assertEqual(0, color[0])
56        self.assertEqual(0, color[1])
57        self.assertEqual(1, color[2])
58
59    def test_rgb2hsv_and_hsv2rgb(self):
60        for rgb, hsv in (((1.0, 0.0, 0.0), (0.0, 1.0, 1.0)),
61                         ((1.0, 0.5, 0.0), (30.0, 1.0, 1.0)),
62                         ((1.0, 1.0, 0.0), (60.0, 1.0, 1.0)),
63                         ((0.5, 1.0, 0.0), (90.0, 1.0, 1.0)),
64                         ((0.0, 1.0, 0.0), (120.0, 1.0, 1.0)),
65                         ((0.0, 1.0, 0.5), (150.0, 1.0, 1.0)),
66                         ((0.0, 1.0, 1.0), (180.0, 1.0, 1.0)),
67                         ((0.0, 0.5, 1.0), (210.0, 1.0, 1.0)),
68                         ((0.0, 0.0, 1.0), (240.0, 1.0, 1.0)),
69                         ((0.5, 0.0, 1.0), (270.0, 1.0, 1.0)),
70                         ((1.0, 0.0, 1.0), (300.0, 1.0, 1.0)),
71                         ((1.0, 0.0, 0.5), (330.0, 1.0, 1.0)),
72                         ((0.375, 0.5, 0.25), (90.0, 0.5, 0.5)),
73                         ((0.21875, 0.25, 0.1875), (90.0, 0.25, 0.25))):
74            self._assertColors(pycha.color.rgb2hsv(*rgb), hsv, 5)
75            self._assertColors(pycha.color.hsv2rgb(*hsv), rgb, 5)
76
77    def test_lighten(self):
78        r, g, b = (1.0, 1.0, 0.0)
79        r2, g2, b2 = pycha.color.lighten(r, g, b, 0.1)
80        self.assertEqual((r2, g2, b2), (1.0, 1.0, 0.1))
81
82        r3, g3, b3 = pycha.color.lighten(r2, g2, b2, 0.5)
83        self.assertEqual((r3, g3, b3), (1.0, 1.0, 0.6))
84
85    def _assertColors(self, c1, c2, precission):
86        for i in range(3):
87            self.assertAlmostEqual(c1[i], c2[i], precission)
88
89    def test_basicColors(self):
90        colors = ('red', 'green', 'blue', 'grey', 'black', 'darkcyan')
91        for color in colors:
92            self.assert_(color in pycha.color.basicColors)
93
94    def test_ColorSchemeRegistry(self):
95        self.assertEquals(SimpleColorScheme,
96                          pycha.color.ColorScheme.getColorScheme('simple'))
97        self.assertEquals(None,
98                          pycha.color.ColorScheme.getColorScheme('foo'))
99
100    def test_FixedColorScheme(self):
101        keys = range(3)
102        colors = ((1.0, 0.0, 0.0), (0.0, 1.0, 0.0), (0.0, 0.0, 1.0))
103        scheme = pycha.color.FixedColorScheme(keys, colors)
104        self._assertColors(scheme[0], (1.0, 0.0, 0.0), 1)
105        self._assertColors(scheme[1], (0.0, 1.0, 0.0), 3)
106        self._assertColors(scheme[2], (0.0, 0.0, 1.0), 3)
107
108    def test_GradientColorScheme(self):
109        keys = range(5)
110        scheme = pycha.color.GradientColorScheme(keys, "#000000")
111        self._assertColors(scheme[0], (0.0, 0.0, 0.0), 3)
112        self._assertColors(scheme[1], (0.1, 0.1, 0.1), 3)
113        self._assertColors(scheme[2], (0.2, 0.2, 0.2), 3)
114        self._assertColors(scheme[3], (0.3, 0.3, 0.3), 3)
115        self._assertColors(scheme[4], (0.4, 0.4, 0.4), 3)
116
117    def test_autoLighting(self):
118        """This test ensures that the colors don't get to white too fast.
119
120        See bug #8.
121        """
122        # we have a lot of keys
123        n = 50
124        keys = range(n)
125        color = '#ff0000'
126        scheme = pycha.color.GradientColorScheme(keys, color)
127
128        # ensure that the last color is not completely white
129        color = scheme[n-1]
130
131        # the red component was already 1
132        self.assertAlmostEqual(color[0], 1.0, 4)
133        self.assertNotAlmostEqual(color[1], 1.0, 4)
134        self.assertNotAlmostEqual(color[2], 1.0, 4)
135
136    def test_RainbowColorScheme(self):
137        keys = range(5)
138        scheme = pycha.color.GradientColorScheme(keys, "#ff0000")
139        self._assertColors(scheme[0], (1.0, 0.0, 0.0), 3)
140        self._assertColors(scheme[1], (1.0, 0.1, 0.1), 3)
141        self._assertColors(scheme[2], (1.0, 0.2, 0.2), 3)
142        self._assertColors(scheme[3], (1.0, 0.3, 0.3), 3)
143        self._assertColors(scheme[4], (1.0, 0.4, 0.4), 3)
144
145
146def test_suite():
147    return unittest.TestSuite((
148        unittest.makeSuite(ColorTests),
149    ))
150
151
152if __name__ == '__main__':
153    unittest.main(defaultTest='test_suite')
Note: See TracBrowser for help on using the browser.