@noxlovette/material 0.8.1 → 0.8.2
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.
|
@@ -15,8 +15,24 @@ const CONTAINER_TOKENS = [
|
|
|
15
15
|
];
|
|
16
16
|
/** Used server-side, or if a token hasn't been painted yet (styles not loaded). */
|
|
17
17
|
const FALLBACK_PALETTE = ['e9ddff', 'e8def8', 'ffd9e3', 'ffdad6', 'e6e0e9'];
|
|
18
|
-
/**
|
|
19
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Converts a token's resolved value to a 6-digit hex. Dev serves the tokens as written in the
|
|
20
|
+
* theme CSS, `rgb(r g b)` (or `rgb(r, g, b)`), but a production build's CSS minifier rewrites
|
|
21
|
+
* them to hex (`#d1e5f3`, or the short `#fff`): pulling the numbers out of a hex string reads
|
|
22
|
+
* its digit runs as channels and yields a near-black colour, so hex is handled on its own.
|
|
23
|
+
*/
|
|
24
|
+
function colorToHex(value) {
|
|
25
|
+
const hex = value.match(/^#([0-9a-f]{3,8})$/i)?.[1];
|
|
26
|
+
if (hex) {
|
|
27
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
28
|
+
return [...hex.slice(0, 3)]
|
|
29
|
+
.map((c) => c + c)
|
|
30
|
+
.join('')
|
|
31
|
+
.toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
// 6 digits, or 8 with alpha (dropped: DiceBear takes opaque backgrounds).
|
|
34
|
+
return hex.length === 6 || hex.length === 8 ? hex.slice(0, 6).toLowerCase() : undefined;
|
|
35
|
+
}
|
|
20
36
|
const channels = value.match(/\d+(?:\.\d+)?/g);
|
|
21
37
|
if (!channels || channels.length < 3)
|
|
22
38
|
return undefined;
|
|
@@ -38,7 +54,7 @@ export function getMaterialAvatarPalette() {
|
|
|
38
54
|
const styles = getComputedStyle(document.documentElement);
|
|
39
55
|
const palette = CONTAINER_TOKENS.map((token) => {
|
|
40
56
|
const raw = styles.getPropertyValue(token).trim();
|
|
41
|
-
return raw ?
|
|
57
|
+
return raw ? colorToHex(raw) : undefined;
|
|
42
58
|
}).filter((hex) => Boolean(hex));
|
|
43
59
|
return palette.length > 0 ? palette : FALLBACK_PALETTE;
|
|
44
60
|
}
|