// @ts-expect-error import Color from '../node_modules/colorjs.io/src/color' // @ts-expect-error import OKLCH from '../node_modules/colorjs.io/src/spaces/oklch' // @ts-expect-error import SRGB from '../node_modules/colorjs.io/src/spaces/srgb' Color.spaces.oklch = OKLCH Color.spaces.srgb = SRGB const formatOklch = ({l, c, h, a = 1}: {l: number, c: number, h: number, a?: number}) => { return `oklch(${l}% ${c} ${h}${a < 1 ? ` / ${(a * 100).toFixed(0)}%` : ''})` } export const rgbToOklch = ({r, g, b, a = 1}: {r: number, g: number, b: number, a?: number}) => { const color = new Color('srgb', [r / 255, g / 255, b / 255], a) const oklch = color.to('oklch') return formatOklch({ l: parseFloat((oklch.coords[0] * 100).toFixed(2)), c: parseFloat(oklch.coords[1].toFixed(3)), h: Number.isNaN(oklch.coords[2]) ? 0 : parseFloat(oklch.coords[2].toFixed(1)), a: parseFloat(oklch.alpha.toFixed(2)), }) } export function oklchToRgb(input: string) { const color = new Color(input) const rgb = color.to('srgb') return { r: Math.round(rgb.coords[0] * 255), g: Math.round(rgb.coords[1] * 255), b: Math.round(rgb.coords[2] * 255), a: parseFloat(rgb.alpha.toFixed(2)), } }