Skip to content
Merged
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
21 changes: 20 additions & 1 deletion catppuccin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions catppuccin/extras/matplotlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
81 changes: 80 additions & 1 deletion catppuccin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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))
35 changes: 34 additions & 1 deletion tests/test_catppuccin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from catppuccin import PALETTE
import pytest

from catppuccin import PALETTE, ColorName, FlavorName


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