Skip to content

Commit 1fdc227

Browse files
feat: add ANSI colours (#98)
Co-authored-by: lemon <[email protected]>
1 parent 6632482 commit 1fdc227

File tree

7 files changed

+587
-18
lines changed

7 files changed

+587
-18
lines changed

README.md

+17-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { flavors, flavorEntries, version } from "@catppuccin/palette";
2525
import chalk from "chalk";
2626

2727
// a string containing the version of the library
28-
console.log(version)
28+
console.log(version);
2929

3030
// an object containing all catppuccin flavors
3131
console.log(flavors);
@@ -44,6 +44,16 @@ flavorEntries.map(([_, flavor]) => {
4444
);
4545
});
4646
console.log("\n");
47+
48+
// same for the ansi colors
49+
flavor.ansiColorEntries.map(([colorName, ansi]) => {
50+
console.log(
51+
chalk.hex(ansi.normal.hex)(`[${ansi.normal.code}] Normal ${colorName}`)
52+
);
53+
console.log(
54+
chalk.hex(ansi.bright.hex)(`[${ansi.bright.code}] Bright ${colorName}`)
55+
);
56+
});
4757
});
4858
```
4959

@@ -52,11 +62,15 @@ flavorEntries.map(([_, flavor]) => {
5262
The library is available through [JSR](https://jsr.io/@catppuccin/palette) and [`deno.land/x/catppuccin`](https://deno.land/x/catppuccin):
5363

5464
```ts
55-
import { flavors, flavorEntries, version } from "https://deno.land/x/catppuccin/mod.ts";
65+
import {
66+
flavors,
67+
flavorEntries,
68+
version,
69+
} from "https://deno.land/x/catppuccin/mod.ts";
5670
import { bgRgb24 } from "https://deno.land/std/fmt/colors.ts";
5771

5872
// a string containing the version of the library
59-
console.log(version)
73+
console.log(version);
6074

6175
// an object containing all catppuccin flavors
6276
console.log(flavors);

deno.lock

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

import_map.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dnt": "https://deno.land/x/[email protected]/mod.ts",
1111
"ase-utils": "npm:[email protected]",
1212
"procreate-swatches": "npm:[email protected]",
13-
"tinycolor2": "npm:[email protected]"
13+
"tinycolor2": "npm:[email protected]",
14+
"colorjs": "npm:[email protected]"
1415
}
1516
}

mod.test.ts

+60-10
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,71 @@ import { flavorEntries, flavors, version } from "@catppuccin/palette";
44
import palette from "@/palette.json" with { type: "json" };
55

