react-smart-copy 1.17.2 → 1.18.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/README.md +9 -3
- package/dist/capture.cjs +30 -153
- package/dist/capture.cjs.map +1 -1
- package/dist/capture.d.cts +2 -25
- package/dist/capture.d.ts +2 -25
- package/dist/capture.js +5 -128
- package/dist/capture.js.map +1 -1
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs +102 -0
- package/dist/chunks/clipboard-adapter-Cfz-rzw3.cjs.map +1 -0
- package/dist/chunks/clipboard-adapter-CtH4VELE.d.cts +43 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js +97 -0
- package/dist/chunks/clipboard-adapter-DEXkXxwV.js.map +1 -0
- package/dist/chunks/clipboard-adapter-Ds0irroN.d.ts +43 -0
- package/dist/chunks/contrast-C6CVklEI.cjs +842 -0
- package/dist/chunks/contrast-C6CVklEI.cjs.map +1 -0
- package/dist/chunks/contrast-DpK2JIVu.js +789 -0
- package/dist/chunks/contrast-DpK2JIVu.js.map +1 -0
- package/dist/chunks/copy-interceptor-C6NABy1l.d.ts +141 -0
- package/dist/chunks/copy-interceptor-CZWzGOkL.d.cts +141 -0
- package/dist/chunks/core-Cp2HSpaz.js +1035 -0
- package/dist/chunks/core-Cp2HSpaz.js.map +1 -0
- package/dist/chunks/core-DYf_Lhbe.cjs +1142 -0
- package/dist/chunks/core-DYf_Lhbe.cjs.map +1 -0
- package/dist/chunks/deadline-C29AlS_Q.js +46 -0
- package/dist/chunks/deadline-C29AlS_Q.js.map +1 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs +51 -0
- package/dist/chunks/deadline-DkiB8ZHE.cjs.map +1 -0
- package/dist/chunks/errors-9h8sdFFX.js +120 -0
- package/dist/chunks/errors-9h8sdFFX.js.map +1 -0
- package/dist/chunks/errors-BwOF6T_M.cjs +167 -0
- package/dist/chunks/errors-BwOF6T_M.cjs.map +1 -0
- package/dist/chunks/index-mr7sSAgP.d.ts +426 -0
- package/dist/chunks/index-vz0iW5V2.d.cts +426 -0
- package/dist/chunks/mime--LB7kdqG.cjs +153 -0
- package/dist/chunks/mime--LB7kdqG.cjs.map +1 -0
- package/dist/chunks/mime-aa-HA2dm.js +100 -0
- package/dist/chunks/mime-aa-HA2dm.js.map +1 -0
- package/dist/chunks/paste-adapter--SPL4PRE.d.cts +35 -0
- package/dist/chunks/paste-adapter-uahXnTYk.d.ts +35 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs +289 -0
- package/dist/chunks/paste-reader-Bo01IMC_.cjs.map +1 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js +224 -0
- package/dist/chunks/paste-reader-D9i7yMXF.js.map +1 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.cts +96 -0
- package/dist/chunks/paste-reader-DcXnRWQr.d.ts +96 -0
- package/dist/chunks/payload-CUymxwim.cjs +100 -0
- package/dist/chunks/payload-CUymxwim.cjs.map +1 -0
- package/dist/chunks/payload-CcwKJ_bU.d.cts +162 -0
- package/dist/chunks/payload-Cl_8hCI5.d.ts +162 -0
- package/dist/chunks/payload-DGw-JOo7.js +71 -0
- package/dist/chunks/payload-DGw-JOo7.js.map +1 -0
- package/dist/chunks/payload-DI5AorDz.js +103 -0
- package/dist/chunks/payload-DI5AorDz.js.map +1 -0
- package/dist/chunks/payload-J6GjHuh3.cjs +132 -0
- package/dist/chunks/payload-J6GjHuh3.cjs.map +1 -0
- package/dist/chunks/share-Bop47Znn.js +208 -0
- package/dist/chunks/share-Bop47Znn.js.map +1 -0
- package/dist/chunks/share-C0esXvRK.d.cts +115 -0
- package/dist/chunks/share-ayDz974N.cjs +231 -0
- package/dist/chunks/share-ayDz974N.cjs.map +1 -0
- package/dist/chunks/share-e7FenIr2.d.ts +115 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs +77 -0
- package/dist/chunks/text-field-Ba8W3v4R.cjs.map +1 -0
- package/dist/chunks/text-field-C0SGmvvR.d.cts +18 -0
- package/dist/chunks/text-field-C0SGmvvR.d.ts +18 -0
- package/dist/chunks/text-field-DUvrMMaK.js +66 -0
- package/dist/chunks/text-field-DUvrMMaK.js.map +1 -0
- package/dist/chunks/url-BcMYVCXQ.js +135 -0
- package/dist/chunks/url-BcMYVCXQ.js.map +1 -0
- package/dist/chunks/url-D4PGsRm8.cjs +164 -0
- package/dist/chunks/url-D4PGsRm8.cjs.map +1 -0
- package/dist/chunks/url-DWdElCT3.d.cts +43 -0
- package/dist/chunks/url-DWdElCT3.d.ts +43 -0
- package/dist/code.cjs +20 -112
- package/dist/code.cjs.map +1 -1
- package/dist/code.d.cts +4 -28
- package/dist/code.d.ts +4 -28
- package/dist/code.js +3 -95
- package/dist/code.js.map +1 -1
- package/dist/color.cjs +21 -893
- package/dist/color.cjs.map +1 -1
- package/dist/color.d.cts +4 -28
- package/dist/color.d.ts +4 -28
- package/dist/color.js +4 -876
- package/dist/color.js.map +1 -1
- package/dist/core.cjs +52 -1659
- package/dist/core.d.cts +8 -878
- package/dist/core.d.ts +8 -878
- package/dist/core.js +7 -1614
- package/dist/image.cjs +23 -94
- package/dist/image.cjs.map +1 -1
- package/dist/image.d.cts +1 -0
- package/dist/image.d.ts +1 -0
- package/dist/image.js +3 -74
- package/dist/image.js.map +1 -1
- package/dist/index.cjs +74 -2072
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -978
- package/dist/index.d.ts +8 -978
- package/dist/index.js +7 -2005
- package/dist/index.js.map +1 -1
- package/dist/link.cjs +24 -338
- package/dist/link.cjs.map +1 -1
- package/dist/link.d.cts +5 -83
- package/dist/link.d.ts +5 -83
- package/dist/link.js +5 -319
- package/dist/link.js.map +1 -1
- package/dist/markdown.cjs +8 -97
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +3 -16
- package/dist/markdown.d.ts +3 -16
- package/dist/markdown.js +3 -92
- package/dist/markdown.js.map +1 -1
- package/dist/rich.cjs +26 -934
- package/dist/rich.cjs.map +1 -1
- package/dist/rich.d.cts +4 -90
- package/dist/rich.d.ts +4 -90
- package/dist/rich.js +4 -912
- package/dist/rich.js.map +1 -1
- package/dist/share.cjs +5 -640
- package/dist/share.d.cts +3 -248
- package/dist/share.d.ts +3 -248
- package/dist/share.js +1 -637
- package/dist/structured.cjs +13 -95
- package/dist/structured.cjs.map +1 -1
- package/dist/structured.d.cts +2 -11
- package/dist/structured.d.ts +2 -11
- package/dist/structured.js +4 -86
- package/dist/structured.js.map +1 -1
- package/dist/table.cjs +11 -39
- package/dist/table.cjs.map +1 -1
- package/dist/table.d.cts +2 -12
- package/dist/table.d.ts +2 -12
- package/dist/table.js +2 -30
- package/dist/table.js.map +1 -1
- package/dist/testing.cjs +10 -43
- package/dist/testing.cjs.map +1 -1
- package/dist/testing.d.cts +6 -181
- package/dist/testing.d.ts +6 -181
- package/dist/testing.js +3 -36
- package/dist/testing.js.map +1 -1
- package/package.json +20 -2
- package/dist/core.cjs.map +0 -1
- package/dist/core.js.map +0 -1
- package/dist/share.cjs.map +0 -1
- package/dist/share.js.map +0 -1
package/dist/color.js
CHANGED
|
@@ -1,879 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
b,
|
|
6
|
-
alpha
|
|
7
|
-
});
|
|
8
|
-
const mul = (m, [x, y, z]) => [
|
|
9
|
-
m[0][0] * x + m[0][1] * y + m[0][2] * z,
|
|
10
|
-
m[1][0] * x + m[1][1] * y + m[1][2] * z,
|
|
11
|
-
m[2][0] * x + m[2][1] * y + m[2][2] * z
|
|
12
|
-
];
|
|
13
|
-
/** The sRGB (and Display P3) transfer function, extended to negative values by symmetry. */
|
|
14
|
-
const toLinear = (c) => {
|
|
15
|
-
const abs = Math.abs(c);
|
|
16
|
-
return abs <= .04045 ? c / 12.92 : Math.sign(c) * ((abs + .055) / 1.055) ** 2.4;
|
|
17
|
-
};
|
|
18
|
-
const fromLinear = (c) => {
|
|
19
|
-
const abs = Math.abs(c);
|
|
20
|
-
return abs > .0031308 ? Math.sign(c) * (1.055 * abs ** (1 / 2.4) - .055) : 12.92 * c;
|
|
21
|
-
};
|
|
22
|
-
/** Applies `f` to each component; the result stays a three-component vector. */
|
|
23
|
-
const map3 = ([x, y, z], f) => [
|
|
24
|
-
f(x, 0),
|
|
25
|
-
f(y, 1),
|
|
26
|
-
f(z, 2)
|
|
27
|
-
];
|
|
28
|
-
/** Three numbers as a vector, or null when one is missing. */
|
|
29
|
-
const vec3 = ([x, y, z]) => typeof x === "number" && typeof y === "number" && typeof z === "number" ? [
|
|
30
|
-
x,
|
|
31
|
-
y,
|
|
32
|
-
z
|
|
33
|
-
] : null;
|
|
34
|
-
const linearRgb = (c) => [
|
|
35
|
-
toLinear(c.r),
|
|
36
|
-
toLinear(c.g),
|
|
37
|
-
toLinear(c.b)
|
|
38
|
-
];
|
|
39
|
-
const fromLinearRgb = ([r, g, b], alpha) => color(fromLinear(r), fromLinear(g), fromLinear(b), alpha);
|
|
40
|
-
const SRGB_TO_XYZ = [
|
|
41
|
-
[
|
|
42
|
-
.41239079926595934,
|
|
43
|
-
.357584339383878,
|
|
44
|
-
.1804807884018343
|
|
45
|
-
],
|
|
46
|
-
[
|
|
47
|
-
.21263900587151027,
|
|
48
|
-
.715168678767756,
|
|
49
|
-
.07219231536073371
|
|
50
|
-
],
|
|
51
|
-
[
|
|
52
|
-
.01933081871559182,
|
|
53
|
-
.11919477979462598,
|
|
54
|
-
.9505321522496607
|
|
55
|
-
]
|
|
56
|
-
];
|
|
57
|
-
const XYZ_TO_SRGB = [
|
|
58
|
-
[
|
|
59
|
-
3.2409699419045226,
|
|
60
|
-
-1.537383177570094,
|
|
61
|
-
-.4986107602930034
|
|
62
|
-
],
|
|
63
|
-
[
|
|
64
|
-
-.9692436362808796,
|
|
65
|
-
1.8759675015077202,
|
|
66
|
-
.04155505740717559
|
|
67
|
-
],
|
|
68
|
-
[
|
|
69
|
-
.05563007969699366,
|
|
70
|
-
-.20397695888897652,
|
|
71
|
-
1.0569715142428786
|
|
72
|
-
]
|
|
73
|
-
];
|
|
74
|
-
const P3_TO_XYZ = [
|
|
75
|
-
[
|
|
76
|
-
.4865709486482162,
|
|
77
|
-
.26566769316909306,
|
|
78
|
-
.1982172852343625
|
|
79
|
-
],
|
|
80
|
-
[
|
|
81
|
-
.2289745640697488,
|
|
82
|
-
.6917385218365064,
|
|
83
|
-
.079286914093745
|
|
84
|
-
],
|
|
85
|
-
[
|
|
86
|
-
0,
|
|
87
|
-
.04511338185890264,
|
|
88
|
-
1.043944368900976
|
|
89
|
-
]
|
|
90
|
-
];
|
|
91
|
-
const XYZ_TO_P3 = [
|
|
92
|
-
[
|
|
93
|
-
2.493496911941425,
|
|
94
|
-
-.9313836179191239,
|
|
95
|
-
-.40271078445071684
|
|
96
|
-
],
|
|
97
|
-
[
|
|
98
|
-
-.8294889695615747,
|
|
99
|
-
1.7626640603183463,
|
|
100
|
-
.023624685841943577
|
|
101
|
-
],
|
|
102
|
-
[
|
|
103
|
-
.03584583024378447,
|
|
104
|
-
-.07617238926804182,
|
|
105
|
-
.9568845240076872
|
|
106
|
-
]
|
|
107
|
-
];
|
|
108
|
-
/** Bradford chromatic adaptation between the D65 and D50 white points (Lab and LCH use D50). */
|
|
109
|
-
const D65_TO_D50 = [
|
|
110
|
-
[
|
|
111
|
-
1.0479297925449969,
|
|
112
|
-
.022946870601609652,
|
|
113
|
-
-.05019226628920524
|
|
114
|
-
],
|
|
115
|
-
[
|
|
116
|
-
.02962780877005599,
|
|
117
|
-
.9904344267538799,
|
|
118
|
-
-.017073799063418826
|
|
119
|
-
],
|
|
120
|
-
[
|
|
121
|
-
-.009243040646204504,
|
|
122
|
-
.015055191490298152,
|
|
123
|
-
.7518742814281371
|
|
124
|
-
]
|
|
125
|
-
];
|
|
126
|
-
const D50_TO_D65 = [
|
|
127
|
-
[
|
|
128
|
-
.955473421488075,
|
|
129
|
-
-.02309845494876471,
|
|
130
|
-
.06325924320057072
|
|
131
|
-
],
|
|
132
|
-
[
|
|
133
|
-
-.0283697093338637,
|
|
134
|
-
1.0099953980813041,
|
|
135
|
-
.021041441191917323
|
|
136
|
-
],
|
|
137
|
-
[
|
|
138
|
-
.012314014864481998,
|
|
139
|
-
-.020507649298898964,
|
|
140
|
-
1.330365926242124
|
|
141
|
-
]
|
|
142
|
-
];
|
|
143
|
-
/** Linear sRGB to OKLab's cone responses, and back. */
|
|
144
|
-
const LRGB_TO_LMS = [
|
|
145
|
-
[
|
|
146
|
-
.412221469470763,
|
|
147
|
-
.5363325372617348,
|
|
148
|
-
.0514459932675022
|
|
149
|
-
],
|
|
150
|
-
[
|
|
151
|
-
.2119034958178252,
|
|
152
|
-
.6806995506452344,
|
|
153
|
-
.1073969535369406
|
|
154
|
-
],
|
|
155
|
-
[
|
|
156
|
-
.0883024591900564,
|
|
157
|
-
.2817188391361215,
|
|
158
|
-
.6299787016738222
|
|
159
|
-
]
|
|
160
|
-
];
|
|
161
|
-
const LMS_TO_LRGB = [
|
|
162
|
-
[
|
|
163
|
-
4.076741636075957,
|
|
164
|
-
-3.3077115392580616,
|
|
165
|
-
.2309699031821044
|
|
166
|
-
],
|
|
167
|
-
[
|
|
168
|
-
-1.2684379732850317,
|
|
169
|
-
2.6097573492876887,
|
|
170
|
-
-.3413193760026573
|
|
171
|
-
],
|
|
172
|
-
[
|
|
173
|
-
-.0041960761386756,
|
|
174
|
-
-.7034186179359362,
|
|
175
|
-
1.7076146940746117
|
|
176
|
-
]
|
|
177
|
-
];
|
|
178
|
-
const LMS_TO_OKLAB = [
|
|
179
|
-
[
|
|
180
|
-
.210454268309314,
|
|
181
|
-
.7936177747023054,
|
|
182
|
-
-.0040720430116193
|
|
183
|
-
],
|
|
184
|
-
[
|
|
185
|
-
1.9779985324311684,
|
|
186
|
-
-2.42859224204858,
|
|
187
|
-
.450593709617411
|
|
188
|
-
],
|
|
189
|
-
[
|
|
190
|
-
.0259040424655478,
|
|
191
|
-
.7827717124575296,
|
|
192
|
-
-.8086757549230774
|
|
193
|
-
]
|
|
194
|
-
];
|
|
195
|
-
const OKLAB_TO_LMS = [
|
|
196
|
-
[
|
|
197
|
-
1,
|
|
198
|
-
.3963377773761749,
|
|
199
|
-
.2158037573099136
|
|
200
|
-
],
|
|
201
|
-
[
|
|
202
|
-
1,
|
|
203
|
-
-.1055613458156586,
|
|
204
|
-
-.0638541728258133
|
|
205
|
-
],
|
|
206
|
-
[
|
|
207
|
-
1,
|
|
208
|
-
-.0894841775298119,
|
|
209
|
-
-1.2914855480194092
|
|
210
|
-
]
|
|
211
|
-
];
|
|
212
|
-
const toXyz = (c) => mul(SRGB_TO_XYZ, linearRgb(c));
|
|
213
|
-
const fromXyz = (xyz, alpha) => fromLinearRgb(mul(XYZ_TO_SRGB, xyz), alpha);
|
|
214
|
-
/** Display P3 channels (gamma-encoded, 0 to 1 inside P3). */
|
|
215
|
-
const toP3 = (c) => map3(mul(XYZ_TO_P3, toXyz(c)), fromLinear);
|
|
216
|
-
const fromP3 = (p3, alpha) => fromXyz(mul(P3_TO_XYZ, map3(p3, toLinear)), alpha);
|
|
217
|
-
const fromXyzD50 = (xyz, alpha) => fromXyz(mul(D50_TO_D65, xyz), alpha);
|
|
218
|
-
/** OKLab: L in [0, 1], a and b around ±0.4. */
|
|
219
|
-
const toOklab = (c) => mul(LMS_TO_OKLAB, map3(mul(LRGB_TO_LMS, linearRgb(c)), Math.cbrt));
|
|
220
|
-
const fromOklab = (lab, alpha) => fromLinearRgb(mul(LMS_TO_LRGB, map3(mul(OKLAB_TO_LMS, lab), (v) => v ** 3)), alpha);
|
|
221
|
-
const D50_WHITE = [
|
|
222
|
-
.3457 / .3585,
|
|
223
|
-
1,
|
|
224
|
-
.2958 / .3585
|
|
225
|
-
];
|
|
226
|
-
const KAPPA = 24389 / 27;
|
|
227
|
-
const EPSILON = 216 / 24389;
|
|
228
|
-
/** CIE Lab (D50): L in [0, 100], a and b around ±125. */
|
|
229
|
-
const toLab = (c) => {
|
|
230
|
-
const [x, y, z] = map3(mul(D65_TO_D50, toXyz(c)), (v, i) => v / D50_WHITE[i]);
|
|
231
|
-
const f = (v) => v > EPSILON ? Math.cbrt(v) : (KAPPA * v + 16) / 116;
|
|
232
|
-
const [fx, fy, fz] = [
|
|
233
|
-
f(x),
|
|
234
|
-
f(y),
|
|
235
|
-
f(z)
|
|
236
|
-
];
|
|
237
|
-
return [
|
|
238
|
-
116 * fy - 16,
|
|
239
|
-
500 * (fx - fy),
|
|
240
|
-
200 * (fy - fz)
|
|
241
|
-
];
|
|
242
|
-
};
|
|
243
|
-
const fromLab = ([l, a, b], alpha) => {
|
|
244
|
-
const fy = (l + 16) / 116;
|
|
245
|
-
const fx = a / 500 + fy;
|
|
246
|
-
const fz = fy - b / 200;
|
|
247
|
-
const xyz = [
|
|
248
|
-
fx ** 3 > EPSILON ? fx ** 3 : (116 * fx - 16) / KAPPA,
|
|
249
|
-
l > KAPPA * EPSILON ? fy ** 3 : l / KAPPA,
|
|
250
|
-
fz ** 3 > EPSILON ? fz ** 3 : (116 * fz - 16) / KAPPA
|
|
251
|
-
];
|
|
252
|
-
return fromXyzD50(map3(xyz, (v, i) => v * D50_WHITE[i]), alpha);
|
|
253
|
-
};
|
|
254
|
-
/** Lab-like to its polar form: lightness, chroma, and hue in degrees from 0 to 360. */
|
|
255
|
-
const toPolar = ([l, a, b]) => [
|
|
256
|
-
l,
|
|
257
|
-
Math.hypot(a, b),
|
|
258
|
-
(Math.atan2(b, a) * 180 / Math.PI + 360) % 360
|
|
259
|
-
];
|
|
260
|
-
const fromPolar = ([l, c, h]) => [
|
|
261
|
-
l,
|
|
262
|
-
c * Math.cos(h * Math.PI / 180),
|
|
263
|
-
c * Math.sin(h * Math.PI / 180)
|
|
264
|
-
];
|
|
265
|
-
const clip01 = (v) => Math.min(1, Math.max(0, v));
|
|
266
|
-
/**
|
|
267
|
-
* Below this spread between channels a colour is grey: what is left is rounding error from
|
|
268
|
-
* converting it (from Lab, OKLCH, P3...), and its hue and saturation would be noise. The
|
|
269
|
-
* faintest real tint in 8-bit sRGB, such as #fffffe, has a spread of about 0.004.
|
|
270
|
-
*/
|
|
271
|
-
const GREY = 1e-9;
|
|
272
|
-
/**
|
|
273
|
-
* Hue, saturation and lightness of a colour inside sRGB (hue 0 for greys), with s and l in [0, 1].
|
|
274
|
-
* Channels are clipped first: colours count as inside sRGB within rounding error, a little past 0 or 1.
|
|
275
|
-
*/
|
|
276
|
-
function toHsl(c) {
|
|
277
|
-
const [r, g, b] = map3([
|
|
278
|
-
c.r,
|
|
279
|
-
c.g,
|
|
280
|
-
c.b
|
|
281
|
-
], clip01);
|
|
282
|
-
const max = Math.max(r, g, b);
|
|
283
|
-
const min = Math.min(r, g, b);
|
|
284
|
-
const l = (max + min) / 2;
|
|
285
|
-
const d = max - min;
|
|
286
|
-
if (d < GREY) return [
|
|
287
|
-
0,
|
|
288
|
-
0,
|
|
289
|
-
l
|
|
290
|
-
];
|
|
291
|
-
const s = (max - l) / Math.min(l, 1 - l);
|
|
292
|
-
let h;
|
|
293
|
-
if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
|
|
294
|
-
else if (max === g) h = (b - r) / d + 2;
|
|
295
|
-
else h = (r - g) / d + 4;
|
|
296
|
-
return [
|
|
297
|
-
h * 60,
|
|
298
|
-
s,
|
|
299
|
-
l
|
|
300
|
-
];
|
|
301
|
-
}
|
|
302
|
-
function fromHsl(h, s, l, alpha) {
|
|
303
|
-
const hue = (h % 360 + 360) % 360;
|
|
304
|
-
const f = (n) => {
|
|
305
|
-
const k = (n + hue / 30) % 12;
|
|
306
|
-
return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
|
|
307
|
-
};
|
|
308
|
-
return color(f(0), f(8), f(4), alpha);
|
|
309
|
-
}
|
|
310
|
-
function fromHwb(h, w, b, alpha) {
|
|
311
|
-
if (w + b >= 1) {
|
|
312
|
-
const grey = w / (w + b);
|
|
313
|
-
return color(grey, grey, grey, alpha);
|
|
314
|
-
}
|
|
315
|
-
const pure = fromHsl(h, 1, .5, 1);
|
|
316
|
-
const scale = (v) => v * (1 - w - b) + w;
|
|
317
|
-
return color(scale(pure.r), scale(pure.g), scale(pure.b), alpha);
|
|
318
|
-
}
|
|
319
|
-
/** Hue, whiteness and blackness of an sRGB colour, with w and b in [0, 1] (channels clipped, as in `toHsl`). */
|
|
320
|
-
function toHwb(c) {
|
|
321
|
-
const [h] = toHsl(c);
|
|
322
|
-
const [r, g, b] = map3([
|
|
323
|
-
c.r,
|
|
324
|
-
c.g,
|
|
325
|
-
c.b
|
|
326
|
-
], clip01);
|
|
327
|
-
return [
|
|
328
|
-
h,
|
|
329
|
-
Math.min(r, g, b),
|
|
330
|
-
1 - Math.max(r, g, b)
|
|
331
|
-
];
|
|
332
|
-
}
|
|
333
|
-
/** Distance in OKLab: about 0.02 is the smallest difference people notice. */
|
|
334
|
-
const deltaEOK = (a, b) => {
|
|
335
|
-
const [l1, a1, b1] = toOklab(a);
|
|
336
|
-
const [l2, a2, b2] = toOklab(b);
|
|
337
|
-
return Math.hypot(l1 - l2, a1 - a2, b1 - b2);
|
|
338
|
-
};
|
|
339
|
-
const channels = (c, gamut) => gamut === "srgb" ? [
|
|
340
|
-
c.r,
|
|
341
|
-
c.g,
|
|
342
|
-
c.b
|
|
343
|
-
] : toP3(c);
|
|
344
|
-
const clipTo = (c, gamut) => {
|
|
345
|
-
const [x, y, z] = map3(channels(c, gamut), clip01);
|
|
346
|
-
return gamut === "srgb" ? color(x, y, z, c.alpha) : fromP3([
|
|
347
|
-
x,
|
|
348
|
-
y,
|
|
349
|
-
z
|
|
350
|
-
], c.alpha);
|
|
351
|
-
};
|
|
352
|
-
/** Whether a colour is inside a gamut, within rounding error. */
|
|
353
|
-
function inGamut(c, gamut = "srgb") {
|
|
354
|
-
return channels(c, gamut).every((v) => v >= -1e-6 && v <= 1 + 1e-6);
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* The colour inside `gamut` that looks closest: the CSS Color 4 gamut mapping
|
|
358
|
-
* algorithm, which keeps lightness and hue and reduces chroma in OKLCH until
|
|
359
|
-
* clipping is no longer visible (`"map"`), or plain clipping of each channel (`"clip"`).
|
|
360
|
-
*/
|
|
361
|
-
function toGamut(c, gamut = "srgb", method = "map") {
|
|
362
|
-
if (inGamut(c, gamut)) return clipTo(c, gamut);
|
|
363
|
-
if (method === "clip") return clipTo(c, gamut);
|
|
364
|
-
const [l, chroma, hue] = toPolar(toOklab(c));
|
|
365
|
-
if (l >= 1 - 1e-7) return clipTo(color(1, 1, 1, c.alpha), gamut);
|
|
366
|
-
if (l <= 1e-7) return clipTo(color(0, 0, 0, c.alpha), gamut);
|
|
367
|
-
const JND = .02;
|
|
368
|
-
let low = 0;
|
|
369
|
-
let high = chroma;
|
|
370
|
-
let lowInGamut = true;
|
|
371
|
-
let current = c;
|
|
372
|
-
let clipped = clipTo(current, gamut);
|
|
373
|
-
if (deltaEOK(clipped, current) < JND) return clipped;
|
|
374
|
-
while (high - low > 1e-4) {
|
|
375
|
-
const mid = (low + high) / 2;
|
|
376
|
-
current = fromOklab(fromPolar([
|
|
377
|
-
l,
|
|
378
|
-
mid,
|
|
379
|
-
hue
|
|
380
|
-
]), c.alpha);
|
|
381
|
-
if (lowInGamut && inGamut(current, gamut)) {
|
|
382
|
-
low = mid;
|
|
383
|
-
continue;
|
|
384
|
-
}
|
|
385
|
-
clipped = clipTo(current, gamut);
|
|
386
|
-
const e = deltaEOK(clipped, current);
|
|
387
|
-
if (e < JND) {
|
|
388
|
-
if (JND - e < 1e-4) return clipped;
|
|
389
|
-
lowInGamut = false;
|
|
390
|
-
low = mid;
|
|
391
|
-
} else high = mid;
|
|
392
|
-
}
|
|
393
|
-
return clipped;
|
|
394
|
-
}
|
|
395
|
-
//#endregion
|
|
396
|
-
//#region src/core/errors.ts
|
|
397
|
-
function createCopyError(type, message, cause) {
|
|
398
|
-
return cause === void 0 ? {
|
|
399
|
-
type,
|
|
400
|
-
message
|
|
401
|
-
} : {
|
|
402
|
-
type,
|
|
403
|
-
message,
|
|
404
|
-
cause
|
|
405
|
-
};
|
|
406
|
-
}
|
|
407
|
-
/**
|
|
408
|
-
* Branded (not `instanceof`) so failures stay recognisable even when the core
|
|
409
|
-
* is bundled twice, e.g. `react-smart-copy` and `react-smart-copy/core` together.
|
|
410
|
-
*/
|
|
411
|
-
const COPY_FAILURE_BRAND = Symbol.for("react-smart-copy.CopyFailure");
|
|
412
|
-
/** Error thrown by adapters to carry a fully classified {@link CopyError}. */
|
|
413
|
-
var CopyFailure = class extends Error {
|
|
414
|
-
constructor(copyError) {
|
|
415
|
-
super(copyError.message);
|
|
416
|
-
this[COPY_FAILURE_BRAND] = true;
|
|
417
|
-
this.name = "CopyFailure";
|
|
418
|
-
this.copyError = copyError;
|
|
419
|
-
}
|
|
420
|
-
};
|
|
421
|
-
/** Shorthand for throwing a classified failure. */
|
|
422
|
-
function copyFailure(type, message, cause) {
|
|
423
|
-
return new CopyFailure(createCopyError(type, message, cause));
|
|
424
|
-
}
|
|
425
|
-
//#endregion
|
|
426
|
-
//#region src/color/names.ts
|
|
427
|
-
/**
|
|
428
|
-
* The 148 named colours of CSS Color 4, as "name rrggbb" pairs. Parsed into a map
|
|
429
|
-
* the first time a name is looked up, so the table costs nothing until then.
|
|
430
|
-
*/
|
|
431
|
-
const NAMED = [
|
|
432
|
-
"aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4",
|
|
433
|
-
"black 000000 blanchedalmond ffebcd blue 0000ff blueviolet 8a2be2 brown a52a2a burlywood deb887",
|
|
434
|
-
"cadetblue 5f9ea0 chartreuse 7fff00 chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc",
|
|
435
|
-
"crimson dc143c cyan 00ffff darkblue 00008b darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9",
|
|
436
|
-
"darkgreen 006400 darkgrey a9a9a9 darkkhaki bdb76b darkmagenta 8b008b darkolivegreen 556b2f darkorange ff8c00",
|
|
437
|
-
"darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f darkslateblue 483d8b",
|
|
438
|
-
"darkslategray 2f4f4f darkslategrey 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493",
|
|
439
|
-
"deepskyblue 00bfff dimgray 696969 dimgrey 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0",
|
|
440
|
-
"forestgreen 228b22 fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 gray 808080",
|
|
441
|
-
"green 008000 greenyellow adff2f grey 808080 honeydew f0fff0 hotpink ff69b4 indianred cd5c5c indigo 4b0082",
|
|
442
|
-
"ivory fffff0 khaki f0e68c lavender e6e6fa lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd",
|
|
443
|
-
"lightblue add8e6 lightcoral f08080 lightcyan e0ffff lightgoldenrodyellow fafad2 lightgray d3d3d3",
|
|
444
|
-
"lightgreen 90ee90 lightgrey d3d3d3 lightpink ffb6c1 lightsalmon ffa07a lightseagreen 20b2aa",
|
|
445
|
-
"lightskyblue 87cefa lightslategray 778899 lightslategrey 778899 lightsteelblue b0c4de lightyellow ffffe0",
|
|
446
|
-
"lime 00ff00 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa",
|
|
447
|
-
"mediumblue 0000cd mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee",
|
|
448
|
-
"mediumspringgreen 00fa9a mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa",
|
|
449
|
-
"mistyrose ffe4e1 moccasin ffe4b5 navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23",
|
|
450
|
-
"orange ffa500 orangered ff4500 orchid da70d6 palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee",
|
|
451
|
-
"palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9 peru cd853f pink ffc0cb plum dda0dd powderblue b0e0e6",
|
|
452
|
-
"purple 800080 rebeccapurple 663399 red ff0000 rosybrown bc8f8f royalblue 4169e1 saddlebrown 8b4513",
|
|
453
|
-
"salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d silver c0c0c0 skyblue 87ceeb",
|
|
454
|
-
"slateblue 6a5acd slategray 708090 slategrey 708090 snow fffafa springgreen 00ff7f steelblue 4682b4 tan d2b48c",
|
|
455
|
-
"teal 008080 thistle d8bfd8 tomato ff6347 turquoise 40e0d0 violet ee82ee wheat f5deb3 white ffffff",
|
|
456
|
-
"whitesmoke f5f5f5 yellow ffff00 yellowgreen 9acd32"
|
|
457
|
-
].join(" ");
|
|
458
|
-
let table;
|
|
459
|
-
/** The colour for a CSS colour name (lowercase), as 0xrrggbb, or `undefined`. */
|
|
460
|
-
function namedColor(name) {
|
|
461
|
-
if (table === void 0) {
|
|
462
|
-
table = /* @__PURE__ */ new Map();
|
|
463
|
-
const parts = NAMED.split(" ");
|
|
464
|
-
for (let i = 0; i < parts.length; i += 2) table.set(parts[i], parseInt(parts[i + 1], 16));
|
|
465
|
-
}
|
|
466
|
-
return table.get(name);
|
|
467
|
-
}
|
|
468
|
-
//#endregion
|
|
469
|
-
//#region src/color/parse.ts
|
|
470
|
-
/** Longer input is not a colour: parsing stays cheap whatever is pasted. */
|
|
471
|
-
const MAX_LENGTH = 256;
|
|
472
|
-
const NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/;
|
|
473
|
-
const HEX = /^#([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/;
|
|
474
|
-
const FUNCTION = /^([a-z][a-z\d-]*)\(([^()]*)\)$/;
|
|
475
|
-
const ANGLE_UNITS = {
|
|
476
|
-
deg: 1,
|
|
477
|
-
grad: .9,
|
|
478
|
-
rad: 180 / Math.PI,
|
|
479
|
-
turn: 360
|
|
480
|
-
};
|
|
481
|
-
/** One component: a number, a percentage, an angle (in degrees) or `none`. */
|
|
482
|
-
function token(text) {
|
|
483
|
-
if (text === "none") return {
|
|
484
|
-
kind: "none",
|
|
485
|
-
value: 0
|
|
486
|
-
};
|
|
487
|
-
if (text.endsWith("%")) {
|
|
488
|
-
const body = text.slice(0, -1);
|
|
489
|
-
return NUMBER.test(body) ? {
|
|
490
|
-
kind: "percent",
|
|
491
|
-
value: Number(body)
|
|
492
|
-
} : null;
|
|
493
|
-
}
|
|
494
|
-
const unit = /[a-z]+$/.exec(text)?.[0];
|
|
495
|
-
if (unit !== void 0) {
|
|
496
|
-
const factor = ANGLE_UNITS[unit];
|
|
497
|
-
const body = text.slice(0, -unit.length);
|
|
498
|
-
return factor === void 0 || !NUMBER.test(body) ? null : {
|
|
499
|
-
kind: "angle",
|
|
500
|
-
value: Number(body) * factor
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
return NUMBER.test(text) ? {
|
|
504
|
-
kind: "number",
|
|
505
|
-
value: Number(text)
|
|
506
|
-
} : null;
|
|
507
|
-
}
|
|
508
|
-
const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
|
|
509
|
-
/** A component's value: numbers as they are, percentages scaled so 100% is `full`. */
|
|
510
|
-
function amount(t, full, allowed) {
|
|
511
|
-
if (t.kind === "none") return 0;
|
|
512
|
-
if (!allowed.includes(t.kind)) return null;
|
|
513
|
-
return t.kind === "percent" ? t.value / 100 * full : t.value;
|
|
514
|
-
}
|
|
515
|
-
const hueOf = (t) => amount(t, 0, ["number", "angle"]);
|
|
516
|
-
const alphaOf = (t) => {
|
|
517
|
-
if (t === void 0) return 1;
|
|
518
|
-
const value = amount(t, 1, ["number", "percent"]);
|
|
519
|
-
return value === null ? null : clamp(value, 0, 1);
|
|
520
|
-
};
|
|
521
|
-
/** Splits arguments into three components and an optional alpha; `legacy` for the comma syntax. */
|
|
522
|
-
function split(args) {
|
|
523
|
-
const legacy = args.includes(",");
|
|
524
|
-
let main;
|
|
525
|
-
let alphaText;
|
|
526
|
-
if (legacy) {
|
|
527
|
-
const list = args.split(",").map((part) => part.trim());
|
|
528
|
-
if (list.length < 3 || list.length > 4) return null;
|
|
529
|
-
main = list.slice(0, 3);
|
|
530
|
-
alphaText = list[3];
|
|
531
|
-
} else {
|
|
532
|
-
const [body, rest, extra] = args.split("/");
|
|
533
|
-
if (extra !== void 0) return null;
|
|
534
|
-
main = body.trim().split(/\s+/);
|
|
535
|
-
alphaText = rest?.trim();
|
|
536
|
-
if (main.length !== 3 || alphaText === "") return null;
|
|
537
|
-
}
|
|
538
|
-
const parts = main.map(token);
|
|
539
|
-
if (parts.some((part) => part === null)) return null;
|
|
540
|
-
const alpha = alphaText === void 0 ? void 0 : token(alphaText);
|
|
541
|
-
if (alpha === null) return null;
|
|
542
|
-
if (legacy && [...parts, alpha].some((part) => part?.kind === "none")) return null;
|
|
543
|
-
return {
|
|
544
|
-
parts,
|
|
545
|
-
alpha,
|
|
546
|
-
legacy
|
|
547
|
-
};
|
|
548
|
-
}
|
|
549
|
-
function rgb(parts, legacy) {
|
|
550
|
-
if (legacy && new Set(parts.map((part) => part.kind)).size !== 1) return null;
|
|
551
|
-
const values = parts.map((part) => amount(part, 255, ["number", "percent"]));
|
|
552
|
-
const vector = vec3(values);
|
|
553
|
-
return vector === null ? null : map3(vector, (v) => clamp(v, 0, 255) / 255);
|
|
554
|
-
}
|
|
555
|
-
const PREDEFINED = {
|
|
556
|
-
srgb: ([r, g, b], alpha) => color(r, g, b, alpha),
|
|
557
|
-
"srgb-linear": fromLinearRgb,
|
|
558
|
-
"display-p3": fromP3,
|
|
559
|
-
xyz: fromXyz,
|
|
560
|
-
"xyz-d65": fromXyz,
|
|
561
|
-
"xyz-d50": fromXyzD50
|
|
562
|
-
};
|
|
563
|
-
/** `color(space c1 c2 c3 / alpha)`. */
|
|
564
|
-
function predefined(args) {
|
|
565
|
-
const match = /^([a-z\d-]+)\s+(.*)$/.exec(args.trim());
|
|
566
|
-
if (match === null) return null;
|
|
567
|
-
const convert = PREDEFINED[match[1]];
|
|
568
|
-
const parsed = split(match[2]);
|
|
569
|
-
if (convert === void 0 || parsed === null || parsed.legacy) return null;
|
|
570
|
-
const values = parsed.parts.map((part) => amount(part, 1, ["number", "percent"]));
|
|
571
|
-
const alpha = alphaOf(parsed.alpha);
|
|
572
|
-
const vector = vec3(values);
|
|
573
|
-
return vector === null || alpha === null ? null : convert(vector, alpha);
|
|
574
|
-
}
|
|
575
|
-
function functional(name, args) {
|
|
576
|
-
if (name === "color") return predefined(args);
|
|
577
|
-
const parsed = split(args);
|
|
578
|
-
if (parsed === null) return null;
|
|
579
|
-
const { parts, legacy } = parsed;
|
|
580
|
-
const [a, b, c] = parts;
|
|
581
|
-
const alpha = alphaOf(parsed.alpha);
|
|
582
|
-
if (alpha === null) return null;
|
|
583
|
-
switch (name) {
|
|
584
|
-
case "rgb":
|
|
585
|
-
case "rgba": {
|
|
586
|
-
const values = rgb(parts, legacy);
|
|
587
|
-
return values === null ? null : color(values[0], values[1], values[2], alpha);
|
|
588
|
-
}
|
|
589
|
-
case "hsl":
|
|
590
|
-
case "hsla": {
|
|
591
|
-
const hue = hueOf(a);
|
|
592
|
-
const kinds = legacy ? ["percent"] : ["number", "percent"];
|
|
593
|
-
const s = amount(b, 100, kinds);
|
|
594
|
-
const l = amount(c, 100, kinds);
|
|
595
|
-
if (hue === null || s === null || l === null) return null;
|
|
596
|
-
return fromHsl(hue, clamp(s, 0, 100) / 100, clamp(l, 0, 100) / 100, alpha);
|
|
597
|
-
}
|
|
598
|
-
case "hwb": {
|
|
599
|
-
const hue = hueOf(a);
|
|
600
|
-
const w = amount(b, 100, ["number", "percent"]);
|
|
601
|
-
const k = amount(c, 100, ["number", "percent"]);
|
|
602
|
-
if (legacy || hue === null || w === null || k === null) return null;
|
|
603
|
-
return fromHwb(hue, clamp(w, 0, 100) / 100, clamp(k, 0, 100) / 100, alpha);
|
|
604
|
-
}
|
|
605
|
-
default: return legacy ? null : lab(name, a, b, c, alpha);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
/** lab(), lch(), oklab() and oklch(). */
|
|
609
|
-
function lab(name, a, b, c, alpha) {
|
|
610
|
-
const ok = name.startsWith("ok");
|
|
611
|
-
const polar = name.endsWith("ch");
|
|
612
|
-
if (![
|
|
613
|
-
"lab",
|
|
614
|
-
"lch",
|
|
615
|
-
"oklab",
|
|
616
|
-
"oklch"
|
|
617
|
-
].includes(name)) return null;
|
|
618
|
-
const lightMax = ok ? 1 : 100;
|
|
619
|
-
const l = amount(a, lightMax, ["number", "percent"]);
|
|
620
|
-
const second = amount(b, ok ? .4 : polar ? 150 : 125, ["number", "percent"]);
|
|
621
|
-
const third = polar ? hueOf(c) : amount(c, ok ? .4 : 125, ["number", "percent"]);
|
|
622
|
-
if (l === null || second === null || third === null) return null;
|
|
623
|
-
const lightness = clamp(l, 0, lightMax);
|
|
624
|
-
const values = polar ? fromPolar([
|
|
625
|
-
lightness,
|
|
626
|
-
Math.max(0, second),
|
|
627
|
-
third
|
|
628
|
-
]) : [
|
|
629
|
-
lightness,
|
|
630
|
-
second,
|
|
631
|
-
third
|
|
632
|
-
];
|
|
633
|
-
return ok ? fromOklab(values, alpha) : fromLab(values, alpha);
|
|
634
|
-
}
|
|
635
|
-
/**
|
|
636
|
-
* Parses a CSS colour, or returns `null`: hex (`#rgb`, `#rgba`, `#rrggbb`,
|
|
637
|
-
* `#rrggbbaa`), the 148 named colours and `transparent`, `rgb()` and `hsl()` in
|
|
638
|
-
* both syntaxes, `hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()`, and `color()`
|
|
639
|
-
* in `srgb`, `srgb-linear`, `display-p3`, `xyz`, `xyz-d65` and `xyz-d50`.
|
|
640
|
-
* Never throws. Colours outside sRGB keep their exact value.
|
|
641
|
-
*
|
|
642
|
-
* Not colours on their own: `currentcolor`, system colours, `var()`, `calc()`,
|
|
643
|
-
* `color-mix()` and relative colours.
|
|
644
|
-
*
|
|
645
|
-
* @example
|
|
646
|
-
* parseColor('oklch(62.8% 0.258 29.23)'); // { r: 1, g: 0, b: 0, alpha: 1 } (rounded)
|
|
647
|
-
*/
|
|
648
|
-
function parseColor(input) {
|
|
649
|
-
if (typeof input !== "string" || input.length > MAX_LENGTH) return null;
|
|
650
|
-
const text = input.trim().toLowerCase();
|
|
651
|
-
const hex = HEX.exec(text)?.[1];
|
|
652
|
-
if (hex !== void 0) {
|
|
653
|
-
const full = hex.length <= 4 ? hex.replace(/./g, (digit) => digit + digit) : hex;
|
|
654
|
-
const byte = (i) => parseInt(full.slice(i, i + 2), 16) / 255;
|
|
655
|
-
return color(byte(0), byte(2), byte(4), full.length === 8 ? byte(6) : 1);
|
|
656
|
-
}
|
|
657
|
-
if (text === "transparent") return color(0, 0, 0, 0);
|
|
658
|
-
const named = namedColor(text);
|
|
659
|
-
if (named !== void 0) return color((named >> 16 & 255) / 255, (named >> 8 & 255) / 255, (named & 255) / 255);
|
|
660
|
-
const match = FUNCTION.exec(text);
|
|
661
|
-
return match === null ? null : functional(match[1], match[2].trim());
|
|
662
|
-
}
|
|
663
|
-
//#endregion
|
|
664
|
-
//#region src/color/format.ts
|
|
665
|
-
const COLOR_FORMATS = /* @__PURE__ */ Object.freeze([
|
|
666
|
-
"hex",
|
|
667
|
-
"rgb",
|
|
668
|
-
"hsl",
|
|
669
|
-
"hwb",
|
|
670
|
-
"oklch",
|
|
671
|
-
"oklab",
|
|
672
|
-
"lab",
|
|
673
|
-
"lch",
|
|
674
|
-
"p3"
|
|
675
|
-
]);
|
|
676
|
-
const isColor = (value) => typeof value === "object" && value !== null && [
|
|
677
|
-
"r",
|
|
678
|
-
"g",
|
|
679
|
-
"b",
|
|
680
|
-
"alpha"
|
|
681
|
-
].every((key) => Number.isFinite(value[key]));
|
|
682
|
-
/** Resolves a colour input, throwing a typed error for anything that is not one. */
|
|
683
|
-
function toColor(input) {
|
|
684
|
-
if (typeof input === "string") {
|
|
685
|
-
const parsed = parseColor(input);
|
|
686
|
-
if (parsed === null) throw copyFailure("invalid-payload", `Not a colour: ${JSON.stringify(input.slice(0, 100))}.`);
|
|
687
|
-
return parsed;
|
|
688
|
-
}
|
|
689
|
-
if (!isColor(input)) throw copyFailure("invalid-payload", "A colour must be a CSS colour string or an { r, g, b, alpha } object with finite numbers.");
|
|
690
|
-
return input;
|
|
691
|
-
}
|
|
692
|
-
/** Rounds to `places` decimals, without trailing zeros or `-0`. */
|
|
693
|
-
function num(value, places) {
|
|
694
|
-
const factor = 10 ** places;
|
|
695
|
-
const rounded = Math.round(value * factor) / factor;
|
|
696
|
-
return String(Object.is(rounded, -0) ? 0 : rounded);
|
|
697
|
-
}
|
|
698
|
-
/** A hue in degrees, with 360 written as 0. */
|
|
699
|
-
const hue = (value, places) => {
|
|
700
|
-
const text = num(value, places);
|
|
701
|
-
return text === "360" ? "0" : text;
|
|
702
|
-
};
|
|
703
|
-
const byte = (v) => Math.round(Math.min(1, Math.max(0, v)) * 255);
|
|
704
|
-
/** The alpha suffix of a functional notation: nothing when opaque. */
|
|
705
|
-
const alphaPart = (alpha, legacy, places) => alpha >= 1 ? "" : legacy ? `, ${num(alpha, places)}` : ` / ${num(alpha, places)}`;
|
|
706
|
-
/**
|
|
707
|
-
* Writes a colour in a CSS format. Text is parsed first, so this also converts
|
|
708
|
-
* between formats.
|
|
709
|
-
*
|
|
710
|
-
* @example
|
|
711
|
-
* formatColor('rgb(255 0 0)', 'oklch'); // 'oklch(62.8% 0.2577 29.23)'
|
|
712
|
-
* formatColor('color(display-p3 1 0 0)', 'hex'); // '#ff0000' after gamut mapping, never a wrong hue
|
|
713
|
-
*/
|
|
714
|
-
function formatColor(input, format = "hex", options = {}) {
|
|
715
|
-
const c = toColor(input);
|
|
716
|
-
const { gamut = "map", legacy = false, uppercase = false, precision } = options;
|
|
717
|
-
const method = gamut;
|
|
718
|
-
if (method !== "map" && method !== "clip") throw copyFailure("invalid-payload", "`gamut` must be \"map\" or \"clip\".");
|
|
719
|
-
if (precision !== void 0 && !(Number.isInteger(precision) && precision >= 0 && precision <= 10)) throw copyFailure("invalid-payload", "`precision` must be an integer from 0 to 10.");
|
|
720
|
-
const dp = (fallback) => precision ?? fallback;
|
|
721
|
-
const a = c.alpha;
|
|
722
|
-
switch (format) {
|
|
723
|
-
case "hex": {
|
|
724
|
-
const s = toGamut(c, "srgb", gamut);
|
|
725
|
-
const digits = [
|
|
726
|
-
s.r,
|
|
727
|
-
s.g,
|
|
728
|
-
s.b,
|
|
729
|
-
...a < 1 ? [a] : []
|
|
730
|
-
].map((v) => byte(v).toString(16).padStart(2, "0")).join("");
|
|
731
|
-
return `#${uppercase ? digits.toUpperCase() : digits}`;
|
|
732
|
-
}
|
|
733
|
-
case "rgb": {
|
|
734
|
-
const s = toGamut(c, "srgb", gamut);
|
|
735
|
-
const [r, g, b] = [
|
|
736
|
-
s.r,
|
|
737
|
-
s.g,
|
|
738
|
-
s.b
|
|
739
|
-
].map(byte);
|
|
740
|
-
return legacy ? `${a < 1 ? "rgba" : "rgb"}(${String(r)}, ${String(g)}, ${String(b)}${alphaPart(a, true, dp(3))})` : `rgb(${String(r)} ${String(g)} ${String(b)}${alphaPart(a, false, dp(3))})`;
|
|
741
|
-
}
|
|
742
|
-
case "hsl": {
|
|
743
|
-
const [h, s, l] = toHsl(toGamut(c, "srgb", gamut));
|
|
744
|
-
const [hh, ss, ll] = [
|
|
745
|
-
hue(h, dp(2)),
|
|
746
|
-
`${num(s * 100, dp(2))}%`,
|
|
747
|
-
`${num(l * 100, dp(2))}%`
|
|
748
|
-
];
|
|
749
|
-
return legacy ? `${a < 1 ? "hsla" : "hsl"}(${hh}, ${ss}, ${ll}${alphaPart(a, true, dp(3))})` : `hsl(${hh} ${ss} ${ll}${alphaPart(a, false, dp(3))})`;
|
|
750
|
-
}
|
|
751
|
-
case "hwb": {
|
|
752
|
-
const [h, w, b] = toHwb(toGamut(c, "srgb", gamut));
|
|
753
|
-
return `hwb(${hue(h, dp(2))} ${num(w * 100, dp(2))}% ${num(b * 100, dp(2))}%${alphaPart(a, false, dp(3))})`;
|
|
754
|
-
}
|
|
755
|
-
case "oklab": {
|
|
756
|
-
const [l, x, y] = toOklab(c);
|
|
757
|
-
return `oklab(${num(l * 100, dp(2))}% ${num(x, dp(5))} ${num(y, dp(5))}${alphaPart(a, false, dp(3))})`;
|
|
758
|
-
}
|
|
759
|
-
case "oklch": {
|
|
760
|
-
const [l, ch, h] = toPolar(toOklab(c));
|
|
761
|
-
const chroma = num(ch, dp(5));
|
|
762
|
-
return `oklch(${num(l * 100, dp(2))}% ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
763
|
-
}
|
|
764
|
-
case "lab": {
|
|
765
|
-
const [l, x, y] = toLab(c);
|
|
766
|
-
return `lab(${num(l, dp(2))} ${num(x, dp(2))} ${num(y, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
767
|
-
}
|
|
768
|
-
case "lch": {
|
|
769
|
-
const [l, ch, h] = toPolar(toLab(c));
|
|
770
|
-
const chroma = num(ch, dp(2));
|
|
771
|
-
return `lch(${num(l, dp(2))} ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
|
|
772
|
-
}
|
|
773
|
-
case "p3": {
|
|
774
|
-
const [r, g, b] = toP3(toGamut(c, "p3", gamut));
|
|
775
|
-
return `color(display-p3 ${num(r, dp(4))} ${num(g, dp(4))} ${num(b, dp(4))}${alphaPart(a, false, dp(3))})`;
|
|
776
|
-
}
|
|
777
|
-
default: throw copyFailure("invalid-payload", `Unknown colour format ${JSON.stringify(String(format))}: use one of ${COLOR_FORMATS.join(", ")}.`);
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
//#endregion
|
|
781
|
-
//#region src/color/contrast.ts
|
|
782
|
-
const WHITE = /* @__PURE__ */ color(1, 1, 1);
|
|
783
|
-
/** `top` drawn over `bottom`, both made opaque first. */
|
|
784
|
-
function over(top, bottom) {
|
|
785
|
-
const mix = (a, b) => a * top.alpha + b * (1 - top.alpha);
|
|
786
|
-
return color(mix(top.r, bottom.r), mix(top.g, bottom.g), mix(top.b, bottom.b));
|
|
787
|
-
}
|
|
788
|
-
/** WCAG 2 relative luminance of an opaque sRGB colour. */
|
|
789
|
-
function luminance(c) {
|
|
790
|
-
const lin = (v) => v <= .04045 ? v / 12.92 : ((v + .055) / 1.055) ** 2.4;
|
|
791
|
-
return .2126 * lin(c.r) + .7152 * lin(c.g) + .0722 * lin(c.b);
|
|
792
|
-
}
|
|
793
|
-
/**
|
|
794
|
-
* The WCAG 2 contrast ratio of text over a background, from 1 to 21. Colours are
|
|
795
|
-
* mapped into sRGB, a translucent background is drawn over white, and translucent
|
|
796
|
-
* text over the background, as a browser would show them. Never rounded: 4.499 is
|
|
797
|
-
* not 4.5.
|
|
798
|
-
*
|
|
799
|
-
* @example
|
|
800
|
-
* contrastRatio('#767676', '#fff'); // 4.54
|
|
801
|
-
*/
|
|
802
|
-
function contrastRatio(text, background) {
|
|
803
|
-
const bg = over(toGamut(toColor(background)), WHITE);
|
|
804
|
-
const [light, dark] = [luminance(over(toGamut(toColor(text)), bg)), luminance(bg)].sort((a, b) => b - a);
|
|
805
|
-
return (light + .05) / (dark + .05);
|
|
806
|
-
}
|
|
807
|
-
/** The WCAG 2 level met by a contrast ratio (`contrastRatio`), for normal or large text. */
|
|
808
|
-
function contrastLevel(ratio, options = {}) {
|
|
809
|
-
if (!Number.isFinite(ratio) || ratio < 1) throw copyFailure("invalid-payload", "A contrast ratio is a number from 1 to 21.");
|
|
810
|
-
const [aa, aaa] = options.large === true ? [3, 4.5] : [4.5, 7];
|
|
811
|
-
if (ratio >= aaa) return "AAA";
|
|
812
|
-
return ratio >= aa ? "AA" : "fail";
|
|
813
|
-
}
|
|
814
|
-
//#endregion
|
|
815
|
-
//#region src/core/text-field.ts
|
|
816
|
-
const TEXT_INPUTS = /* @__PURE__ */ new Set([
|
|
817
|
-
"text",
|
|
818
|
-
"search",
|
|
819
|
-
"url",
|
|
820
|
-
"tel",
|
|
821
|
-
"password"
|
|
822
|
-
]);
|
|
823
|
-
function asTextField(target) {
|
|
824
|
-
if (typeof target !== "object" || target === null) return null;
|
|
825
|
-
const element = target;
|
|
826
|
-
if (element.nodeName === "TEXTAREA") return target;
|
|
827
|
-
if (element.nodeName === "INPUT" && typeof element.type === "string" && TEXT_INPUTS.has(element.type)) return target;
|
|
828
|
-
return null;
|
|
829
|
-
}
|
|
830
|
-
/**
|
|
831
|
-
* Inserts text at the cursor of a `<textarea>` or text `<input>`, replacing the
|
|
832
|
-
* selection, as typing or pasting would: the browser's undo (Ctrl/⌘+Z) still
|
|
833
|
-
* works, `input` events fire (so React and other frameworks see the change),
|
|
834
|
-
* and `maxlength` is respected. Returns `false` for disabled or read-only fields.
|
|
835
|
-
*/
|
|
836
|
-
function insertText(field, text) {
|
|
837
|
-
if (field.disabled || field.readOnly) return false;
|
|
838
|
-
const value = field.nodeName === "INPUT" ? text.replace(/[ \t]*(?:\r\n|\r|\n)+[ \t]*/g, " ") : text;
|
|
839
|
-
const doc = field.ownerDocument;
|
|
840
|
-
field.focus();
|
|
841
|
-
const start = field.selectionStart;
|
|
842
|
-
const end = field.selectionEnd;
|
|
843
|
-
try {
|
|
844
|
-
if (start !== null && end !== null) field.setSelectionRange(start, end);
|
|
845
|
-
if (doc.activeElement === field && doc.execCommand("insertText", false, value)) return true;
|
|
846
|
-
} catch {}
|
|
847
|
-
const from = start ?? field.value.length;
|
|
848
|
-
const to = end ?? from;
|
|
849
|
-
let insert = value;
|
|
850
|
-
if (field.maxLength >= 0) {
|
|
851
|
-
const room = Math.max(0, field.maxLength - (field.value.length - (to - from)));
|
|
852
|
-
insert = insert.slice(0, room);
|
|
853
|
-
if (/[\ud800-\udbff]$/.test(insert)) insert = insert.slice(0, -1);
|
|
854
|
-
}
|
|
855
|
-
if (start === null) setNativeValue(field, field.value + insert);
|
|
856
|
-
else field.setRangeText(insert, from, to, "end");
|
|
857
|
-
const view = doc.defaultView;
|
|
858
|
-
const event = view !== null && typeof view.InputEvent === "function" ? new view.InputEvent("input", {
|
|
859
|
-
bubbles: true,
|
|
860
|
-
inputType: "insertFromPaste",
|
|
861
|
-
data: insert
|
|
862
|
-
}) : new Event("input", { bubbles: true });
|
|
863
|
-
field.dispatchEvent(event);
|
|
864
|
-
return true;
|
|
865
|
-
}
|
|
866
|
-
/**
|
|
867
|
-
* Sets a field's value through the prototype's setter. React tracks the value
|
|
868
|
-
* set on the element itself; going around it lets React see the change and
|
|
869
|
-
* call `onChange` for the `input` event that follows.
|
|
870
|
-
*/
|
|
871
|
-
function setNativeValue(field, next) {
|
|
872
|
-
const setter = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(field), "value")?.set;
|
|
873
|
-
if (setter) setter.call(field, next);
|
|
874
|
-
else field.value = next;
|
|
875
|
-
}
|
|
876
|
-
//#endregion
|
|
1
|
+
|
|
2
|
+
import { n as copyFailure } from "./chunks/errors-9h8sdFFX.js";
|
|
3
|
+
import { n as insertText, t as asTextField } from "./chunks/text-field-DUvrMMaK.js";
|
|
4
|
+
import { a as toColor, c as inGamut, i as formatColor, l as toGamut, n as contrastRatio, o as parseColor, r as COLOR_FORMATS, t as contrastLevel } from "./chunks/contrast-DpK2JIVu.js";
|
|
877
5
|
//#region src/color/payload.ts
|
|
878
6
|
/** The sRGB hex of a colour for the swatch's inline style: always generated here, never user text. */
|
|
879
7
|
const swatchColor = (c) => formatColor(toGamut(c), "hex");
|