diff options
Diffstat (limited to 'thcolor/_color.py')
-rwxr-xr-x | thcolor/_color.py | 36 |
1 files changed, 24 insertions, 12 deletions
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. |