unocss-preset-magicolor 0.3.0 → 0.4.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/dist/helper.js CHANGED
@@ -1,40 +1,80 @@
1
- import { mc as m } from "magic-color";
2
- import { i as h, t as C, a as g, c as d, g as u, r as b } from "./common-Dv0krm1V.js";
3
- function M(p) {
4
- const { name: c, color: a, dom: i } = p;
5
- if (!i)
6
- return;
7
- const s = a?.split(/-\d+-?/)[0];
8
- if (h(s))
9
- return;
10
- const e = {}, t = {};
11
- try {
12
- if (m.valid(s)) {
13
- e[`--mc-${c}-color`] = s;
14
- const o = m.theme(s, { type: "hex" });
15
- for (const r of C) {
16
- const n = g({ type: "hex", components: [o[r]], alpha: 1 });
17
- t[r] = n;
18
- }
19
- }
20
- } catch (o) {
21
- console.error(`[updateMagicColor] get ${s} theme fail, please use another color.`), console.error(o);
22
- }
23
- const l = a.match(/.*-(\d+)/)?.[1];
24
- if (l) {
25
- const { originDepth: o, beforeDepth: r, afterDepth: n } = b(l);
26
- t[r] && t[n] && (e[`--mc-${c}-color`] = d({
27
- originDepth: o,
28
- beforeDepth: r,
29
- beforeComponents: t[r].components,
30
- afterComponents: t[n].components
31
- }));
32
- }
33
- const f = u(c, t);
34
- Object.assign(e, f);
35
- for (const o in e)
36
- e[o] && i.style.setProperty(o, e[o].toString());
1
+ import { a as resolveColorParts, n as getThemeDepthColor, r as isInvalidColor, t as getMcThemeMetaColors } from "./utils-u9jMSN0V.js";
2
+ import { mc } from "magic-color";
3
+ //#region src/helper.ts
4
+ function escapeRegExp(value) {
5
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
37
6
  }
38
- export {
39
- M as updateMagicColor
40
- };
7
+ function collectStyleVariables(name, style, usage) {
8
+ const colorVariableRE = new RegExp(`^--mc-${escapeRegExp(name)}(?:-(\\d+))?-color$`);
9
+ for (let i = 0; i < style.length; i++) {
10
+ const match = style.item(i).match(colorVariableRE);
11
+ if (!match) continue;
12
+ const [, depth] = match;
13
+ if (depth) usage.depths.add(depth);
14
+ else usage.hasBase = true;
15
+ }
16
+ }
17
+ function collectDefinedColorVariables(name, dom) {
18
+ const usage = {
19
+ hasBase: false,
20
+ depths: /* @__PURE__ */ new Set()
21
+ };
22
+ const window = dom.ownerDocument.defaultView;
23
+ collectStyleVariables(name, dom.style, usage);
24
+ if (window) collectStyleVariables(name, window.getComputedStyle(dom), usage);
25
+ return usage;
26
+ }
27
+ function generateColorVariable(name, color, depth) {
28
+ return { [`--mc-${name}${depth == null ? "" : `-${depth}`}-color`]: color };
29
+ }
30
+ /**
31
+ * Generate CSS color variables for a magic color.
32
+ * @param params params Parameter object
33
+ * @param params.name Color name
34
+ * @param params.color Color value, supports a color depth suffix like `#9c1d1e-457`
35
+ * @param params.hasBase Whether to generate the base color variable
36
+ * @param params.depths Color depths that need depth-specific variables
37
+ * @returns CSS variables for the requested magic color usage
38
+ */
39
+ function getMagicColorStyle(params) {
40
+ const { name, color, hasBase, depths } = params;
41
+ const { originColor, bodyNo } = resolveColorParts(color);
42
+ if (isInvalidColor(originColor)) return {};
43
+ const themeMetaColors = getMcThemeMetaColors(originColor);
44
+ const css = {};
45
+ if (hasBase) {
46
+ if (bodyNo) {
47
+ const baseColor = getThemeDepthColor(themeMetaColors, bodyNo);
48
+ baseColor && Object.assign(css, generateColorVariable(name, baseColor));
49
+ } else if (mc.valid(originColor)) {
50
+ const baseColor = mc(originColor).css("oklch");
51
+ Object.assign(css, generateColorVariable(name, baseColor));
52
+ }
53
+ }
54
+ for (const depth of depths) {
55
+ const depthColor = getThemeDepthColor(themeMetaColors, depth);
56
+ if (depthColor) Object.assign(css, generateColorVariable(name, depthColor, depth));
57
+ }
58
+ return css;
59
+ }
60
+ /**
61
+ * Modify the value of the color variable
62
+ * @param params params Parameter object
63
+ * @param params.name Color name
64
+ * @param params.color Color
65
+ * @param params.dom params.dom Target element, modifying the entire page theme when passing `document.documentElement`
66
+ */
67
+ function updateMagicColor(params) {
68
+ const { name, color, dom } = params;
69
+ if (!dom) return;
70
+ const { hasBase, depths } = collectDefinedColorVariables(name, dom);
71
+ const css = getMagicColorStyle({
72
+ name,
73
+ color,
74
+ hasBase,
75
+ depths
76
+ });
77
+ for (const [variable, value] of Object.entries(css)) if (value) dom.style.setProperty(variable, value.toString());
78
+ }
79
+ //#endregion
80
+ export { getMagicColorStyle, updateMagicColor };