Skip to content

Commit 411e2df

Browse files
authored
feat: add iteration over Palette and FlavorColors (#30)
1 parent 8695144 commit 411e2df

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ PALETTE.mocha.colors.teal.rgb
3434

3535
The `Palette` data structure matches [the palette JSON](https://github.com/catppuccin/palette/blob/main/palette.json).
3636

37+
### Iteration
38+
39+
Both `Palette` and `FlavorColors` can be iterated to yield flavors and colors respectively:
40+
41+
```python
42+
for flavor in PALETTE:
43+
print(flavor.name)
44+
45+
# Latte
46+
# Frappé
47+
# Macchiato
48+
# Mocha
49+
50+
for color in PALETTE.latte.colors:
51+
print(f"{color.name}: {color.hex}")
52+
53+
# Rosewater: #f2d5cf
54+
# Flamingo: #eebebe
55+
# Pink: #f4b8e4
56+
# ...
57+
# Base: #303446
58+
# Mantle: #292c3c
59+
# Crust: #232634
60+
```
61+
3762
### dataclasses
3863

3964
`Palette`, `Flavor`, `Color` et cetera are all [`dataclasses`](https://docs.python.org/3/library/dataclasses.html),

catppuccin/models.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Dataclass definitions for the Catppuccin palette data structure."""
22
from dataclasses import dataclass
3+
from typing import Iterator
34

45

56
@dataclass(frozen=True)
@@ -63,6 +64,37 @@ class FlavorColors:
6364
mantle: Color
6465
crust: Color
6566

67+
def __iter__(self) -> Iterator[Color]:
68+
"""Iterate over colors in the flavor."""
69+
yield from [
70+
self.rosewater,
71+
self.flamingo,
72+
self.pink,
73+
self.mauve,
74+
self.red,
75+
self.maroon,
76+
self.peach,
77+
self.yellow,
78+
self.green,
79+
self.teal,
80+
self.sky,
81+
self.sapphire,
82+
self.blue,
83+
self.lavender,
84+
self.text,
85+
self.subtext1,
86+
self.subtext0,
87+
self.overlay2,
88+
self.overlay1,
89+
self.overlay0,
90+
self.surface2,
91+
self.surface1,
92+
self.surface0,
93+
self.base,
94+
self.mantle,
95+
self.crust,
96+
]
97+
6698

6799
@dataclass(frozen=True)
68100
class Flavor:
@@ -91,3 +123,7 @@ class Palette:
91123
frappe: Flavor
92124
macchiato: Flavor
93125
mocha: Flavor
126+
127+
def __iter__(self) -> Iterator[Flavor]:
128+
"""Iterate over flavors in the palette."""
129+
yield from [self.latte, self.frappe, self.macchiato, self.mocha]

tests/test_catppuccin.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,45 @@ def test_some_colors() -> None:
66
assert PALETTE.latte.colors.mauve.rgb.r == 136
77
assert PALETTE.latte.colors.mauve.rgb.g == 57
88
assert PALETTE.latte.colors.mauve.rgb.b == 239
9+
10+
11+
def test_iterate_palette() -> None:
12+
order = [PALETTE.latte, PALETTE.frappe, PALETTE.macchiato, PALETTE.mocha]
13+
for i, flavor in enumerate(PALETTE):
14+
assert order[i] == flavor
15+
assert list(PALETTE) == order
16+
17+
18+
def test_iterate_flavor_colors() -> None:
19+
colors = PALETTE.latte.colors
20+
order = [
21+
colors.rosewater,
22+
colors.flamingo,
23+
colors.pink,
24+
colors.mauve,
25+
colors.red,
26+
colors.maroon,
27+
colors.peach,
28+
colors.yellow,
29+
colors.green,
30+
colors.teal,
31+
colors.sky,
32+
colors.sapphire,
33+
colors.blue,
34+
colors.lavender,
35+
colors.text,
36+
colors.subtext1,
37+
colors.subtext0,
38+
colors.overlay2,
39+
colors.overlay1,
40+
colors.overlay0,
41+
colors.surface2,
42+
colors.surface1,
43+
colors.surface0,
44+
colors.base,
45+
colors.mantle,
46+
colors.crust,
47+
]
48+
for i, color in enumerate(colors):
49+
assert order[i] == color
50+
assert list(colors) == order

0 commit comments

Comments
 (0)