2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
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
|
2013-02-16 09:43:27 +01:00
|
|
|
exports.sRGB_to_linear = function (v) {
|
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);
|
2013-02-16 09:43:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compute luminance (CIE Y stimulus) from linear intensity
|
|
|
|
// of sRGB / Rec. 709 primaries.
|
|
|
|
exports.rgb_luminance = function (channel) {
|
2018-06-04 21:13:07 +02:00
|
|
|
return 0.2126 * channel[0] + 0.7152 * channel[1] + 0.0722 * channel[2];
|
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
|
2013-02-16 09:43:27 +01:00
|
|
|
exports.luminance_to_lightness = function (luminance) {
|
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;
|
2013-02-16 09:43:27 +01:00
|
|
|
};
|
|
|
|
|
2014-01-08 16:12:22 +01:00
|
|
|
exports.getDecimalColor = function (hexcolor) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return {
|
2020-10-07 09:41:22 +02:00
|
|
|
r: Number.parseInt(hexcolor.slice(1, 3), 16),
|
|
|
|
g: Number.parseInt(hexcolor.slice(3, 5), 16),
|
|
|
|
b: Number.parseInt(hexcolor.slice(5, 7), 16),
|
2020-07-15 00:34:28 +02:00
|
|
|
};
|
2014-01-08 16:12:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getLighterColor = function (rgb, lightness) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return {
|
|
|
|
r: Math.round(lightness * 255 + (1 - lightness) * rgb.r),
|
|
|
|
g: Math.round(lightness * 255 + (1 - lightness) * rgb.g),
|
|
|
|
b: Math.round(lightness * 255 + (1 - lightness) * rgb.b),
|
|
|
|
};
|
2014-01-08 16:12:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
exports.getHexColor = function (rgb) {
|
2020-07-15 00:34:28 +02:00
|
|
|
return (
|
|
|
|
"#" +
|
2020-10-07 09:17:30 +02:00
|
|
|
Number.parseInt(rgb.r, 10).toString(16) +
|
|
|
|
Number.parseInt(rgb.g, 10).toString(16) +
|
|
|
|
Number.parseInt(rgb.b, 10).toString(16)
|
2020-07-15 00:34:28 +02:00
|
|
|
);
|
2014-01-08 16:12:22 +01:00
|
|
|
};
|
|
|
|
|
2019-10-25 09:45:13 +02:00
|
|
|
window.colorspace = exports;
|