2013-02-16 09:43:27 +01:00
|
|
|
// Convert an sRGB value in [0, 255] to a linear intensity
|
|
|
|
// value in [0, 1].
|
|
|
|
//
|
2020-03-27 01:32:21 +01:00
|
|
|
// https://en.wikipedia.org/wiki/SRGB#The_reverse_transformation
|
2021-05-16 20:20:48 +02:00
|
|
|
export function sRGB_to_linear(v: number): number {
|
2020-10-07 11:54:18 +02:00
|
|
|
v = v / 255;
|
2013-02-16 09:43:27 +01:00
|
|
|
if (v <= 0.04045) {
|
|
|
|
return v / 12.92;
|
|
|
|
}
|
2016-12-02 21:34:35 +01:00
|
|
|
return Math.pow((v + 0.055) / 1.055, 2.4);
|
2021-02-10 17:04:19 +01:00
|
|
|
}
|
2013-02-16 09:43:27 +01:00
|
|
|
|
|
|
|
// Compute luminance (CIE Y stimulus) from linear intensity
|
|
|
|
// of sRGB / Rec. 709 primaries.
|
2021-05-16 20:20:48 +02:00
|
|
|
export function rgb_luminance(channel: [number, number, number]): number {
|
2018-06-04 21:13:07 +02:00
|
|
|
return 0.2126 * channel[0] + 0.7152 * channel[1] + 0.0722 * channel[2];
|
2021-02-10 17:04:19 +01:00
|
|
|
}
|
2013-02-16 09:43:27 +01:00
|
|
|
|
|
|
|
// Convert luminance (photometric, CIE Y)
|
|
|
|
// to lightness (perceptual, CIE L*)
|
|
|
|
//
|
2020-03-27 01:32:21 +01:00
|
|
|
// https://en.wikipedia.org/wiki/Lab_color_space#Forward_transformation
|
2021-05-16 20:20:48 +02:00
|
|
|
export function luminance_to_lightness(luminance: number): number {
|
2019-11-02 00:06:25 +01:00
|
|
|
let v;
|
2018-06-06 18:50:09 +02:00
|
|
|
if (luminance <= 216 / 24389) {
|
2020-07-15 00:34:28 +02:00
|
|
|
v = (841 / 108) * luminance + 4 / 29;
|
2013-02-16 09:43:27 +01:00
|
|
|
} else {
|
2018-06-04 21:13:07 +02:00
|
|
|
v = Math.pow(luminance, 1 / 3);
|
2013-02-16 09:43:27 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:13:07 +02:00
|
|
|
return 116 * v - 16;
|
2021-02-10 17:04:19 +01:00
|
|
|
}
|