chroma-js
Version:
JavaScript library for color conversions
35 lines (30 loc) • 1.19 kB
JavaScript
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 = [
[ ],
[-0.0405757452148008, 1.112286803280317, -0.0717110580655164],
[-0.0763729366746601, -0.4214933324022432, 1.5869240198367816]
];
var OKLabtoLMS = [
[ ],
[ ],
[ ]
];
var LMSnl = multiplyMatrices(OKLabtoLMS, OKLab);
return multiplyMatrices(
LMStoXYZ,
LMSnl.map((c) => c ** 3)
);
}
export default oklab2rgb;