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
package/dist/rich.cjs CHANGED
@@ -1,139 +1,8 @@
1
+
1
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/core/errors.ts
3
- function createCopyError(type, message, cause) {
4
- return cause === void 0 ? {
5
- type,
6
- message
7
- } : {
8
- type,
9
- message,
10
- cause
11
- };
12
- }
13
- /**
14
- * Branded (not `instanceof`) so failures stay recognisable even when the core
15
- * is bundled twice, e.g. `react-smart-copy` and `react-smart-copy/core` together.
16
- */
17
- const COPY_FAILURE_BRAND = Symbol.for("react-smart-copy.CopyFailure");
18
- /** Error thrown by adapters to carry a fully classified {@link CopyError}. */
19
- var CopyFailure = class extends Error {
20
- constructor(copyError) {
21
- super(copyError.message);
22
- this[COPY_FAILURE_BRAND] = true;
23
- this.name = "CopyFailure";
24
- this.copyError = copyError;
25
- }
26
- };
27
- /** Shorthand for throwing a classified failure. */
28
- function copyFailure(type, message, cause) {
29
- return new CopyFailure(createCopyError(type, message, cause));
30
- }
31
- //#endregion
32
- //#region src/link/url.ts
33
- /** Schemes a copied or pasted link may use by default. Never `javascript:`, `data:`, `vbscript:` or `file:`. */
34
- const DEFAULT_LINK_PROTOCOLS = /* @__PURE__ */ Object.freeze([
35
- "https:",
36
- "http:",
37
- "mailto:",
38
- "tel:"
39
- ]);
40
- /**
41
- * Query parameters that only track where a click came from: the defaults of
42
- * {@link cleanLinkUrl}. `utm_` matches every parameter starting with it.
43
- */
44
- const TRACKING_PARAMETERS = /* @__PURE__ */ Object.freeze([
45
- "utm_",
46
- "fbclid",
47
- "gclid",
48
- "gclsrc",
49
- "dclid",
50
- "gbraid",
51
- "wbraid",
52
- "msclkid",
53
- "yclid",
54
- "twclid",
55
- "ttclid",
56
- "li_fat_id",
57
- "igshid",
58
- "igsh",
59
- "mc_cid",
60
- "mc_eid",
61
- "_hsenc",
62
- "_hsmi",
63
- "mkt_tok",
64
- "oly_anon_id",
65
- "oly_enc_id",
66
- "vero_id",
67
- "wickedid",
68
- "_gl"
69
- ]);
70
- const invalid = (message) => copyFailure("invalid-payload", message);
71
- function baseUrl(base) {
72
- if (base !== void 0) {
73
- if (typeof base === "string" || base instanceof URL) return String(base);
74
- throw invalid("`base` must be a string or a URL.");
75
- }
76
- return typeof location === "undefined" ? void 0 : location.href;
77
- }
78
- function protocolSet(protocols) {
79
- const list = protocols ?? DEFAULT_LINK_PROTOCOLS;
80
- const valid = (protocol) => typeof protocol === "string" && /^[a-z][a-z\d+.-]*:$/i.test(protocol);
81
- if (!Array.isArray(list) || list.length === 0 || !list.every(valid)) throw invalid("`protocols` must be a non-empty list of schemes with their colon, like \"https:\".");
82
- return new Set(list.map((protocol) => protocol.toLowerCase()));
83
- }
84
- function matcher(params) {
85
- const list = params ?? TRACKING_PARAMETERS;
86
- if (!Array.isArray(list) || !list.every((param) => typeof param === "string" || param instanceof RegExp)) throw invalid("`clean.params` must be a list of strings and regular expressions.");
87
- return (name) => {
88
- const lower = name.toLowerCase();
89
- return list.some((param) => {
90
- if (typeof param !== "string") return new RegExp(param.source, param.flags.replace(/[gy]/g, "")).test(name);
91
- const wanted = param.toLowerCase();
92
- return wanted.endsWith("_") ? lower.startsWith(wanted) : lower === wanted;
93
- });
94
- };
95
- }
96
- /** The cleaning step `clean` asks for, or `null`. Throws for an invalid option. */
97
- function cleaner(option) {
98
- const cleaning = option ?? false;
99
- if (cleaning === false) return null;
100
- if (cleaning !== true && (typeof cleaning !== "object" || cleaning === null)) throw invalid("`clean` must be a boolean or an options object.");
101
- const settings = cleaning === true ? {} : cleaning;
102
- const tracking = matcher(settings.params);
103
- const keep = settings.keep ?? [];
104
- if (!Array.isArray(keep) || !keep.every((name) => typeof name === "string")) throw invalid("`clean.keep` must be a list of strings.");
105
- const kept = new Set(keep.map((name) => name.toLowerCase()));
106
- return (url) => {
107
- for (const name of [...new Set(url.searchParams.keys())]) if (tracking(name) && !kept.has(name.toLowerCase())) url.searchParams.delete(name);
108
- };
109
- }
110
- /**
111
- * Validates `options` once (throwing a typed `invalid-payload` error for invalid
112
- * ones) and returns a resolver that throws only for invalid links.
113
- * @internal
114
- */
115
- function linkUrlResolver(options = {}) {
116
- const protocols = protocolSet(options.protocols);
117
- const base = baseUrl(options.base);
118
- const clean = cleaner(options.clean);
119
- return (input) => {
120
- if (typeof input !== "string" && !(input instanceof URL)) throw invalid("A link must be a string or a URL.");
121
- const raw = String(input).trim();
122
- if (raw === "") throw invalid("A link must not be empty.");
123
- let url;
124
- try {
125
- url = new URL(raw, base);
126
- } catch (cause) {
127
- throw copyFailure("invalid-payload", `Not a valid link: ${JSON.stringify(raw.slice(0, 200))}.`, cause);
128
- }
129
- if (!protocols.has(url.protocol)) throw invalid(`Links with the scheme "${url.protocol}" are not allowed (allowed: ${[...protocols].join(", ")}).`);
130
- url.username = "";
131
- url.password = "";
132
- clean?.(url);
133
- return url.href;
134
- };
135
- }
136
- //#endregion
3
+ const require_errors = require("./chunks/errors-BwOF6T_M.cjs");
4
+ const require_url = require("./chunks/url-D4PGsRm8.cjs");
5
+ const require_contrast = require("./chunks/contrast-C6CVklEI.cjs");
137
6
  //#region src/rich/policy.ts
