UNPKG

chroma-js

Version:

JavaScript library for color conversions

35 lines (30 loc) 1.19 kB
import { unpack } from '../../utils/index.js'; import multiplyMatrices from '../../utils/multiply-matrices.js'; import { xyz2rgb } from '../lab/lab2rgb.js'; const oklab2rgb = (...args) => { args = unpack(args, 'lab'); const [L, a, b, ...rest] = args; const [X, Y, Z] = OKLab_to_XYZ([L, a, b]); const [r, g, b_] = xyz2rgb(X, Y, Z); return [r, g, b_, ...(rest.length > 0 && rest[0] < 1 ? [rest[0]] : [])]; }; // from https://www.w3.org/TR/css-color-4/#color-conversion-code function OKLab_to_XYZ(OKLab) { // Given OKLab, convert to XYZ relative to D65 var LMStoXYZ = [ [1.2268798758459243, -0.5578149944602171, 0.2813910456659647], [-0.0405757452148008, 1.112286803280317, -0.0717110580655164], [-0.0763729366746601, -0.4214933324022432, 1.5869240198367816] ]; var OKLabtoLMS = [ [1.0, 0.3963377773761749, 0.2158037573099136], [1.0, -0.1055613458156586, -0.0638541728258133], [1.0, -0.0894841775298119, -1.2914855480194092] ]; var LMSnl = multiplyMatrices(OKLabtoLMS, OKLab); return multiplyMatrices( LMStoXYZ, LMSnl.map((c) => c ** 3) ); } export default oklab2rgb;