UNPKG

1.39 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5function rgbToHsl(color) {
6 // make sure rgb are contained in a set of [0, 255]
7 var red = color.red / 255;
8 var green = color.green / 255;
9 var blue = color.blue / 255;
10 var max = Math.max(red, green, blue);
11 var min = Math.min(red, green, blue);
12 var lightness = (max + min) / 2;
13 if (max === min) {
14 // achromatic
15 if (color.alpha !== undefined) {
16 return {
17 hue: 0,
18 saturation: 0,
19 lightness: lightness,
20 alpha: color.alpha
21 };
22 } else {
23 return {
24 hue: 0,
25 saturation: 0,
26 lightness: lightness
27 };
28 }
29 }
30 var hue;
31 var delta = max - min;
32 var saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
33 switch (max) {
34 case red:
35 hue = (green - blue) / delta + (green < blue ? 6 : 0);
36 break;
37 case green:
38 hue = (blue - red) / delta + 2;
39 break;
40 default:
41 // blue case
42 hue = (red - green) / delta + 4;
43 break;
44 }
45 hue *= 60;
46 if (color.alpha !== undefined) {
47 return {
48 hue: hue,
49 saturation: saturation,
50 lightness: lightness,
51 alpha: color.alpha
52 };
53 }
54 return {
55 hue: hue,
56 saturation: saturation,
57 lightness: lightness
58 };
59}
60var _default = exports["default"] = rgbToHsl;
61module.exports = exports.default;
\No newline at end of file