@luckydye/calendar 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Color.ts ADDED
@@ -0,0 +1,64 @@
1
+
2
+ function componentToHex(c) {
3
+ var hex = c.toString(16);
4
+ return hex.length === 1 ? "0" + hex : hex;
5
+ }
6
+
7
+ export function rgbToHex(r, g, b) {
8
+ return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
9
+ }
10
+
11
+ export function hexToRgb(hex: string) {
12
+ return [
13
+ parseInt(`${hex[1]}${hex[2]}`, 16),
14
+ parseInt(`${hex[3]}${hex[4]}`, 16),
15
+ parseInt(`${hex[5]}${hex[6]}`, 16)
16
+ ] as [number, number, number];
17
+ }
18
+
19
+ export function rgbToHsl([r,g,b]) {
20
+ // Make r, g, and b fractions of 1
21
+ r /= 255;
22
+ g /= 255;
23
+ b /= 255;
24
+
25
+ // Find greatest and smallest channel values
26
+ let cmin = Math.min(r,g,b),
27
+ cmax = Math.max(r,g,b),
28
+ delta = cmax - cmin,
29
+ h = 0,
30
+ s = 0,
31
+ l = 0;
32
+
33
+ // Calculate hue
34
+ // No difference
35
+ if (delta === 0)
36
+ h = 0;
37
+ // Red is max
38
+ else if (cmax === r)
39
+ h = ((g - b) / delta) % 6;
40
+ // Green is max
41
+ else if (cmax === g)
42
+ h = (b - r) / delta + 2;
43
+ // Blue is max
44
+ else
45
+ h = (r - g) / delta + 4;
46
+
47
+ h = Math.round(h * 60);
48
+
49
+ // Make negative hues positive behind 360°
50
+ if (h < 0)
51
+ h += 360;
52
+
53
+ // Calculate lightness
54
+ l = (cmax + cmin) / 2;
55
+
56
+ // Calculate saturation
57
+ s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));
58
+
59
+ // Multiply l and s by 100
60
+ s = +(s * 100).toFixed(1);
61
+ l = +(l * 100).toFixed(1);
62
+
63
+ return [h, s, l];
64
+ }