diff --git a/build.py b/build.py index 44d7540..412dc22 100644 --- a/build.py +++ b/build.py @@ -6,12 +6,14 @@ import subprocess from dataclasses import asdict from pathlib import Path -from typing import Any, cast +from typing import Any, Iterable, cast from catppuccin.models import HSL, RGB, Color, Flavor, FlavorColors, Palette from example_plots import example_plots, plot_palette HEADER = '''"""Catppuccin palette definition.""" +from enum import Enum + from catppuccin.models import HSL, RGB, Color, Flavor, FlavorColors, Palette''' DPI = 200 @@ -52,6 +54,29 @@ def make_flavor(identifier: str, fields: dict[str, Any]) -> Flavor: ) +# we unfortunately can't just `repr` the *name enums. +def make_flavorname_enum(names: Iterable[str]) -> str: + """Create a `FlavorName` enum class from a list of flavor names.""" + lines = [ + "class FlavorName(str, Enum):", + ' """Enumeration of flavor names."""', + " __slots__ = ()", + *[f" {name.upper()} = '{name}'" for name in names], + ] + return "\n".join(lines) + + +def make_colorname_enum(names: Iterable[str]) -> str: + """Create a `ColorName` enum class from a list of color names.""" + lines = [ + "class ColorName(str, Enum):", + ' """Enumeration of color names."""', + " __slots__ = ()", + *[f" {name.upper()} = '{name}'" for name in names], + ] + return "\n".join(lines) + + def codegen() -> str: """Generate contents of `catppuccin/palette.py`.""" palette_json = load_palette_json() @@ -65,6 +90,8 @@ def codegen() -> str: lines = [ HEADER, f"PALETTE = {palette!r}", + make_flavorname_enum(palette_json.keys()), + make_colorname_enum(palette_json["latte"]["colors"].keys()), ] return "\n".join(lines) diff --git a/catppuccin/__init__.py b/catppuccin/__init__.py index af89d48..90e8202 100644 --- a/catppuccin/__init__.py +++ b/catppuccin/__init__.py @@ -2,9 +2,9 @@ import importlib.util -from catppuccin.palette import PALETTE +from catppuccin.palette import PALETTE, ColorName, FlavorName -__all__ = ["PALETTE"] +__all__ = ["PALETTE", "ColorName", "FlavorName"] # Attempt to register styles and colormaps if matplotlib is available if importlib.util.find_spec("matplotlib") is not None: diff --git a/catppuccin/palette.py b/catppuccin/palette.py index e871753..696043d 100644 --- a/catppuccin/palette.py +++ b/catppuccin/palette.py @@ -1,5 +1,7 @@ """Catppuccin palette definition.""" +from enum import Enum + from catppuccin.models import HSL, RGB, Color, Flavor, FlavorColors, Palette PALETTE = Palette( @@ -1140,3 +1142,45 @@ ), ), ) + + +class FlavorName(str, Enum): + """Enumeration of flavor names.""" + + __slots__ = () + LATTE = "latte" + FRAPPE = "frappe" + MACCHIATO = "macchiato" + MOCHA = "mocha" + + +class ColorName(str, Enum): + """Enumeration of color names.""" + + __slots__ = () + 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" diff --git a/tests/test_catppuccin.py b/tests/test_catppuccin.py index f63f816..c5a430d 100644 --- a/tests/test_catppuccin.py +++ b/tests/test_catppuccin.py @@ -1,4 +1,5 @@ -from catppuccin import PALETTE +from catppuccin import PALETTE, ColorName +from catppuccin.palette import FlavorName def test_some_colors() -> None: @@ -51,3 +52,10 @@ def test_iterate_flavor_colors() -> None: for i, color in enumerate(colors): assert order[i] == color assert list(colors) == order + + +def test_name_enums() -> None: + assert ( + getattr(getattr(PALETTE, FlavorName.LATTE).colors, ColorName.ROSEWATER) + == PALETTE.latte.colors.rosewater + )