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