138
7
  /**
139
8
  * What portable rich text may contain: the elements, attributes and CSS that Gmail,
@@ -453,783 +322,6 @@ function attributeAllowed(tag, name, value) {
453
322
  }
454
323
  }
455
324
  //#endregion
456
- //#region src/color/convert.ts
457
- const color = (r, g, b, alpha = 1) => Object.freeze({
458
- r,
459
- g,
460
- b,
461
- alpha
462
- });
463
- const mul = (m, [x, y, z]) => [
464
- m[0][0] * x + m[0][1] * y + m[0][2] * z,
465
- m[1][0] * x + m[1][1] * y + m[1][2] * z,
466
- m[2][0] * x + m[2][1] * y + m[2][2] * z
467
- ];
468
- /** The sRGB (and Display P3) transfer function, extended to negative values by symmetry. */
469
- const toLinear = (c) => {
470
- const abs = Math.abs(c);
471
- return abs <= .04045 ? c / 12.92 : Math.sign(c) * ((abs + .055) / 1.055) ** 2.4;
472
- };
473
- const fromLinear = (c) => {
474
- const abs = Math.abs(c);
475
- return abs > .0031308 ? Math.sign(c) * (1.055 * abs ** (1 / 2.4) - .055) : 12.92 * c;
476
- };
477
- /** Applies `f` to each component; the result stays a three-component vector. */
478
- const map3 = ([x, y, z], f) => [
479
- f(x, 0),
480
- f(y, 1),
481
- f(z, 2)
482
- ];
483
- /** Three numbers as a vector, or null when one is missing. */
484
- const vec3 = ([x, y, z]) => typeof x === "number" && typeof y === "number" && typeof z === "number" ? [
485
- x,
486
- y,
487
- z
488
- ] : null;
489
- const linearRgb = (c) => [
490
- toLinear(c.r),
491
- toLinear(c.g),
492
- toLinear(c.b)
493
- ];
494
- const fromLinearRgb = ([r, g, b], alpha) => color(fromLinear(r), fromLinear(g), fromLinear(b), alpha);
495
- const SRGB_TO_XYZ = [
496
- [
497
- .41239079926595934,
498
- .357584339383878,
499
- .1804807884018343
500
- ],
501
- [
502
- .21263900587151027,
503
- .715168678767756,
504
- .07219231536073371
505
- ],
506
- [
507
- .01933081871559182,
508
- .11919477979462598,
509
- .9505321522496607
510
- ]
511
- ];
512
- const XYZ_TO_SRGB = [
513
- [
514
- 3.2409699419045226,
515
- -1.537383177570094,
516
- -.4986107602930034
517
- ],
518
- [
519
- -.9692436362808796,
520
- 1.8759675015077202,
521
- .04155505740717559
522
- ],
523
- [
524
- .05563007969699366,
525
- -.20397695888897652,
526
- 1.0569715142428786
527
- ]
528
- ];
529
- const P3_TO_XYZ = [
530
- [
531
- .4865709486482162,
532
- .26566769316909306,
533
- .1982172852343625
534
- ],
535
- [
536
- .2289745640697488,
537
- .6917385218365064,
538
- .079286914093745
539
- ],
540
- [
541
- 0,
542
- .04511338185890264,
543
- 1.043944368900976
544
- ]
545
- ];
546
- const XYZ_TO_P3 = [
547
- [
548
- 2.493496911941425,
549
- -.9313836179191239,
550
- -.40271078445071684
551
- ],
552
- [
553
- -.8294889695615747,
554
- 1.7626640603183463,
555
- .023624685841943577
556
- ],
557
- [
558
- .03584583024378447,
559
- -.07617238926804182,
560
- .9568845240076872
561
- ]
562
- ];
563
- /** Bradford chromatic adaptation between the D65 and D50 white points (Lab and LCH use D50). */
564
- const D65_TO_D50 = [
565
- [
566
- 1.0479297925449969,
567
- .022946870601609652,
568
- -.05019226628920524
569
- ],
570
- [
571
- .02962780877005599,
572
- .9904344267538799,
573
- -.017073799063418826
574
- ],
575
- [
576
- -.009243040646204504,
577
- .015055191490298152,
578
- .7518742814281371
579
- ]
580
- ];
581
- const D50_TO_D65 = [
582
- [
583
- .955473421488075,
584
- -.02309845494876471,
585
- .06325924320057072
586
- ],
587
- [
588
- -.0283697093338637,
589
- 1.0099953980813041,
590
- .021041441191917323
591
- ],
592
- [
593
- .012314014864481998,
594
- -.020507649298898964,
595
- 1.330365926242124
596
- ]
597
- ];
598
- /** Linear sRGB to OKLab's cone responses, and back. */
599
- const LRGB_TO_LMS = [
600
- [
601
- .412221469470763,
602
- .5363325372617348,
603
- .0514459932675022
604
- ],
605
- [
606
- .2119034958178252,
607
- .6806995506452344,
608
- .1073969535369406
609
- ],
610
- [
611
- .0883024591900564,
612
- .2817188391361215,
613
- .6299787016738222
614
- ]
615
- ];
616
- const LMS_TO_LRGB = [
617
- [
618
- 4.076741636075957,
619
- -3.3077115392580616,
620
- .2309699031821044
621
- ],
622
- [
623
- -1.2684379732850317,
624
- 2.6097573492876887,
625
- -.3413193760026573
626
- ],
627
- [
628
- -.0041960761386756,
629
- -.7034186179359362,
630
- 1.7076146940746117
631
- ]
632
- ];
633
- const LMS_TO_OKLAB = [
634
- [
635
- .210454268309314,
636
- .7936177747023054,
637
- -.0040720430116193
638
- ],
639
- [
640
- 1.9779985324311684,
641
- -2.42859224204858,
642
- .450593709617411
643
- ],
644
- [
645
- .0259040424655478,
646
- .7827717124575296,
647
- -.8086757549230774
648
- ]
649
- ];
650
- const OKLAB_TO_LMS = [
651
- [
652
- 1,
653
- .3963377773761749,
654
- .2158037573099136
655
- ],
656
- [
657
- 1,
658
- -.1055613458156586,
659
- -.0638541728258133
660
- ],
661
- [
662
- 1,
663
- -.0894841775298119,
664
- -1.2914855480194092
665
- ]
666
- ];
667
- const toXyz = (c) => mul(SRGB_TO_XYZ, linearRgb(c));
668
- const fromXyz = (xyz, alpha) => fromLinearRgb(mul(XYZ_TO_SRGB, xyz), alpha);
669
- /** Display P3 channels (gamma-encoded, 0 to 1 inside P3). */
670
- const toP3 = (c) => map3(mul(XYZ_TO_P3, toXyz(c)), fromLinear);
671
- const fromP3 = (p3, alpha) => fromXyz(mul(P3_TO_XYZ, map3(p3, toLinear)), alpha);
672
- const fromXyzD50 = (xyz, alpha) => fromXyz(mul(D50_TO_D65, xyz), alpha);
673
- /** OKLab: L in [0, 1], a and b around ±0.4. */
674
- const toOklab = (c) => mul(LMS_TO_OKLAB, map3(mul(LRGB_TO_LMS, linearRgb(c)), Math.cbrt));
675
- const fromOklab = (lab, alpha) => fromLinearRgb(mul(LMS_TO_LRGB, map3(mul(OKLAB_TO_LMS, lab), (v) => v ** 3)), alpha);
676
- const D50_WHITE = [
677
- .3457 / .3585,
678
- 1,
679
- .2958 / .3585
680
- ];
681
- const KAPPA = 24389 / 27;
682
- const EPSILON = 216 / 24389;
683
- /** CIE Lab (D50): L in [0, 100], a and b around ±125. */
684
- const toLab = (c) => {
685
- const [x, y, z] = map3(mul(D65_TO_D50, toXyz(c)), (v, i) => v / D50_WHITE[i]);
686
- const f = (v) => v > EPSILON ? Math.cbrt(v) : (KAPPA * v + 16) / 116;
687
- const [fx, fy, fz] = [
688
- f(x),
689
- f(y),
690
- f(z)
691
- ];
692
- return [
693
- 116 * fy - 16,
694
- 500 * (fx - fy),
695
- 200 * (fy - fz)
696
- ];
697
- };
698
- const fromLab = ([l, a, b], alpha) => {
699
- const fy = (l + 16) / 116;
700
- const fx = a / 500 + fy;
701
- const fz = fy - b / 200;
702
- const xyz = [
703
- fx ** 3 > EPSILON ? fx ** 3 : (116 * fx - 16) / KAPPA,
704
- l > KAPPA * EPSILON ? fy ** 3 : l / KAPPA,
705
- fz ** 3 > EPSILON ? fz ** 3 : (116 * fz - 16) / KAPPA
706
- ];
707
- return fromXyzD50(map3(xyz, (v, i) => v * D50_WHITE[i]), alpha);
708
- };
709
- /** Lab-like to its polar form: lightness, chroma, and hue in degrees from 0 to 360. */
710
- const toPolar = ([l, a, b]) => [
711
- l,
712
- Math.hypot(a, b),
713
- (Math.atan2(b, a) * 180 / Math.PI + 360) % 360
714
- ];
715
- const fromPolar = ([l, c, h]) => [
716
- l,
717
- c * Math.cos(h * Math.PI / 180),
718
- c * Math.sin(h * Math.PI / 180)
719
- ];
720
- const clip01 = (v) => Math.min(1, Math.max(0, v));
721
- /**
722
- * Below this spread between channels a colour is grey: what is left is rounding error from
723
- * converting it (from Lab, OKLCH, P3...), and its hue and saturation would be noise. The
724
- * faintest real tint in 8-bit sRGB, such as #fffffe, has a spread of about 0.004.
725
- */
726
- const GREY = 1e-9;
727
- /**
728
- * Hue, saturation and lightness of a colour inside sRGB (hue 0 for greys), with s and l in [0, 1].
729
- * Channels are clipped first: colours count as inside sRGB within rounding error, a little past 0 or 1.
730
- */
731
- function toHsl(c) {
732
- const [r, g, b] = map3([
733
- c.r,
734
- c.g,
735
- c.b
736
- ], clip01);
737
- const max = Math.max(r, g, b);
738
- const min = Math.min(r, g, b);
739
- const l = (max + min) / 2;
740
- const d = max - min;
741
- if (d < GREY) return [
742
- 0,
743
- 0,
744
- l
745
- ];
746
- const s = (max - l) / Math.min(l, 1 - l);
747
- let h;
748
- if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
749
- else if (max === g) h = (b - r) / d + 2;
750
- else h = (r - g) / d + 4;
751
- return [
752
- h * 60,
753
- s,
754
- l
755
- ];
756
- }
757
- function fromHsl(h, s, l, alpha) {
758
- const hue = (h % 360 + 360) % 360;
759
- const f = (n) => {
760
- const k = (n + hue / 30) % 12;
761
- return l - s * Math.min(l, 1 - l) * Math.max(-1, Math.min(k - 3, 9 - k, 1));
762
- };
763
- return color(f(0), f(8), f(4), alpha);
764
- }
765
- function fromHwb(h, w, b, alpha) {
766
- if (w + b >= 1) {
767
- const grey = w / (w + b);
768
- return color(grey, grey, grey, alpha);
769
- }
770
- const pure = fromHsl(h, 1, .5, 1);
771
- const scale = (v) => v * (1 - w - b) + w;
772
- return color(scale(pure.r), scale(pure.g), scale(pure.b), alpha);
773
- }
774
- /** Hue, whiteness and blackness of an sRGB colour, with w and b in [0, 1] (channels clipped, as in `toHsl`). */
775
- function toHwb(c) {
776
- const [h] = toHsl(c);
777
- const [r, g, b] = map3([
778
- c.r,
779
- c.g,
780
- c.b
781
- ], clip01);
782
- return [
783
- h,
784
- Math.min(r, g, b),
785
- 1 - Math.max(r, g, b)
786
- ];
787
- }
788
- /** Distance in OKLab: about 0.02 is the smallest difference people notice. */
789
- const deltaEOK = (a, b) => {
790
- const [l1, a1, b1] = toOklab(a);
791
- const [l2, a2, b2] = toOklab(b);
792
- return Math.hypot(l1 - l2, a1 - a2, b1 - b2);
793
- };
794
- const channels = (c, gamut) => gamut === "srgb" ? [
795
- c.r,
796
- c.g,
797
- c.b
798
- ] : toP3(c);
799
- const clipTo = (c, gamut) => {
800
- const [x, y, z] = map3(channels(c, gamut), clip01);
801
- return gamut === "srgb" ? color(x, y, z, c.alpha) : fromP3([
802
- x,
803
- y,
804
- z
805
- ], c.alpha);
806
- };
807
- /** Whether a colour is inside a gamut, within rounding error. */
808
- function inGamut(c, gamut = "srgb") {
809
- return channels(c, gamut).every((v) => v >= -1e-6 && v <= 1 + 1e-6);
810
- }
811
- /**
812
- * The colour inside `gamut` that looks closest: the CSS Color 4 gamut mapping
813
- * algorithm, which keeps lightness and hue and reduces chroma in OKLCH until
814
- * clipping is no longer visible (`"map"`), or plain clipping of each channel (`"clip"`).
815
- */
816
- function toGamut(c, gamut = "srgb", method = "map") {
817
- if (inGamut(c, gamut)) return clipTo(c, gamut);
818
- if (method === "clip") return clipTo(c, gamut);
819
- const [l, chroma, hue] = toPolar(toOklab(c));
820
- if (l >= 1 - 1e-7) return clipTo(color(1, 1, 1, c.alpha), gamut);
821
- if (l <= 1e-7) return clipTo(color(0, 0, 0, c.alpha), gamut);
822
- const JND = .02;
823
- let low = 0;
824
- let high = chroma;
825
- let lowInGamut = true;
826
- let current = c;
827
- let clipped = clipTo(current, gamut);
828
- if (deltaEOK(clipped, current) < JND) return clipped;
829
- while (high - low > 1e-4) {
830
- const mid = (low + high) / 2;
831
- current = fromOklab(fromPolar([
832
- l,
833
- mid,
834
- hue
835
- ]), c.alpha);
836
- if (lowInGamut && inGamut(current, gamut)) {
837
- low = mid;
838
- continue;
839
- }
840
- clipped = clipTo(current, gamut);
841
- const e = deltaEOK(clipped, current);
842
- if (e < JND) {
843
- if (JND - e < 1e-4) return clipped;
844
- lowInGamut = false;
845
- low = mid;
846
- } else high = mid;
847
- }
848
- return clipped;
849
- }
850
- //#endregion
851
- //#region src/color/names.ts
852
- /**
853
- * The 148 named colours of CSS Color 4, as "name rrggbb" pairs. Parsed into a map
854
- * the first time a name is looked up, so the table costs nothing until then.
855
- */
856
- const NAMED = [
857
- "aliceblue f0f8ff antiquewhite faebd7 aqua 00ffff aquamarine 7fffd4 azure f0ffff beige f5f5dc bisque ffe4c4",
858
- "black 000000 blanchedalmond ffebcd blue 0000ff blueviolet 8a2be2 brown a52a2a burlywood deb887",
859
- "cadetblue 5f9ea0 chartreuse 7fff00 chocolate d2691e coral ff7f50 cornflowerblue 6495ed cornsilk fff8dc",
860
- "crimson dc143c cyan 00ffff darkblue 00008b darkcyan 008b8b darkgoldenrod b8860b darkgray a9a9a9",
861
- "darkgreen 006400 darkgrey a9a9a9 darkkhaki bdb76b darkmagenta 8b008b darkolivegreen 556b2f darkorange ff8c00",
862
- "darkorchid 9932cc darkred 8b0000 darksalmon e9967a darkseagreen 8fbc8f darkslateblue 483d8b",
863
- "darkslategray 2f4f4f darkslategrey 2f4f4f darkturquoise 00ced1 darkviolet 9400d3 deeppink ff1493",
864
- "deepskyblue 00bfff dimgray 696969 dimgrey 696969 dodgerblue 1e90ff firebrick b22222 floralwhite fffaf0",
865
- "forestgreen 228b22 fuchsia ff00ff gainsboro dcdcdc ghostwhite f8f8ff gold ffd700 goldenrod daa520 gray 808080",
866
- "green 008000 greenyellow adff2f grey 808080 honeydew f0fff0 hotpink ff69b4 indianred cd5c5c indigo 4b0082",
867
- "ivory fffff0 khaki f0e68c lavender e6e6fa lavenderblush fff0f5 lawngreen 7cfc00 lemonchiffon fffacd",
868
- "lightblue add8e6 lightcoral f08080 lightcyan e0ffff lightgoldenrodyellow fafad2 lightgray d3d3d3",
869
- "lightgreen 90ee90 lightgrey d3d3d3 lightpink ffb6c1 lightsalmon ffa07a lightseagreen 20b2aa",
870
- "lightskyblue 87cefa lightslategray 778899 lightslategrey 778899 lightsteelblue b0c4de lightyellow ffffe0",
871
- "lime 00ff00 limegreen 32cd32 linen faf0e6 magenta ff00ff maroon 800000 mediumaquamarine 66cdaa",
872
- "mediumblue 0000cd mediumorchid ba55d3 mediumpurple 9370db mediumseagreen 3cb371 mediumslateblue 7b68ee",
873
- "mediumspringgreen 00fa9a mediumturquoise 48d1cc mediumvioletred c71585 midnightblue 191970 mintcream f5fffa",
874
- "mistyrose ffe4e1 moccasin ffe4b5 navajowhite ffdead navy 000080 oldlace fdf5e6 olive 808000 olivedrab 6b8e23",
875
- "orange ffa500 orangered ff4500 orchid da70d6 palegoldenrod eee8aa palegreen 98fb98 paleturquoise afeeee",
876
- "palevioletred db7093 papayawhip ffefd5 peachpuff ffdab9 peru cd853f pink ffc0cb plum dda0dd powderblue b0e0e6",
877
- "purple 800080 rebeccapurple 663399 red ff0000 rosybrown bc8f8f royalblue 4169e1 saddlebrown 8b4513",
878
- "salmon fa8072 sandybrown f4a460 seagreen 2e8b57 seashell fff5ee sienna a0522d silver c0c0c0 skyblue 87ceeb",
879
- "slateblue 6a5acd slategray 708090 slategrey 708090 snow fffafa springgreen 00ff7f steelblue 4682b4 tan d2b48c",
880
- "teal 008080 thistle d8bfd8 tomato ff6347 turquoise 40e0d0 violet ee82ee wheat f5deb3 white ffffff",
881
- "whitesmoke f5f5f5 yellow ffff00 yellowgreen 9acd32"
882
- ].join(" ");
883
- let table;
884
- /** The colour for a CSS colour name (lowercase), as 0xrrggbb, or `undefined`. */
885
- function namedColor(name) {
886
- if (table === void 0) {
887
- table = /* @__PURE__ */ new Map();
888
- const parts = NAMED.split(" ");
889
- for (let i = 0; i < parts.length; i += 2) table.set(parts[i], parseInt(parts[i + 1], 16));
890
- }
891
- return table.get(name);
892
- }
893
- //#endregion
894
- //#region src/color/parse.ts
895
- /** Longer input is not a colour: parsing stays cheap whatever is pasted. */
896
- const MAX_LENGTH = 256;
897
- const NUMBER = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/;
898
- const HEX = /^#([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/;
899
- const FUNCTION = /^([a-z][a-z\d-]*)\(([^()]*)\)$/;
900
- const ANGLE_UNITS = {
901
- deg: 1,
902
- grad: .9,
903
- rad: 180 / Math.PI,
904
- turn: 360
905
- };
906
- /** One component: a number, a percentage, an angle (in degrees) or `none`. */
907
- function token(text) {
908
- if (text === "none") return {
909
- kind: "none",
910
- value: 0
911
- };
912
- if (text.endsWith("%")) {
913
- const body = text.slice(0, -1);
914
- return NUMBER.test(body) ? {
915
- kind: "percent",
916
- value: Number(body)
917
- } : null;
918
- }
919
- const unit = /[a-z]+$/.exec(text)?.[0];
920
- if (unit !== void 0) {
921
- const factor = ANGLE_UNITS[unit];
922
- const body = text.slice(0, -unit.length);
923
- return factor === void 0 || !NUMBER.test(body) ? null : {
924
- kind: "angle",
925
- value: Number(body) * factor
926
- };
927
- }
928
- return NUMBER.test(text) ? {
929
- kind: "number",
930
- value: Number(text)
931
- } : null;
932
- }
933
- const clamp = (v, min, max) => Math.min(max, Math.max(min, v));
934
- /** A component's value: numbers as they are, percentages scaled so 100% is `full`. */
935
- function amount(t, full, allowed) {
936
- if (t.kind === "none") return 0;
937
- if (!allowed.includes(t.kind)) return null;
938
- return t.kind === "percent" ? t.value / 100 * full : t.value;
939
- }
940
- const hueOf = (t) => amount(t, 0, ["number", "angle"]);
941
- const alphaOf = (t) => {
942
- if (t === void 0) return 1;
943
- const value = amount(t, 1, ["number", "percent"]);
944
- return value === null ? null : clamp(value, 0, 1);
945
- };
946
- /** Splits arguments into three components and an optional alpha; `legacy` for the comma syntax. */
947
- function split(args) {
948
- const legacy = args.includes(",");
949
- let main;
950
- let alphaText;
951
- if (legacy) {
952
- const list = args.split(",").map((part) => part.trim());
953
- if (list.length < 3 || list.length > 4) return null;
954
- main = list.slice(0, 3);
955
- alphaText = list[3];
956
- } else {
957
- const [body, rest, extra] = args.split("/");
958
- if (extra !== void 0) return null;
959
- main = body.trim().split(/\s+/);
960
- alphaText = rest?.trim();
961
- if (main.length !== 3 || alphaText === "") return null;
962
- }
963
- const parts = main.map(token);
964
- if (parts.some((part) => part === null)) return null;
965
- const alpha = alphaText === void 0 ? void 0 : token(alphaText);
966
- if (alpha === null) return null;
967
- if (legacy && [...parts, alpha].some((part) => part?.kind === "none")) return null;
968
- return {
969
- parts,
970
- alpha,
971
- legacy
972
- };
973
- }
974
- function rgb(parts, legacy) {
975
- if (legacy && new Set(parts.map((part) => part.kind)).size !== 1) return null;
976
- const values = parts.map((part) => amount(part, 255, ["number", "percent"]));
977
- const vector = vec3(values);
978
- return vector === null ? null : map3(vector, (v) => clamp(v, 0, 255) / 255);
979
- }
980
- const PREDEFINED = {
981
- srgb: ([r, g, b], alpha) => color(r, g, b, alpha),
982
- "srgb-linear": fromLinearRgb,
983
- "display-p3": fromP3,
984
- xyz: fromXyz,
985
- "xyz-d65": fromXyz,
986
- "xyz-d50": fromXyzD50
987
- };
988
- /** `color(space c1 c2 c3 / alpha)`. */
989
- function predefined(args) {
990
- const match = /^([a-z\d-]+)\s+(.*)$/.exec(args.trim());
991
- if (match === null) return null;
992
- const convert = PREDEFINED[match[1]];
993
- const parsed = split(match[2]);
994
- if (convert === void 0 || parsed === null || parsed.legacy) return null;
995
- const values = parsed.parts.map((part) => amount(part, 1, ["number", "percent"]));
996
- const alpha = alphaOf(parsed.alpha);
997
- const vector = vec3(values);
998
- return vector === null || alpha === null ? null : convert(vector, alpha);
999
- }
1000
- function functional(name, args) {
1001
- if (name === "color") return predefined(args);
1002
- const parsed = split(args);
1003
- if (parsed === null) return null;
1004
- const { parts, legacy } = parsed;
1005
- const [a, b, c] = parts;
1006
- const alpha = alphaOf(parsed.alpha);
1007
- if (alpha === null) return null;
1008
- switch (name) {
1009
- case "rgb":
1010
- case "rgba": {
1011
- const values = rgb(parts, legacy);
1012
- return values === null ? null : color(values[0], values[1], values[2], alpha);
1013
- }
1014
- case "hsl":
1015
- case "hsla": {
1016
- const hue = hueOf(a);
1017
- const kinds = legacy ? ["percent"] : ["number", "percent"];
1018
- const s = amount(b, 100, kinds);
1019
- const l = amount(c, 100, kinds);
1020
- if (hue === null || s === null || l === null) return null;
1021
- return fromHsl(hue, clamp(s, 0, 100) / 100, clamp(l, 0, 100) / 100, alpha);
1022
- }
1023
- case "hwb": {
1024
- const hue = hueOf(a);
1025
- const w = amount(b, 100, ["number", "percent"]);
1026
- const k = amount(c, 100, ["number", "percent"]);
1027
- if (legacy || hue === null || w === null || k === null) return null;
1028
- return fromHwb(hue, clamp(w, 0, 100) / 100, clamp(k, 0, 100) / 100, alpha);
1029
- }
1030
- default: return legacy ? null : lab(name, a, b, c, alpha);
1031
- }
1032
- }
1033
- /** lab(), lch(), oklab() and oklch(). */
1034
- function lab(name, a, b, c, alpha) {
1035
- const ok = name.startsWith("ok");
1036
- const polar = name.endsWith("ch");
1037
- if (![
1038
- "lab",
1039
- "lch",
1040
- "oklab",
1041
- "oklch"
1042
- ].includes(name)) return null;
1043
- const lightMax = ok ? 1 : 100;
1044
- const l = amount(a, lightMax, ["number", "percent"]);
1045
- const second = amount(b, ok ? .4 : polar ? 150 : 125, ["number", "percent"]);
1046
- const third = polar ? hueOf(c) : amount(c, ok ? .4 : 125, ["number", "percent"]);
1047
- if (l === null || second === null || third === null) return null;
1048
- const lightness = clamp(l, 0, lightMax);
1049
- const values = polar ? fromPolar([
1050
- lightness,
1051
- Math.max(0, second),
1052
- third
1053
- ]) : [
1054
- lightness,
1055
- second,
1056
- third
1057
- ];
1058
- return ok ? fromOklab(values, alpha) : fromLab(values, alpha);
1059
- }
1060
- /**
1061
- * Parses a CSS colour, or returns `null`: hex (`#rgb`, `#rgba`, `#rrggbb`,
1062
- * `#rrggbbaa`), the 148 named colours and `transparent`, `rgb()` and `hsl()` in
1063
- * both syntaxes, `hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()`, and `color()`
1064
- * in `srgb`, `srgb-linear`, `display-p3`, `xyz`, `xyz-d65` and `xyz-d50`.
1065
- * Never throws. Colours outside sRGB keep their exact value.
1066
- *
1067
- * Not colours on their own: `currentcolor`, system colours, `var()`, `calc()`,
1068
- * `color-mix()` and relative colours.
1069
- *
1070
- * @example
1071
- * parseColor('oklch(62.8% 0.258 29.23)'); // { r: 1, g: 0, b: 0, alpha: 1 } (rounded)
1072
- */
1073
- function parseColor(input) {
1074
- if (typeof input !== "string" || input.length > MAX_LENGTH) return null;
1075
- const text = input.trim().toLowerCase();
1076
- const hex = HEX.exec(text)?.[1];
1077
- if (hex !== void 0) {
1078
- const full = hex.length <= 4 ? hex.replace(/./g, (digit) => digit + digit) : hex;
1079
- const byte = (i) => parseInt(full.slice(i, i + 2), 16) / 255;
1080
- return color(byte(0), byte(2), byte(4), full.length === 8 ? byte(6) : 1);
1081
- }
1082
- if (text === "transparent") return color(0, 0, 0, 0);
1083
- const named = namedColor(text);
1084
- if (named !== void 0) return color((named >> 16 & 255) / 255, (named >> 8 & 255) / 255, (named & 255) / 255);
1085
- const match = FUNCTION.exec(text);
1086
- return match === null ? null : functional(match[1], match[2].trim());
1087
- }
1088
- //#endregion
1089
- //#region src/color/format.ts
1090
- const COLOR_FORMATS = /* @__PURE__ */ Object.freeze([
1091
- "hex",
1092
- "rgb",
1093
- "hsl",
1094
- "hwb",
1095
- "oklch",
1096
- "oklab",
1097
- "lab",
1098
- "lch",
1099
- "p3"
1100
- ]);
1101
- const isColor = (value) => typeof value === "object" && value !== null && [
1102
- "r",
1103
- "g",
1104
- "b",
1105
- "alpha"
1106
- ].every((key) => Number.isFinite(value[key]));
1107
- /** Resolves a colour input, throwing a typed error for anything that is not one. */
1108
- function toColor(input) {
1109
- if (typeof input === "string") {
1110
- const parsed = parseColor(input);
1111
- if (parsed === null) throw copyFailure("invalid-payload", `Not a colour: ${JSON.stringify(input.slice(0, 100))}.`);
1112
- return parsed;
1113
- }
1114
- 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.");
1115
- return input;
1116
- }
1117
- /** Rounds to `places` decimals, without trailing zeros or `-0`. */
1118
- function num(value, places) {
1119
- const factor = 10 ** places;
1120
- const rounded = Math.round(value * factor) / factor;
1121
- return String(Object.is(rounded, -0) ? 0 : rounded);
1122
- }
1123
- /** A hue in degrees, with 360 written as 0. */
1124
- const hue = (value, places) => {
1125
- const text = num(value, places);
1126
- return text === "360" ? "0" : text;
1127
- };
1128
- const byte = (v) => Math.round(Math.min(1, Math.max(0, v)) * 255);
1129
- /** The alpha suffix of a functional notation: nothing when opaque. */
1130
- const alphaPart = (alpha, legacy, places) => alpha >= 1 ? "" : legacy ? `, ${num(alpha, places)}` : ` / ${num(alpha, places)}`;
1131
- /**
1132
- * Writes a colour in a CSS format. Text is parsed first, so this also converts
1133
- * between formats.
1134
- *
1135
- * @example
1136
- * formatColor('rgb(255 0 0)', 'oklch'); // 'oklch(62.8% 0.2577 29.23)'
1137
- * formatColor('color(display-p3 1 0 0)', 'hex'); // '#ff0000' after gamut mapping, never a wrong hue
1138
- */
1139
- function formatColor(input, format = "hex", options = {}) {
1140
- const c = toColor(input);
1141
- const { gamut = "map", legacy = false, uppercase = false, precision } = options;
1142
- const method = gamut;
1143
- if (method !== "map" && method !== "clip") throw copyFailure("invalid-payload", "`gamut` must be \"map\" or \"clip\".");
1144
- if (precision !== void 0 && !(Number.isInteger(precision) && precision >= 0 && precision <= 10)) throw copyFailure("invalid-payload", "`precision` must be an integer from 0 to 10.");
1145
- const dp = (fallback) => precision ?? fallback;
1146
- const a = c.alpha;
1147
- switch (format) {
1148
- case "hex": {
1149
- const s = toGamut(c, "srgb", gamut);
1150
- const digits = [
1151
- s.r,
1152
- s.g,
1153
- s.b,
1154
- ...a < 1 ? [a] : []
1155
- ].map((v) => byte(v).toString(16).padStart(2, "0")).join("");
1156
- return `#${uppercase ? digits.toUpperCase() : digits}`;
1157
- }
1158
- case "rgb": {
1159
- const s = toGamut(c, "srgb", gamut);
1160
- const [r, g, b] = [
1161
- s.r,
1162
- s.g,
1163
- s.b
1164
- ].map(byte);
1165
- 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))})`;
1166
- }
1167
- case "hsl": {
1168
- const [h, s, l] = toHsl(toGamut(c, "srgb", gamut));
1169
- const [hh, ss, ll] = [
1170
- hue(h, dp(2)),
1171
- `${num(s * 100, dp(2))}%`,
1172
- `${num(l * 100, dp(2))}%`
1173
- ];
1174
- return legacy ? `${a < 1 ? "hsla" : "hsl"}(${hh}, ${ss}, ${ll}${alphaPart(a, true, dp(3))})` : `hsl(${hh} ${ss} ${ll}${alphaPart(a, false, dp(3))})`;
1175
- }
1176
- case "hwb": {
1177
- const [h, w, b] = toHwb(toGamut(c, "srgb", gamut));
1178
- return `hwb(${hue(h, dp(2))} ${num(w * 100, dp(2))}% ${num(b * 100, dp(2))}%${alphaPart(a, false, dp(3))})`;
1179
- }
1180
- case "oklab": {
1181
- const [l, x, y] = toOklab(c);
1182
- return `oklab(${num(l * 100, dp(2))}% ${num(x, dp(5))} ${num(y, dp(5))}${alphaPart(a, false, dp(3))})`;
1183
- }
1184
- case "oklch": {
1185
- const [l, ch, h] = toPolar(toOklab(c));
1186
- const chroma = num(ch, dp(5));
1187
- return `oklch(${num(l * 100, dp(2))}% ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
1188
- }
1189
- case "lab": {
1190
- const [l, x, y] = toLab(c);
1191
- return `lab(${num(l, dp(2))} ${num(x, dp(2))} ${num(y, dp(2))}${alphaPart(a, false, dp(3))})`;
1192
- }
1193
- case "lch": {
1194
- const [l, ch, h] = toPolar(toLab(c));
1195
- const chroma = num(ch, dp(2));
1196
- return `lch(${num(l, dp(2))} ${chroma} ${chroma === "0" ? "0" : hue(h, dp(2))}${alphaPart(a, false, dp(3))})`;
1197
- }
1198
- case "p3": {
1199
- const [r, g, b] = toP3(toGamut(c, "p3", gamut));
1200
- return `color(display-p3 ${num(r, dp(4))} ${num(g, dp(4))} ${num(b, dp(4))}${alphaPart(a, false, dp(3))})`;
1201
- }
1202
- default: throw copyFailure("invalid-payload", `Unknown colour format ${JSON.stringify(String(format))}: use one of ${COLOR_FORMATS.join(", ")}.`);
1203
- }
1204
- }
1205
- //#endregion
1206
- //#region src/color/contrast.ts
1207
- const WHITE$1 = /* @__PURE__ */ color(1, 1, 1);
1208
- /** `top` drawn over `bottom`, both made opaque first. */
1209
- function over(top, bottom) {
1210
- const mix = (a, b) => a * top.alpha + b * (1 - top.alpha);
1211
- return color(mix(top.r, bottom.r), mix(top.g, bottom.g), mix(top.b, bottom.b));
1212
- }
1213
- /** WCAG 2 relative luminance of an opaque sRGB colour. */
1214
- function luminance(c) {
1215
- const lin = (v) => v <= .04045 ? v / 12.92 : ((v + .055) / 1.055) ** 2.4;
1216
- return .2126 * lin(c.r) + .7152 * lin(c.g) + .0722 * lin(c.b);
1217
- }
1218
- /**
1219
- * The WCAG 2 contrast ratio of text over a background, from 1 to 21. Colours are
1220
- * mapped into sRGB, a translucent background is drawn over white, and translucent
1221
- * text over the background, as a browser would show them. Never rounded: 4.499 is
1222
- * not 4.5.
1223
- *
1224
- * @example
1225
- * contrastRatio('#767676', '#fff'); // 4.54
1226
- */
1227
- function contrastRatio(text, background) {
1228
- const bg = over(toGamut(toColor(background)), WHITE$1);
1229
- const [light, dark] = [luminance(over(toGamut(toColor(text)), bg)), luminance(bg)].sort((a, b) => b - a);
1230
- return (light + .05) / (dark + .05);
1231
- }
1232
- //#endregion
1233
325
  //#region src/rich/style.ts
