aboutsummaryrefslogtreecommitdiff
path: root/thcolor/builtin.py
diff options
context:
space:
mode:
Diffstat (limited to 'thcolor/builtin.py')
-rw-r--r--thcolor/builtin.py108
1 files changed, 64 insertions, 44 deletions
diff --git a/thcolor/builtin.py b/thcolor/builtin.py
index 229a746..0179768 100644
--- a/thcolor/builtin.py
+++ b/thcolor/builtin.py
@@ -3,7 +3,7 @@
# Copyright (C) 2019-2022 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
# This file is part of the thcolor project, which is MIT-licensed.
# *****************************************************************************
-""" Builtin decoders using the base elements. """
+"""Builtin decoders using the base elements."""
from typing import Union as _Union
@@ -29,7 +29,7 @@ _number = _Union[int, float]
def _rgb(x):
- """ Return an RGB color out of the given 6-digit hexadecimal code. """
+ """Return an RGB color out of the given 6-digit hexadecimal code."""
from thcolor.colors import SRGBColor as _SRGBColor
@@ -46,9 +46,9 @@ def _rgb(x):
class CSS1ColorDecoder(_MetaColorDecoder):
- """ Named colors from CSS Level 1.
+ """Named colors from CSS Level 1.
- See `<https://www.w3.org/TR/CSS1/>`_ for more information.
+ See `<https://www.w3.org/TR/CSS1/>`_ for more information.
"""
__defaults_to_netscape_color__ = True
@@ -73,8 +73,9 @@ class CSS1ColorDecoder(_MetaColorDecoder):
transparent = _SRGBColor(0, 0, 0, 0)
+ @staticmethod
def rgb(red: int = 0, green: int = 0, blue: int = 0) -> _Color:
- """ Make an RGB color out of the given components. """
+ """Make an RGB color out of the given components."""
return _SRGBColor(
red=_factor(red, max_=255, clip=True),
@@ -85,18 +86,18 @@ class CSS1ColorDecoder(_MetaColorDecoder):
class CSS2ColorDecoder(CSS1ColorDecoder):
- """ Named colors from CSS Level 2 (Revision 1).
+ """Named colors from CSS Level 2 (Revision 1).
- See `<https://www.w3.org/TR/CSS2/>`_ for more information.
+ See `<https://www.w3.org/TR/CSS2/>`_ for more information.
"""
orange = _rgb('#ffa500')
class CSS3ColorDecoder(CSS2ColorDecoder):
- """ Named colors and functions from CSS Color Module Level 3.
+ """Named colors and functions from CSS Color Module Level 3.
- See `<https://drafts.csswg.org/css-color-3/>`_ for more information.
+ See `<https://drafts.csswg.org/css-color-3/>`_ for more information.
"""
darkblue = _rgb('#00008B')
@@ -230,13 +231,14 @@ class CSS3ColorDecoder(CSS2ColorDecoder):
lightyellow = _rgb('#FFFFE0')
ivory = _rgb('#FFFFF0')
+ @staticmethod
def rgb(
red: _number = 0,
green: _number = 0,
blue: _number = 0,
alpha: _number = 1.0,
) -> _Color:
- """ Make an RGB color out of the given components. """
+ """Make an RGB color out of the given components."""
return _SRGBColor(
red=_factor(red, max_=255, clip=True),
@@ -245,13 +247,14 @@ class CSS3ColorDecoder(CSS2ColorDecoder):
alpha=_factor(alpha, clip=True),
)
+ @staticmethod
def hsl(
hue: _Angle,
saturation: _number,
lightness: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make an HSL color out of the given components. """
+ """Make an HSL color out of the given components."""
return _HSLColor(
hue=hue,
@@ -265,22 +268,23 @@ class CSS3ColorDecoder(CSS2ColorDecoder):
class CSS4ColorDecoder(CSS3ColorDecoder):
- """ Named colors and functions from CSS Color Module Level 4.
+ """Named colors and functions from CSS Color Module Level 4.
- See `<https://drafts.csswg.org/css-color/>`_ for more information..
+ See `<https://drafts.csswg.org/css-color/>`_ for more information..
"""
__extended_hex_support__ = True
rebeccapurple = _rgb('#663399')
+ @staticmethod
def hwb(
hue: _Angle,
whiteness: _number = 0.0,
blackness: _number = 0.0,
alpha: _number = 1.0,
) -> _Color:
- """ Make an HWB color out of the given components. """
+ """Make an HWB color out of the given components."""
return _HWBColor(
hue=hue,
@@ -289,19 +293,21 @@ class CSS4ColorDecoder(CSS3ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def gray(gray: _number, alpha: _number = 1.0) -> _Color:
- """ Make a gray-scale color out of the given components. """
+ """Make a gray-scale color out of the given components."""
gray = _factor(gray, max_=255)
return _SRGBColor(gray, gray, gray, _factor(alpha))
+ @staticmethod
def lab(
light: _number,
a: _number,
b: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make an LAB color out of the given components. """
+ """Make an LAB color out of the given components."""
return _LABColor(
lightness=max(_factor(light), 0.0),
@@ -309,16 +315,17 @@ class CSS4ColorDecoder(CSS3ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def lch(
light: _number,
chroma: _number,
hue: _Angle,
alpha: _number = 1.0,
) -> _Color:
- """ Make an LCH color out of the given components. """
+ """Make an LCH color out of the given components."""
return _LCHColor(
- light=max(_factor(light), 0.0),
+ lightness=max(_factor(light), 0.0),
chroma=max(chroma, 0.0),
hue=hue,
alpha=_factor(alpha),
@@ -328,7 +335,7 @@ class CSS4ColorDecoder(CSS3ColorDecoder):
class DefaultColorDecoder(CSS4ColorDecoder):
- """ Functions extending the CSS Color Module Level 4 reference. """
+ """Functions extending the CSS Color Module Level 4 reference."""
__ncol_support__ = True
@@ -348,6 +355,7 @@ class DefaultColorDecoder(CSS4ColorDecoder):
hbwa = _alias('hwb', args=('hue', 'blackness', 'whiteness', 'alpha'))
device_cmyk = _alias('cmyk')
+ @staticmethod
def cmyk(
cyan: _number,
magenta: _number = 0.0,
@@ -355,7 +363,7 @@ class DefaultColorDecoder(CSS4ColorDecoder):
black: _number = 0.0,
alpha: _number = 1.0,
) -> _Color:
- """ Make a CMYK color out of the given components. """
+ """Make a CMYK color out of the given components."""
return _CMYKColor(
cyan=_factor(cyan),
@@ -365,13 +373,14 @@ class DefaultColorDecoder(CSS4ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def hsv(
hue: _Angle,
saturation: _number,
value: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make an HSV color out of the given components. """
+ """Make an HSV color out of the given components."""
return _HSVColor(
hue=hue,
@@ -380,13 +389,14 @@ class DefaultColorDecoder(CSS4ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def xyz(
x: _number,
y: _number,
z: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make an XYZ color out of the given components. """
+ """Make an XYZ color out of the given components."""
return _XYZColor(
x=_factor(x),
@@ -395,13 +405,14 @@ class DefaultColorDecoder(CSS4ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def yiq(
y: _number,
i: _number,
q: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make a YIQ color out of the given components. """
+ """Make a YIQ color out of the given components."""
return _YIQColor(
y=_factor(y),
@@ -410,13 +421,14 @@ class DefaultColorDecoder(CSS4ColorDecoder):
alpha=_factor(alpha),
)
+ @staticmethod
def yuv(
y: _number,
u: _number,
v: _number,
alpha: _number = 1.0,
) -> _Color:
- """ Make a YUV color out of the given components. """
+ """Make a YUV color out of the given components."""
return _YUVColor(
y=_factor(y),
@@ -430,62 +442,69 @@ class DefaultColorDecoder(CSS4ColorDecoder):
# ---
@_fallback(_rgb('#ff0000'))
+ @staticmethod
def red(color: _Color) -> int:
- """ Get the red channel value from an RGB color. """
+ """Get the red channel value from an RGB color."""
r, g, b, _ = color.assrgb()
return r
@_fallback(_rgb('#00ff00'))
+ @staticmethod
def green(color: _Color) -> int:
- """ Get the green channel value from an RGB color. """
+ """Get the green channel value from an RGB color."""
r, g, b, _ = color.assrgb()
return g
@_fallback(_rgb('#0000ff'))
+ @staticmethod
def blue(color: _Color) -> int:
- """ Get the blue channel value from an RGB color. """
+ """Get the blue channel value from an RGB color."""
- r, g, b, _ = color.ascolor().assrgb()
+ r, g, b, _ = color.assrgb()
return b
# ---
# Manage the lightness and saturation for HSL colors.
# ---
+ @staticmethod
def darker(by: float, color: _Color) -> _Color:
- """ Make the color darker by a given factor.
+ """Make the color darker by a given factor.
- This is accomplished by calling
- :py:meth:`thcolor.colors.Color.darker`.
+ This is accomplished by calling
+ :py:meth:`thcolor.colors.Color.darker`.
"""
return color.darker(by)
+ @staticmethod
def lighter(by: float, color: _Color) -> _Color:
- """ Make the color lighter by a given factor.
+ """Make the color lighter by a given factor.
- This is accomplished by calling
- :py:meth:`thcolor.colors.Color.lighter`.
+ This is accomplished by calling
+ :py:meth:`thcolor.colors.Color.lighter`.
"""
return color.lighter(by)
+ @staticmethod
def desaturate(by: float, color: _Color) -> _Color:
- """ Desaturate the color by a given factor.
+ """Desaturate the color by a given factor.
- This is accomplished by calling
- :py:meth:`thcolor.colors.Color.desaturate`.
+ This is accomplished by calling
+ :py:meth:`thcolor.colors.Color.desaturate`.
"""
return color.desaturate(by)
+ @staticmethod
def saturate(by: float, color: _Color) -> _Color:
- """ Saturate the color by a given factor.
+ """Saturate the color by a given factor.
- This is accomplished by calling
- :py:meth:`thcolor.colors.Color.saturate`.
+ This is accomplished by calling
+ :py:meth:`thcolor.colors.Color.saturate`.
"""
return color.saturate(by)
@@ -494,12 +513,13 @@ class DefaultColorDecoder(CSS4ColorDecoder):
# Others.
# ---
+ @staticmethod
def ncol(color: _Color) -> _Color:
- """ Return a natural color (NCol).
+ """Return a natural color (NCol).
- This method is actually compatibility with w3color.js.
- NCols are managed directly without the function, so
- the function only needs to return the color.
+ This method is actually compatibility with w3color.js.
+ NCols are managed directly without the function, so
+ the function only needs to return the color.
"""
return color