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

This commit is contained in:
robotis 2014-02-18 12:00:07 +00:00
parent 19ed65645d
commit cb291c5e80
4 changed files with 21 additions and 11 deletions

View File

@ -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')

View File

@ -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.

View File

@ -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);
}

View File

@ -0,0 +1,4 @@
a.color {
color: hsla(0, 0%, 0%, 0.5);
color: hsla(31, 1%, 4%, 0%);
}