Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ANSI colours #98

Merged
merged 19 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
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.surface2.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.surface1.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.subtext1.hex,
);
assertEquals(
ansiColor.bright.hex,
palette[flavorName].colors.subtext0.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"
| "purple"
| "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 neccesarily "brighter" than the normal colors, but rather more bold and saturated.
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
*/
bright: AnsiColorFormat;
sgoudham marked this conversation as resolved.
Show resolved Hide resolved
}>;

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