Permalink
Browse files

Fix for issue 31. hsl(a) processing on floats.

  • Loading branch information...
1 parent 19ed656 commit cb291c5e80c8835e5d4005ba3a32f265268cdeb2 @robotis robotis committed Feb 18, 2014
Showing with 21 additions and 11 deletions.
  1. +3 −11 lesscpy/lessc/color.py
  2. +10 −0 lesscpy/lessc/utility.py
  3. +4 −0 test/css/issues/issue31.css
  4. +4 −0 test/less/issues/issue31.less
View
@@ -157,11 +157,7 @@ def hsl(self, *args):
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 @@ def hsla(self, *args):
"""
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')
View
@@ -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.
@@ -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);
+}
@@ -0,0 +1,4 @@
+a.color {
+ color: hsla(0, 0%, 0%, 0.5);
+ color: hsla(31, 1%, 4%, 0%);
+}

0 comments on commit cb291c5

Please sign in to comment.