1 line
12 KiB
Plaintext
1 line
12 KiB
Plaintext
{"version":3,"sources":["../src/color.ts","../../../../node_modules/.pnpm/dlv@1.1.3/node_modules/dlv/index.js"],"sourcesContent":["import { getCSSVar } from \"@chakra-ui/styled-system\"\nimport {\n toHex,\n parseToRgba,\n transparentize as setTransparency,\n mix,\n darken as reduceLightness,\n lighten as increaseLightness,\n getContrast,\n parseToHsla,\n hsla,\n getLuminance,\n} from \"color2k\"\n\nimport get from \"dlv\"\n\ntype Dict = { [key: string]: any }\nconst isEmptyObject = (obj: any) => Object.keys(obj).length === 0\n\n/**\n * Get the color raw value from theme\n * @param theme - the theme object\n * @param color - the color path (\"green.200\")\n * @param fallback - the fallback color\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const getColor = (theme: Dict, color: string, fallback?: string) => {\n const hex = get(theme, `colors.${color}`, color)\n try {\n toHex(hex)\n return hex\n } catch {\n // returning black to stay consistent with TinyColor behaviour so as to prevent breaking change\n return fallback ?? \"#000000\"\n }\n}\n\n/**\n * Get the color css variable from theme\n */\nexport const getColorVar = (theme: Dict, color: string, fallback?: string) => {\n return getCSSVar(theme, \"colors\", color) ?? fallback\n}\n\nconst getBrightness = (color: string) => {\n const [r, g, b] = parseToRgba(color)\n // http://www.w3.org/TR/AERT#color-contrast\n return (r * 299 + g * 587 + b * 114) / 1000\n}\n\n/**\n * Determines if the tone of given color is \"light\" or \"dark\"\n * @param color - the color in hex, rgb, or hsl\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const tone = (color: string) => (theme: Dict) => {\n const hex = getColor(theme, color)\n const brightness = getBrightness(hex)\n const isDark = brightness < 128\n return isDark ? \"dark\" : \"light\"\n}\n\n/**\n * Determines if a color tone is \"dark\"\n * @param color - the color in hex, rgb, or hsl\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const isDark = (color: string) => (theme: Dict) =>\n tone(color)(theme) === \"dark\"\n\n/**\n * Determines if a color tone is \"light\"\n * @param color - the color in hex, rgb, or hsl\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const isLight = (color: string) => (theme: Dict) =>\n tone(color)(theme) === \"light\"\n\n/**\n * Make a color transparent\n * @param color - the color in hex, rgb, or hsl\n * @param opacity - the amount of opacity the color should have (0-1)\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const transparentize =\n (color: string, opacity: number) => (theme: Dict) => {\n const raw = getColor(theme, color)\n return setTransparency(raw, 1 - opacity)\n }\n\n/**\n * Add white to a color\n * @param color - the color in hex, rgb, or hsl\n * @param amount - the amount white to add (0-100)\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const whiten = (color: string, amount: number) => (theme: Dict) => {\n const raw = getColor(theme, color)\n return toHex(mix(raw, \"#fff\", amount))\n}\n\n/**\n * Add black to a color\n * @param color - the color in hex, rgb, or hsl\n * @param amount - the amount black to add (0-100)\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const blacken = (color: string, amount: number) => (theme: Dict) => {\n const raw = getColor(theme, color)\n return toHex(mix(raw, \"#000\", amount / 100))\n}\n\n/**\n * Darken a specified color\n * @param color - the color in hex, rgb, or hsl\n * @param amount - the amount to darken (0-100)\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const darken = (color: string, amount: number) => (theme: Dict) => {\n const raw = getColor(theme, color)\n return toHex(reduceLightness(raw, amount / 100))\n}\n\n/**\n * Lighten a specified color\n * @param color - the color in hex, rgb, or hsl\n * @param amount - the amount to lighten (0-100)\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const lighten = (color: string, amount: number) => (theme: Dict) => {\n const raw = getColor(theme, color)\n toHex(increaseLightness(raw, amount / 100))\n}\n\n/**\n * Checks the contract ratio of between 2 colors,\n * based on the Web Content Accessibility Guidelines (Version 2.0).\n *\n * @param fg - the foreground or text color\n * @param bg - the background color\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const contrast = (fg: string, bg: string) => (theme: Dict) =>\n getContrast(getColor(theme, bg), getColor(theme, fg))\n\ninterface WCAG2Params {\n level?: \"AA\" | \"AAA\"\n size?: \"large\" | \"small\"\n}\n\n/**\n * Checks if a color meets the Web Content Accessibility\n * Guidelines (Version 2.0) for contrast ratio.\n *\n * @param textColor - the foreground or text color\n * @param bgColor - the background color\n * @param options\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const isAccessible =\n (textColor: string, bgColor: string, options?: WCAG2Params) =>\n (theme: Dict) =>\n isReadable(getColor(theme, bgColor), getColor(theme, textColor), options)\n\nexport function isReadable(\n color1: string,\n color2: string,\n wcag2: WCAG2Params = { level: \"AA\", size: \"small\" },\n): boolean {\n const readabilityLevel = readability(color1, color2)\n switch ((wcag2.level ?? \"AA\") + (wcag2.size ?? \"small\")) {\n case \"AAsmall\":\n case \"AAAlarge\":\n return readabilityLevel >= 4.5\n case \"AAlarge\":\n return readabilityLevel >= 3\n case \"AAAsmall\":\n return readabilityLevel >= 7\n default:\n return false\n }\n}\n\nexport function readability(color1: string, color2: string): number {\n return (\n (Math.max(getLuminance(color1), getLuminance(color2)) + 0.05) /\n (Math.min(getLuminance(color1), getLuminance(color2)) + 0.05)\n )\n}\n/**\n *\n * @deprecated This will be removed in the next major release.\n */\nexport const complementary = (color: string) => (theme: Dict) => {\n const raw = getColor(theme, color)\n const hsl = parseToHsla(raw)\n const complementHsl: [number, number, number, number] = Object.assign(hsl, [\n (hsl[0] + 180) % 360,\n ])\n return toHex(hsla(...complementHsl))\n}\n\nexport function generateStripe(\n size = \"1rem\",\n color = \"rgba(255, 255, 255, 0.15)\",\n) {\n return {\n backgroundImage: `linear-gradient(\n 45deg,\n ${color} 25%,\n transparent 25%,\n transparent 50%,\n ${color} 50%,\n ${color} 75%,\n transparent 75%,\n transparent\n )`,\n backgroundSize: `${size} ${size}`,\n }\n}\n\ninterface RandomColorOptions {\n /**\n * If passed, string will be used to generate\n * random color\n */\n string?: string\n /**\n * List of colors to pick from at random\n */\n colors?: string[]\n}\n\nconst randomHex = () =>\n `#${Math.floor(Math.random() * 0xffffff)\n .toString(16)\n .padEnd(6, \"0\")}`\n\nexport function randomColor(opts?: RandomColorOptions) {\n const fallback = randomHex()\n\n if (!opts || isEmptyObject(opts)) {\n return fallback\n }\n\n if (opts.string && opts.colors) {\n return randomColorFromList(opts.string, opts.colors)\n }\n\n if (opts.string && !opts.colors) {\n return randomColorFromString(opts.string)\n }\n\n if (opts.colors && !opts.string) {\n return randomFromList(opts.colors)\n }\n\n return fallback\n}\n\nfunction randomColorFromString(str: string) {\n let hash = 0\n if (str.length === 0) return hash.toString()\n for (let i = 0; i < str.length; i += 1) {\n hash = str.charCodeAt(i) + ((hash << 5) - hash)\n hash = hash & hash\n }\n let color = \"#\"\n for (let j = 0; j < 3; j += 1) {\n const value = (hash >> (j * 8)) & 255\n color += `00${value.toString(16)}`.substr(-2)\n }\n return color\n}\n\nfunction randomColorFromList(str: string, list: string[]) {\n let index = 0\n if (str.length === 0) return list[0]\n for (let i = 0; i < str.length; i += 1) {\n index = str.charCodeAt(i) + ((index << 5) - index)\n index = index & index\n }\n index = ((index % list.length) + list.length) % list.length\n return list[index]\n}\n\nfunction randomFromList(list: string[]) {\n return list[Math.floor(Math.random() * list.length)]\n}\n","export default function dlv(obj, key, def, p, undef) {\n\tkey = key.split ? key.split('.') : key;\n\tfor (p = 0; p < key.length; p++) {\n\t\tobj = obj ? obj[key[p]] : undef;\n\t}\n\treturn obj === undef ? def : obj;\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B;AAAA,EACE;AAAA,EACA;AAAA,EACA,kBAAkB;AAAA,EAClB;AAAA,EACA,UAAU;AAAA,EACV,WAAW;AAAA,EACX;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACZQ,wBAAaA,GAAKC,GAAKC,GAAKC,GAAGC,GAAAA;AAAAA,OAC7CH,IAAMA,EAAII,QAAQJ,EAAII,MAAM,GAAA,IAAOJ,GAC9BE,IAAI,GAAGA,IAAIF,EAAIK,QAAQH;AAC3BH,QAAMA,IAAMA,EAAIC,EAAIE,CAAAA,CAAAA,IAAMC;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