0001 import unittest, test.test_support 0002 import colorsys 0003 0004 def frange(start, stop, step): 0005 while start <= stop: 0006 yield start 0007 start += stop 0008 0009 class ColorsysTest(unittest.TestCase): 0010 0011 def assertTripleEqual(self, tr1, tr2): 0012 self.assertEqual(len(tr1), 3) 0013 self.assertEqual(len(tr2), 3) 0014 self.assertAlmostEqual(tr1[0], tr2[0]) 0015 self.assertAlmostEqual(tr1[1], tr2[1]) 0016 self.assertAlmostEqual(tr1[2], tr2[2]) 0017 0018 def test_hsv_roundtrip(self): 0019 for r in frange(0.0, 1.0, 0.2): 0020 for g in frange(0.0, 1.0, 0.2): 0021 for b in frange(0.0, 1.0, 0.2): 0022 rgb = (r, g, b) 0023 self.assertTripleEqual( 0024 rgb, 0025 colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb)) 0026 ) 0027 0028 def test_hsv_values(self): 0029 values = [ 0030 # rgb, hsv 0031 ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black 0032 ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue 0033 ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green 0034 ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan 0035 ((1.0, 0.0, 0.0), ( 0 , 1.0, 1.0)), # red 0036 ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple 0037 ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow 0038 ((1.0, 1.0, 1.0), ( 0 , 0.0, 1.0)), # white 0039 ((0.5, 0.5, 0.5), ( 0 , 0.0, 0.5)), # grey 0040 ] 0041 for (rgb, hsv) in values: 0042 self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb)) 0043 self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) 0044 0045 def test_hls_roundtrip(self): 0046 for r in frange(0.0, 1.0, 0.2): 0047 for g in frange(0.0, 1.0, 0.2): 0048 for b in frange(0.0, 1.0, 0.2): 0049 rgb = (r, g, b) 0050 self.assertTripleEqual( 0051 rgb, 0052 colorsys.hls_to_rgb(*colorsys.rgb_to_hls(*rgb)) 0053 ) 0054 0055 def test_hls_values(self): 0056 values = [ 0057 # rgb, hls 0058 ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black 0059 ((0.0, 0.0, 1.0), (4./6., 0.5, 1.0)), # blue 0060 ((0.0, 1.0, 0.0), (2./6., 0.5, 1.0)), # green 0061 ((0.0, 1.0, 1.0), (3./6., 0.5, 1.0)), # cyan 0062 ((1.0, 0.0, 0.0), ( 0 , 0.5, 1.0)), # red 0063 ((1.0, 0.0, 1.0), (5./6., 0.5, 1.0)), # purple 0064 ((1.0, 1.0, 0.0), (1./6., 0.5, 1.0)), # yellow 0065 ((1.0, 1.0, 1.0), ( 0 , 1.0, 0.0)), # white 0066 ((0.5, 0.5, 0.5), ( 0 , 0.5, 0.0)), # grey 0067 ] 0068 for (rgb, hls) in values: 0069 self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb)) 0070 self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) 0071 0072 def test_main(): 0073 test.test_support.run_unittest(ColorsysTest) 0074 0075 if __name__ == "__main__": 0076 test_main() 0077
Generated by PyXR 0.9.4