@poupe/theme-builder
Version:
Design token management and theme generation system for Poupe UI framework
408 lines (402 loc) • 11.7 kB
JavaScript
import { Hct } from '@poupe/material-color-utilities';
import { extend, colord as colord$1, Colord } from 'colord';
import '@poupe/css';
import mixPlugin from 'colord/plugins/mix';
const uint32 = (n) => n >>> 0;
const uint8 = (n) => n >>> 0 & 255;
const alphaFromArgb = (argb) => uint8(uint32(argb) >> 24);
const redFromArgb = (argb) => uint8(uint32(argb) >> 16);
const greenFromArgb = (argb) => uint8(uint32(argb) >> 8);
const blueFromArgb = (argb) => uint8(uint32(argb));
const defaultColors = {
// Reds
indianred: "#cd5c5c",
lightcoral: "#f08080",
salmon: "#fa8072",
darksalmon: "#e9967a",
crimson: "#dc143c",
red: "#ff0000",
firebrick: "#b22222",
darkred: "#8b0000",
// Pinks
pink: "#ffc0cb",
lightpink: "#ffb6c1",
hotpink: "#ff69b4",
deeppink: "#ff1493",
mediumvioletred: "#c71585",
palevioletred: "#db7093",
// Oranges
lightsalmon: "#ffa07a",
coral: "#ff7f50",
tomato: "#ff6347",
orangered: "#ff4500",
darkorange: "#ff8c00",
orange: "#ffa500",
// Yellows
gold: "#ffd700",
yellow: "#ffff00",
lightyellow: "#ffffe0",
lemonchiffon: "#fffacd",
lightgoldenrodyellow: "#fafad2",
papayawhip: "#ffefd5",
moccasin: "#ffe4b5",
peachpuff: "#ffdab9",
palegoldenrod: "#eee8aa",
khaki: "#f0e68c",
darkkhaki: "#bdb76b",
// Purples
lavender: "#e6e6fa",
thistle: "#d8bfd8",
plum: "#dda0dd",
violet: "#ee82ee",
orchid: "#da70d6",
fuchsia: "#ff00ff",
magenta: "#ff00ff",
mediumorchid: "#ba55d3",
mediumpurple: "#9370db",
rebeccapurple: "#663399",
blueviolet: "#8a2be2",
darkviolet: "#9400d3",
darkorchid: "#9932cc",
darkmagenta: "#8b008b",
purple: "#800080",
indigo: "#4b0082",
slateblue: "#6a5acd",
darkslateblue: "#483d8b",
mediumslateblue: "#7b68ee",
// Greens
greenyellow: "#adff2f",
chartreuse: "#7fff00",
lawngreen: "#7cfc00",
lime: "#00ff00",
limegreen: "#32cd32",
palegreen: "#98fb98",
lightgreen: "#90ee90",
mediumspringgreen: "#00fa9a",
springgreen: "#00ff7f",
mediumseagreen: "#3cb371",
seagreen: "#2e8b57",
forestgreen: "#228b22",
green: "#008000",
darkgreen: "#006400",
yellowgreen: "#9acd32",
olivedrab: "#6b8e23",
olive: "#808000",
darkolivegreen: "#556b2f",
mediumaquamarine: "#66cdaa",
darkseagreen: "#8fbc8f",
lightseagreen: "#20b2aa",
darkcyan: "#008b8b",
teal: "#008080",
// Blues/Cyans
aqua: "#00ffff",
cyan: "#00ffff",
lightcyan: "#e0ffff",
paleturquoise: "#afeeee",
aquamarine: "#7fffd4",
turquoise: "#40e0d0",
mediumturquoise: "#48d1cc",
darkturquoise: "#00ced1",
cadetblue: "#5f9ea0",
steelblue: "#4682b4",
lightsteelblue: "#b0c4de",
powderblue: "#b0e0e6",
lightblue: "#add8e6",
skyblue: "#87ceeb",
lightskyblue: "#87cefa",
deepskyblue: "#00bfff",
dodgerblue: "#1e90ff",
cornflowerblue: "#6495ed",
royalblue: "#4169e1",
blue: "#0000ff",
mediumblue: "#0000cd",
darkblue: "#00008b",
navy: "#000080",
midnightblue: "#191970",
// Browns
cornsilk: "#fff8dc",
blanchedalmond: "#ffebcd",
bisque: "#ffe4c4",
navajowhite: "#ffdead",
wheat: "#f5deb3",
burlywood: "#deb887",
tan: "#d2b48c",
rosybrown: "#bc8f8f",
sandybrown: "#f4a460",
goldenrod: "#daa520",
darkgoldenrod: "#b8860b",
peru: "#cd853f",
chocolate: "#d2691e",
saddlebrown: "#8b4513",
sienna: "#a0522d",
brown: "#a52a2a",
maroon: "#800000",
// Whites
white: "#ffffff",
snow: "#fffafa",
honeydew: "#f0fff0",
mintcream: "#f5fffa",
azure: "#f0ffff",
aliceblue: "#f0f8ff",
ghostwhite: "#f8f8ff",
whitesmoke: "#f5f5f5",
seashell: "#fff5ee",
beige: "#f5f5dc",
oldlace: "#fdf5e6",
floralwhite: "#fffaf0",
ivory: "#fffff0",
antiquewhite: "#faebd7",
linen: "#faf0e6",
lavenderblush: "#fff0f5",
mistyrose: "#ffe4e1",
// Grays
gainsboro: "#dcdcdc",
lightgray: "#d3d3d3",
lightgrey: "#d3d3d3",
silver: "#c0c0c0",
darkgray: "#a9a9a9",
darkgrey: "#a9a9a9",
gray: "#808080",
grey: "#808080",
dimgray: "#696969",
dimgrey: "#696969",
lightslategray: "#778899",
lightslategrey: "#778899",
slategray: "#708090",
slategrey: "#708090",
darkslategray: "#2f4f4f",
darkslategrey: "#2f4f4f",
black: "#000000",
// Special colors
transparent: "#00000000"
};
function withKnownColor(c) {
if (typeof c !== "string" || !reOnlyLetters.test(c)) {
return c;
}
const name = c.toLowerCase();
if (name in defaultColors) {
return defaultColors[name];
}
return c;
}
const reOnlyLetters = /^[a-zA-Z]+$/;
extend([mixPlugin]);
const isObjectColor = (c) => {
if (c === null || typeof c !== "object")
return false;
else if (c instanceof Hct || c instanceof Colord)
return false;
else if ("r" in c && "g" in c && "b" in c || "h" in c && "c" in c && "t" in c || "h" in c && "s" in c && "l" in c || "h" in c && "s" in c && "v" in c || "h" in c && "w" in c && "b" in c || "x" in c && "y" in c && "z" in c || "l" in c && "a" in c && "b" in c || "l" in c && "c" in c && "h" in c || "c" in c && "m" in c && "y" in c && "k" in c)
return true;
else
return false;
};
function normalizeAlpha(a) {
if (a === void 0) {
return void 0;
}
const n = a > 1 ? a / 255 : a;
return Math.min(Math.max(n, 0), 1);
}
function withNormalizedAlpha(c) {
if ("a" in c && c.a !== void 0) {
const a = normalizeAlpha(c.a);
return {
...c,
a
};
}
return c;
}
const rgbFromRgbaColor = (c) => {
const r255 = uint8(c.r);
const g255 = uint8(c.g);
const b255 = uint8(c.b);
return uint32(r255 << 16 | g255 << 8 | b255);
};
const rgbaFromHctColor = (c) => splitArgb(argbFromHctColor(c));
const rgba = (c) => splitArgb(argb(c));
const argbFromHct = (c) => c.toInt();
const argbFromRgbaColor = (c) => {
const a = normalizeAlpha(c.a) ?? 1;
const a255 = uint8(Math.round(a * 255));
return uint32(a255 << 24 | rgbFromRgbaColor(c));
};
const argbFromHctColor = (c) => {
const argb2 = argbFromHct(Hct.from(c.h, c.c, c.t));
if (c.a === void 0) {
return argb2;
}
const a = normalizeAlpha(c.a);
const a255 = uint8(Math.round(a * 255));
return uint32(a255 << 24 | argb2 & 16777215);
};
const argbFromColord = (c) => {
if (!c.isValid()) {
throw new Error("Invalid color");
}
return argbFromRgbaColor(c.rgba);
};
const argbFromString = (s) => argbFromColord(colordFromString(s));
const splitArgb = (argb2) => {
const a255 = alphaFromArgb(argb2);
return {
r: redFromArgb(argb2),
g: greenFromArgb(argb2),
b: blueFromArgb(argb2),
...a255 > 0 ? { a: a255 / 255 } : {}
};
};
const argb = (c) => {
if (c instanceof Hct) {
return argbFromHct(c);
} else if (c instanceof Colord) {
return argbFromRgbaColor(c.rgba);
} else if (typeof c === "number") {
return c;
} else if (typeof c === "string") {
return argbFromString(c);
} else if ("h" in c && "c" in c && "t" in c) {
return argbFromHctColor(c);
} else {
return argbFromColord(colord(c));
}
};
const colordFromArgb = (argb2) => colord$1(splitArgb(argb2));
const colordFromHct = (c) => colordFromArgb(argbFromHct(c));
const colordFromHctColor = (c) => colordFromArgb(argbFromHctColor(c));
const colordFromString = (c) => {
const c1 = colord$1(withKnownColor(c));
if (!c1.isValid()) {
throw new Error(`Invalid color '${c}'`);
}
return c1;
};
const colord = (c) => {
if (c instanceof Colord) {
return c;
} else if (c instanceof Hct) {
return colordFromHct(c);
} else if (typeof c === "number") {
return colordFromArgb(c);
} else if (typeof c === "string") {
return colordFromString(c);
} else if ("h" in c && "c" in c && "t" in c) {
return colordFromHctColor(c);
} else if (!isObjectColor(c)) {
throw new Error("Invalid color");
} else if ("a" in c && c.a !== void 0) {
const a = normalizeAlpha(c.a);
return colord$1({ ...c, a });
} else {
return colord$1(c);
}
};
const hctFromArgb = (argb2) => Hct.fromInt(argb2);
const hctFromRgbaColor = (c) => Hct.fromInt(argbFromRgbaColor(c));
const hctFromColord = (c) => {
if (!c.isValid()) {
throw new Error("Invalid color");
}
return hctFromRgbaColor(c.rgba);
};
const hctFromString = (s) => hctFromColord(colordFromString(s));
const splitHct = (c) => {
return { h: c.hue, c: c.chroma, t: c.tone };
};
const hct = (c) => {
if (c instanceof Hct) {
return c;
} else if (c instanceof Colord) {
return hctFromColord(c);
} else if (typeof c === "number") {
return hctFromArgb(c);
} else if (typeof c === "string") {
return hctFromString(c);
} else if ("h" in c && "c" in c && "t" in c) {
return Hct.from(c.h, c.c, c.t);
} else {
return hctFromColord(colord(c));
}
};
const hslFromColord = (c) => {
if (!c.isValid()) {
throw new Error("Invalid color");
}
return c.toHsl();
};
const hslFromArgb = (argb2) => hslFromColord(colord(splitArgb(argb2)));
const hslFromHct = (c) => hslFromArgb(argbFromHct(c));
const hslFromHctColor = (c) => hslFromColord(colord(rgbaFromHctColor(c)));
const hslFromString = (s) => hslFromColord(colordFromString(s));
const hsl = (c) => {
if (c instanceof Hct) {
return hslFromHct(c);
} else if (c instanceof Colord) {
return hslFromColord(c);
} else if (typeof c === "number") {
return hslFromArgb(c);
} else if (typeof c === "string") {
return hslFromString(c);
} else if ("h" in c && "c" in c && "t" in c) {
return hslFromHctColor(c);
} else {
return hslFromColord(colord(c));
}
};
const hexFromColord = (c) => {
if (!c.isValid()) {
throw new Error("Invalid color");
}
return c.toHex();
};
const hexFromArgb = (argb2) => hexFromColord(colord(splitArgb(argb2)));
const hexFromHct = (c) => hexFromArgb(argbFromHct(c));
const hexFromHctColor = (c) => hexFromColord(colord$1(rgbaFromHctColor(c)));
function colorFormatter(v = "rgb") {
if (typeof v === "function") return v;
if (v === "numbers") {
return (c) => {
const { r, g, b } = rgba(c);
return `${r} ${g} ${b}`;
};
}
if (v === "hsl") return hslString;
if (v === "hex") return hexString;
return rgbaString;
}
const hexString = (c) => {
if (c instanceof Hct) {
return hexFromHct(c);
} else if (typeof c === "number") {
return hexFromArgb(c);
}
const c1 = colord(c);
if (!c1.isValid) {
throw new Error("Invalid color");
}
return hexFromColord(c1);
};
function hslString(c, alpha = true) {
const c1 = colord(c);
if (!c1.isValid) {
throw new Error("Invalid color");
}
const { h, s, l, a: a0 } = c1.toHsl();
const a = alpha === false ? 1 : a0;
if (a < 1) {
return `hsla(${h}, ${s}%, ${l}%, ${a})`;
}
return `hsl(${h}, ${s}%, ${l}%)`;
}
const rgbaString = (c, alpha = true) => {
const { r, g, b, a: a0 = 1 } = rgba(c);
const a = alpha === false ? 1 : a0;
if (a < 1) {
return `rgb(${r} ${g} ${b} / ${a.toFixed(2)})`;
}
return `rgb(${r} ${g} ${b})`;
};
export { hslFromArgb as A, hslFromHct as B, hslFromHctColor as C, hslFromString as D, hsl as E, hexFromColord as F, hexFromArgb as G, hexFromHct as H, hexFromHctColor as I, defaultColors as J, withKnownColor as K, colorFormatter as L, hexString as M, hslString as N, rgbFromRgbaColor as a, rgbaFromHctColor as b, rgba as c, argbFromHct as d, argbFromRgbaColor as e, argbFromHctColor as f, argbFromColord as g, hct as h, isObjectColor as i, argbFromString as j, argb as k, colordFromArgb as l, colordFromHct as m, normalizeAlpha as n, colordFromHctColor as o, colordFromString as p, colord as q, rgbaString as r, splitArgb as s, hctFromArgb as t, hctFromRgbaColor as u, hctFromColord as v, withNormalizedAlpha as w, hctFromString as x, splitHct as y, hslFromColord as z };
//# sourceMappingURL=theme-builder.BWnUhZKi.mjs.map