diff --git a/catppuccin/__init__.py b/catppuccin/__init__.py index 4385b90..f84e51c 100644 --- a/catppuccin/__init__.py +++ b/catppuccin/__init__.py @@ -42,6 +42,23 @@ # Crust: #232634 ``` +## Lookup by name + +Flavors and colors can also be looked up via the `FlavorName` and `ColorName` +enums: + +```python +from catppuccin import PALETTE, ColorName, FlavorName + +PALETTE[FlavorName.MOCHA].colors[ColorName.RED].hex +# '#f38ba8' +PALETTE["latte"].colors["mauve"].hex +# '#8839ef' + +def accent(flavor: FlavorName, color: ColorName) -> str: + return PALETTE[flavor].colors[color].hex +``` + ## Dataclasses `Palette`, `Flavor`, `Color` et cetera are all @@ -57,7 +74,7 @@ flavor = PALETTE.frappe for field in fields(flavor.colors): - color = getattr(flavor.colors, field.name) + color = flavor.colors[field.name] print(f"{field.name}: {color.hex}") # rosewater: #f2d5cf @@ -87,6 +104,8 @@ import importlib.util +from catppuccin.models import ColorName as ColorName +from catppuccin.models import FlavorName as FlavorName from catppuccin.palette import PALETTE as PALETTE # Register colormaps if matplotlib is available diff --git a/catppuccin/extras/matplotlib.py b/catppuccin/extras/matplotlib.py index 80aa59a..b7b4101 100644 --- a/catppuccin/extras/matplotlib.py +++ b/catppuccin/extras/matplotlib.py @@ -74,8 +74,8 @@ cmap = LinearSegmentedColormap.from_list( "my_custom_palette", [ - color.hex for color in catppuccin.PALETTE.frappe.colors - if color.identifier in ["red", "peach", "yellow", "green"] + catppuccin.PALETTE.frappe.colors[name].hex + for name in ["red", "peach", "yellow", "green"] ], ) rng = np.random.default_rng() @@ -113,6 +113,6 @@ def style_name(flavor: Flavor) -> str: for _palette in PALETTE: _cmap = matplotlib.colors.LinearSegmentedColormap.from_list( style_name(_palette), - [getattr(_palette.colors, name).hex for name in DEFAULT_COLORMAP_COLORS], + [_palette.colors[name].hex for name in DEFAULT_COLORMAP_COLORS], ) mpl.colormaps.register(cmap=_cmap, force=True) diff --git a/catppuccin/models.py b/catppuccin/models.py index b4befa7..e2b17dc 100644 --- a/catppuccin/models.py +++ b/catppuccin/models.py @@ -2,8 +2,59 @@ from collections.abc import Iterator from dataclasses import dataclass +from enum import StrEnum +from typing import cast -__all__ = ["Palette", "Flavor", "FlavorColors", "Color", "RGB", "HSL"] +__all__ = [ + "Palette", + "Flavor", + "FlavorColors", + "Color", + "RGB", + "HSL", + "FlavorName", + "ColorName", +] + + +class FlavorName(StrEnum): + """Identifiers of the flavors, in palette order.""" + + LATTE = "latte" + FRAPPE = "frappe" + MACCHIATO = "macchiato" + MOCHA = "mocha" + + +class ColorName(StrEnum): + """Identifiers of the colors in a flavor, in palette order.""" + + ROSEWATER = "rosewater" + FLAMINGO = "flamingo" + PINK = "pink" + MAUVE = "mauve" + RED = "red" + MAROON = "maroon" + PEACH = "peach" + YELLOW = "yellow" + GREEN = "green" + TEAL = "teal" + SKY = "sky" + SAPPHIRE = "sapphire" + BLUE = "blue" + LAVENDER = "lavender" + TEXT = "text" + SUBTEXT1 = "subtext1" + SUBTEXT0 = "subtext0" + OVERLAY2 = "overlay2" + OVERLAY1 = "overlay1" + OVERLAY0 = "overlay0" + SURFACE2 = "surface2" + SURFACE1 = "surface1" + SURFACE0 = "surface0" + BASE = "base" + MANTLE = "mantle" + CRUST = "crust" @dataclass(frozen=True) @@ -60,6 +111,9 @@ class FlavorColors: """All of the colors for a particular flavor of Catppuccin. Can be iterated over, in which case the colors are yielded in order. + + Colors can also be looked up by identifier, such as `colors["red"]` or + `colors[ColorName.RED]`. """ rosewater: Color @@ -146,6 +200,17 @@ def __iter__(self) -> Iterator[Color]: self.crust, ] + def __getitem__(self, name: str) -> Color: + """Look up a color by identifier, such as `"red"` or `ColorName.RED`. + + Raises `KeyError` if there is no color with that identifier. + """ + try: + identifier = ColorName(name) + except ValueError: + raise KeyError(name) from None + return cast(Color, getattr(self, identifier)) + @dataclass(frozen=True) class Flavor: @@ -175,6 +240,9 @@ class Palette: Primarily used via the `PALETTE` constant. Can be iterated over, in which case the flavors are yielded in the canonical order: Latte, Frappé, Macchiato, Mocha. + + Flavors can also be looked up by identifier, such as `PALETTE["mocha"]` or + `PALETTE[FlavorName.MOCHA]`. """ latte: Flavor @@ -189,3 +257,14 @@ class Palette: def __iter__(self) -> Iterator[Flavor]: """Iterate over flavors in the palette.""" yield from [self.latte, self.frappe, self.macchiato, self.mocha] + + def __getitem__(self, name: str) -> Flavor: + """Look up a flavor by identifier, such as `"mocha"` or `FlavorName.MOCHA`. + + Raises `KeyError` if there is no flavor with that identifier. + """ + try: + identifier = FlavorName(name) + except ValueError: + raise KeyError(name) from None + return cast(Flavor, getattr(self, identifier)) diff --git a/tests/test_catppuccin.py b/tests/test_catppuccin.py index f63f816..fd24a42 100644 --- a/tests/test_catppuccin.py +++ b/tests/test_catppuccin.py @@ -1,4 +1,6 @@ -from catppuccin import PALETTE +import pytest + +from catppuccin import PALETTE, ColorName, FlavorName def test_some_colors() -> None: @@ -51,3 +53,34 @@ def test_iterate_flavor_colors() -> None: for i, color in enumerate(colors): assert order[i] == color assert list(colors) == order + + +def test_name_enums_match_models() -> None: + assert [flavor.identifier for flavor in PALETTE] == list(FlavorName) + for flavor in PALETTE: + assert [color.identifier for color in flavor.colors] == list(ColorName) + + +@pytest.mark.parametrize("flavor", list(FlavorName)) +def test_lookup_flavor(flavor: FlavorName) -> None: + assert PALETTE[flavor] is getattr(PALETTE, flavor) + assert PALETTE[str(flavor)] is PALETTE[flavor] + + +@pytest.mark.parametrize("color", list(ColorName)) +def test_lookup_color(color: ColorName) -> None: + colors = PALETTE.mocha.colors + assert colors[color] is getattr(colors, color) + assert colors[str(color)] is colors[color] + + +@pytest.mark.parametrize("name", ["", "Mocha", "colors", "__iter__", "red"]) +def test_lookup_unknown_flavor(name: str) -> None: + with pytest.raises(KeyError): + PALETTE[name] + + +@pytest.mark.parametrize("name", ["", "Red", "accent", "__iter__", "mocha"]) +def test_lookup_unknown_color(name: str) -> None: + with pytest.raises(KeyError): + PALETTE.mocha.colors[name]