utilium 2.6.3 → 2.7.0

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.
@@ -0,0 +1,57 @@
1
+ export type RGB = [r: number, g: number, b: number];
2
+ export type HSL = [h: number, s: number, l: number];
3
+ export type HSV = [h: number, s: number, v: number];
4
+ /**
5
+ * Converts a hex string to RGB.
6
+ * `#` is optional.
7
+ */
8
+ export declare function hexToRGB(hex: string): RGB;
9
+ /**
10
+ * @returns RGB values in hex format.
11
+ * Does not include a leading `#` (in case you want to do something else with the value)
12
+ */
13
+ export declare function rgbToHex(r: number, g: number, b: number): string;
14
+ /**
15
+ * Converts RGB to HSL.
16
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
17
+ */
18
+ export declare function rgbToHSL(r: number, g: number, b: number): HSL;
19
+ /**
20
+ * Converts HSL to RGB
21
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
22
+ */
23
+ export declare function hslToRGB(h: number, s: number, l: number): RGB;
24
+ /**
25
+ * Converts RGB to HSV
26
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
27
+ */
28
+ export declare function rgbToHSV(r: number, g: number, b: number): HSV;
29
+ /**
30
+ * Converts HSV to RGB.
31
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
32
+ */
33
+ export declare function hsvToRGB(h: number, s: number, v: number): RGB;
34
+ /**
35
+ * Converts a hex string to HSL.
36
+ */
37
+ export declare function hexToHSL(hex: string): HSL;
38
+ /**
39
+ * Converts HSL to a hex string.
40
+ */
41
+ export declare function hslToHex(h: number, s: number, l: number): string;
42
+ /**
43
+ * Converts a hex string to HSV.
44
+ */
45
+ export declare function hexToHSV(hex: string): HSV;
46
+ /**
47
+ * Converts HSV to a hex string.
48
+ */
49
+ export declare function hsvToHex(h: number, s: number, v: number): string;
50
+ /**
51
+ * Converts HSL to HSV.
52
+ */
53
+ export declare function hslToHSV(h: number, s: number, l: number): HSV;
54
+ /**
55
+ * Converts HSV to HSL.
56
+ */
57
+ export declare function hsvToHSL(h: number, s: number, v: number): HSL;
package/dist/color.js ADDED
@@ -0,0 +1,144 @@
1
+ // SPDX-License-Identifier: LGPL-3.0-or-later
2
+ // Copyright (c) 2026 James Prevett
3
+ /**
4
+ * Converts a hex string to RGB.
5
+ * `#` is optional.
6
+ */
7
+ export function hexToRGB(hex) {
8
+ if (hex[0] == '#')
9
+ hex = hex.slice(1);
10
+ return [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16)];
11
+ }
12
+ /**
13
+ * @returns RGB values in hex format.
14
+ * Does not include a leading `#` (in case you want to do something else with the value)
15
+ */
16
+ export function rgbToHex(r, g, b) {
17
+ return r.toString(16).padStart(2, '0') + g.toString(16).padStart(2, '0') + b.toString(16).padStart(2, '0');
18
+ }
19
+ /**
20
+ * Converts RGB to HSL.
21
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
22
+ */
23
+ export function rgbToHSL(r, g, b) {
24
+ r /= 255;
25
+ g /= 255;
26
+ b /= 255;
27
+ const min = Math.min(r, g, b), max = Math.max(r, g, b);
28
+ const l = (max + min) / 2;
29
+ const delta = max - min;
30
+ if (delta === 0)
31
+ return [0, 0, l];
32
+ const sat = delta / (l > 0.5 ? 2 - max - min : max + min);
33
+ const hue = max === r ? (g - b) / delta + (g < b ? 6 : 0) : max === g ? (b - r) / delta + 2 : (r - g) / delta + 4;
34
+ return [hue / 6, sat, l];
35
+ }
36
+ function hueToRGB(p, q, t) {
37
+ if (t < 0)
38
+ t += 1;
39
+ if (t > 1)
40
+ t -= 1;
41
+ if (t < 1 / 6)
42
+ return Math.round(255 * (p + (q - p) * 6 * t));
43
+ if (t < 1 / 2)
44
+ return Math.round(255 * q);
45
+ if (t < 2 / 3)
46
+ return Math.round(255 * (p + (q - p) * (2 / 3 - t) * 6));
47
+ return Math.round(255 * p);
48
+ }
49
+ /**
50
+ * Converts HSL to RGB
51
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
52
+ */
53
+ export function hslToRGB(h, s, l) {
54
+ if (s === 0) {
55
+ l = Math.round(l * 255);
56
+ return [l, l, l];
57
+ }
58
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
59
+ const p = 2 * l - q;
60
+ return [hueToRGB(p, q, h + 1 / 3), hueToRGB(p, q, h), hueToRGB(p, q, h - 1 / 3)];
61
+ }
62
+ /**
63
+ * Converts RGB to HSV
64
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
65
+ */
66
+ export function rgbToHSV(r, g, b) {
67
+ r /= 255;
68
+ g /= 255;
69
+ b /= 255;
70
+ const min = Math.min(r, g, b), max = Math.max(r, g, b);
71
+ const delta = max - min;
72
+ const sat = max == 0 ? 0 : delta / max;
73
+ if (delta === 0)
74
+ return [0, sat, max];
75
+ const hue = max === r ? (g - b) / delta + (g < b ? 6 : 0) : max === g ? (b - r) / delta + 2 : (r - g) / delta + 4;
76
+ return [hue / 6, sat, max];
77
+ }
78
+ /**
79
+ * Converts HSV to RGB.
80
+ * @see https://en.wikipedia.org/wiki/HSL_and_HSV
81
+ */
82
+ export function hsvToRGB(h, s, v) {
83
+ v = Math.round(v * 255);
84
+ const i = Math.floor(h * 6);
85
+ const f = h * 6 - i;
86
+ const p = Math.round(v * (1 - s));
87
+ const q = Math.round(v * (1 - f * s));
88
+ const t = Math.round(v * (1 - (1 - f) * s));
89
+ switch (i % 6) {
90
+ case 0:
91
+ return [v, t, p];
92
+ case 1:
93
+ return [q, v, p];
94
+ case 2:
95
+ return [p, v, t];
96
+ case 3:
97
+ return [p, q, v];
98
+ case 4:
99
+ return [t, p, v];
100
+ case 5:
101
+ return [v, p, q];
102
+ }
103
+ throw new Error('Unreachable code in hsvToRGB');
104
+ }
105
+ /**
106
+ * Converts a hex string to HSL.
107
+ */
108
+ export function hexToHSL(hex) {
109
+ return rgbToHSL(...hexToRGB(hex));
110
+ }
111
+ /**
112
+ * Converts HSL to a hex string.
113
+ */
114
+ export function hslToHex(h, s, l) {
115
+ return rgbToHex(...hslToRGB(h, s, l));
116
+ }
117
+ /**
118
+ * Converts a hex string to HSV.
119
+ */
120
+ export function hexToHSV(hex) {
121
+ return rgbToHSV(...hexToRGB(hex));
122
+ }
123
+ /**
124
+ * Converts HSV to a hex string.
125
+ */
126
+ export function hsvToHex(h, s, v) {
127
+ return rgbToHex(...hsvToRGB(h, s, v));
128
+ }
129
+ /**
130
+ * Converts HSL to HSV.
131
+ */
132
+ export function hslToHSV(h, s, l) {
133
+ const v = l + s * Math.min(l, 1 - l);
134
+ const newS = v === 0 ? 0 : 2 * (1 - l / v);
135
+ return [h, newS, v];
136
+ }
137
+ /**
138
+ * Converts HSV to HSL.
139
+ */
140
+ export function hsvToHSL(h, s, v) {
141
+ const l = v * (1 - s / 2);
142
+ const newS = l === 0 || l === 1 ? 0 : (v - l) / Math.min(l, 1 - l);
143
+ return [h, newS, l];
144
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "2.6.3",
3
+ "version": "2.7.0",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",