aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Touhey <thomas@touhey.fr>2019-05-21 15:33:48 +0200
committerThomas Touhey <thomas@touhey.fr>2019-05-21 15:33:48 +0200
commitb3edd31df8c96035d70c0216e444d6dd9951699f (patch)
tree1990a3b3d82b2f078777074512c6c099423a709f
parent191aa991c32b9e0124bf8256c1ae7c130b74b248 (diff)
Added an alias directive.HEADmaster
-rw-r--r--docs/Makefile3
-rwxr-xr-xthcolor/_ref.py56
-rwxr-xr-xthcolor/_sys.py2
-rwxr-xr-xthcolor/builtin/_css.py17
-rwxr-xr-xthcolor/builtin/_default.py43
5 files changed, 61 insertions, 60 deletions
diff --git a/docs/Makefile b/docs/Makefile
index 871993a..28d11c9 100644
--- a/docs/Makefile
+++ b/docs/Makefile
@@ -7,8 +7,7 @@ SPHINXBUILD = sphinx-build
SPHINXWATCH = sphinx-autobuild
SOURCEDIR = .
BUILDDIR = _build
-
-WEBROOT := thcolor.touhey.pro:thcolor_doc
+WEBROOT = thcolor.touhey.pro:thcolor_doc
# Put it first so that "make" without argument is like "make help".
help:
diff --git a/thcolor/_ref.py b/thcolor/_ref.py
index 1229634..8a39cbb 100755
--- a/thcolor/_ref.py
+++ b/thcolor/_ref.py
@@ -252,6 +252,23 @@ class Reference:
return self._value
# ---
+ # Function helper.
+ # ---
+
+ def alias(*names):
+ """ Decorator for aliasing with another name. Defines a lot, you
+ know. """
+
+ def _decorator(func):
+ if not hasattr(func, '_ref_aliases'):
+ func._ref_aliases = ()
+
+ func._ref_aliases += names
+ return func
+
+ return _decorator
+
+ # ---
# Function and named color getters.
# ---
@@ -275,18 +292,41 @@ class Reference:
def __getitem__(self, name):
fref = self._fref
+ found = False
- # Find the method.
-
- if name[0:1] == '_' or name in ('functions', 'named',
- 'default'):
- raise KeyError(repr(name))
+ # First, check if the function name is valid and if the
+ # method exists.
members = dict(_getmembers(fref, predicate = _ismethod))
+ validname = lambda n: type(n) == str and n[0:1] != '_' \
+ and n not in ('functions', 'named', 'default', 'alias')
- try:
- method = members[name]
- except (KeyError, AssertionError):
+ if validname(name):
+ try:
+ method = members[name]
+ found = True
+ except (KeyError, AssertionError):
+ pass
+
+ # Then, if we haven't found the method, check the aliases.
+
+ if not found:
+ for member in members.values():
+ try:
+ aliases = member._ref_aliases
+ except (AttributeError, AssertionError):
+ continue
+
+ if name not in aliases:
+ continue
+ method = member
+ found = True
+ break
+
+ # If we still haven't found the method, well… time to raise
+ # the not found exception.
+
+ if not found:
raise KeyError(repr(name))
# Make a function separated from the class, copy the
diff --git a/thcolor/_sys.py b/thcolor/_sys.py
index 23c1ec0..014270a 100755
--- a/thcolor/_sys.py
+++ b/thcolor/_sys.py
@@ -33,7 +33,7 @@ def _rgb_to_lrgb(r, g, b):
return _pow((val + 0.055) / 1.055, 2.4)
- return (*map(_linearize, (r, g, b)))
+ return tuple(map(_linearize, (r, g, b)))
def hls_to_rgb(hue, l, s):
""" Convert HLS to RGB. """
diff --git a/thcolor/builtin/_css.py b/thcolor/builtin/_css.py
index f60cdaa..ac9e434 100755
--- a/thcolor/builtin/_css.py
+++ b/thcolor/builtin/_css.py
@@ -309,24 +309,17 @@ class CSS3Reference(CSS2Reference):
# Functions.
# ---
+ @_Reference.alias('rgba')
def rgb(self, r: number | percentage,
g: number | percentage = number(0), b: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (0, 1, 2))
- def rgba(self, r: number | percentage,
- g: number | percentage = number(0), b: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (0, 1, 2))
-
+ @_Reference.alias('hsla')
def hsl(self, h: number | angle, s: number | percentage,
l: number | percentage, alpha: number | percentage = number(1.0)):
return self._hsl((h, s, l, alpha), (0, 1, 2))
- def hsla(self, h: number | angle, s: number | percentage,
- l: number | percentage, alpha: number | percentage = number(1.0)):
- return self._hsl((h, s, l, alpha), (0, 1, 2))
-
class CSS4Reference(CSS3Reference):
""" Named colors and functions from `CSS Color Module Level 4
<https://drafts.csswg.org/css-color/>`_. """
@@ -386,16 +379,12 @@ class CSS4Reference(CSS3Reference):
# Functions.
# ---
+ @_Reference.alias('hwba')
def hwb(self, h: number | angle, w: number | percentage = number(0),
b: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._hwb((h, w, b, alpha), (0, 1, 2))
- def hwba(self, h: number | angle, w: number | percentage = number(0),
- b: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._hwb((h, w, b, alpha), (0, 1, 2))
-
def gray(self, g: number | percentage,
alpha: number | percentage = number(1.0)):
try:
diff --git a/thcolor/builtin/_default.py b/thcolor/builtin/_default.py
index 31b1434..dd54a2b 100755
--- a/thcolor/builtin/_default.py
+++ b/thcolor/builtin/_default.py
@@ -27,78 +27,51 @@ class DefaultReference(_CSS4Reference):
# RGB functions.
# ---
+ @_Reference.alias('rbga')
def rbg(self, r: number | percentage,
b: number | percentage = number(0), g: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (0, 2, 1))
- def rbga(self, r: number | percentage,
- b: number | percentage = number(0), g: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (0, 2, 1))
-
+ @_Reference.alias('brga')
def brg(self, b: number | percentage,
r: number | percentage = number(0), g: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (1, 2, 0))
- def brga(self, b: number | percentage,
- r: number | percentage = number(0), g: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (1, 2, 0))
-
+ @_Reference.alias('bgra')
def bgr(self, b: number | percentage,
g: number | percentage = number(0), r: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (2, 1, 0))
- def bgra(self, b: number | percentage,
- g: number | percentage = number(0), r: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (2, 1, 0))
-
+ @_Reference.alias('gbra')
def gbr(self, g: number | percentage,
b: number | percentage = number(0), r: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (2, 0, 1))
- def gbra(self, g: number | percentage,
- b: number | percentage = number(0), r: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (2, 0, 1))
-
+ @_Reference.alias('grba')
def grb(self, g: number | percentage,
r: number | percentage = number(0), b: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._rgb((r, g, b, alpha), (1, 0, 2))
- def grba(self, g: number | percentage,
- r: number | percentage = number(0), b: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._rgb((r, g, b, alpha), (1, 0, 2))
-
# ---
# HLS and HWB aliases.
# ---
+ @_Reference.alias('hlsa')
def hls(self, h: number | angle, l: number | percentage,
s: number | percentage, alpha: number | percentage = number(1.0)):
return self._hsl((h, s, l, alpha), (0, 2, 1))
- def hlsa(self, h: number | angle, l: number | percentage,
- s: number | percentage, alpha: number | percentage = number(1.0)):
- return self._hsl((h, s, l, alpha), (0, 2, 1))
-
+ @_Reference.alias('hbwa')
def hbw(self, h: number | angle, b: number | percentage = number(0),
w: number | percentage = number(0),
alpha: number | percentage = number(1.0)):
return self._hwb((h, w, b, alpha), (0, 2, 1))
- def hbwa(self, h: number | angle, b: number | percentage = number(0),
- w: number | percentage = number(0),
- alpha: number | percentage = number(1.0)):
- return self._hwb((h, w, b, alpha), (0, 2, 1))
-
# ---
# CMYK utilities and extensions.
# ---
@@ -252,7 +225,7 @@ class DefaultReference(_CSS4Reference):
def ncol(self, col: color) -> color:
# Compatibility with w3color.js! NCols are managed directly without
- # the function, so the functio doesn't do anything.
+ # the function, so the function doesn't do anything.
return col