Skip to content

Commit

Permalink
feat: normalize lab
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed Mar 29, 2022
1 parent a39e0d7 commit b3f0277
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
3 changes: 3 additions & 0 deletions css.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export function getCSSHSL(color, precision = 5) {
*/
export function getCSSLab(color, precision = 5) {
getLab(color, TMP, D50);
TMP[0] *= 100;
TMP[1] *= 100;
TMP[2] *= 100;
if (precision !== undefined) floorArray(TMP, precision);
return `lab(${TMP[0]}% ${TMP[1]} ${TMP[2]}${
color[3] !== undefined ? ` / ${color[3]}` : ""
Expand Down
16 changes: 7 additions & 9 deletions lab.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function fromLabValueToXYZValue(val, white) {

function fromXYZValueToLabValue(val, white) {
val /= white;
return val > 0.008856 ? val ** (1 / 3) : 7.787037 * val + 16 / 116;
return val > 0.008856 ? Math.cbrt(val) : 7.787037 * val + 16 / 116;
}

/**
Expand All @@ -38,15 +38,13 @@ function fromXYZValueToLabValue(val, white) {
* @return {color}
*/
export function fromLab(color, l, a, b, α, illuminant = D65) {
const y = (l + 16) / 116;
const x = a / 500 + y;
const z = y - b / 200;
const y = (l + 0.16) / 1.16;

return fromXYZ(
color,
fromLabValueToXYZValue(x, illuminant[0]),
fromLabValueToXYZValue(a / 5 + y, illuminant[0]),
fromLabValueToXYZValue(y, illuminant[1]),
fromLabValueToXYZValue(z, illuminant[2]),
fromLabValueToXYZValue(y - b / 2, illuminant[2]),
α
);
}
Expand All @@ -65,9 +63,9 @@ export function getLab(color, out = [], illuminant = D65) {
const y = fromXYZValueToLabValue(xyz[1], illuminant[1]);
const z = fromXYZValueToLabValue(xyz[2], illuminant[2]);

out[0] = 116 * y - 16;
out[1] = 500 * (x - y);
out[2] = 200 * (y - z);
out[0] = 1.16 * y - 0.16;
out[1] = 5 * (x - y);
out[2] = 2 * (y - z);

return setAlpha(out, color[3]);
}

0 comments on commit b3f0277

Please sign in to comment.