@zag-js/color-utils 0.10.4 → 0.11.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.
- package/dist/{types.d.ts → index.d.mts} +27 -5
- package/dist/index.d.ts +84 -3
- package/dist/index.js +456 -7
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +434 -2
- package/dist/index.mjs.map +1 -0
- package/package.json +2 -2
- package/src/hsb-color.ts +6 -1
- package/src/hsl-color.ts +6 -1
- package/src/rgb-color.ts +6 -1
- package/dist/color.d.ts +0 -17
- package/dist/color.js +0 -35
- package/dist/color.mjs +0 -31
- package/dist/hsb-color.d.ts +0 -29
- package/dist/hsb-color.js +0 -111
- package/dist/hsb-color.mjs +0 -107
- package/dist/hsl-color.d.ts +0 -30
- package/dist/hsl-color.js +0 -112
- package/dist/hsl-color.mjs +0 -107
- package/dist/parse-color.d.ts +0 -3
- package/dist/parse-color.js +0 -25
- package/dist/parse-color.mjs +0 -20
- package/dist/rgb-color.d.ts +0 -30
- package/dist/rgb-color.js +0 -166
- package/dist/rgb-color.mjs +0 -162
- package/dist/utils.d.ts +0 -3
- package/dist/utils.js +0 -17
- package/dist/utils.mjs +0 -11
package/dist/hsb-color.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const color = require('./color.js');
|
|
6
|
-
const hslColor = require('./hsl-color.js');
|
|
7
|
-
const rgbColor = require('./rgb-color.js');
|
|
8
|
-
const utils = require('./utils.js');
|
|
9
|
-
|
|
10
|
-
const HSB_REGEX = /hsb\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsba\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
11
|
-
class HSBColor extends color.Color {
|
|
12
|
-
constructor(hue, saturation, brightness, alpha) {
|
|
13
|
-
super();
|
|
14
|
-
this.hue = hue;
|
|
15
|
-
this.saturation = saturation;
|
|
16
|
-
this.brightness = brightness;
|
|
17
|
-
this.alpha = alpha;
|
|
18
|
-
}
|
|
19
|
-
static parse(value) {
|
|
20
|
-
let m;
|
|
21
|
-
if (m = value.match(HSB_REGEX)) {
|
|
22
|
-
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
23
|
-
return new HSBColor(utils.mod(h, 360), utils.clampValue(s, 0, 100), utils.clampValue(b, 0, 100), utils.clampValue(a ?? 1, 0, 1));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
toString(format) {
|
|
27
|
-
switch (format) {
|
|
28
|
-
case "css":
|
|
29
|
-
return this.toHSL().toString("css");
|
|
30
|
-
case "hex":
|
|
31
|
-
return this.toRGB().toString("hex");
|
|
32
|
-
case "hexa":
|
|
33
|
-
return this.toRGB().toString("hexa");
|
|
34
|
-
case "hsb":
|
|
35
|
-
return `hsb(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.brightness, 2)}%)`;
|
|
36
|
-
case "hsba":
|
|
37
|
-
return `hsba(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
38
|
-
default:
|
|
39
|
-
return this.toFormat(format).toString(format);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
toFormat(format) {
|
|
43
|
-
switch (format) {
|
|
44
|
-
case "hsb":
|
|
45
|
-
case "hsba":
|
|
46
|
-
return this;
|
|
47
|
-
case "hsl":
|
|
48
|
-
case "hsla":
|
|
49
|
-
return this.toHSL();
|
|
50
|
-
case "rgb":
|
|
51
|
-
case "rgba":
|
|
52
|
-
return this.toRGB();
|
|
53
|
-
default:
|
|
54
|
-
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Converts a HSB color to HSL.
|
|
59
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
60
|
-
* @returns An HSLColor object.
|
|
61
|
-
*/
|
|
62
|
-
toHSL() {
|
|
63
|
-
let saturation = this.saturation / 100;
|
|
64
|
-
let brightness = this.brightness / 100;
|
|
65
|
-
let lightness = brightness * (1 - saturation / 2);
|
|
66
|
-
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
67
|
-
return new hslColor.HSLColor(
|
|
68
|
-
utils.toFixedNumber(this.hue, 2),
|
|
69
|
-
utils.toFixedNumber(saturation * 100, 2),
|
|
70
|
-
utils.toFixedNumber(lightness * 100, 2),
|
|
71
|
-
this.alpha
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Converts a HSV color value to RGB.
|
|
76
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
77
|
-
* @returns An RGBColor object.
|
|
78
|
-
*/
|
|
79
|
-
toRGB() {
|
|
80
|
-
let hue = this.hue;
|
|
81
|
-
let saturation = this.saturation / 100;
|
|
82
|
-
let brightness = this.brightness / 100;
|
|
83
|
-
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
84
|
-
return new rgbColor.RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
85
|
-
}
|
|
86
|
-
clone() {
|
|
87
|
-
return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
88
|
-
}
|
|
89
|
-
getChannelRange(channel) {
|
|
90
|
-
switch (channel) {
|
|
91
|
-
case "hue":
|
|
92
|
-
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
93
|
-
case "saturation":
|
|
94
|
-
case "brightness":
|
|
95
|
-
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
96
|
-
case "alpha":
|
|
97
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
98
|
-
default:
|
|
99
|
-
throw new Error("Unknown color channel: " + channel);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
getColorSpace() {
|
|
103
|
-
return "hsb";
|
|
104
|
-
}
|
|
105
|
-
static colorChannels = ["hue", "saturation", "brightness"];
|
|
106
|
-
getColorChannels() {
|
|
107
|
-
return HSBColor.colorChannels;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
exports.HSBColor = HSBColor;
|
package/dist/hsb-color.mjs
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Color } from './color.mjs';
|
|
2
|
-
import { HSLColor } from './hsl-color.mjs';
|
|
3
|
-
import { RGBColor } from './rgb-color.mjs';
|
|
4
|
-
import { mod, clampValue, toFixedNumber } from './utils.mjs';
|
|
5
|
-
|
|
6
|
-
const HSB_REGEX = /hsb\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsba\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
7
|
-
class HSBColor extends Color {
|
|
8
|
-
constructor(hue, saturation, brightness, alpha) {
|
|
9
|
-
super();
|
|
10
|
-
this.hue = hue;
|
|
11
|
-
this.saturation = saturation;
|
|
12
|
-
this.brightness = brightness;
|
|
13
|
-
this.alpha = alpha;
|
|
14
|
-
}
|
|
15
|
-
static parse(value) {
|
|
16
|
-
let m;
|
|
17
|
-
if (m = value.match(HSB_REGEX)) {
|
|
18
|
-
const [h, s, b, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
19
|
-
return new HSBColor(mod(h, 360), clampValue(s, 0, 100), clampValue(b, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
toString(format) {
|
|
23
|
-
switch (format) {
|
|
24
|
-
case "css":
|
|
25
|
-
return this.toHSL().toString("css");
|
|
26
|
-
case "hex":
|
|
27
|
-
return this.toRGB().toString("hex");
|
|
28
|
-
case "hexa":
|
|
29
|
-
return this.toRGB().toString("hexa");
|
|
30
|
-
case "hsb":
|
|
31
|
-
return `hsb(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%)`;
|
|
32
|
-
case "hsba":
|
|
33
|
-
return `hsba(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.brightness, 2)}%, ${this.alpha})`;
|
|
34
|
-
default:
|
|
35
|
-
return this.toFormat(format).toString(format);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
toFormat(format) {
|
|
39
|
-
switch (format) {
|
|
40
|
-
case "hsb":
|
|
41
|
-
case "hsba":
|
|
42
|
-
return this;
|
|
43
|
-
case "hsl":
|
|
44
|
-
case "hsla":
|
|
45
|
-
return this.toHSL();
|
|
46
|
-
case "rgb":
|
|
47
|
-
case "rgba":
|
|
48
|
-
return this.toRGB();
|
|
49
|
-
default:
|
|
50
|
-
throw new Error("Unsupported color conversion: hsb -> " + format);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Converts a HSB color to HSL.
|
|
55
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_HSL.
|
|
56
|
-
* @returns An HSLColor object.
|
|
57
|
-
*/
|
|
58
|
-
toHSL() {
|
|
59
|
-
let saturation = this.saturation / 100;
|
|
60
|
-
let brightness = this.brightness / 100;
|
|
61
|
-
let lightness = brightness * (1 - saturation / 2);
|
|
62
|
-
saturation = lightness === 0 || lightness === 1 ? 0 : (brightness - lightness) / Math.min(lightness, 1 - lightness);
|
|
63
|
-
return new HSLColor(
|
|
64
|
-
toFixedNumber(this.hue, 2),
|
|
65
|
-
toFixedNumber(saturation * 100, 2),
|
|
66
|
-
toFixedNumber(lightness * 100, 2),
|
|
67
|
-
this.alpha
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Converts a HSV color value to RGB.
|
|
72
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB_alternative.
|
|
73
|
-
* @returns An RGBColor object.
|
|
74
|
-
*/
|
|
75
|
-
toRGB() {
|
|
76
|
-
let hue = this.hue;
|
|
77
|
-
let saturation = this.saturation / 100;
|
|
78
|
-
let brightness = this.brightness / 100;
|
|
79
|
-
let fn = (n, k = (n + hue / 60) % 6) => brightness - saturation * brightness * Math.max(Math.min(k, 4 - k, 1), 0);
|
|
80
|
-
return new RGBColor(Math.round(fn(5) * 255), Math.round(fn(3) * 255), Math.round(fn(1) * 255), this.alpha);
|
|
81
|
-
}
|
|
82
|
-
clone() {
|
|
83
|
-
return new HSBColor(this.hue, this.saturation, this.brightness, this.alpha);
|
|
84
|
-
}
|
|
85
|
-
getChannelRange(channel) {
|
|
86
|
-
switch (channel) {
|
|
87
|
-
case "hue":
|
|
88
|
-
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
89
|
-
case "saturation":
|
|
90
|
-
case "brightness":
|
|
91
|
-
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
92
|
-
case "alpha":
|
|
93
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
94
|
-
default:
|
|
95
|
-
throw new Error("Unknown color channel: " + channel);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
getColorSpace() {
|
|
99
|
-
return "hsb";
|
|
100
|
-
}
|
|
101
|
-
static colorChannels = ["hue", "saturation", "brightness"];
|
|
102
|
-
getColorChannels() {
|
|
103
|
-
return HSBColor.colorChannels;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export { HSBColor };
|
package/dist/hsl-color.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Color } from "./color";
|
|
2
|
-
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types";
|
|
3
|
-
export declare const HSL_REGEX: RegExp;
|
|
4
|
-
export declare class HSLColor extends Color {
|
|
5
|
-
private hue;
|
|
6
|
-
private saturation;
|
|
7
|
-
private lightness;
|
|
8
|
-
private alpha;
|
|
9
|
-
constructor(hue: number, saturation: number, lightness: number, alpha: number);
|
|
10
|
-
static parse(value: string): HSLColor | void;
|
|
11
|
-
toString(format: ColorFormat | "css"): string;
|
|
12
|
-
toFormat(format: ColorFormat): ColorType;
|
|
13
|
-
/**
|
|
14
|
-
* Converts a HSL color to HSB.
|
|
15
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
16
|
-
* @returns An HSBColor object.
|
|
17
|
-
*/
|
|
18
|
-
private toHSB;
|
|
19
|
-
/**
|
|
20
|
-
* Converts a HSL color to RGB.
|
|
21
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
22
|
-
* @returns An RGBColor object.
|
|
23
|
-
*/
|
|
24
|
-
private toRGB;
|
|
25
|
-
clone(): ColorType;
|
|
26
|
-
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
27
|
-
getColorSpace(): ColorFormat;
|
|
28
|
-
private static colorChannels;
|
|
29
|
-
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
30
|
-
}
|
package/dist/hsl-color.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const color = require('./color.js');
|
|
6
|
-
const hsbColor = require('./hsb-color.js');
|
|
7
|
-
const rgbColor = require('./rgb-color.js');
|
|
8
|
-
const utils = require('./utils.js');
|
|
9
|
-
|
|
10
|
-
const HSL_REGEX = /hsl\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsla\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
11
|
-
class HSLColor extends color.Color {
|
|
12
|
-
constructor(hue, saturation, lightness, alpha) {
|
|
13
|
-
super();
|
|
14
|
-
this.hue = hue;
|
|
15
|
-
this.saturation = saturation;
|
|
16
|
-
this.lightness = lightness;
|
|
17
|
-
this.alpha = alpha;
|
|
18
|
-
}
|
|
19
|
-
static parse(value) {
|
|
20
|
-
let m;
|
|
21
|
-
if (m = value.match(HSL_REGEX)) {
|
|
22
|
-
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
23
|
-
return new HSLColor(utils.mod(h, 360), utils.clampValue(s, 0, 100), utils.clampValue(l, 0, 100), utils.clampValue(a ?? 1, 0, 1));
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
toString(format) {
|
|
27
|
-
switch (format) {
|
|
28
|
-
case "hex":
|
|
29
|
-
return this.toRGB().toString("hex");
|
|
30
|
-
case "hexa":
|
|
31
|
-
return this.toRGB().toString("hexa");
|
|
32
|
-
case "hsl":
|
|
33
|
-
return `hsl(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.lightness, 2)}%)`;
|
|
34
|
-
case "css":
|
|
35
|
-
case "hsla":
|
|
36
|
-
return `hsla(${this.hue}, ${utils.toFixedNumber(this.saturation, 2)}%, ${utils.toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
37
|
-
default:
|
|
38
|
-
return this.toFormat(format).toString(format);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
toFormat(format) {
|
|
42
|
-
switch (format) {
|
|
43
|
-
case "hsl":
|
|
44
|
-
case "hsla":
|
|
45
|
-
return this;
|
|
46
|
-
case "hsb":
|
|
47
|
-
case "hsba":
|
|
48
|
-
return this.toHSB();
|
|
49
|
-
case "rgb":
|
|
50
|
-
case "rgba":
|
|
51
|
-
return this.toRGB();
|
|
52
|
-
default:
|
|
53
|
-
throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Converts a HSL color to HSB.
|
|
58
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
59
|
-
* @returns An HSBColor object.
|
|
60
|
-
*/
|
|
61
|
-
toHSB() {
|
|
62
|
-
let saturation = this.saturation / 100;
|
|
63
|
-
let lightness = this.lightness / 100;
|
|
64
|
-
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
65
|
-
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
66
|
-
return new hsbColor.HSBColor(
|
|
67
|
-
utils.toFixedNumber(this.hue, 2),
|
|
68
|
-
utils.toFixedNumber(saturation * 100, 2),
|
|
69
|
-
utils.toFixedNumber(brightness * 100, 2),
|
|
70
|
-
this.alpha
|
|
71
|
-
);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Converts a HSL color to RGB.
|
|
75
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
76
|
-
* @returns An RGBColor object.
|
|
77
|
-
*/
|
|
78
|
-
toRGB() {
|
|
79
|
-
let hue = this.hue;
|
|
80
|
-
let saturation = this.saturation / 100;
|
|
81
|
-
let lightness = this.lightness / 100;
|
|
82
|
-
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
83
|
-
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
84
|
-
return new rgbColor.RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
85
|
-
}
|
|
86
|
-
clone() {
|
|
87
|
-
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
88
|
-
}
|
|
89
|
-
getChannelRange(channel) {
|
|
90
|
-
switch (channel) {
|
|
91
|
-
case "hue":
|
|
92
|
-
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
93
|
-
case "saturation":
|
|
94
|
-
case "lightness":
|
|
95
|
-
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
96
|
-
case "alpha":
|
|
97
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
98
|
-
default:
|
|
99
|
-
throw new Error("Unknown color channel: " + channel);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
getColorSpace() {
|
|
103
|
-
return "hsl";
|
|
104
|
-
}
|
|
105
|
-
static colorChannels = ["hue", "saturation", "lightness"];
|
|
106
|
-
getColorChannels() {
|
|
107
|
-
return HSLColor.colorChannels;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
exports.HSLColor = HSLColor;
|
|
112
|
-
exports.HSL_REGEX = HSL_REGEX;
|
package/dist/hsl-color.mjs
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
import { Color } from './color.mjs';
|
|
2
|
-
import { HSBColor } from './hsb-color.mjs';
|
|
3
|
-
import { RGBColor } from './rgb-color.mjs';
|
|
4
|
-
import { mod, clampValue, toFixedNumber } from './utils.mjs';
|
|
5
|
-
|
|
6
|
-
const HSL_REGEX = /hsl\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%)\)|hsla\(([-+]?\d+(?:.\d+)?\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d+(?:.\d+)?%\s*,\s*[-+]?\d(.\d+)?)\)/;
|
|
7
|
-
class HSLColor extends Color {
|
|
8
|
-
constructor(hue, saturation, lightness, alpha) {
|
|
9
|
-
super();
|
|
10
|
-
this.hue = hue;
|
|
11
|
-
this.saturation = saturation;
|
|
12
|
-
this.lightness = lightness;
|
|
13
|
-
this.alpha = alpha;
|
|
14
|
-
}
|
|
15
|
-
static parse(value) {
|
|
16
|
-
let m;
|
|
17
|
-
if (m = value.match(HSL_REGEX)) {
|
|
18
|
-
const [h, s, l, a] = (m[1] ?? m[2]).split(",").map((n) => Number(n.trim().replace("%", "")));
|
|
19
|
-
return new HSLColor(mod(h, 360), clampValue(s, 0, 100), clampValue(l, 0, 100), clampValue(a ?? 1, 0, 1));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
toString(format) {
|
|
23
|
-
switch (format) {
|
|
24
|
-
case "hex":
|
|
25
|
-
return this.toRGB().toString("hex");
|
|
26
|
-
case "hexa":
|
|
27
|
-
return this.toRGB().toString("hexa");
|
|
28
|
-
case "hsl":
|
|
29
|
-
return `hsl(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%)`;
|
|
30
|
-
case "css":
|
|
31
|
-
case "hsla":
|
|
32
|
-
return `hsla(${this.hue}, ${toFixedNumber(this.saturation, 2)}%, ${toFixedNumber(this.lightness, 2)}%, ${this.alpha})`;
|
|
33
|
-
default:
|
|
34
|
-
return this.toFormat(format).toString(format);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
toFormat(format) {
|
|
38
|
-
switch (format) {
|
|
39
|
-
case "hsl":
|
|
40
|
-
case "hsla":
|
|
41
|
-
return this;
|
|
42
|
-
case "hsb":
|
|
43
|
-
case "hsba":
|
|
44
|
-
return this.toHSB();
|
|
45
|
-
case "rgb":
|
|
46
|
-
case "rgba":
|
|
47
|
-
return this.toRGB();
|
|
48
|
-
default:
|
|
49
|
-
throw new Error("Unsupported color conversion: hsl -> " + format);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Converts a HSL color to HSB.
|
|
54
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_HSV.
|
|
55
|
-
* @returns An HSBColor object.
|
|
56
|
-
*/
|
|
57
|
-
toHSB() {
|
|
58
|
-
let saturation = this.saturation / 100;
|
|
59
|
-
let lightness = this.lightness / 100;
|
|
60
|
-
let brightness = lightness + saturation * Math.min(lightness, 1 - lightness);
|
|
61
|
-
saturation = brightness === 0 ? 0 : 2 * (1 - lightness / brightness);
|
|
62
|
-
return new HSBColor(
|
|
63
|
-
toFixedNumber(this.hue, 2),
|
|
64
|
-
toFixedNumber(saturation * 100, 2),
|
|
65
|
-
toFixedNumber(brightness * 100, 2),
|
|
66
|
-
this.alpha
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Converts a HSL color to RGB.
|
|
71
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative.
|
|
72
|
-
* @returns An RGBColor object.
|
|
73
|
-
*/
|
|
74
|
-
toRGB() {
|
|
75
|
-
let hue = this.hue;
|
|
76
|
-
let saturation = this.saturation / 100;
|
|
77
|
-
let lightness = this.lightness / 100;
|
|
78
|
-
let a = saturation * Math.min(lightness, 1 - lightness);
|
|
79
|
-
let fn = (n, k = (n + hue / 30) % 12) => lightness - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
|
|
80
|
-
return new RGBColor(Math.round(fn(0) * 255), Math.round(fn(8) * 255), Math.round(fn(4) * 255), this.alpha);
|
|
81
|
-
}
|
|
82
|
-
clone() {
|
|
83
|
-
return new HSLColor(this.hue, this.saturation, this.lightness, this.alpha);
|
|
84
|
-
}
|
|
85
|
-
getChannelRange(channel) {
|
|
86
|
-
switch (channel) {
|
|
87
|
-
case "hue":
|
|
88
|
-
return { minValue: 0, maxValue: 360, step: 1, pageSize: 15 };
|
|
89
|
-
case "saturation":
|
|
90
|
-
case "lightness":
|
|
91
|
-
return { minValue: 0, maxValue: 100, step: 1, pageSize: 10 };
|
|
92
|
-
case "alpha":
|
|
93
|
-
return { minValue: 0, maxValue: 1, step: 0.01, pageSize: 0.1 };
|
|
94
|
-
default:
|
|
95
|
-
throw new Error("Unknown color channel: " + channel);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
getColorSpace() {
|
|
99
|
-
return "hsl";
|
|
100
|
-
}
|
|
101
|
-
static colorChannels = ["hue", "saturation", "lightness"];
|
|
102
|
-
getColorChannels() {
|
|
103
|
-
return HSLColor.colorChannels;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export { HSLColor, HSL_REGEX };
|
package/dist/parse-color.d.ts
DELETED
package/dist/parse-color.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
-
|
|
5
|
-
const hsbColor = require('./hsb-color.js');
|
|
6
|
-
const hslColor = require('./hsl-color.js');
|
|
7
|
-
const rgbColor = require('./rgb-color.js');
|
|
8
|
-
|
|
9
|
-
function parseColor(value) {
|
|
10
|
-
let result = rgbColor.RGBColor.parse(value) || hsbColor.HSBColor.parse(value) || hslColor.HSLColor.parse(value);
|
|
11
|
-
if (!result) {
|
|
12
|
-
throw new Error("Invalid color value: " + value);
|
|
13
|
-
}
|
|
14
|
-
return result;
|
|
15
|
-
}
|
|
16
|
-
function normalizeColor(v) {
|
|
17
|
-
if (typeof v === "string") {
|
|
18
|
-
return parseColor(v);
|
|
19
|
-
} else {
|
|
20
|
-
return v;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
exports.normalizeColor = normalizeColor;
|
|
25
|
-
exports.parseColor = parseColor;
|
package/dist/parse-color.mjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { HSBColor } from './hsb-color.mjs';
|
|
2
|
-
import { HSLColor } from './hsl-color.mjs';
|
|
3
|
-
import { RGBColor } from './rgb-color.mjs';
|
|
4
|
-
|
|
5
|
-
function parseColor(value) {
|
|
6
|
-
let result = RGBColor.parse(value) || HSBColor.parse(value) || HSLColor.parse(value);
|
|
7
|
-
if (!result) {
|
|
8
|
-
throw new Error("Invalid color value: " + value);
|
|
9
|
-
}
|
|
10
|
-
return result;
|
|
11
|
-
}
|
|
12
|
-
function normalizeColor(v) {
|
|
13
|
-
if (typeof v === "string") {
|
|
14
|
-
return parseColor(v);
|
|
15
|
-
} else {
|
|
16
|
-
return v;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export { normalizeColor, parseColor };
|
package/dist/rgb-color.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { Color } from "./color";
|
|
2
|
-
import { ColorChannel, ColorChannelRange, ColorFormat, ColorType } from "./types";
|
|
3
|
-
export declare class RGBColor extends Color {
|
|
4
|
-
private red;
|
|
5
|
-
private green;
|
|
6
|
-
private blue;
|
|
7
|
-
private alpha;
|
|
8
|
-
constructor(red: number, green: number, blue: number, alpha: number);
|
|
9
|
-
static parse(value: string): RGBColor | undefined;
|
|
10
|
-
toString(format: ColorFormat | "css"): string;
|
|
11
|
-
toFormat(format: ColorFormat): ColorType;
|
|
12
|
-
toHexInt(): number;
|
|
13
|
-
/**
|
|
14
|
-
* Converts an RGB color value to HSB.
|
|
15
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
16
|
-
* @returns An HSBColor object.
|
|
17
|
-
*/
|
|
18
|
-
private toHSB;
|
|
19
|
-
/**
|
|
20
|
-
* Converts an RGB color value to HSL.
|
|
21
|
-
* Conversion formula adapted from https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB.
|
|
22
|
-
* @returns An HSLColor object.
|
|
23
|
-
*/
|
|
24
|
-
private toHSL;
|
|
25
|
-
clone(): ColorType;
|
|
26
|
-
getChannelRange(channel: ColorChannel): ColorChannelRange;
|
|
27
|
-
getColorSpace(): ColorFormat;
|
|
28
|
-
private static colorChannels;
|
|
29
|
-
getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
|
|
30
|
-
}
|