lib-colors
Version:
Simple node.js library for work with colors
27 lines (26 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.labToRgb = labToRgb;
const lab_to_xyz_helper_1 = require("./lab_to_xyz.helper");
function labToRgb(lab) {
const xyz = (0, lab_to_xyz_helper_1.labToXyz)(lab);
return xyzToRgb(xyz);
}
function xyzToRgb(xyz) {
const { x, y, z, alpha } = { ...xyz };
const xNorm = x / 100;
const yNorm = y / 100;
const zNorm = z / 100;
let r = xNorm * 3.2404542 + yNorm * -1.5371385 + zNorm * -0.4985314;
let g = xNorm * -0.969266 + yNorm * 1.8760108 + zNorm * 0.041556;
let b = xNorm * 0.0556434 + yNorm * -0.2040259 + zNorm * 1.0572252;
r = r > 0.0031308 ? 1.055 * Math.pow(r, 1 / 2.4) - 0.055 : 12.92 * r;
g = g > 0.0031308 ? 1.055 * Math.pow(g, 1 / 2.4) - 0.055 : 12.92 * g;
b = b > 0.0031308 ? 1.055 * Math.pow(b, 1 / 2.4) - 0.055 : 12.92 * b;
return {
r: Math.round(Math.max(0, Math.min(1, r)) * 255),
g: Math.round(Math.max(0, Math.min(1, g)) * 255),
b: Math.round(Math.max(0, Math.min(1, b)) * 255),
a: alpha,
};
}