3 // these algorithms are sourced from https://drafts.csswg.org/css-color/#color-conversion-code
5 function lin_sRGB(RGB) {
6 // convert an array of sRGB values in the range 0.0 - 1.0
7 // to linear light (un-companded) form.
8 // https://en.wikipedia.org/wiki/SRGB
9 return RGB.map(function(val) {
14 return Math.pow((val + 0.055) / 1.055, 2.4);
18 function matrixMultiple3d(matrix, vector) {
20 matrix[0][0] * vector[0] +
21 matrix[0][1] * vector[1] +
22 matrix[0][2] * vector[2],
23 matrix[1][0] * vector[0] +
24 matrix[1][1] * vector[1] +
25 matrix[1][2] * vector[2],
26 matrix[2][0] * vector[0] +
27 matrix[2][1] * vector[1] +
28 matrix[2][2] * vector[2]
32 function srgb2xyz(srgb) {
33 return matrixMultiple3d(
35 [0.4124564, 0.3575761, 0.1804375],
36 [0.2126729, 0.7151522, 0.072175],
37 [0.0193339, 0.119192, 0.9503041]
43 function chromaticAdaptationD65_D50(xyz) {
44 return matrixMultiple3d(
46 [1.0478112, 0.0228866, -0.050127],
47 [0.0295424, 0.9904844, -0.0170491],
48 [-0.0092345, 0.0150436, 0.7521316]
54 function xyz2lab(xyzIn) {
55 // Assuming XYZ is relative to D50, convert to CIE Lab
56 // from CIE standard, which now defines these as a rational fraction
57 const ε = 216 / 24389; // 6^3/29^3
58 const κ = 24389 / 27; // 29^3/3^3
59 const white = [0.9642, 1.0, 0.8249]; // D50 reference white
61 // compute xyz, which is XYZ scaled relative to reference white
62 const xyz = xyzIn.map((value, i) => value / white[i]);
66 value => (value > ε ? Math.cbrt(value) : (κ * value + 16) / 116)
71 500 * (f[0] - f[1]), // a
72 200 * (f[1] - f[2]) // b
76 function rgb2hsl(r, g, b) {
81 const M = Math.max(r, g, b);
82 const m = Math.min(r, g, b);
87 h = ((g - b) / d) % 6;
105 s = d / (1 - Math.abs(2 * l - 1));
111 return [Math.round(h), Math.round(s), Math.round(l)];
114 function rgb2hwb(rgb_r, rgb_g, rgb_b) {
119 const w = Math.min(rgb_r, rgb_g, rgb_b);
120 const v = Math.max(rgb_r, rgb_g, rgb_b);
125 return [0, Math.round(w * 100), Math.round(b * 100)];
129 rgb_r === w ? rgb_g - rgb_b : rgb_g === w ? rgb_b - rgb_r : rgb_r - rgb_g;
130 const i = rgb_r === w ? 3 : rgb_g === w ? 5 : 1;
133 Math.round((i - f / (v - w)) / 6 * 360) % 360,
139 function perc255(value) {
140 return Math.round(value * 100 / 255) + "%";
143 function generateColorFuncs(hexString) {
144 if (hexString.length !== 7) {
146 "Invalid hex string color definition (" +
148 ") - expected 6 character hex string"
151 const rgb = [0, 0, 0];
152 for (let i = 0; i < 3; i += 1) {
153 rgb[i] = parseInt(hexString.substr(2 * i + 1, 2), 16);
155 const hsl = rgb2hsl(rgb[0], rgb[1], rgb[2]);
156 const hwb = rgb2hwb(rgb[0], rgb[1], rgb[2]);
158 const rgbStr = rgb[0] + "," + rgb[1] + "," + rgb[2];
160 perc255(rgb[0]) + "," + perc255(rgb[1]) + "," + perc255(rgb[2]);
161 const hslStr = hsl[0] + "," + hsl[1] + "%," + hsl[2] + "%";
162 const hwbStr = hwb[0] + "," + hwb[1] + "%," + hwb[2] + "%";
164 // *very* convoluted process, just to be able to establish if the color
165 // is gray -- or not.
166 const linRgb = lin_sRGB([rgb[0] / 255, rgb[1] / 255, rgb[2] / 255]);
167 const xyz_d65 = srgb2xyz(linRgb);
168 const xyz_d50 = chromaticAdaptationD65_D50(xyz_d65);
169 const lab = xyz2lab(xyz_d50);
171 func.push("rgb(" + rgbStr + ")");
172 func.push("rgba(" + rgbStr + ",1)");
173 func.push("rgba(" + rgbStr + ",100%)");
174 func.push("rgb(" + rgbPercStr + ")");
175 func.push("rgba(" + rgbPercStr + ",1)");
176 func.push("rgba(" + rgbPercStr + ",100%)");
177 func.push("hsl(" + hslStr + ")");
178 func.push("hsla(" + hslStr + ",1)");
179 func.push("hsla(" + hslStr + ",100%)");
180 func.push("hwb(" + hwbStr + ")");
181 func.push("hwb(" + hwbStr + ",1)");
182 func.push("hwb(" + hwbStr + ",100%)");
184 // technically, this should be 0 - but then #808080 wouldn't even be gray
185 if (lab[1] * lab[1] < 0.01 && lab[2] * lab[2] < 0.01) {
187 const grayStr = Math.round(lab[0]);
188 func.push("gray(" + grayStr + ")");
189 func.push("gray(" + grayStr + ",1)");
190 func.push("gray(" + grayStr + ",100%)");
191 func.push("gray(" + grayStr + "%)");
192 func.push("gray(" + grayStr + "%,1)");
193 func.push("gray(" + grayStr + "%,100%)");
198 module.exports = generateColorFuncs;