diff options
Diffstat (limited to 'thcolor/_builtin/_default.py')
-rwxr-xr-x | thcolor/_builtin/_default.py | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/thcolor/_builtin/_default.py b/thcolor/_builtin/_default.py index 8ae5768..b047519 100755 --- a/thcolor/_builtin/_default.py +++ b/thcolor/_builtin/_default.py @@ -24,7 +24,7 @@ class DefaultReference(_CSS4Reference): color = _Reference.color # --- - # Utilities. + # RGB functions. # --- def rbg(self, r: number | percentage, @@ -77,6 +77,10 @@ class DefaultReference(_CSS4Reference): alpha: number | percentage = number(1.0)): return self._rgb((r, g, b, alpha), (1, 0, 2)) + # --- + # HLS and HWB aliases. + # --- + 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)) @@ -95,6 +99,26 @@ class DefaultReference(_CSS4Reference): alpha: number | percentage = number(1.0)): return self._hwb((h, w, b, alpha), (0, 2, 1)) + # --- + # Get the RGB components of a color. + # --- + + def red(self, col: color) -> number: + r, g, b = col.to_color().rgb() + return _Reference.number(r) + + def green(self, col: color) -> number: + r, g, b = col.to_color().rgb() + return _Reference.number(g) + + def blue(self, col: color) -> number: + r, g, b = col.to_color().rgb() + return _Reference.number(b) + + # --- + # Manage the lightness and saturation for HSL colors. + # --- + def darker(self, by: number | percentage, col: color) -> color: try: by = by.to_factor() @@ -127,4 +151,46 @@ class DefaultReference(_CSS4Reference): return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a)) + def desaturate(self, by: number | percentage, col: color) -> color: + try: + by = by.to_factor() + except ValueError as e: + raise _InvalidArgumentValueError(0, str(e)) + + try: + col = col.to_color() + except ValueError as e: + raise _InvalidArgumentValueError(0, str(e)) + + h, l, s, a = col.hlsa() + s = max(s - by, 0.0) + + return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a)) + + def saturate(self, by: number | percentage, col: color) -> color: + try: + by = by.to_factor() + except ValueError as e: + raise _InvalidArgumentValueError(0, str(e)) + + try: + col = col.to_color() + except ValueError as e: + raise _InvalidArgumentValueError(0, str(e)) + + h, l, s, a = col.hlsa() + s = min(s + by, 1.0) + + return _Reference.color(_Color(_Color.Type.HSL, h, s, l, a)) + + # --- + # Others. + # --- + + def ncol(self, col: color) -> color: + # Compatibility with w3color.js! NCols are managed directly without + # the function, so the functio doesn't do anything. + + return col + # End of file. |