Skip to content

Commit

Permalink
patch bug with using black/white alpha scales
Browse files Browse the repository at this point in the history
i think
  • Loading branch information
endigma committed Dec 24, 2024
1 parent d804e2c commit 02ec61c
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 17 deletions.
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ export function presetRadix(options: PresetRadixOptions): Preset<Theme> {
...aliases,
...hues,

white: "#ffffff",
black: "#000000",
transparent: "transparent",
current: "currentColor",
inherit: "inherit",
Expand Down
16 changes: 15 additions & 1 deletion src/radix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export const radixScales = [
"irisA",
"irisDark",
"irisDarkA",
"blackA",
"whiteA",
] as const;

export type RadixScales = (typeof radixScales)[number];
Expand Down Expand Up @@ -161,11 +163,13 @@ export const radixColors = [
"jade",
"iris",
"ruby",
"black",
"white"
] as const;

export type RadixColors = (typeof radixColors)[number];

export const radixSteps = [
export const radixSolidSteps = [
1,
2,
3,
Expand All @@ -178,6 +182,9 @@ export const radixSteps = [
10,
11,
12,
] as const;

export const radixAlphaSteps = [
"1A",
"2A",
"3A",
Expand All @@ -192,4 +199,11 @@ export const radixSteps = [
"12A",
] as const;

export const radixSteps = [
...radixSolidSteps,
...radixAlphaSteps
] as const;

export type RadixSteps = (typeof radixSteps)[number];
export type RadixAlphaSteps = (typeof radixAlphaSteps)[number];
export type RadixSolidSteps = (typeof radixSolidSteps)[number];
86 changes: 72 additions & 14 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,100 @@
import * as radix from "@radix-ui/colors";
import type { RadixScales, RadixColors, RadixSteps } from "./radix";
import type { RadixScales, RadixColors, RadixSteps, RadixSolidSteps, RadixAlphaSteps } from "./radix";

type Scale = {
readonly [key in RadixSteps]: string;
};

type SolidScale = {
readonly [key in RadixSolidSteps]: string;
}

type AlphaScale = {
readonly [key in RadixAlphaSteps]: string;
}

type Palette = [string, Color][];

type Color = {
light: Scale;
lightAlpha: Scale;
dark: Scale;
darkAlpha: Scale;
light: SolidScale;
lightAlpha: AlphaScale;
dark: SolidScale;
darkAlpha: AlphaScale;
};

function getScale(name: keyof typeof radix): Scale {
function getSolidScale(name: keyof typeof radix): SolidScale {
const rawScale = radix[name as RadixScales] as Record<string, string>;

const keyValues = Object.keys(rawScale).map((key) => {
const parsedKey = key.match(/.*?(\d+)/)![1];
return [parseInt(parsedKey), rawScale[key]];
});

return Object.fromEntries(keyValues);
}

function getAlphaScale(name: keyof typeof radix): AlphaScale {
const rawScale = radix[name as RadixScales] as Record<string, string>;

const keyValues = Object.keys(rawScale).map((key) => {
// rome-ignore lint/style/noNonNullAssertion: We know the source object here
const parsedKey = key.match(/.*?(\d+)/)![1];
return [parseInt(parsedKey), rawScale[key]];
});

return Object.fromEntries(keyValues);
}

const pureScales: Record<"white" | "black", SolidScale> = {
white: {
1: "#fff",
2: "#fff",
3: "#fff",
4: "#fff",
5: "#fff",
6: "#fff",
7: "#fff",
8: "#fff",
9: "#fff",
10: "#fff",
11: "#fff",
12: "#fff",
},
black: {
1: "#000",
2: "#000",
3: "#000",
4: "#000",
5: "#000",
6: "#000",
7: "#000",
8: "#000",
9: "#000",
10: "#000",
11: "#000",
12: "#000",
}
} as const

export function getColor(name: RadixColors): Color {
if (name === "black" || name === "white") {
return {
light: pureScales[name],
lightAlpha: getAlphaScale(`${name}A`),
dark: pureScales[name],
darkAlpha: getAlphaScale(`${name}A`),
};
}

return {
light: getScale(name),
lightAlpha: getScale(`${name}A`),
dark: getScale(`${name}Dark`),
darkAlpha: getScale(`${name}DarkA`),
light: getSolidScale(name),
lightAlpha: getAlphaScale(`${name}A`),
dark: getSolidScale(`${name}Dark`),
darkAlpha: getAlphaScale(`${name}DarkA`),
};
}

function fg(color: string) {
if (["sky", "mint", "lime", "yellow", "amber"].includes(color)) {
if (["sky", "mint", "lime", "yellow", "amber", "white"].includes(color)) {
return "black";
}
return "white";
Expand Down Expand Up @@ -133,10 +191,10 @@ export function genCSS(
for (const [label, color] of palette) {
css.push(`${prefix}${label}-fg:${fg(label)};`);
}
Object.entries(getScale("blackA")).forEach((entry) =>
Object.entries(getAlphaScale("blackA")).forEach((entry) =>
pushVar("black", entry, true)
);
Object.entries(getScale("whiteA")).forEach((entry) =>
Object.entries(getAlphaScale("whiteA")).forEach((entry) =>
pushVar("white", entry, true)
);
css.push("}");
Expand Down

1 comment on commit 02ec61c

@lafllamme
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good and exactly what I talked about! Nice Job

Please sign in to comment.