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