UNPKG

@upstart.gg/sdk

Version:

You can test the CLI without recompiling by running:

98 lines (96 loc) 3.3 kB
import { kebabCase } from "lodash-es"; import chroma from "chroma-js"; import { css } from "@upstart.gg/style-system/twind"; //#region src/shared/themes/color-system.ts const baseColors = { primary: "Primary color", secondary: "Secondary color", accent: "Accent color", neutral: "Neutral color", base100: "Base color", base200: "Base (level 2)", base300: "Base (level 3)" }; const shadeNumbers = [ 900, 800, 700, 600, 500, 400, 300, 200, 100, 50 ]; const semanticAliases = { 100: "subtle", 300: "light", 700: "dark" }; function getColorsSuggestions(baseHueOrColor, theme) { return []; } function isStandardColor(color) { if (typeof color !== "string") return false; return color.startsWith("oklch") || color.startsWith("oklab") || color.startsWith("rgb") || color.startsWith("hsl") || color.startsWith("#") || color.startsWith("var(--"); } function propToStyle(prop, cssAttr) { if (typeof prop === "undefined") return; return isStandardColor(prop) || typeof prop === "number" ? css({ [cssAttr]: prop }) : prop; } function getContrastingTextColor(backgroundColor, contrastThreshold = 3.5) { const bgColor = chroma(backgroundColor); const white = "#ffffff"; const black = "#000000"; const contrastWithWhite = chroma.contrast(bgColor, white); const contrastWithBlack = chroma.contrast(bgColor, black); if (contrastWithWhite >= contrastThreshold && contrastWithBlack >= contrastThreshold) return white; if (contrastWithWhite >= contrastThreshold) return white; if (contrastWithBlack >= contrastThreshold) return black; return contrastWithWhite >= contrastWithBlack ? white : black; } function propToClass(value, classPrefix) { if (typeof value === "undefined") return; return isStandardColor(value) || typeof value === "number" ? `${classPrefix}-[${value}]` : value; } function generateColorsVars(theme) { const shades = {}; Object.keys(baseColors).forEach((_colorName) => { const color = theme.colors[_colorName]; const colorName = kebabCase(_colorName); shades[`color-${colorName}`] = color; shades[`color-${colorName}-500`] = color; const contentColor = getContrastingTextColor(color); shades[`color-${colorName}-content`] = contentColor; shades[`color-${colorName}-500-content`] = contentColor; const baseColor = chroma(color); const darkest = baseColor.darken(2.5); const lightest = baseColor.brighten(3.5); const colorScale = chroma.scale([ lightest, baseColor, darkest ]).domain([ 50, 500, 900 ]).mode("oklch"); if (!colorName.startsWith("base")) shadeNumbers.forEach((shade) => { if (shade === 500) return; const varName = `color-${colorName}-${shade}`; const shadedColor = colorScale(shade); shades[varName] = shadedColor.css("oklch"); const contentColor$1 = getContrastingTextColor(shadedColor); shades[`color-${colorName}-${shade}-content`] = contentColor$1; if (semanticAliases[shade]) { shades[`color-${colorName}-${semanticAliases[shade]}`] = shades[varName]; shades[`color-${colorName}-${semanticAliases[shade]}-content`] = contentColor$1; } }); }); return shades; } //#endregion export { baseColors, generateColorsVars, getColorsSuggestions, getContrastingTextColor, isStandardColor, propToClass, propToStyle, shadeNumbers }; //# sourceMappingURL=color-system.js.map