Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions catppuccin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
44 changes: 44 additions & 0 deletions catppuccin/palette.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Catppuccin palette definition."""

from enum import Enum

from catppuccin.models import HSL, RGB, Color, Flavor, FlavorColors, Palette

PALETTE = Palette(
Expand Down Expand Up @@ -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"
10 changes: 9 additions & 1 deletion tests/test_catppuccin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from catppuccin import PALETTE
from catppuccin import PALETTE, ColorName
from catppuccin.palette import FlavorName


def test_some_colors() -> None:
Expand Down Expand Up @@ -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
)