1234
326
  const INHERITED = [
1235
327
  "color",
@@ -1350,13 +442,13 @@ const align = (value) => {
1350
442
  };
1351
443
  /** A colour as an opaque hex value, drawn over `under`; `null` for none or invisible. */
1352
444
  function opaque(value, under) {
1353
- const c = parseColor(value);
445
+ const c = require_contrast.parseColor(value);
1354
446
  if (c === null || c.alpha === 0) return null;
1355
- const mapped = toGamut(c);
447
+ const mapped = require_contrast.toGamut(c);
1356
448
  const mix = (top, bottom) => top * c.alpha + bottom * (1 - c.alpha);
1357
- return color(mix(mapped.r, under.r), mix(mapped.g, under.g), mix(mapped.b, under.b));
449
+ return require_contrast.color(mix(mapped.r, under.r), mix(mapped.g, under.g), mix(mapped.b, under.b));
1358
450
  }
1359
- const WHITE = /* @__PURE__ */ color(1, 1, 1);
451
+ const WHITE = /* @__PURE__ */ require_contrast.color(1, 1, 1);
1360
452
  /** Pixel lengths rounded to 2 decimals; other units kept as written. */
1361
453
  function length(value) {
1362
454
  if (!LENGTH.test(value)) return null;
@@ -1376,7 +468,7 @@ function declarations(tag, values, parent, background, colors) {
1376
468
  const bg = opaque(values["background-color"], background);
1377
469
  if (bg !== null) {
1378
470
  own = bg;
1379
- out.push(`background-color:${formatColor(bg)}`);
471
+ out.push(`background-color:${require_contrast.formatColor(bg)}`);
1380
472
  }
1381
473
  }
1382
474
  const family = values["font-family"];
@@ -1403,8 +495,8 @@ function declarations(tag, values, parent, background, colors) {
1403
495
  if (colors !== "none" && changed("color", values["color"])) {
1404
496
  const text = opaque(values["color"], own);
1405
497
  if (text !== null) {
1406
- if (colors === "readable" && contrastRatio(text, own) < 3) droppedColor = true;
1407
- else out.push(`color:${formatColor(text)}`);
498
+ if (colors === "readable" && require_contrast.contrastRatio(text, own) < 3) droppedColor = true;
499
+ else out.push(`color:${require_contrast.formatColor(text)}`);
1408
500
  }
1409
501
  }
1410
502
  const decoration = values["text-decoration-line"];
@@ -1427,7 +519,7 @@ function box(values, background, colors) {
1427
519
  const style = values[`border-${side}-style`];
1428
520
  if (width === null || width === "0" || width === "0px" || !/^(?:solid|dashed|dotted|double)$/.test(style)) return null;
1429
521
  const lineColor = colors === "none" ? null : opaque(values[`border-${side}-color`], background);
1430
- return `${width} ${style}${lineColor === null ? "" : ` ${formatColor(lineColor)}`}`;
522
+ return `${width} ${style}${lineColor === null ? "" : ` ${require_contrast.formatColor(lineColor)}`}`;
1431
523
  });
1432
524
  const out = [];
1433
525
  if (borders.every((border) => border !== null && border === borders[0])) out.push(`border:${borders[0]}`);
@@ -1541,7 +633,7 @@ const DATA_IMAGE = /^data:image\/(?:png|jpeg|gif|webp);base64,[a-z\d+/]+=*$/i;
1541
633
  function positive(value, fallback, name) {
1542
634
  if (value === void 0) return fallback;
1543
635
  if (Number.isInteger(value) && value > 0) return value;
1544
- throw copyFailure("invalid-payload", `\`${name}\` must be a positive integer.`);
636
+ throw require_errors.copyFailure("invalid-payload", `\`${name}\` must be a positive integer.`);
1545
637
  }
1546
638
  /** Whether `source` holds an element that would close a paragraph around it. */
1547
639
  function holdsBlocks(source) {
@@ -1571,11 +663,11 @@ var Builder = class {
1571
663
  this.implied = /* @__PURE__ */ new WeakSet();
1572
664
  this.scheme = (raw) => /^\s*([a-z][a-z\d+.-]*):/i.exec(raw ?? "")?.[1]?.toLowerCase() ?? "relative";
1573
665
  const baseOption = base === void 0 ? {} : { base };
1574
- this.resolveLink = linkUrlResolver({
666
+ this.resolveLink = require_url.linkUrlResolver({
1575
667
  ...baseOption,
1576
668
  ...options.protocols === void 0 ? {} : { protocols: options.protocols }
1577
669
  });
1578
- this.resolveImage = linkUrlResolver({
670
+ this.resolveImage = require_url.linkUrlResolver({
1579
671
  ...baseOption,
1580
672
  protocols: options.dataImages === true ? [
1581
673
  "https:",
@@ -1770,7 +862,7 @@ var Builder = class {
1770
862
  /** The DOM document to read and write with, or a typed error outside a browser. */
1771
863
  function domFor(source) {
1772
864
  if (typeof source === "string") {
1773
- if (typeof DOMParser === "undefined") throw copyFailure("unsupported", "Reading HTML text needs a DOM (DOMParser); pass a DOM node, or use a browser or jsdom.");
865
+ if (typeof DOMParser === "undefined") throw require_errors.copyFailure("unsupported", "Reading HTML text needs a DOM (DOMParser); pass a DOM node, or use a browser or jsdom.");
1774
866
  const doc = new DOMParser().parseFromString(`<!doctype html><body>${source}`, "text/html");
1775
867
  return {
1776
868
  doc,
@@ -1782,7 +874,7 @@ function domFor(source) {
1782
874
  if (isRange(source)) {
1783
875
  const container = source.commonAncestorContainer;
1784
876
  const element = container.nodeType === 1 ? container : container.parentNode;
1785
- if (element === null || container.ownerDocument === null) throw copyFailure("invalid-payload", "The range is not in a document.");
877
+ if (element === null || container.ownerDocument === null) throw require_errors.copyFailure("invalid-payload", "The range is not in a document.");
1786
878
  return {
1787
879
  doc: container.ownerDocument,
1788
880
  root: element,
@@ -1791,7 +883,7 @@ function domFor(source) {
1791
883
  };
1792
884
  }
1793
885
  const value = source;
1794
- if (typeof value !== "object" || value === null || !("nodeType" in value) || value.nodeType !== 1 && value.nodeType !== 11) throw copyFailure("invalid-payload", "Rich content must be an element, a fragment, a range or HTML text.");
886
+ if (typeof value !== "object" || value === null || !("nodeType" in value) || value.nodeType !== 1 && value.nodeType !== 11) throw require_errors.copyFailure("invalid-payload", "Rich content must be an element, a fragment, a range or HTML text.");
1795
887
  const node = value;
1796
888
  return {
1797
889
  doc: node.ownerDocument,
@@ -1822,19 +914,19 @@ function prepareRich(source, options = {}) {
1822
914
  const { doc, root, range, live } = domFor(source);
1823
915
  const mode = options.styles ?? (live && doc.defaultView !== null ? "computed" : "inline");
1824
916
  const modeValue = mode;
1825
- if (modeValue !== "computed" && modeValue !== "inline" && modeValue !== "none") throw copyFailure("invalid-payload", "`styles` must be \"computed\", \"inline\" or \"none\".");
917
+ if (modeValue !== "computed" && modeValue !== "inline" && modeValue !== "none") throw require_errors.copyFailure("invalid-payload", "`styles` must be \"computed\", \"inline\" or \"none\".");
1826
918
  const colors = options.colors ?? "readable";
1827
919
  const colorsValue = colors;
1828
- if (colorsValue !== "readable" && colorsValue !== "keep" && colorsValue !== "none") throw copyFailure("invalid-payload", "`colors` must be \"readable\", \"keep\" or \"none\".");
920
+ if (colorsValue !== "readable" && colorsValue !== "keep" && colorsValue !== "none") throw require_errors.copyFailure("invalid-payload", "`colors` must be \"readable\", \"keep\" or \"none\".");
1829
921
  const images = options.images ?? "keep";
1830
- if (images !== "keep" && images !== "alt" && images !== "drop") throw copyFailure("invalid-payload", "`images` must be \"keep\", \"alt\" or \"drop\".");
922
+ if (images !== "keep" && images !== "alt" && images !== "drop") throw require_errors.copyFailure("invalid-payload", "`images` must be \"keep\", \"alt\" or \"drop\".");
1831
923
  const view = doc.defaultView;
1832
924
  const computed = options.computedStyle ?? ((element) => view === null ? null : view.getComputedStyle(element));
1833
925
  const page = options.background === void 0 ? WHITE : declarationsBackground(options.background);
1834
926
  if (options.ignore !== void 0) try {
1835
927
  doc.createElement("div").matches(options.ignore);
1836
928
  } catch (cause) {
1837
- throw copyFailure("invalid-payload", `\`ignore\` is not a valid CSS selector: ${options.ignore}`, cause);
929
+ throw require_errors.copyFailure("invalid-payload", `\`ignore\` is not a valid CSS selector: ${options.ignore}`, cause);
1838
930
  }
1839
931
  const out = doc.implementation.createHTMLDocument("");
1840
932
  const builder = new Builder(out, options, mode, computed, colors, options.ignore ?? null, positive(options.maxElements, 1e4, "maxElements"), positive(options.maxLength, 1e6, "maxLength"), options.base ?? (doc.baseURI === "about:blank" ? void 0 : doc.baseURI));
@@ -1843,7 +935,7 @@ function prepareRich(source, options = {}) {
1843
935
  else builder.node(root, container, ROOT_PARENT, page, range, true);
1844
936
  container.normalize();
1845
937
  const text = (options.text ?? plainText)(container);
1846
- if (typeof text !== "string") throw copyFailure("invalid-payload", "`text` must return a string.");
938
+ if (typeof text !== "string") throw require_errors.copyFailure("invalid-payload", "`text` must return a string.");
1847
939
  const first = container.firstChild;
1848
940
  if (first?.nodeType === 3) first.data = first.data.replace(/^\s+/, "");
1849
941
  const last = container.lastChild;
@@ -1858,8 +950,8 @@ function prepareRich(source, options = {}) {
1858
950
  }
1859
951
  /** The destination background as an opaque colour; a typed error for anything else. */
1860
952
  function declarationsBackground(value) {
1861
- const parsed = typeof value === "string" ? parseColor(value) : null;
1862
- if (parsed === null || parsed.alpha !== 1) throw copyFailure("invalid-payload", "`background` must be an opaque CSS colour.");
953
+ const parsed = typeof value === "string" ? require_contrast.parseColor(value) : null;
954
+ if (parsed === null || parsed.alpha !== 1) throw require_errors.copyFailure("invalid-payload", "`background` must be an opaque CSS colour.");
1863
955
  return parsed;
1864
956
  }
1865
957
  //#endregion
@@ -1874,7 +966,7 @@ function declarationsBackground(value) {
1874
966
  */
1875
967
  function richPayload(source, options = {}) {
1876
968
  const { html, text } = prepareRich(source, options);
1877
- if (html === "" || text === "") throw copyFailure("invalid-payload", "There is nothing to copy: the content is empty, hidden or entirely left out.");
969
+ if (html === "" || text === "") throw require_errors.copyFailure("invalid-payload", "There is nothing to copy: the content is empty, hidden or entirely left out.");
1878
970
  return {
1879
971
  kind: "html",
1880
972
  html,