UNPKG

@saas-ui/palette

Version:

Color palette generator for Chakra UI

151 lines (150 loc) 4.4 kB
'use client' "use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { createPalette: () => createPalette }); module.exports = __toCommonJS(src_exports); var import_chroma_js = __toESM(require("chroma-js")); var import_react = require("@chakra-ui/react"); var names = { 0: "red", 30: "orange", 50: "yellow", 150: "green", 180: "teal", 190: "cyan", 210: "blue", 260: "purple", 330: "pink" }; var hueName = (h) => { const i = Math.round((h - 2) / 10) * 10; const name = names[i]; return name; }; var getLumsFromThemeColors = (name) => { const themeColors = import_react.theme.colors; const lums = []; let color = themeColors[name]; if (!color) { color = themeColors.red; } for (const lum in color) { lums.push(import_chroma_js.default.hex(color[lum]).luminance()); } return lums; }; var createArray = (length) => { const arr = []; for (let i = 0; i < length; i++) { arr.push(i); } return arr; }; var createHues = (length) => { const hueStep = 360 / length; return (base) => { const hues = createArray(length).map( (n) => Math.floor((base + n * hueStep) % 360) ); return hues; }; }; var createBlack = (hex, lum = 0) => { return (0, import_chroma_js.default)(hex).luminance(lum).hex(); }; var createShades = (hex, lums) => { return lums.map((lum) => { return (0, import_chroma_js.default)(hex).luminance(lum).hex(); }); }; var getColorName = (hex) => { const [hue, sat] = (0, import_chroma_js.default)(hex).hsl(); const name = hueName(hue); return name; }; var mapValues = (values) => { const keys = [ "50", "100", "200", "300", "400", "500", "600", "700", "800", "900" ]; const obj = {}; for (const key in values) { obj[keys[key]] = values[key]; } return obj; }; var createPalette = (hex, options = {}) => { const colors = options.colors || {}; const color = (0, import_chroma_js.default)(hex); const palette = {}; const [hue, sat, lte] = color.hsl(); const hues = createHues(36)(hue); const gray = colors.gray || color.hex(); palette.black = createBlack(gray, options.blackLuminance); palette.gray = mapValues(createShades(gray, getLumsFromThemeColors("gray"))); hues.forEach((h) => { let c = import_chroma_js.default.hsl(h, sat, lte); const name = getColorName(c); if (!name) { return; } if (colors[name]) { c = import_chroma_js.default.hex(colors[name]); } palette[name] = mapValues( createShades(c.hex(), getLumsFromThemeColors(name)) ); }); Object.entries(colors).forEach(([name, value]) => { if (!palette[name]) { const c = (0, import_chroma_js.default)(value); palette[name] = mapValues( createShades(c.hex(), getLumsFromThemeColors(name)) ); } }); return Object.assign(palette); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createPalette }); //# sourceMappingURL=index.js.map