66
Deno.test("flavorEntries", () => {
7-
flavorEntries
8-
.map(([flavorName, flavor]) =>
9-
flavor.colorEntries
10-
.map(([colorName, color]) =>
11-
assertEquals(palette[flavorName].colors[colorName].hex, color.hex)
12-
)
7+
flavorEntries.map(([flavorName, flavor]) => {
8+
flavor.colorEntries.map(([colorName, color]) =>
9+
assertEquals(color.hex, palette[flavorName].colors[colorName].hex)
1310
);
11+
});
1412
});
1513

1614
Deno.test("flavors", () => {
1715
flavorEntries.map(([flavorName]) => {
18-
assertEquals(
19-
flavors[flavorName].name,
20-
palette[flavorName].name,
21-
);
16+
assertEquals(flavors[flavorName].name, palette[flavorName].name);
17+
});
18+
});
19+
20+
Deno.test("ansiEntries", () => {
21+
flavorEntries.map(([flavorName, flavor]) => {
22+
flavor.ansiColorEntries.map(([ansiColorName, ansiColor]) => {
23+
assertEquals(
24+
ansiColor.normal.hex,
25+
palette[flavorName].ansiColors[ansiColorName].normal.hex,
26+
);
27+
28+
if (ansiColorName == "black") {
29+
if (flavorName == "latte") {
30+
assertEquals(
31+
ansiColor.normal.hex,
32+
palette[flavorName].colors.subtext1.hex,
33+
);
34+
assertEquals(
35+
ansiColor.bright.hex,
36+
palette[flavorName].colors.subtext0.hex,
37+
);
38+
} else {
39+
assertEquals(
40+
ansiColor.normal.hex,
41+
palette[flavorName].colors.surface1.hex,
42+
);
43+
assertEquals(
44+
ansiColor.bright.hex,
45+
palette[flavorName].colors.surface2.hex,
46+
);
47+
}
48+
}
49+
50+
if (ansiColorName == "white") {
51+
if (flavorName == "latte") {
52+
assertEquals(
53+
ansiColor.normal.hex,
54+
palette[flavorName].colors.surface2.hex,
55+
);
56+
assertEquals(
57+
ansiColor.bright.hex,
58+
palette[flavorName].colors.surface1.hex,
59+
);
60+
} else {
61+
assertEquals(
62+
ansiColor.normal.hex,
63+
palette[flavorName].colors.subtext0.hex,
64+
);
65+
assertEquals(
66+
ansiColor.bright.hex,
67+
palette[flavorName].colors.subtext1.hex,
68+
);
69+
}
70+
}
71+
});
2272
});
2373
});
2474

mod.ts

+59
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ export type MonochromaticName =
4949
| "mantle"
5050
| "crust";
5151

52+
type AnsiName =
53+
| "black"
54+
| "red"
55+
| "green"
56+
| "yellow"
57+
| "blue"
58+
| "magenta"
59+
| "cyan"
60+
| "white";
61+
5262
/**
5363
* All color names of Catppuccin
5464
*/
@@ -59,6 +69,11 @@ export type ColorName = AccentName | MonochromaticName;
5969
*/
6070
export type Colors<T> = Record<ColorName, T>;
6171

72+
/**
73+
* Generic to map type T to all ANSI color names
74+
*/
75+
export type AnsiColors<T> = Record<AnsiName, T>;
76+
6277
/**
6378
* A flavor of Catppuccin
6479
*/
@@ -88,17 +103,32 @@ export type CatppuccinFlavor = Readonly<{
88103
*/
89104
colors: CatppuccinColors;
90105

106+
/**
107+
* An object containing all the ANSI color mappings of the flavor
108+
*/
109+
ansiColors: CatppuccinAnsiColors;
110+
91111
/**
92112
* A typed Object.entries iterable of the colors of the flavor
93113
*/
94114
colorEntries: Entries<CatppuccinColors>;
115+
116+
/**
117+
* A typed Object.entries iterable of the ANSI colors of the flavor
118+
*/
119+
ansiColorEntries: Entries<CatppuccinAnsiColors>;
95120
}>;
96121

97122
/**
98123
* All colors of Catppuccin
99124
*/
100125
export type CatppuccinColors = Readonly<Colors<ColorFormat>>;
101126

127+
/**
128+
* All ANSI color mappings of Catppuccin
129+
*/
130+
export type CatppuccinAnsiColors = Readonly<AnsiColors<AnsiColorGroups>>;
131+
102132
/**
103133
* All flavors of Catppuccin
104134
*/
@@ -187,6 +217,34 @@ export type ColorFormat = Readonly<{
187217
accent: boolean;
188218
}>;
189219

220+
export type AnsiColorGroups = Readonly<{
221+
/**
222+
* An object containing all the ANSI "normal" colors, which are the 8 standard colors from 0 to 7.
223+
*/
224+
normal: AnsiColorFormat;
225+
226+
/**
227+
* An object containing all the ANSI "bright" colors, which are the 8 standard colors from 8 to 15.
228+
*
229+
* Note: These bright colors are not necessarily "brighter" than the normal colors, but rather more bold and saturated.
230+
*/
231+
bright: AnsiColorFormat;
232+
}>;
233+
234+
export type AnsiColorFormat = Readonly<{
235+
/**
236+
* String-formatted hex value
237+
* @example "#babbf1"
238+
*/
239+
hex: string;
240+
241+
/**
242+
* The ANSI color code
243+
* @example 4
244+
*/
245+
code: number;
246+
}>;
247+
190248
const { version: _, ...jsonFlavors } = definitions;
191249

192250
/**
@@ -203,6 +261,7 @@ export const flavors: CatppuccinFlavors = entriesFromObject(
203261
acc[flavorName] = {
204262
...flavor,
205263
colorEntries: entriesFromObject(flavor.colors),
264+
ansiColorEntries: entriesFromObject(flavor.ansiColors),
206265
};
207266
return acc;
208267
}, {} as CatppuccinFlavors);

0 commit comments

Comments
 (0)