image-js / image-js-typescript

Temporary repository to work on the migration of image-js to TypeScript
https://image-js.github.io/image-js-typescript/
MIT License
5 stars 5 forks source link

Convert L*a*b* to RGB #106

Closed opatiny closed 2 years ago

opatiny commented 2 years ago

Find a package that converts LAB color codes into RGB.

Wikipedia article on L*a*b*: https://en.wikipedia.org/wiki/CIELAB_color_space

"The lightness value, L*, also referred to as "Lstar," defines black at 0 and white at 100. The a* axis is relative to the green–red opponent colors, with negative values toward green and positive values toward red. The b* axis represents the blue–yellow opponents, with negative numbers toward blue and positive toward yellow."

"The a and b axes are unbounded, and depending on the reference white they can easily exceed ±150 to cover the human gamut. Nevertheless, software implementations often clamp these values for practical reasons. For instance, if integer math is being used it is common to clamp a and b in the range of −128 to 127."

Possible libraries achieving that:

opatiny commented 2 years ago

In the end, we use the colord library and convert the colors like this:

import { colord, extend } from 'colord';
import labPlugin from 'colord/plugins/lab';

extend([labPlugin]);

console.log(colord({ r: 255, g: 0, b: 0 }).toLab());
console.log(colord({ l: 50, a: -128, b: -128 }).toRgb());