UNPKG

1.65 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5function colorToInt(color) {
6 return Math.round(color * 255);
7}
8function convertToInt(red, green, blue) {
9 return colorToInt(red) + "," + colorToInt(green) + "," + colorToInt(blue);
10}
11function hslToRgb(hue, saturation, lightness, convert) {
12 if (convert === void 0) {
13 convert = convertToInt;
14 }
15 if (saturation === 0) {
16 // achromatic
17 return convert(lightness, lightness, lightness);
18 }
19
20 // formulae from https://en.wikipedia.org/wiki/HSL_and_HSV
21 var huePrime = (hue % 360 + 360) % 360 / 60;
22 var chroma = (1 - Math.abs(2 * lightness - 1)) * saturation;
23 var secondComponent = chroma * (1 - Math.abs(huePrime % 2 - 1));
24 var red = 0;
25 var green = 0;
26 var blue = 0;
27 if (huePrime >= 0 && huePrime < 1) {
28 red = chroma;
29 green = secondComponent;
30 } else if (huePrime >= 1 && huePrime < 2) {
31 red = secondComponent;
32 green = chroma;
33 } else if (huePrime >= 2 && huePrime < 3) {
34 green = chroma;
35 blue = secondComponent;
36 } else if (huePrime >= 3 && huePrime < 4) {
37 green = secondComponent;
38 blue = chroma;
39 } else if (huePrime >= 4 && huePrime < 5) {
40 red = secondComponent;
41 blue = chroma;
42 } else if (huePrime >= 5 && huePrime < 6) {
43 red = chroma;
44 blue = secondComponent;
45 }
46 var lightnessModification = lightness - chroma / 2;
47 var finalRed = red + lightnessModification;
48 var finalGreen = green + lightnessModification;
49 var finalBlue = blue + lightnessModification;
50 return convert(finalRed, finalGreen, finalBlue);
51}
52var _default = exports["default"] = hslToRgb;
53module.exports = exports.default;
\No newline at end of file