Using the rgb_to_hls() and hls_to_rgb() functions in CleverCSS to
convert colors back and forth reveals a mistake in the HLS to RGB
algorithm. Fractional parts are stripped when rounding should be done
instead. Patch with unit tests:
diff --git a/clevercss.py b/clevercss.py
index 19acfeb..e362d47 100644
--- a/clevercss.py
+++ b/clevercss.py
@@ -455,7 +455,7 @@ def rgb_to_hls(red, green, blue):
def hls_to_rgb(hue, saturation, lightness):
"""Convert HSL back to RGB."""
t = colorsys.hls_to_rgb(hue, saturation, lightness)
- return tuple(int(x * 255) for x in t)
+ return tuple(int(round(x * 255)) for x in t)
class ParserError(Exception):
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..2bde135
--- /dev/null
+++ b/tests/__init__.py
@@ -0,0 +1,58 @@
+from unittest import TestCase, main
+
+from clevercss import rgb_to_hls
+
+class RgbToHlsTestCase(TestCase):
+
+ def test_01_rgb_to_hls(self):
+ self.assertEqualHLS(rgb_to_hls(10, 100, 250),
+ [0.6042, 0.5098, 0.9600])
+
+ def test_02_rgb_to_hls_underflow(self):
+ self.assertEqualHLS(rgb_to_hls(-10, 100, 250),
+ [0.5962, 0.4706, 1.0833])
+
+ def test_03_rgb_to_hls_overflow(self):
+ self.assertEqualHLS(rgb_to_hls(10, 300, 250),
+ [0.4713, 0.6078, 1.4500])
+
+ def assertEqualHLS(self, got, expected):
+ self.assertEqual([round(x, 4) for x in got],
+ [round(x, 4) for x in expected])
+
+from clevercss import hls_to_rgb
+
+class HlsToRgbTestCase(TestCase):
+ def test_01_hls_to_rgb(self):
+ self.assertEqual(hls_to_rgb(0.6042, 0.5098, 0.9600),
+ (10, 100, 250))
+
+ def test_02_hls_to_rgb_underflow(self):
+ self.assertEqual(hls_to_rgb(0.5962, 0.4706, 1.0833),
+ (-10, 100, 250))
+
+ def test_03_hls_to_rgb_overflow(self):
+ self.assertEqual(hls_to_rgb(0.4713, 0.6078, 1.4500),
+ (10, 300, 250))
+
+ def assertEqualHLS(self, got, expected):
+ self.assertEqual([round(x, 4) for x in got],
+ [round(x, 4) for x in expected])
+
+class HlsRgbFuzzyTestCase(TestCase):
+ def test_01_hls_to_rgb_and_back_fuzzy(self):
+ for i in xrange(100):
+ self.do_fuzzy()
+
+ def do_fuzzy(self):
+ from random import seed, randint
+ seed(0)
+ rgb = tuple(randint(0, 255) for i in range(3))
+ hls = rgb_to_hls(*rgb)
+ hls2rgb = hls_to_rgb(*hls)
+ self.assertEqual(rgb, hls2rgb)
+ rgb2hls = rgb_to_hls(*hls2rgb)
+ self.assertEqual(rgb2hls, hls)
+
+if __name__ == '__main__':
+ main()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---