From cb291c5e80c8835e5d4005ba3a32f265268cdeb2 Mon Sep 17 00:00:00 2001 From: robotis Date: Tue, 18 Feb 2014 12:00:07 +0000 Subject: [PATCH] Fix for issue 31. hsl(a) processing on floats. --- lesscpy/lessc/color.py | 14 +++----------- lesscpy/lessc/utility.py | 10 ++++++++++ test/css/issues/issue31.css | 4 ++++ test/less/issues/issue31.less | 4 ++++ 4 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 test/css/issues/issue31.css create mode 100644 test/less/issues/issue31.less diff --git a/lesscpy/lessc/color.py b/lesscpy/lessc/color.py index d8b38cf..76ebb32 100644 --- a/lesscpy/lessc/color.py +++ b/lesscpy/lessc/color.py @@ -157,11 +157,7 @@ class Color(): return self.hsla(*args) elif len(args) == 3: h, s, l = args - if isinstance(l, str): - l = int(l.strip('%')) - if isinstance(s, str): - s = int(s.strip('%')) - rgb = colorsys.hls_to_rgb(int(h) / 360.0, l / 100.0, s / 100.0) + rgb = colorsys.hls_to_rgb(int(h) / 360.0, utility.pc_or_float(l), utility.pc_or_float(s)) color = (utility.convergent_round(c * 255) for c in rgb) return self._rgbatohex(color) raise ValueError('Illegal color values') @@ -175,13 +171,9 @@ class Color(): """ if len(args) == 4: h, s, l, a = args - if isinstance(l, str): - l = int(l.strip('%')) - if isinstance(s, str): - s = int(s.strip('%')) - rgb = colorsys.hls_to_rgb(int(h) / 360.0, l / 100.0, s / 100.0) + rgb = colorsys.hls_to_rgb(int(h) / 360.0, utility.pc_or_float(l), utility.pc_or_float(s)) color = [float(utility.convergent_round(c * 255)) for c in rgb] - color.append(utility.convergent_round(float(a[:-1]) / 100.0, 2)) + color.append(utility.pc_or_float(a)) return "rgba(%s,%s,%s,%s)" % tuple(color) raise ValueError('Illegal color values') diff --git a/lesscpy/lessc/utility.py b/lesscpy/lessc/utility.py index 3649353..d0cd75f 100644 --- a/lesscpy/lessc/utility.py +++ b/lesscpy/lessc/utility.py @@ -278,6 +278,16 @@ def convergent_round(value, ndigits=0): return math.ceil(nearest_even) return round(value, ndigits) +def pc_or_float(s): + """ Utility function to process strings that contain either percentiles or floats + args: + str: s + returns: + float + """ + if isinstance(s, str) and '%' in s: + return float(s.strip('%')) / 100.0 + return float(s) def permutations_with_replacement(iterable, r=None): """Return successive r length permutations of elements in the iterable. diff --git a/test/css/issues/issue31.css b/test/css/issues/issue31.css new file mode 100644 index 0000000..8fad20f --- /dev/null +++ b/test/css/issues/issue31.css @@ -0,0 +1,4 @@ +a.color { + color: rgba(0.0,0.0,0.0,0.5); + color: rgba(10.0,10.0,10.0,0.0); +} \ No newline at end of file diff --git a/test/less/issues/issue31.less b/test/less/issues/issue31.less new file mode 100644 index 0000000..14eb545 --- /dev/null +++ b/test/less/issues/issue31.less @@ -0,0 +1,4 @@ +a.color { + color: hsla(0, 0%, 0%, 0.5); + color: hsla(31, 1%, 4%, 0%); +}