Skip to content

color

Color

Color()

Bases: ABC

Base class for all color representations.

Source code in stdl/color.py
def __init__(self) -> None:
    object.__setattr__(self, "_frozen", False)

to_rgb abstractmethod

to_rgb() -> RGB

Convert to RGB color space.

Source code in stdl/color.py
@abstractmethod
def to_rgb(self) -> RGB:
    """Convert to RGB color space."""

to_hex abstractmethod

to_hex() -> HEX

Convert to HEX color space.

Source code in stdl/color.py
@abstractmethod
def to_hex(self) -> HEX:
    """Convert to HEX color space."""

to_hsv abstractmethod

to_hsv() -> HSV

Convert to HSV color space.

Source code in stdl/color.py
@abstractmethod
def to_hsv(self) -> HSV:
    """Convert to HSV color space."""

to_hsl abstractmethod

to_hsl() -> HSL

Convert to HSL color space.

Source code in stdl/color.py
@abstractmethod
def to_hsl(self) -> HSL:
    """Convert to HSL color space."""

to_cmyk abstractmethod

to_cmyk() -> CMYK

Convert to CMYK color space.

Source code in stdl/color.py
@abstractmethod
def to_cmyk(self) -> CMYK:
    """Convert to CMYK color space."""

to_assa abstractmethod

to_assa() -> ASSA

Convert to ASS color format.

Source code in stdl/color.py
@abstractmethod
def to_assa(self) -> ASSA:
    """Convert to ASS color format."""

ASSA

ASSA(value: str)

Bases: Color

Initialize ASSA color with hex digits.

Source code in stdl/color.py
def __init__(self, value: str) -> None:
    """Initialize ASSA color with hex digits."""
    super().__init__()
    clean_value = self._normalize_value(value)
    self.value = f"&H{clean_value}&"
    self.validate()
    self._freeze()

embed_text

embed_text(text: str) -> str

Embed text with this color in ASS format.

Source code in stdl/color.py
def embed_text(self, text: str) -> str:
    """Embed text with this color in ASS format."""
    return f"{{\\c{self.value}}}{text}{{\\c}}"

from_value classmethod

from_value(value: str) -> ASSA

Create an ASSA color from a string (with or without &H markers).

Source code in stdl/color.py
@classmethod
def from_value(cls, value: str) -> ASSA:
    """Create an ASSA color from a string (with or without &H markers)."""
    return cls(value)

normalize_color

normalize_color(color: str | CssColorName | tuple[int, int, int] | tuple[int, int, int, float] | Sequence[int] | Sequence[float]) -> RGB | RGBA | HEX | ASSA | webcolor

Convert various color formats to a Color object.

Source code in stdl/color.py
def normalize_color(
    color: (
        str
        | CssColorName
        | tuple[int, int, int]
        | tuple[int, int, int, float]
        | Sequence[int]
        | Sequence[float]
    ),
) -> RGB | RGBA | HEX | ASSA | webcolor:
    """Convert various color formats to a Color object."""
    if (
        isinstance(color, SequenceType)
        and not isinstance(color, (str, bytes, bytearray))
        and all(isinstance(x, (int, float)) and not isinstance(x, bool) for x in color)
    ):
        if len(color) == 3:
            return RGB(*color)
        if len(color) == 4:
            return RGBA(*color)

        raise ColorValueError(f"Invalid sequence length: {len(color)}")

    if isinstance(color, str):
        if color.startswith("#"):
            return HEX(color)
        if color.startswith(("&h", "&H")):
            return ASSA.from_value(color)

        return WebColor(color)

    raise ColorValueError(f"Unsupported color format: {color}")