diff options
Diffstat (limited to 'thcolor/_ref.py')
-rwxr-xr-x[-rw-r--r--] | thcolor/_ref.py | 55 |
1 files changed, 40 insertions, 15 deletions
diff --git a/thcolor/_ref.py b/thcolor/_ref.py index b224b52..5bc51c1 100644..100755 --- a/thcolor/_ref.py +++ b/thcolor/_ref.py @@ -9,6 +9,7 @@ from inspect import (getfullargspec as _getfullargspec, getmembers as _getmembers, ismethod as _ismethod) from itertools import count as _count +from ._angle import Angle as _Angle from ._sys import netscape_color as _netscape_color from ._exc import (NotEnoughArgumentsError as _NotEnoughArgumentsError, TooManyArgumentsError as _TooManyArgumentsError, @@ -131,6 +132,9 @@ class Reference: Color = _get_color_class() return Color(Color.Type.RGB, r, g, b, 1.0) + def to_hue(self): + return _Angle(_Angle.Type.DEG, self._value) + class percentage(metaclass = base_type): def __init__(self, value): self._value = value @@ -153,17 +157,17 @@ class Reference: return self._value / 100 class angle(metaclass = base_type): - def __init__(self, value, unit): - self._value = value % 360.0 - - self._unit = unit - if not self._unit: - self._unit = 'deg' + def __init__(self, value): + if not isinstance(value, _Angle): + raise TypeError("expected an Angle instance") - assert self._unit in ('deg', 'grad', 'rad', 'turn') + self._value = value def __repr__(self): - return f"{self._value} {self._unit}" + return repr(self._value) + + def to_hue(self): + return self._value class color(metaclass = base_type): def __init__(self, value): @@ -244,11 +248,32 @@ class Reference: arg_types = self._args \ + self._optargs[:len(args) - len(self._args)] - - for index, arg, exp in zip(_count(), args, arg_types): - if exp is not None and type(arg) not in exp: - raise _InvalidArgumentTypeError(index, - exp, type(arg), self._name) + arg_source = args + args = [] + + lit = zip(_count(), arg_source, arg_types) + for index, arg, exp in lit: + args.append(arg) + if exp is None: + continue + if type(arg) in exp: + continue + + # If we haven't got a color but a color is one + # of the accepted types, try to transform into + # a color to manage number colors using the + # Netscape transformation such as '123'. + + if color in exp: + try: + args[-1] = arg.to_color() + except: + pass + else: + continue + + raise _InvalidArgumentTypeError(index, + exp, type(arg), self._name) return self._func(*args) @@ -288,8 +313,8 @@ class Reference: if _default_reference is not None: return _default_reference - from ._builtin import CSS4Reference - _default_reference = CSS4Reference() + from ._builtin import DefaultReference + _default_reference = DefaultReference() return _default_reference # End of file. |