aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Touhey <thomas@touhey.fr>2019-05-10 11:53:55 +0200
committerThomas Touhey <thomas@touhey.fr>2019-05-10 11:53:55 +0200
commitaa910dfe205af86c5359002f452dac6d099ad08a (patch)
tree1f62068c0794a240549d070dc5390471a82c2633
parentd4c11d05f31c4e28c5af8988cf3e4d2b743de806 (diff)
Corrected CSS producing and added tests for it.
-rwxr-xr-xtests/test_text.py15
-rwxr-xr-xthcolor/_color.py36
2 files changed, 39 insertions, 12 deletions
diff --git a/tests/test_text.py b/tests/test_text.py
index eb3d5c3..c29bb25 100755
--- a/tests/test_text.py
+++ b/tests/test_text.py
@@ -45,4 +45,19 @@ def test_rgba(test_input, expected):
def test_hsla(test_input, expected):
assert Color.from_text(test_input).hsla() == expected
+@pytest.mark.parametrize('test_input,expected', (
+ ('blue',
+ ('#0000FF',)),
+ (' rgb (1,22, 242 , 50.0% )',
+ ('#0116F2', 'rgba(1, 22, 242, 50%)')),
+ ('darker(10%, hsl(0, 1, 50.0%))',
+ ('#CC0000', 'hsl(0deg, 100%, 40%)')),
+ ('hls(0 / 1 0.5 , 0.2)',
+ ('#FFFFFF', 'rgba(255, 255, 255, 20%)', 'hsla(0deg, 50%, 100%, 20%)')),
+ ('hbw(127 .5)',
+ ('#00800F', 'hwb(127deg, 0%, 50%)')),
+))
+def test_css(test_input, expected):
+ assert Color.from_text(test_input).css() == expected
+
# End of file.
diff --git a/thcolor/_color.py b/thcolor/_color.py
index 7850ac4..44672aa 100755
--- a/thcolor/_color.py
+++ b/thcolor/_color.py
@@ -451,6 +451,18 @@ class Color:
def css(self):
""" Get the CSS declarations (with compatibility management). """
+ def _percent(prop):
+ per = round(prop, 4) * 100
+ if per == int(per):
+ per = int(per)
+ return per
+
+ def _deg(agl):
+ agl = round(agl.degrees, 2)
+ if agl == int(agl):
+ agl = int(agl)
+ return agl
+
def statements():
# Start by yelling a #RRGGBB color, compatible with most
# web browsers around the world, followed by the rgba()
@@ -461,29 +473,29 @@ class Color:
yield f'#{r:02X}{g:02X}{b:02X}'
if a < 1.0:
- yield f'rgba({r}, {g}, {b}, {round(a, 3) * 100}%)'
+ yield f'rgba({r}, {g}, {b}, {_percent(a)}%)'
# Then yield more specific CSS declarations in case
# they're supported (which would be neat!).
- if self._type == Type.HSL:
- s = round(self._sat, 5) * 100
- l = round(self._lgt, 5) * 100
+ if self._type == Color.Type.HSL:
+ args = f'{_deg(self._hue)}deg, ' \
+ f'{_percent(self._sat)}%, {_percent(self._lgt)}%'
if a < 1.0:
- yield f'hsla({self._hue.degrees}deg, {s}%, {l}%, {a})'
+ yield f'hsla({args}, {_percent(a)}%)'
else:
- yield f'hsl({self._hue.degrees}deg, {s}%, {l}%)'
- elif self._type == Type.HWB:
- w = round(self._wht, 5) * 100
- b = round(self._blk, 5) * 100
+ yield f'hsl({args})'
+ elif self._type == Color.Type.HWB:
+ args = f'{_deg(self._hue)}deg, ' \
+ f'{_percent(self._wht)}%, {_percent(self._blk)}%'
if a < 1.0:
- yield f'hwba({self._hue.degrees}deg, {w}%, {b}%, {a})'
+ yield f'hwba({args}, {_percent(a)}%)'
else:
- yield f'hwb({self._hue.degrees}deg, {w}%, {b}%)'
+ yield f'hwb({args})'
- return list(statements())
+ return tuple(statements())
# ---
# Static methods for decoding.