@soybeanjs/colord 0.0.1

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,26 @@
1
+ import { c as round, t as clamp } from "../utils-BgIyY6bK.js";
2
+ import "../rgb-CfVJB1Bj.js";
3
+ import "../xyz-MII3ndWO.js";
4
+ import { a as rgbToLab, i as parseLabString, o as roundLab, r as parseLab } from "../lab-CJHLdqfe.js";
5
+ import { r as getDeltaE2000 } from "../get-BFaklfVd.js";
6
+
7
+ //#region src/plugins/lab.ts
8
+ /**
9
+ * A plugin adding support for CIELAB color space.
10
+ * https://en.wikipedia.org/wiki/CIELAB_color_space
11
+ */
12
+ const labPlugin = (ColordClass, parsers) => {
13
+ ColordClass.prototype.toLab = function toLab() {
14
+ return roundLab(rgbToLab(this.rgb));
15
+ };
16
+ ColordClass.prototype.delta = function delta(color = "#FFF") {
17
+ const compared = color instanceof ColordClass ? color : new ColordClass(color);
18
+ return clamp(round(getDeltaE2000(this.toLab(), compared.toLab()) / 100, 3));
19
+ };
20
+ parsers.object.push([parseLab, "lab"]);
21
+ parsers.string.push([parseLabString, "lab"]);
22
+ };
23
+ var lab_default = labPlugin;
24
+
25
+ //#endregion
26
+ export { lab_default as default, labPlugin };
@@ -0,0 +1,27 @@
1
+ import { d as LchColor } from "../colord-xpgrVWRV.js";
2
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
3
+
4
+ //#region src/plugins/lch.d.ts
5
+ declare module '../colord' {
6
+ interface Colord {
7
+ /**
8
+ * Converts a color to CIELCH (Lightness-Chroma-Hue) color space and returns an object.
9
+ * https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/
10
+ * https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model
11
+ */
12
+ toLch(): LchColor;
13
+ /**
14
+ * Converts a color to CIELCH (Lightness-Chroma-Hue) color space and returns a string.
15
+ * https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/lch()
16
+ */
17
+ toLchString(): string;
18
+ }
19
+ }
20
+ /**
21
+ * A plugin adding support for CIELCH color space.
22
+ * https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/
23
+ * https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model
24
+ */
25
+ declare const lchPlugin: Plugin;
26
+ //#endregion
27
+ export { lchPlugin as default, lchPlugin };
@@ -0,0 +1,122 @@
1
+ import { _ as ALPHA_PRECISION, c as round, i as isPresent, n as clampHue, o as parseAlpha, s as parseHue, t as clamp } from "../utils-BgIyY6bK.js";
2
+ import "../rgb-CfVJB1Bj.js";
3
+ import "../xyz-MII3ndWO.js";
4
+ import { a as rgbToLab, n as labToRgb } from "../lab-CJHLdqfe.js";
5
+
6
+ //#region src/models/lch.ts
7
+ /**
8
+ * Limits LCH axis values.
9
+ * https://www.w3.org/TR/css-color-4/#specifying-lab-lch
10
+ * https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/#how-does-lch-work
11
+ */
12
+ const clampLch = (lab) => {
13
+ const { l, c, h, alpha } = lab;
14
+ return {
15
+ l: clamp(l, 0, 100),
16
+ c: clamp(c),
17
+ h: clampHue(h),
18
+ alpha: clamp(alpha)
19
+ };
20
+ };
21
+ const roundLch = (lab) => {
22
+ const { l, c, h, alpha } = lab;
23
+ return {
24
+ l: round(l, 3),
25
+ c: round(c, 3),
26
+ h: round(h, 3),
27
+ alpha: round(alpha, ALPHA_PRECISION)
28
+ };
29
+ };
30
+ /**
31
+ * Performs RGB → CIEXYZ → CIELAB → CIELCH color conversion
32
+ * https://www.w3.org/TR/css-color-4/#color-conversion-code
33
+ */
34
+ const rgbToLch = (rgb) => {
35
+ const { l, a, b, alpha } = rgbToLab(rgb);
36
+ const c = Math.sqrt(a * a + b * b);
37
+ let h;
38
+ if (c < 1e-4) h = 0;
39
+ else {
40
+ h = Math.atan2(b, a) * 180 / Math.PI;
41
+ if (h < 0) h += 360;
42
+ }
43
+ return {
44
+ l,
45
+ c,
46
+ h,
47
+ alpha
48
+ };
49
+ };
50
+ /**
51
+ * Performs CIELCH → CIELAB → CIEXYZ → RGB color conversion
52
+ * https://www.w3.org/TR/css-color-4/#color-conversion-code
53
+ */
54
+ const lchaToRgb = (lcha) => {
55
+ const { l, c, h, alpha } = lcha;
56
+ const hRad = h * Math.PI / 180;
57
+ return labToRgb({
58
+ l,
59
+ a: c * Math.cos(hRad),
60
+ b: c * Math.sin(hRad),
61
+ alpha
62
+ });
63
+ };
64
+ const parseLch = ({ l, c, h, alpha = 1 }) => {
65
+ if (!isPresent(l) || !isPresent(c) || !isPresent(h)) return null;
66
+ return lchaToRgb(clampLch({
67
+ l: Number(l),
68
+ c: Number(c),
69
+ h: Number(h),
70
+ alpha: Number(alpha)
71
+ }));
72
+ };
73
+ /**
74
+ * Parsing syntax: lch(L c h [/ alpha])
75
+ * - L: <number|percentage> [0,100]
76
+ * - c: <number> [0,150]
77
+ * - h: <number|angle> [0,360] (deg, rad, grad, turn)
78
+ * - alpha: <number|percentage> [0,1]
79
+ *
80
+ */
81
+ const lchaMatcher = /^lch\(\s*([+-]?[\d.]+)%?\s+([+-]?[\d.]+)\s+([+-]?[\d.]+)(deg|grad|rad|turn)?(?:\s*\/\s*([+-]?[\d.]+%?))?\s*\)$/i;
82
+ /**
83
+ * Parses a valid LCH CSS color function/string
84
+ * https://www.w3.org/TR/css-color-4/#specifying-lab-lch
85
+ */
86
+ const parseLchString = (input) => {
87
+ const match = lchaMatcher.exec(input);
88
+ if (!match) return null;
89
+ const [, l, c, h, unit, alpha] = match;
90
+ return lchaToRgb(clampLch({
91
+ l: Number.parseFloat(l),
92
+ c: Number.parseFloat(c),
93
+ h: parseHue(h, unit),
94
+ alpha: parseAlpha(alpha)
95
+ }));
96
+ };
97
+ const rgbToLchString = (rgb) => {
98
+ const { l, c, h, alpha } = roundLch(rgbToLch(rgb));
99
+ return alpha < 1 ? `lch(${l}% ${c} ${h} / ${alpha})` : `lch(${l}% ${c} ${h})`;
100
+ };
101
+
102
+ //#endregion
103
+ //#region src/plugins/lch.ts
104
+ /**
105
+ * A plugin adding support for CIELCH color space.
106
+ * https://lea.verou.me/2020/04/lch-colors-in-css-what-why-and-how/
107
+ * https://en.wikipedia.org/wiki/CIELAB_color_space#Cylindrical_model
108
+ */
109
+ const lchPlugin = (ColordClass, parsers) => {
110
+ ColordClass.prototype.toLch = function toLch() {
111
+ return roundLch(rgbToLch(this.rgb));
112
+ };
113
+ ColordClass.prototype.toLchString = function toLchString() {
114
+ return rgbToLchString(this.rgb);
115
+ };
116
+ parsers.string.push([parseLchString, "lch"]);
117
+ parsers.object.push([parseLch, "lch"]);
118
+ };
119
+ var lch_default = lchPlugin;
120
+
121
+ //#endregion
122
+ export { lch_default as default, lchPlugin };
@@ -0,0 +1,23 @@
1
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
2
+
3
+ //#region src/plugins/minify.d.ts
4
+ interface MinificationOptions {
5
+ hex?: boolean;
6
+ alphaHex?: boolean;
7
+ rgb?: boolean;
8
+ hsl?: boolean;
9
+ name?: boolean;
10
+ transparent?: boolean;
11
+ }
12
+ declare module '../colord' {
13
+ interface Colord {
14
+ /** Returns the shortest string representation of the color */
15
+ minify(options?: MinificationOptions): string;
16
+ }
17
+ }
18
+ /**
19
+ * A plugin adding a color minification utilities.
20
+ */
21
+ declare const minifyPlugin: Plugin;
22
+ //#endregion
23
+ export { minifyPlugin as default, minifyPlugin };
@@ -0,0 +1,61 @@
1
+ import { c as round } from "../utils-BgIyY6bK.js";
2
+
3
+ //#region src/plugins/minify.ts
4
+ /**
5
+ * A plugin adding a color minification utilities.
6
+ */
7
+ const minifyPlugin = (ColordClass) => {
8
+ const minifyHex = (instance) => {
9
+ const hex = instance.toHex();
10
+ const alpha = instance.alpha();
11
+ const [, r1, r2, g1, g2, b1, b2, a1, a2] = hex.split("");
12
+ if (alpha > 0 && alpha < 1 && round(Number.parseInt(a1 + a2, 16) / 255, 2) !== alpha) return null;
13
+ if (r1 === r2 && g1 === g2 && b1 === b2) {
14
+ if (alpha === 1) return `#${r1}${g1}${b1}`;
15
+ else if (a1 === a2) return `#${r1}${g1}${b1}${a1}`;
16
+ }
17
+ return hex;
18
+ };
19
+ const findShortestString = (variants) => {
20
+ let shortest = variants[0];
21
+ for (let index = 1; index < variants.length; index += 1) if (variants[index].length < shortest.length) shortest = variants[index];
22
+ return shortest;
23
+ };
24
+ const shortenNumber = (number) => {
25
+ if (number > 0 && number < 1) return number.toString().replace("0.", ".");
26
+ return number;
27
+ };
28
+ ColordClass.prototype.minify = function minify(options = {}) {
29
+ const rgb = this.toRgb();
30
+ const r = shortenNumber(rgb.r);
31
+ const g = shortenNumber(rgb.g);
32
+ const b = shortenNumber(rgb.b);
33
+ const hsl = this.toHsl();
34
+ const h = shortenNumber(hsl.h);
35
+ const s = shortenNumber(hsl.s);
36
+ const l = shortenNumber(hsl.l);
37
+ const a = shortenNumber(this.alpha());
38
+ const settings = Object.assign({
39
+ hex: true,
40
+ rgb: true,
41
+ hsl: true
42
+ }, options);
43
+ const variants = [];
44
+ if (settings.hex && (a === 1 || settings.alphaHex)) {
45
+ const hex = minifyHex(this);
46
+ if (hex) variants.push(hex);
47
+ }
48
+ if (settings.rgb) variants.push(a === 1 ? `rgb(${r},${g},${b})` : `rgb(${r},${g},${b},${a})`);
49
+ if (settings.hsl) variants.push(a === 1 ? `hsl(${h},${s}%,${l}%)` : `hsl(${h},${s}%,${l}%,${a})`);
50
+ if (settings.transparent && r === 0 && g === 0 && b === 0 && a === 0) variants.push("transparent");
51
+ else if (a === 1 && settings.name && typeof this.toName === "function") {
52
+ const name = this.toName();
53
+ if (name) variants.push(name);
54
+ }
55
+ return findShortestString(variants);
56
+ };
57
+ };
58
+ var minify_default = minifyPlugin;
59
+
60
+ //#endregion
61
+ export { minify_default as default, minifyPlugin };
@@ -0,0 +1,30 @@
1
+ import { r as AnyColor } from "../colord-xpgrVWRV.js";
2
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
3
+
4
+ //#region src/plugins/mix.d.ts
5
+ declare module '../colord' {
6
+ interface Colord {
7
+ /**
8
+ * Produces a mixture of two colors through CIE LAB color space and returns a new Colord instance.
9
+ */
10
+ mix(color2: AnyColor | Colord, ratio?: number): Colord;
11
+ /**
12
+ * Generates a tints palette based on original color.
13
+ */
14
+ tints(count?: number): Colord[];
15
+ /**
16
+ * Generates a shades palette based on original color.
17
+ */
18
+ shades(count?: number): Colord[];
19
+ /**
20
+ * Generates a tones palette based on original color.
21
+ */
22
+ tones(count?: number): Colord[];
23
+ }
24
+ }
25
+ /**
26
+ * A plugin adding a color mixing utilities.
27
+ */
28
+ declare const mixPlugin: Plugin;
29
+ //#endregion
30
+ export { mixPlugin as default, mixPlugin };
@@ -0,0 +1,39 @@
1
+ import "../utils-BgIyY6bK.js";
2
+ import "../rgb-CfVJB1Bj.js";
3
+ import "../hsv-UWMaaOSN.js";
4
+ import { i as mix } from "../manipulate-CeAgrUd6.js";
5
+ import "../xyz-MII3ndWO.js";
6
+ import "../lab-CJHLdqfe.js";
7
+
8
+ //#region src/plugins/mix.ts
9
+ /**
10
+ * A plugin adding a color mixing utilities.
11
+ */
12
+ const mixPlugin = (ColordClass) => {
13
+ ColordClass.prototype.mix = function mix$1(color2, ratio = .5) {
14
+ const instance2 = color2 instanceof ColordClass ? color2 : new ColordClass(color2);
15
+ return new ColordClass(mix(this.toRgb(), instance2.toRgb(), ratio));
16
+ };
17
+ /**
18
+ * Generate a palette from mixing a source color with another.
19
+ */
20
+ function mixPalette(source, hex, count = 5) {
21
+ const palette = [];
22
+ const step = 1 / (count - 1);
23
+ for (let i = 0; i <= count - 1; i += 1) palette.push(source.mix(hex, step * i));
24
+ return palette;
25
+ }
26
+ ColordClass.prototype.tints = function tints(count) {
27
+ return mixPalette(this, "#fff", count);
28
+ };
29
+ ColordClass.prototype.shades = function shades(count) {
30
+ return mixPalette(this, "#000", count);
31
+ };
32
+ ColordClass.prototype.tones = function tones(count) {
33
+ return mixPalette(this, "#808080", count);
34
+ };
35
+ };
36
+ var mix_default = mixPlugin;
37
+
38
+ //#endregion
39
+ export { mix_default as default, mixPlugin };
@@ -0,0 +1,22 @@
1
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
2
+
3
+ //#region src/plugins/names.d.ts
4
+ interface ConvertOptions {
5
+ closest?: boolean;
6
+ }
7
+ declare module '../colord' {
8
+ interface Colord {
9
+ /** Finds CSS color keyword that matches with the color value */
10
+ toName(options?: ConvertOptions): string | undefined;
11
+ }
12
+ }
13
+ /**
14
+ * Plugin to work with named colors.
15
+ * Adds a parser to read CSS color names and `toName` method.
16
+ * See https://www.w3.org/TR/css-color-4/#named-colors
17
+ * Supports 'transparent' string as defined in
18
+ * https://drafts.csswg.org/css-color/#transparent-color
19
+ */
20
+ declare const namesPlugin: Plugin;
21
+ //#endregion
22
+ export { namesPlugin as default, namesPlugin };
@@ -0,0 +1,201 @@
1
+ //#region src/plugins/names.ts
2
+ /**
3
+ * Plugin to work with named colors.
4
+ * Adds a parser to read CSS color names and `toName` method.
5
+ * See https://www.w3.org/TR/css-color-4/#named-colors
6
+ * Supports 'transparent' string as defined in
7
+ * https://drafts.csswg.org/css-color/#transparent-color
8
+ */
9
+ const namesPlugin = (ColordClass, parsers) => {
10
+ const NAME_HEX_STORE = {
11
+ white: "#ffffff",
12
+ bisque: "#ffe4c4",
13
+ blue: "#0000ff",
14
+ cadetblue: "#5f9ea0",
15
+ chartreuse: "#7fff00",
16
+ chocolate: "#d2691e",
17
+ coral: "#ff7f50",
18
+ antiquewhite: "#faebd7",
19
+ aqua: "#00ffff",
20
+ azure: "#f0ffff",
21
+ whitesmoke: "#f5f5f5",
22
+ papayawhip: "#ffefd5",
23
+ plum: "#dda0dd",
24
+ blanchedalmond: "#ffebcd",
25
+ black: "#000000",
26
+ gold: "#ffd700",
27
+ goldenrod: "#daa520",
28
+ gainsboro: "#dcdcdc",
29
+ cornsilk: "#fff8dc",
30
+ cornflowerblue: "#6495ed",
31
+ burlywood: "#deb887",
32
+ aquamarine: "#7fffd4",
33
+ beige: "#f5f5dc",
34
+ crimson: "#dc143c",
35
+ cyan: "#00ffff",
36
+ darkblue: "#00008b",
37
+ darkcyan: "#008b8b",
38
+ darkgoldenrod: "#b8860b",
39
+ darkkhaki: "#bdb76b",
40
+ darkgray: "#a9a9a9",
41
+ darkgreen: "#006400",
42
+ darkgrey: "#a9a9a9",
43
+ peachpuff: "#ffdab9",
44
+ darkmagenta: "#8b008b",
45
+ darkred: "#8b0000",
46
+ darkorchid: "#9932cc",
47
+ darkorange: "#ff8c00",
48
+ darkslateblue: "#483d8b",
49
+ gray: "#808080",
50
+ darkslategray: "#2f4f4f",
51
+ darkslategrey: "#2f4f4f",
52
+ deeppink: "#ff1493",
53
+ deepskyblue: "#00bfff",
54
+ wheat: "#f5deb3",
55
+ firebrick: "#b22222",
56
+ floralwhite: "#fffaf0",
57
+ ghostwhite: "#f8f8ff",
58
+ darkviolet: "#9400d3",
59
+ magenta: "#ff00ff",
60
+ green: "#008000",
61
+ dodgerblue: "#1e90ff",
62
+ grey: "#808080",
63
+ honeydew: "#f0fff0",
64
+ hotpink: "#ff69b4",
65
+ blueviolet: "#8a2be2",
66
+ forestgreen: "#228b22",
67
+ lawngreen: "#7cfc00",
68
+ indianred: "#cd5c5c",
69
+ indigo: "#4b0082",
70
+ fuchsia: "#ff00ff",
71
+ brown: "#a52a2a",
72
+ maroon: "#800000",
73
+ mediumblue: "#0000cd",
74
+ lightcoral: "#f08080",
75
+ darkturquoise: "#00ced1",
76
+ lightcyan: "#e0ffff",
77
+ ivory: "#fffff0",
78
+ lightyellow: "#ffffe0",
79
+ lightsalmon: "#ffa07a",
80
+ lightseagreen: "#20b2aa",
81
+ linen: "#faf0e6",
82
+ mediumaquamarine: "#66cdaa",
83
+ lemonchiffon: "#fffacd",
84
+ lime: "#00ff00",
85
+ khaki: "#f0e68c",
86
+ mediumseagreen: "#3cb371",
87
+ limegreen: "#32cd32",
88
+ mediumspringgreen: "#00fa9a",
89
+ lightskyblue: "#87cefa",
90
+ lightblue: "#add8e6",
91
+ midnightblue: "#191970",
92
+ lightpink: "#ffb6c1",
93
+ mistyrose: "#ffe4e1",
94
+ moccasin: "#ffe4b5",
95
+ mintcream: "#f5fffa",
96
+ lightslategray: "#778899",
97
+ lightslategrey: "#778899",
98
+ navajowhite: "#ffdead",
99
+ navy: "#000080",
100
+ mediumvioletred: "#c71585",
101
+ powderblue: "#b0e0e6",
102
+ palegoldenrod: "#eee8aa",
103
+ oldlace: "#fdf5e6",
104
+ paleturquoise: "#afeeee",
105
+ mediumturquoise: "#48d1cc",
106
+ mediumorchid: "#ba55d3",
107
+ rebeccapurple: "#663399",
108
+ lightsteelblue: "#b0c4de",
109
+ mediumslateblue: "#7b68ee",
110
+ thistle: "#d8bfd8",
111
+ tan: "#d2b48c",
112
+ orchid: "#da70d6",
113
+ mediumpurple: "#9370db",
114
+ purple: "#800080",
115
+ pink: "#ffc0cb",
116
+ skyblue: "#87ceeb",
117
+ springgreen: "#00ff7f",
118
+ palegreen: "#98fb98",
119
+ red: "#ff0000",
120
+ yellow: "#ffff00",
121
+ slateblue: "#6a5acd",
122
+ lavenderblush: "#fff0f5",
123
+ peru: "#cd853f",
124
+ palevioletred: "#db7093",
125
+ violet: "#ee82ee",
126
+ teal: "#008080",
127
+ slategray: "#708090",
128
+ slategrey: "#708090",
129
+ aliceblue: "#f0f8ff",
130
+ darkseagreen: "#8fbc8f",
131
+ darkolivegreen: "#556b2f",
132
+ greenyellow: "#adff2f",
133
+ seagreen: "#2e8b57",
134
+ seashell: "#fff5ee",
135
+ tomato: "#ff6347",
136
+ silver: "#c0c0c0",
137
+ sienna: "#a0522d",
138
+ lavender: "#e6e6fa",
139
+ lightgreen: "#90ee90",
140
+ orange: "#ffa500",
141
+ orangered: "#ff4500",
142
+ steelblue: "#4682b4",
143
+ royalblue: "#4169e1",
144
+ turquoise: "#40e0d0",
145
+ yellowgreen: "#9acd32",
146
+ salmon: "#fa8072",
147
+ saddlebrown: "#8b4513",
148
+ sandybrown: "#f4a460",
149
+ rosybrown: "#bc8f8f",
150
+ darksalmon: "#e9967a",
151
+ lightgoldenrodyellow: "#fafad2",
152
+ snow: "#fffafa",
153
+ lightgrey: "#d3d3d3",
154
+ lightgray: "#d3d3d3",
155
+ dimgray: "#696969",
156
+ dimgrey: "#696969",
157
+ olivedrab: "#6b8e23",
158
+ olive: "#808000"
159
+ };
160
+ const nameHexEntries = Object.entries(NAME_HEX_STORE);
161
+ const HEX_NAME_STORE = {};
162
+ nameHexEntries.forEach(([name, hex]) => {
163
+ HEX_NAME_STORE[hex] = name;
164
+ });
165
+ const NAME_RGBA_STORE = {};
166
+ const getDistanceBetween = (rgb1, rgb2) => {
167
+ return (rgb1.r - rgb2.r) ** 2 + (rgb1.g - rgb2.g) ** 2 + (rgb1.b - rgb2.b) ** 2;
168
+ };
169
+ ColordClass.prototype.toName = function toName(options) {
170
+ if (!this.rgb.alpha && !this.rgb.r && !this.rgb.g && !this.rgb.b) return "transparent";
171
+ const exactMatch = HEX_NAME_STORE[this.toHex()];
172
+ if (exactMatch) return exactMatch;
173
+ if (options?.closest) {
174
+ const rgb = this.toRgb();
175
+ let minDistance = Infinity;
176
+ let closestMatch = "black";
177
+ if (!NAME_RGBA_STORE.length) nameHexEntries.forEach(([name, hex]) => {
178
+ NAME_RGBA_STORE[name] = new ColordClass(hex).toRgb();
179
+ });
180
+ nameHexEntries.forEach(([name, hex]) => {
181
+ const distance = getDistanceBetween(rgb, new ColordClass(hex).toRgb());
182
+ if (distance < minDistance) {
183
+ minDistance = distance;
184
+ closestMatch = name;
185
+ }
186
+ });
187
+ return closestMatch;
188
+ }
189
+ };
190
+ const parseColorName = (input) => {
191
+ const name = input.toLowerCase();
192
+ const hex = name === "transparent" ? "#0000" : NAME_HEX_STORE[name];
193
+ if (hex) return new ColordClass(hex).toRgb();
194
+ return null;
195
+ };
196
+ parsers.string.push([parseColorName, "name"]);
197
+ };
198
+ var names_default = namesPlugin;
199
+
200
+ //#endregion
201
+ export { names_default as default, namesPlugin };
@@ -0,0 +1,16 @@
1
+ import { f as OklabColor } from "../colord-xpgrVWRV.js";
2
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
3
+
4
+ //#region src/plugins/oklab.d.ts
5
+ declare module '../colord' {
6
+ interface Colord {
7
+ toOklab(): OklabColor;
8
+ }
9
+ }
10
+ /**
11
+ * A plugin adding support for OKLAB colorspace.
12
+ * https://bottosson.github.io/posts/oklab/
13
+ */
14
+ declare const oklabPlugin: Plugin;
15
+ //#endregion
16
+ export { oklabPlugin as default, oklabPlugin };
@@ -0,0 +1,103 @@
1
+ import { _ as ALPHA_PRECISION, a as mul3x3, c as round, g as OKLAB_M2_INV, h as OKLAB_M2, i as isPresent, m as OKLAB_M1_INV, o as parseAlpha, p as OKLAB_M1, t as clamp } from "../utils-BgIyY6bK.js";
2
+ import { n as clampRgb, o as rgbToLinearRgb, r as linearRgbToRgb, t as clampLinearRgb } from "../rgb-CfVJB1Bj.js";
3
+
4
+ //#region src/models/oklab.ts
5
+ const clampOklab = (oklab) => {
6
+ const { l, a, b, alpha } = oklab;
7
+ return {
8
+ l: clamp(l, 1e-4, 1),
9
+ a: clamp(a, -.4, .4),
10
+ b: clamp(b, -.4, .4),
11
+ alpha: clamp(alpha)
12
+ };
13
+ };
14
+ const roundOklab = (oklab) => {
15
+ const { l, a, b, alpha } = oklab;
16
+ return {
17
+ l: round(l, 3),
18
+ a: round(a, 3),
19
+ b: round(b, 3),
20
+ alpha: round(alpha, ALPHA_PRECISION)
21
+ };
22
+ };
23
+ const oklabToRgb = (oklab) => {
24
+ const [r, g, b] = mul3x3(OKLAB_M1_INV, mul3x3(OKLAB_M2_INV, [
25
+ oklab.l,
26
+ oklab.a,
27
+ oklab.b
28
+ ]).map((v) => v * v * v));
29
+ return clampRgb(linearRgbToRgb(clampLinearRgb({
30
+ r,
31
+ g,
32
+ b,
33
+ alpha: oklab.alpha
34
+ })));
35
+ };
36
+ const rgbToOklab = (rgb) => {
37
+ const lRgb = rgbToLinearRgb(rgb);
38
+ const [l, a, b] = mul3x3(OKLAB_M2, mul3x3(OKLAB_M1, [
39
+ lRgb.r,
40
+ lRgb.g,
41
+ lRgb.b
42
+ ]).map((v) => Math.cbrt(v)));
43
+ return clampOklab({
44
+ l,
45
+ a,
46
+ b,
47
+ alpha: rgb.alpha
48
+ });
49
+ };
50
+ const parseOklab = ({ l, a, b, alpha = 1 }) => {
51
+ if (!isPresent(l) || !isPresent(a) || !isPresent(b)) return null;
52
+ return oklabToRgb(clampOklab({
53
+ l: Number(l),
54
+ a: Number(a),
55
+ b: Number(b),
56
+ alpha: Number(alpha)
57
+ }));
58
+ };
59
+ /**
60
+ * Parsing syntax: oklab(L a b [/ alpha])
61
+ * - L: <number|percentage>
62
+ * - a: <number>
63
+ * - b: <number>
64
+ * - alpha: <number|percentage>
65
+ */
66
+ const oklabMatcher = /^oklab\(\s*([+-]?[\d.]+)%?\s+([+-]?[\d.]+)\s+([+-]?[\d.]+)(?:\s*\/\s*([+-]?[\d.]+%?))?\s*\)$/i;
67
+ /**
68
+ * Parses a valid OKLAB CSS color function/string
69
+ * https://www.w3.org/TR/css-color-4/#specifying-oklab
70
+ * @param input
71
+ * @returns
72
+ */
73
+ const parseOklabString = (input) => {
74
+ const match = oklabMatcher.exec(input);
75
+ if (!match) return null;
76
+ const [_, L, a, b, alpha] = match;
77
+ let l = Number.parseFloat(L);
78
+ if (L.endsWith("%")) l /= 100;
79
+ return oklabToRgb(clampOklab({
80
+ l,
81
+ a: Number.parseFloat(a),
82
+ b: Number.parseFloat(b),
83
+ alpha: parseAlpha(alpha)
84
+ }));
85
+ };
86
+
87
+ //#endregion
88
+ //#region src/plugins/oklab.ts
89
+ /**
90
+ * A plugin adding support for OKLAB colorspace.
91
+ * https://bottosson.github.io/posts/oklab/
92
+ */
93
+ const oklabPlugin = (ColordClass, parsers) => {
94
+ ColordClass.prototype.toOklab = function toOklab() {
95
+ return roundOklab(rgbToOklab(this.rgb));
96
+ };
97
+ parsers.string.push([parseOklabString, "oklab"]);
98
+ parsers.object.push([parseOklab, "oklab"]);
99
+ };
100
+ var oklab_default = oklabPlugin;
101
+
102
+ //#endregion
103
+ export { oklab_default as default, oklabPlugin };
@@ -0,0 +1,16 @@
1
+ import { p as OklchColor } from "../colord-xpgrVWRV.js";
2
+ import { t as Plugin } from "../extend-DrPfn2Q1.js";
3
+
4
+ //#region src/plugins/oklch.d.ts
5
+ declare module '../colord' {
6
+ interface Colord {
7
+ toOklch(): OklchColor;
8
+ }
9
+ }
10
+ /**
11
+ * A plugin adding support for OKLCH colorspace.
12
+ * https://bottosson.github.io/posts/oklab/
13
+ */
14
+ declare const oklchPlugin: Plugin;
15
+ //#endregion
16
+ export { oklchPlugin as default, oklchPlugin };