1import unittest 2import colorsys 3 4def frange(start, stop, step): 5 while start <= stop: 6 yield start 7 start += step 8 9class ColorsysTest(unittest.TestCase): 10 11 def assertTripleEqual(self, tr1, tr2): 12 self.assertEqual(len(tr1), 3) 13 self.assertEqual(len(tr2), 3) 14 self.assertAlmostEqual(tr1[0], tr2[0]) 15 self.assertAlmostEqual(tr1[1], tr2[1]) 16 self.assertAlmostEqual(tr1[2], tr2[2]) 17 18 def test_hsv_roundtrip(self): 19 for r in frange(0.0, 1.0, 0.2): 20 for g in frange(0.0, 1.0, 0.2): 21 for b in frange(0.0, 1.0, 0.2): 22 rgb = (r, g, b) 23 self.assertTripleEqual( 24 rgb, 25 colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb)) 26 ) 27 28 def test_hsv_values(self): 29 values = [ 30 # rgb, hsv 31 ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black 32 ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue 33 ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green 34 ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan 35 ((1.0, 0.0, 0.0), ( 0 , 1.0, 1.0)), # red 36 ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple 37 ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow 38 ((1.0, 1.0, 1.0), ( 0 , 0.0, 1.0)), # white 39 ((0.5, 0.5, 0.5), ( 0 , 0.0, 0.5)), # grey 40 ] 41 for (rgb, hsv) in values: 42 self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb)) 43 self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) 44 45 def test_hls_roundtrip(self): 46 for r in frange(0.0, 1.0, 0.2): 47 for g in frange(0.0, 1.0, 0.2): 48 for b in frange(0.0, 1.0, 0.2): 49 rgb = (r, g, b) 50 self.assertTripleEqual( 51 rgb, 52 colorsys.hls_to_rgb(*colorsys.rgb_to_hls(*rgb)) 53 ) 54 55 def test_hls_values(self): 56 values = [ 57 # rgb, hls 58 ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black 59 ((0.0, 0.0, 1.0), (4./6., 0.5, 1.0)), # blue 60 ((0.0, 1.0, 0.0), (2./6., 0.5, 1.0)), # green 61 ((0.0, 1.0, 1.0), (3./6., 0.5, 1.0)), # cyan 62 ((1.0, 0.0, 0.0), ( 0 , 0.5, 1.0)), # red 63 ((1.0, 0.0, 1.0), (5./6., 0.5, 1.0)), # purple 64 ((1.0, 1.0, 0.0), (1./6., 0.5, 1.0)), # yellow 65 ((1.0, 1.0, 1.0), ( 0 , 1.0, 0.0)), # white 66 ((0.5, 0.5, 0.5), ( 0 , 0.5, 0.0)), # grey 67 ] 68 for (rgb, hls) in values: 69 self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb)) 70 self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) 71 72 def test_yiq_roundtrip(self): 73 for r in frange(0.0, 1.0, 0.2): 74 for g in frange(0.0, 1.0, 0.2): 75 for b in frange(0.0, 1.0, 0.2): 76 rgb = (r, g, b) 77 self.assertTripleEqual( 78 rgb, 79 colorsys.yiq_to_rgb(*colorsys.rgb_to_yiq(*rgb)) 80 ) 81 82 def test_yiq_values(self): 83 values = [ 84 # rgb, yiq 85 ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), # black 86 ((0.0, 0.0, 1.0), (0.11, -0.3217, 0.3121)), # blue 87 ((0.0, 1.0, 0.0), (0.59, -0.2773, -0.5251)), # green 88 ((0.0, 1.0, 1.0), (0.7, -0.599, -0.213)), # cyan 89 ((1.0, 0.0, 0.0), (0.3, 0.599, 0.213)), # red 90 ((1.0, 0.0, 1.0), (0.41, 0.2773, 0.5251)), # purple 91 ((1.0, 1.0, 0.0), (0.89, 0.3217, -0.3121)), # yellow 92 ((1.0, 1.0, 1.0), (1.0, 0.0, 0.0)), # white 93 ((0.5, 0.5, 0.5), (0.5, 0.0, 0.0)), # grey 94 ] 95 for (rgb, yiq) in values: 96 self.assertTripleEqual(yiq, colorsys.rgb_to_yiq(*rgb)) 97 self.assertTripleEqual(rgb, colorsys.yiq_to_rgb(*yiq)) 98 99if __name__ == "__main__": 100 unittest.main() 101