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AP,EAAuB,CAAvB,EAA0B,GAA1B,CAAN;AACD;;AAED,SAAO,WAAI,GAAJ,EAAU,WAAV,GAAwB,MAAxB,CAA+B,CAA/B,EAAkC,CAAlC,CAAP;AACD;;AAEK,SAAU,QAAV,CAAoB,IAApB,EAA8B;AAClC,SAAO,CAAC,IAAI,CAAC,CAAL,IAAU,EAAX,KAAkB,IAAI,CAAC,CAAL,IAAU,CAA5B,IAAiC,IAAI,CAAC,CAA7C;AACD;AAED;;;;;;;;AAMM,SAAU,aAAV,CAAyB,EAAzB,EAAmC,EAAnC,EAA2C;AAAA,eAChC,0BAAM,QAAQ,CAAC,EAAD,CAAd,CADgC;AAAA;AAAA,MACtC,EADsC;;AAAA,gBAEhC,0BAAM,QAAQ,CAAC,EAAD,CAAd,CAFgC;AAAA;AAAA,MAEtC,EAFsC;;AAI/C,SAAO,CAAC,IAAI,CAAC,GAAL,CAAS,EAAT,EAAa,EAAb,IAAmB,IAApB,KAA6B,IAAI,CAAC,GAAL,CAAS,EAAT,EAAa,EAAb,IAAmB,IAAhD,CAAP;AACD","sourcesContent":["import { consoleWarn } from './console'\nimport { chunk, padEnd } from './helpers'\nimport { toXYZ } from './color/transformSRGB'\n\nexport type ColorInt = number\nexport type XYZ = [number, number, number]\nexport type LAB = [number, number, number]\nexport type HSVA = { h: number, s: number, v: number, a: number }\nexport type RGBA = { r: number, g: number, b: number, a: number }\nexport type HSLA = { h: number, s: number, l: number, a: number }\nexport type Hex = string\nexport type Hexa = string\nexport type Color = string | number | {}\n\nexport function colorToInt (color: Color): ColorInt {\n let rgb\n\n if (typeof color === 'number') {\n rgb = color\n } else if (typeof color === 'string') {\n let c = color[0] === '#' ? color.substring(1) : color\n if (c.length === 3) {\n c = c.split('').map(char => char + char).join('')\n }\n if (c.length !== 6) {\n consoleWarn(`'${color}' is not a valid rgb color`)\n }\n rgb = parseInt(c, 16)\n } else {\n throw new TypeError(`Colors can only be numbers or strings, recieved ${color == null ? color : color.constructor.name} instead`)\n }\n\n if (rgb < 0) {\n consoleWarn(`Colors cannot be negative: '${color}'`)\n rgb = 0\n } else if (rgb > 0xffffff || isNaN(rgb)) {\n consoleWarn(`'${color}' is not a valid rgb color`)\n rgb = 0xffffff\n }\n\n return rgb\n}\n\nexport function intToHex (color: ColorInt): string {\n let hexColor: string = color.toString(16)\n\n if (hexColor.length < 6) hexColor = '0'.repeat(6 - hexColor.length) + hexColor\n\n return '#' + hexColor\n}\n\nexport function colorToHex (color: Color): string {\n return intToHex(colorToInt(color))\n}\n\n/**\n * Converts HSVA to RGBA. Based on formula from https://en.wikipedia.org/wiki/HSL_and_HSV\n *\n * @param color HSVA color as an array [0-360, 0-1, 0-1, 0-1]\n */\nexport function HSVAtoRGBA (hsva: HSVA): RGBA {\n const { h, s, v, a } = hsva\n const f = (n: number) => {\n const k = (n + (h / 60)) % 6\n return v - v * s * Math.max(Math.min(k, 4 - k, 1), 0)\n }\n\n const rgb = [f(5), f(3), f(1)].map(v => Math.round(v * 255))\n\n return { r: rgb[0], g: rgb[1], b: rgb[2], a }\n}\n\n/**\n * Converts RGBA to HSVA. Based on formula from https://en.wikipedia.org/wiki/HSL_and_HSV\n *\n * @param color RGBA color as an array [0-255, 0-255, 0-255, 0-1]\n */\nexport function RGBAtoHSVA (rgba: RGBA): HSVA {\n if (!rgba) return { h: 0, s: 1, v: 1, a: 1 }\n\n const r = rgba.r / 255\n const g = rgba.g / 255\n const b = rgba.b / 255\n const max = Math.max(r, g, b)\n const min = Math.min(r, g, b)\n\n let h = 0\n\n if (max !== min) {\n if (max === r) {\n h = 60 * (0 + ((g - b) / (max - min)))\n } else if (max === g) {\n h = 60 * (2 + ((b - r) / (max - min)))\n } else if (max === b) {\n h = 60 * (4 + ((r - g) / (max - min)))\n }\n }\n\n if (h < 0) h = h + 360\n\n const s = max === 0 ? 0 : (max - min) / max\n const hsv = [h, s, max]\n\n return { h: hsv[0], s: hsv[1], v: hsv[2], a: rgba.a }\n}\n\nexport function HSVAtoHSLA (hsva: HSVA): HSLA {\n const { h, s, v, a } = hsva\n\n const l = v - (v * s / 2)\n\n const sprime = l === 1 || l === 0 ? 0 : (v - l) / Math.min(l, 1 - l)\n\n return { h, s: sprime, l, a }\n}\n\nexport function HSLAtoHSVA (hsl: HSLA): HSVA {\n const { h, s, l, a } = hsl\n\n const v = l + s * Math.min(l, 1 - l)\n\n const sprime = v === 0 ? 0 : 2 - (2 * l / v)\n\n return { h, s: sprime, v, a }\n}\n\nexport function RGBAtoCSS (rgba: RGBA): string {\n return `rgba(${rgba.r}, ${rgba.g}, ${rgba.b}, ${rgba.a})`\n}\n\nexport function RGBtoCSS (rgba: RGBA): string {\n return RGBAtoCSS({ ...rgba, a: 1 })\n}\n\nexport function RGBAtoHex (rgba: RGBA): Hex {\n const toHex = (v: number) => {\n const h = Math.round(v).toString(16)\n return ('00'.substr(0, 2 - h.length) + h).toUpperCase()\n }\n\n return `#${[\n toHex(rgba.r),\n toHex(rgba.g),\n toHex(rgba.b),\n toHex(Math.round(rgba.a * 255)),\n ].join('')}`\n}\n\nexport function HexToRGBA (hex: Hex): RGBA {\n const rgba = chunk(hex.slice(1), 2).map((c: string) => parseInt(c, 16))\n\n return {\n r: rgba[0],\n g: rgba[1],\n b: rgba[2],\n a: Math.round((rgba[3] / 255) * 100) / 100,\n }\n}\n\nexport function HexToHSVA (hex: Hex): HSVA {\n const rgb = HexToRGBA(hex)\n return RGBAtoHSVA(rgb)\n}\n\nexport function HSVAtoHex (hsva: HSVA): Hex {\n return RGBAtoHex(HSVAtoRGBA(hsva))\n}\n\nexport function parseHex (hex: string): Hex {\n if (hex.startsWith('#')) {\n hex = hex.slice(1)\n }\n\n hex = hex.replace(/([^0-9a-f])/gi, 'F')\n\n if (hex.length === 3) {\n hex = hex.split('').map(x => x + x).join('')\n }\n\n if (hex.length === 6) {\n hex = padEnd(hex, 8, 'F')\n } else {\n hex = padEnd(padEnd(hex, 6), 8, 'F')\n }\n\n return `#${hex}`.toUpperCase().substr(0, 9)\n}\n\nexport function RGBtoInt (rgba: RGBA): ColorInt {\n return (rgba.r << 16) + (rgba.g << 8) + rgba.b\n}\n\n/**\n * Returns the contrast ratio (1-21) between two colors.\n *\n * @param c1 First color\n * @param c2 Second color\n */\nexport function contrastRatio (c1: RGBA, c2: RGBA): number {\n const [, y1] = toXYZ(RGBtoInt(c1))\n const [, y2] = toXYZ(RGBtoInt(c2))\n\n return (Math.max(y1, y2) + 0.05) / (Math.min(y1, y2) + 0.05)\n}\n"],"sourceRoot":"","file":"colorUtils.js"} |