Use operator module everywhere
Much cleaner than using getattr(). Fixes issue #13
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
See LICENSE for details.
|
||||
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
|
||||
"""
|
||||
|
||||
import operator
|
||||
|
||||
import colorsys
|
||||
from . import utility
|
||||
from lesscpy.lib import colors
|
||||
@@ -44,12 +47,12 @@ class Color():
|
||||
str
|
||||
"""
|
||||
operation = {
|
||||
'+': '__add__',
|
||||
'-': '__sub__',
|
||||
'*': '__mul__',
|
||||
'/': '__truediv__'
|
||||
'+': operator.add,
|
||||
'-': operator.sub,
|
||||
'*': operator.mul,
|
||||
'/': operator.truediv
|
||||
}.get(operation)
|
||||
return getattr(left, operation)(right)
|
||||
return operation(left, right)
|
||||
|
||||
def rgb(self, *args):
|
||||
""" Translate rgb(...) to color string
|
||||
@@ -184,7 +187,7 @@ class Color():
|
||||
str
|
||||
"""
|
||||
if color and diff:
|
||||
return self._ophsl(color, diff, 1, '__add__')
|
||||
return self._ophsl(color, diff, 1, operator.add)
|
||||
raise ValueError('Illegal color values')
|
||||
|
||||
def darken(self, color, diff, *args):
|
||||
@@ -196,7 +199,7 @@ class Color():
|
||||
str
|
||||
"""
|
||||
if color and diff:
|
||||
return self._ophsl(color, diff, 1, '__sub__')
|
||||
return self._ophsl(color, diff, 1, operator.sub)
|
||||
raise ValueError('Illegal color values')
|
||||
|
||||
def saturate(self, color, diff, *args):
|
||||
@@ -208,7 +211,7 @@ class Color():
|
||||
str
|
||||
"""
|
||||
if color and diff:
|
||||
return self._ophsl(color, diff, 2, '__add__')
|
||||
return self._ophsl(color, diff, 2, operator.add)
|
||||
raise ValueError('Illegal color values')
|
||||
|
||||
def desaturate(self, color, diff, *args):
|
||||
@@ -220,7 +223,7 @@ class Color():
|
||||
str
|
||||
"""
|
||||
if color and diff:
|
||||
return self._ophsl(color, diff, 2, '__sub__')
|
||||
return self._ophsl(color, diff, 2, operator.sub)
|
||||
raise ValueError('Illegal color values')
|
||||
|
||||
def _clamp(self, value):
|
||||
@@ -358,11 +361,11 @@ class Color():
|
||||
rgb = self._hextorgb(hex)
|
||||
return colorsys.rgb_to_hls(*[c / 255.0 for c in rgb])
|
||||
|
||||
def _ophsl(self, color, diff, idx, op):
|
||||
def _ophsl(self, color, diff, idx, operation):
|
||||
if isinstance(diff, str):
|
||||
diff = int(diff.strip('%'))
|
||||
hls = list(self._hextohls(color))
|
||||
hls[idx] = self._clamp(getattr(hls[idx], op)(diff / 100.0))
|
||||
hls[idx] = self._clamp(operation(hls[idx], diff / 100.0))
|
||||
rgb = colorsys.hls_to_rgb(*hls)
|
||||
color = (utility.away_from_zero_round(c * 255) for c in rgb)
|
||||
return self._rgbatohex(color)
|
||||
|
||||
@@ -7,7 +7,10 @@
|
||||
See LICENSE for details.
|
||||
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
|
||||
"""
|
||||
|
||||
import operator
|
||||
import sys
|
||||
|
||||
from .node import Node
|
||||
from lesscpy.lessc import utility
|
||||
from lesscpy.lessc import color
|
||||
@@ -108,51 +111,21 @@ class Expression(Node):
|
||||
mixed
|
||||
"""
|
||||
operation = {
|
||||
'+': '__add__',
|
||||
'-': '__sub__',
|
||||
'*': '__mul__',
|
||||
'/': '__truediv__',
|
||||
'=': '__eq__',
|
||||
'>': '__gt__',
|
||||
'<': '__lt__',
|
||||
'>=': '__ge__',
|
||||
'<=': '__le__',
|
||||
'!=': '__ne__',
|
||||
'<>': '__ne__',
|
||||
'+': operator.add,
|
||||
'-': operator.sub,
|
||||
'*': operator.mul,
|
||||
'/': operator.truediv,
|
||||
'=': operator.eq,
|
||||
'>': operator.gt,
|
||||
'<': operator.lt,
|
||||
'>=': operator.ge,
|
||||
'<=': operator.le,
|
||||
'!=': operator.ne,
|
||||
'<>': operator.ne,
|
||||
}.get(oper)
|
||||
if sys.version_info[0] < 3:
|
||||
ret = self.py2op(vala, operation, valb)
|
||||
else:
|
||||
ret = getattr(vala, operation)(valb)
|
||||
if ret is NotImplemented:
|
||||
# __truediv__(int, float) isn't implemented, but __truediv__(float, float) is.
|
||||
# __add__(int, float) is similar. Simply cast vala to float:
|
||||
ret = getattr(float(vala), operation)(valb)
|
||||
if oper in '+-*/':
|
||||
try:
|
||||
if int(ret) == ret:
|
||||
return int(ret)
|
||||
except ValueError:
|
||||
pass
|
||||
return ret
|
||||
|
||||
def py2op(self, vala, operation, valb):
|
||||
""" Python2 operators
|
||||
"""
|
||||
if operation == '__lt__':
|
||||
ret = (vala < valb)
|
||||
elif operation == '__gt__':
|
||||
ret = (vala > valb)
|
||||
elif operation == '__eq__':
|
||||
ret = (vala == valb)
|
||||
elif operation == '__ge__':
|
||||
ret = (vala >= valb)
|
||||
elif operation == '__le__':
|
||||
ret = (vala <= valb)
|
||||
elif operation == '__ne__':
|
||||
ret = (vala != valb)
|
||||
else:
|
||||
ret = getattr(vala, operation)(valb)
|
||||
ret = operation(vala, valb)
|
||||
if oper in '+-*/' and int(ret) == ret:
|
||||
ret = int(ret)
|
||||
return ret
|
||||
|
||||
def expression(self):
|
||||
|
||||
Reference in New Issue
Block a user