aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--docs/conf.py10
-rwxr-xr-xthcolor/__init__.py2
-rwxr-xr-xthcolor/_builtin/_css.py8
-rwxr-xr-xthcolor/_color.py55
4 files changed, 51 insertions, 24 deletions
diff --git a/docs/conf.py b/docs/conf.py
index ee5a7c5..91fe9c9 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -22,8 +22,16 @@ copyright = '2019, Thomas Touhey'
author = 'Thomas Touhey'
# The full version, including alpha/beta/rc tags
-release = '0.3'
+def _get_release():
+ from os.path import dirname, join
+ from pkg_resources import find_distributions as find_dist
+
+ module_path = join(dirname(__file__), '..')
+ dist = next(find_dist(module_path, True))
+ return dist.version
+
+release = _get_release()
# -- General configuration ---------------------------------------------------
diff --git a/thcolor/__init__.py b/thcolor/__init__.py
index 545bbb4..c2c7536 100755
--- a/thcolor/__init__.py
+++ b/thcolor/__init__.py
@@ -23,6 +23,6 @@ __all__ = ["version", "Color", "Reference", "Angle",
"CSS1Reference", "CSS2Reference", "CSS3Reference",
"CSS4Reference", "DefaultReference"]
-version = "0.3"
+version = "0.3.1"
# End of file.
diff --git a/thcolor/_builtin/_css.py b/thcolor/_builtin/_css.py
index 06bf539..6b49ff9 100755
--- a/thcolor/_builtin/_css.py
+++ b/thcolor/_builtin/_css.py
@@ -13,8 +13,12 @@ from .._exc import InvalidArgumentValueError as _InvalidArgumentValueError
__all__ = ["CSS1Reference", "CSS2Reference", "CSS3Reference",
"CSS4Reference"]
-def _rgb(hex):
- return _Color.from_text(hex, _Reference())
+def _rgb(raw):
+ r = int(raw[1:3], 16)
+ g = int(raw[3:5], 16)
+ b = int(raw[5:7], 16)
+
+ return _Color(_Color.Type.RGB, r, g, b)
class CSS1Reference(_Reference):
""" Named colors from CSS Level 1:
diff --git a/thcolor/_color.py b/thcolor/_color.py
index 9e62072..55795d2 100755
--- a/thcolor/_color.py
+++ b/thcolor/_color.py
@@ -9,12 +9,14 @@
from enum import Enum as _Enum
from warnings import warn as _warn
+_gg_no_re = False
+
try:
import regex as _re
except ImportError:
- _warn("could not import regex, trying the default `re` module.",
+ _warn("could not import regex, text parsing is disabled.",
RuntimeWarning)
- import re as _re
+ _gg_no_re = True
from ._ref import Reference as _Reference
from ._angle import Angle as _Angle
@@ -35,20 +37,32 @@ __all__ = ["Color"]
# Decoding utilities.
# ---
-_ColorPattern = _re.compile(r"""
- (
- ((?P<agl_val>-? ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*
- (?P<agl_typ>deg|grad|rad|turn))
- | ((?P<per>[0-9]+(\.[0-9]*)? | \.[0-9]+) \s* \%)
- | (?P<num>[0-9]+(\.[0-9]*)? | \.[0-9]+)
- | (?P<ncol>[RYGCBM] [0-9]{0,2} (\.[0-9]*)?)
- | (\# (?P<hex>[0-9a-f]{3} | [0-9a-f]{4} | [0-9a-f]{6} | [0-9a-f]{8}))
- | ((?P<name>[a-z]([a-z0-9\s_-]*[a-z0-9_-])?)
- ( \s* \( \s* (?P<arg> (?0)? ) \s* \) )?)
- )
-
- \s* ((?P<sep>[,/\s]) \s* (?P<nextargs> (?0))?)?
-""", _re.VERBOSE | _re.I | _re.M)
+_color_pattern = None
+
+def _get_color_pattern():
+ global _color_pattern
+
+ if _color_pattern is None:
+ if _gg_no_re:
+ raise ImportError("text parsing is disabled until you install " \
+ "the 'regex' module, e.g. via `pip install regex`.")
+
+ _color_pattern = _re.compile(r"""
+ (
+ ((?P<agl_val>-? ([0-9]+\.?|[0-9]*\.[0-9]+)) \s*
+ (?P<agl_typ>deg|grad|rad|turn))
+ | ((?P<per>[0-9]+(\.[0-9]*)? | \.[0-9]+) \s* \%)
+ | (?P<num>[0-9]+(\.[0-9]*)? | \.[0-9]+)
+ | (?P<ncol>[RYGCBM] [0-9]{0,2} (\.[0-9]*)?)
+ | (\# (?P<hex>[0-9a-f]{3} | [0-9a-f]{4} | [0-9a-f]{6} | [0-9a-f]{8}))
+ | ((?P<name>[a-z]([a-z0-9\s_-]*[a-z0-9_-])?)
+ ( \s* \( \s* (?P<arg> (?0)? ) \s* \) )?)
+ )
+
+ \s* ((?P<sep>[,/\s]) \s* (?P<nextargs> (?0))?)?
+ """, _re.VERBOSE | _re.I | _re.M)
+
+ return _color_pattern
# ---
# Color initialization varargs utilities.
@@ -582,7 +596,7 @@ class Color:
# Get the arguments.
args = recurse(column + match.start('arg'),
- _ColorPattern.fullmatch(match['arg']))
+ _get_color_pattern().fullmatch(match['arg']))
# Get the function and call it with the arguments.
@@ -631,7 +645,8 @@ class Color:
# Get the following arguments and check.
args = recurse(column + match.start('nextargs'),
- _ColorPattern.fullmatch(match['nextargs'] or ""))
+ _get_color_pattern().fullmatch(match['nextargs'] \
+ or ""))
try:
assert len(args) >= 2
@@ -671,7 +686,7 @@ class Color:
return (argument(column, value),) \
+ recurse(column + match.start('nextargs'),
- _ColorPattern.fullmatch(match['nextargs'] or ""))
+ _get_color_pattern().fullmatch(match['nextargs'] or ""))
# Strip the expression.
@@ -682,7 +697,7 @@ class Color:
# Match the expression (and check it as a whole directly).
- match = _ColorPattern.fullmatch(expr)
+ match = _get_color_pattern().fullmatch(expr)
if match is None:
raise _ColorExpressionDecodingError("expression parsing failed")