Skip to content

Commit

Permalink
feat: add ANSI colours (#98)
Browse files Browse the repository at this point in the history
Co-authored-by: lemon <[email protected]>
  • Loading branch information
sgoudham and unseen-ninja authored Oct 26, 2024
1 parent 6632482 commit 1fdc227
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 18 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { flavors, flavorEntries, version } from "@catppuccin/palette";
import chalk from "chalk";

// a string containing the version of the library
console.log(version)
console.log(version);

// an object containing all catppuccin flavors
console.log(flavors);
Expand All @@ -44,6 +44,16 @@ flavorEntries.map(([_, flavor]) => {
);
});
console.log("\n");

// same for the ansi colors
flavor.ansiColorEntries.map(([colorName, ansi]) => {
console.log(
chalk.hex(ansi.normal.hex)(`[${ansi.normal.code}] Normal ${colorName}`)
);
console.log(
chalk.hex(ansi.bright.hex)(`[${ansi.bright.code}] Bright ${colorName}`)
);
});
});
```

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

```ts
import { flavors, flavorEntries, version } from "https://deno.land/x/catppuccin/mod.ts";
import {
flavors,
flavorEntries,
version,
} from "https://deno.land/x/catppuccin/mod.ts";
import { bgRgb24 } from "https://deno.land/std/fmt/colors.ts";

// a string containing the version of the library
console.log(version)
console.log(version);

// an object containing all catppuccin flavors
console.log(flavors);
Expand Down
7 changes: 7 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion import_map.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"dnt": "https://deno.land/x/[email protected]/mod.ts",
"ase-utils": "npm:[email protected]",
"procreate-swatches": "npm:[email protected]",
"tinycolor2": "npm:[email protected]"
"tinycolor2": "npm:[email protected]",
"colorjs": "npm:[email protected]"
}
}
70 changes: 60 additions & 10 deletions mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,71 @@ import { flavorEntries, flavors, version } from "@catppuccin/palette";
import palette from "@/palette.json" with { type: "json" };

Deno.test("flavorEntries", () => {
flavorEntries
.map(([flavorName, flavor]) =>
flavor.colorEntries
.map(([colorName, color]) =>
assertEquals(palette[flavorName].colors[colorName].hex, color.hex)
)
flavorEntries.map(([flavorName, flavor]) => {
flavor.colorEntries.map(([colorName, color]) =>
assertEquals(color.hex, palette[flavorName].colors[colorName].hex)
);
});
});

Deno.test("flavors", () => {
flavorEntries.map(([flavorName]) => {
assertEquals(
flavors[flavorName].name,
palette[flavorName].name,
);
assertEquals(flavors[flavorName].name, palette[flavorName].name);
});
});

Deno.test("ansiEntries", () => {
flavorEntries.map(([flavorName, flavor]) => {
flavor.ansiColorEntries.map(([ansiColorName, ansiColor]) => {
assertEquals(
ansiColor.normal.hex,
palette[flavorName].ansiColors[ansiColorName].normal.hex,
);

if (ansiColorName == "black") {
if (flavorName == "latte") {
assertEquals(
ansiColor.normal.hex,
palette[flavorName].colors.subtext1.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.subtext0.hex,
);
} else {
assertEquals(
ansiColor.normal.hex,
palette[flavorName].colors.surface1.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.surface2.hex,
);
}
}

if (ansiColorName == "white") {
if (flavorName == "latte") {
assertEquals(
ansiColor.normal.hex,
palette[flavorName].colors.surface2.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.surface1.hex,
);
} else {
assertEquals(
ansiColor.normal.hex,
palette[flavorName].colors.subtext0.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.subtext1.hex,
);
}
}
});
});
});

Expand Down
59 changes: 59 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ export type MonochromaticName =
| "mantle"
| "crust";

type AnsiName =
| "black"
| "red"
| "green"
| "yellow"
| "blue"
| "magenta"
| "cyan"
| "white";

/**
* All color names of Catppuccin
*/
Expand All @@ -59,6 +69,11 @@ export type ColorName = AccentName | MonochromaticName;
*/
export type Colors<T> = Record<ColorName, T>;

/**
* Generic to map type T to all ANSI color names
*/
export type AnsiColors<T> = Record<AnsiName, T>;

/**
* A flavor of Catppuccin
*/
Expand Down Expand Up @@ -88,17 +103,32 @@ export type CatppuccinFlavor = Readonly<{
*/
colors: CatppuccinColors;

/**
* An object containing all the ANSI color mappings of the flavor
*/
ansiColors: CatppuccinAnsiColors;

/**
* A typed Object.entries iterable of the colors of the flavor
*/
colorEntries: Entries<CatppuccinColors>;

/**
* A typed Object.entries iterable of the ANSI colors of the flavor
*/
ansiColorEntries: Entries<CatppuccinAnsiColors>;
}>;

/**
* All colors of Catppuccin
*/
export type CatppuccinColors = Readonly<Colors<ColorFormat>>;

/**
* All ANSI color mappings of Catppuccin
*/
export type CatppuccinAnsiColors = Readonly<AnsiColors<AnsiColorGroups>>;

/**
* All flavors of Catppuccin
*/
Expand Down Expand Up @@ -187,6 +217,34 @@ export type ColorFormat = Readonly<{
accent: boolean;
}>;

export type AnsiColorGroups = Readonly<{
/**
* An object containing all the ANSI "normal" colors, which are the 8 standard colors from 0 to 7.
*/
normal: AnsiColorFormat;

/**
* An object containing all the ANSI "bright" colors, which are the 8 standard colors from 8 to 15.
*
* Note: These bright colors are not necessarily "brighter" than the normal colors, but rather more bold and saturated.
*/
bright: AnsiColorFormat;
}>;

export type AnsiColorFormat = Readonly<{
/**
* String-formatted hex value
* @example "#babbf1"
*/
hex: string;

/**
* The ANSI color code
* @example 4
*/
code: number;
}>;

const { version: _, ...jsonFlavors } = definitions;

/**
Expand All @@ -203,6 +261,7 @@ export const flavors: CatppuccinFlavors = entriesFromObject(
acc[flavorName] = {
...flavor,
colorEntries: entriesFromObject(flavor.colors),
ansiColorEntries: entriesFromObject(flavor.ansiColors),
};
return acc;
}, {} as CatppuccinFlavors);
Expand Down
Loading

0 comments on commit 1fdc227

Please sign in to comment.