unocss-preset-magicolor 0.1.0 → 0.2.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.
@@ -0,0 +1,57 @@
1
+ import { converter as f } from "culori";
2
+ const h = f("oklch");
3
+ function r(t) {
4
+ return t && Number.parseFloat(t.toString()) || 0;
5
+ }
6
+ function b(t) {
7
+ if (!t)
8
+ return;
9
+ if (t.type === "oklch")
10
+ return t.components[0].toString().includes("%") && (t.components[0] = r(t.components[0]) / 100), t.components = t.components.map(r), t;
11
+ const n = t.components.join(" "), e = h(t.type === "hex" ? n : `${t.type}(${n})`);
12
+ if (e)
13
+ return {
14
+ type: "oklch",
15
+ components: [e.l, e.c, e.h ?? 0],
16
+ alpha: t.alpha
17
+ };
18
+ }
19
+ function i(t) {
20
+ return Math.round(t * 1e3) / 1e3;
21
+ }
22
+ const m = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
23
+ function k(t) {
24
+ let n = Number(t);
25
+ n = n <= 50 ? 50 : n, n = n >= 950 ? 950 : n;
26
+ let e = Math.floor(n / 100) * 100;
27
+ e = e <= 50 ? 50 : e;
28
+ let o = Math.floor((n + 100) / 100) * 100;
29
+ return o = o >= 950 ? 950 : o, { originDepth: n, beforeDepth: e, afterDepth: o };
30
+ }
31
+ function N(t) {
32
+ return !t || !Number.isNaN(Number(t)) ? (console.error(`[unocss-preset-margicolor] The color '${t}' is invalid.`), !0) : !1;
33
+ }
34
+ function D(t) {
35
+ const { originDepth: n, beforeDepth: e, beforeComponents: o, afterComponents: c } = t, p = (n - e) / (n < 100 || n > 900 ? 50 : 100);
36
+ return `oklch(${Array.from({ length: 3 }).map((s, u) => {
37
+ const a = r(o[u]) + (r(c[u]) - r(o[u])) * p;
38
+ return i(a);
39
+ }).join(" ")})`;
40
+ }
41
+ function d(t, n) {
42
+ const e = {};
43
+ for (const o of m)
44
+ if (n[o]) {
45
+ const c = n[o].components;
46
+ e[`--mc-${t}-${o}-l`] = i(r(c[0])), e[`--mc-${t}-${o}-c`] = i(r(c[1])), e[`--mc-${t}-${o}-h`] = i(r(c[2]));
47
+ }
48
+ return e;
49
+ }
50
+ export {
51
+ m as a,
52
+ D as c,
53
+ N as i,
54
+ d as p,
55
+ k as r,
56
+ b as t
57
+ };
package/dist/helper.js CHANGED
@@ -1,5 +1,42 @@
1
- function o() {
1
+ import { formatHex as C } from "culori";
2
+ import { mc as m } from "magic-color";
3
+ import { i as d, a as u, t as g, c as b, p as y, r as D } from "./common-BKEe-sBl.js";
4
+ function O(p) {
5
+ const { name: n, color: a, dom: i } = p;
6
+ if (!i)
7
+ return;
8
+ const c = a?.split(/-\d+-?/)[0];
9
+ if (d(c))
10
+ return;
11
+ const t = {}, e = {};
12
+ try {
13
+ const o = C(c) || c;
14
+ if (o && m.valid(o)) {
15
+ t[`--mc-${n}-color`] = o;
16
+ const r = m.theme(o);
17
+ for (const s of u) {
18
+ const h = g({ type: "hex", components: [r[s]], alpha: 1 });
19
+ e[s] = h;
20
+ }
21
+ }
22
+ } catch (o) {
23
+ console.error(`[updateMagicColor] get ${c} theme fail, please use another color.`), console.error(o);
24
+ }
25
+ const l = a.match(/.*-(\d+)/)?.[1];
26
+ if (l) {
27
+ const { originDepth: o, beforeDepth: r, afterDepth: s } = D(l);
28
+ e[r] && e[s] && (t[`--mc-${n}-color`] = b({
29
+ originDepth: o,
30
+ beforeDepth: r,
31
+ beforeComponents: e[r].components,
32
+ afterComponents: e[s].components
33
+ }));
34
+ }
35
+ const f = y(n, e);
36
+ Object.assign(t, f);
37
+ for (const o in t)
38
+ t[o] && i.style.setProperty(o, t[o].toString());
2
39
  }
3
40
  export {
4
- o as updateMagicColor
41
+ O as updateMagicColor
5
42
  };
package/dist/index.js CHANGED
@@ -1,136 +1,108 @@
1
- import { notLastChildSelectorVariant as A } from "@unocss/preset-wind4/rules";
2
- import { colorCSSGenerator as H, parseColor as y, defineProperty as f, generateThemeVariable as B, themeTracking as I, detectThemeValue as G, h as x, SpecialColorKey as F, numberResolver as R, hyphenate as D } from "@unocss/preset-wind4/utils";
3
- import { converter as q, formatHex as K } from "culori";
4
- import { mc as N } from "magic-color";
5
- import { notNull as J } from "unocss";
6
- const Q = q("oklch");
7
- function h(r) {
8
- return r && Number.parseFloat(r.toString()) || 0;
9
- }
10
- function C(r) {
11
- if (!r)
12
- return;
13
- if (r.type === "oklch")
14
- return r.components[0].toString().includes("%") && (r.components[0] = h(r.components[0]) / 100), r.components = r.components.map(h), r;
15
- const o = r.components.join(" "), e = Q(r.type === "hex" ? o : `${r.type}(${o})`);
16
- if (e)
17
- return {
18
- type: "oklch",
19
- components: [e.l, e.c, e.h ?? 0],
20
- alpha: r.alpha
21
- };
22
- }
23
- const M = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950];
24
- function O(r) {
25
- let o = Number(r);
26
- o = o <= 50 ? 50 : o, o = o >= 950 ? 950 : o;
27
- let e = Math.floor(o / 100) * 100;
28
- e = e <= 50 ? 50 : e;
29
- let n = Math.floor((o + 100) / 100) * 100;
30
- return n = n >= 950 ? 950 : n, { originDepth: o, beforeDepth: e, afterDepth: n };
31
- }
32
- function U(r, o) {
33
- let e = y(r, o);
34
- if (e?.color)
35
- return e;
36
- const [n, a, c] = r.split(/[:/]/), s = n.match(/.*-(\d+)/)?.[1], t = n?.split(/-\d+-?/)[0];
37
- if (e = {
1
+ import { colorCSSGenerator as I, parseColor as f, defineProperty as m, generateThemeVariable as N, themeTracking as z, detectThemeValue as B, h as b, SpecialColorKey as E, numberResolver as M, hyphenate as x } from "@unocss/preset-wind4/utils";
2
+ import { formatHex as L } from "culori";
3
+ import { mc as C } from "magic-color";
4
+ import { i as P, r as V, t as v, c as H, a as y, p as G } from "./common-BKEe-sBl.js";
5
+ import { notNull as U } from "unocss";
6
+ import { notLastChildSelectorVariant as q } from "@unocss/preset-wind4/rules";
7
+ function A(e, o) {
8
+ let r = f(e, o);
9
+ if (r?.color)
10
+ return r;
11
+ const [t, a, s] = e.split(/[:/]/), c = t.match(/.*-(\d+)/)?.[1], n = t?.split(/-\d+-?/)[0];
12
+ if (r = {
38
13
  opacity: a,
39
- modifier: c,
40
- name: t,
41
- no: s,
14
+ modifier: s,
15
+ name: n,
16
+ no: c,
42
17
  color: "",
43
18
  alpha: a && `${a}%`,
44
19
  keys: void 0,
45
20
  cssColor: void 0
46
- }, !t || !Number.isNaN(Number(t)))
47
- return console.error(`[unocss-preset-margicolor][mc-${r}] The color '${t}' is invalid.`), e;
48
- if (!s)
49
- return e;
50
- const { originDepth: i, beforeDepth: u, afterDepth: $ } = O(s);
51
- let l = y(`${t}-${u}`, o)?.cssColor, m = y(`${t}-${$}`, o)?.cssColor;
52
- if (!l || !m)
21
+ }, P(n) || !c)
22
+ return r;
23
+ const { originDepth: l, beforeDepth: i, afterDepth: p } = V(c);
24
+ let $ = f(`${n}-${i}`, o)?.cssColor, d = f(`${n}-${p}`, o)?.cssColor;
25
+ if (!$ || !d)
53
26
  try {
54
- const g = y(t, o)?.color, k = g ? K(g) : void 0;
55
- if (k && N.valid(k)) {
56
- const v = N.theme(k);
57
- l || (l = C({ type: "hex", components: [v[u]], alpha: 1 })), m || (m = C({ type: "hex", components: [v[$]], alpha: 1 }));
27
+ const h = f(n, o)?.color, g = h ? L(h) : void 0;
28
+ if (g && C.valid(g)) {
29
+ const O = C.theme(g);
30
+ $ || ($ = v({ type: "hex", components: [O[i]], alpha: 1 })), d || (d = v({ type: "hex", components: [O[p]], alpha: 1 }));
58
31
  }
59
- } catch (g) {
60
- console.error(`[unocss-preset-margicolor] Error parsing ${r}: get ${t} theme fail, please use another color.`), console.error(g);
32
+ } catch (h) {
33
+ console.error(`[unocss-preset-margicolor] Error parsing ${e}: get ${n} theme fail, please use another color.`), console.error(h);
61
34
  }
62
- if (l = C(l), m = C(m), !l || !m)
63
- return e;
64
- const b = (i - u) / (i < 100 || i > 950 ? 50 : 100), d = Array.from({ length: 3 }).map((g, k) => {
65
- const v = l.components, w = m.components, S = h(v[k]) + (h(w[k]) - h(v[k])) * b;
66
- return `${Math.round(S * 1e3) / 1e3}`;
67
- });
68
- return e.color = `oklch(${d.join(" ")})`, e;
35
+ return $ = v($), d = v(d), !$ || !d || (r.color = H({
36
+ originDepth: l,
37
+ beforeDepth: i,
38
+ beforeComponents: $.components,
39
+ afterComponents: d.components
40
+ })), r;
69
41
  }
70
- function W(r) {
42
+ function F(e) {
71
43
  const o = {};
72
- if (!r || r.color)
73
- return { colorData: r, cssVariable: o };
74
- const { name: e, no: n } = r;
75
- if (!n)
76
- return r.color = `var(--mc-${e}-color)`, { colorData: r, cssVariable: o };
77
- const { originDepth: a, beforeDepth: c, afterDepth: s } = O(n), t = `--mc-${e}-${a}-l`, i = `--mc-${e}-${a}-c`, u = `--mc-${e}-${a}-h`;
78
- if (!M.includes(a)) {
79
- const $ = (a - c) / (a < 100 || a > 950 ? 50 : 100), [l, m, b] = ["l", "c", "h"].map((d) => `calc(var(--mc-${e}-${c}-${d}) + ${$} * (var(--mc-${e}-${s}-${d}) - var(--mc-${e}-${c}-${d})))`);
80
- Object.assign(o, { [t]: l, [i]: m, [u]: b });
44
+ if (!e || e.color)
45
+ return { colorData: e, cssVariable: o };
46
+ const { name: r, no: t } = e;
47
+ if (!t)
48
+ return e.color = `var(--mc-${r}-color)`, { colorData: e, cssVariable: o };
49
+ const { originDepth: a, beforeDepth: s, afterDepth: c } = V(t), n = `--mc-${r}-${a}-l`, l = `--mc-${r}-${a}-c`, i = `--mc-${r}-${a}-h`;
50
+ if (!y.includes(a)) {
51
+ const p = (a - s) / (a < 100 || a > 900 ? 50 : 100), [$, d, h] = ["l", "c", "h"].map((g) => `calc(var(--mc-${r}-${s}-${g}) + ${p} * (var(--mc-${r}-${c}-${g}) - var(--mc-${r}-${s}-${g})))`);
52
+ Object.assign(o, { [n]: $, [l]: d, [i]: h });
81
53
  }
82
- return r.color = `oklch(var(${t}) var(${i}) var(${u}))`, { colorData: r, cssVariable: o };
54
+ return e.color = `oklch(var(${n}) var(${l}) var(${i}))`, { colorData: e, cssVariable: o };
83
55
  }
84
- function j(r, o) {
85
- const e = U(r, o);
86
- return W(e);
56
+ function w(e, o) {
57
+ const r = A(e, o);
58
+ return F(r);
87
59
  }
88
- function p(r, o) {
89
- return ([, e], n) => {
90
- const { colorData: a, cssVariable: c } = j(e ?? "", n.theme);
60
+ function u(e, o) {
61
+ return ([, r], t) => {
62
+ const { colorData: a, cssVariable: s } = w(r ?? "", t.theme);
91
63
  if (a?.color) {
92
- const s = H(a, r, o, n);
93
- return s && s.push({
94
- [n.symbols.selector]: (t) => t,
95
- ...c
96
- }), s;
64
+ const c = I(a, e, o, t);
65
+ return c && c.push({
66
+ [t.symbols.selector]: (n) => n,
67
+ ...s
68
+ }), c;
97
69
  }
98
70
  };
99
71
  }
100
- const X = {
72
+ const J = {
101
73
  transparent: "transparent",
102
74
  current: "currentColor",
103
75
  inherit: "inherit"
104
- }, Y = {
105
- "gradient-position": f("--un-gradient-position"),
106
- "gradient-from": f("--un-gradient-from", { syntax: "<color>", initialValue: "#0000" }),
107
- "gradient-via": f("--un-gradient-via", { syntax: "<color>", initialValue: "#0000" }),
108
- "gradient-to": f("--un-gradient-to", { syntax: "<color>", initialValue: "#0000" }),
109
- "gradient-stops": f("--un-gradient-stops"),
110
- "gradient-via-stops": f("--un-gradient-via-stops"),
111
- "gradient-from-position": f("--un-gradient-from-position", { syntax: "<length-percentage>", initialValue: "0%" }),
112
- "gradient-via-position": f("--un-gradient-via-position", { syntax: "<length-percentage>", initialValue: "50%" }),
113
- "gradient-to-position": f("--un-gradient-to-position", { syntax: "<length-percentage>", initialValue: "100%" })
76
+ }, Q = {
77
+ "gradient-position": m("--un-gradient-position"),
78
+ "gradient-from": m("--un-gradient-from", { syntax: "<color>", initialValue: "#0000" }),
79
+ "gradient-via": m("--un-gradient-via", { syntax: "<color>", initialValue: "#0000" }),
80
+ "gradient-to": m("--un-gradient-to", { syntax: "<color>", initialValue: "#0000" }),
81
+ "gradient-stops": m("--un-gradient-stops"),
82
+ "gradient-via-stops": m("--un-gradient-via-stops"),
83
+ "gradient-from-position": m("--un-gradient-from-position", { syntax: "<length-percentage>", initialValue: "0%" }),
84
+ "gradient-via-position": m("--un-gradient-via-position", { syntax: "<length-percentage>", initialValue: "50%" }),
85
+ "gradient-to-position": m("--un-gradient-to-position", { syntax: "<length-percentage>", initialValue: "100%" })
114
86
  };
115
- function Z() {
116
- return function* ([, r, o], { theme: e, symbols: n }) {
117
- const a = {}, { colorData: c, cssVariable: s } = j(o, e);
118
- if (c) {
119
- const { color: t, keys: i, alpha: u } = c;
120
- if (t) {
121
- if (Object.values(X).includes(t))
122
- a[`--un-gradient-${r}`] = t;
87
+ function W() {
88
+ return function* ([, e, o], { theme: r, symbols: t }) {
89
+ const a = {}, { colorData: s, cssVariable: c } = w(o, r);
90
+ if (s) {
91
+ const { color: n, keys: l, alpha: i } = s;
92
+ if (n) {
93
+ if (Object.values(J).includes(n))
94
+ a[`--un-gradient-${e}`] = n;
123
95
  else {
124
- a[`--un-${r}-opacity`] = u;
125
- const $ = i ? B("colors", i) : t;
126
- a[`--un-gradient-${r}`] = `color-mix(in oklab, ${$} var(--un-${r}-opacity), transparent)`, yield f(`--un-${r}-opacity`, { syntax: "<percentage>", initialValue: "100%" });
96
+ a[`--un-${e}-opacity`] = i;
97
+ const p = l ? N("colors", l) : n;
98
+ a[`--un-gradient-${e}`] = `color-mix(in oklab, ${p} var(--un-${e}-opacity), transparent)`, yield m(`--un-${e}-opacity`, { syntax: "<percentage>", initialValue: "100%" });
127
99
  }
128
- i && I("colors", i), e && G(t, e);
100
+ l && z("colors", l), r && B(n, r);
129
101
  }
130
102
  } else
131
- a[`--un-gradient-${r}`] = x.bracket.cssvar(o);
132
- if (a[`--un-gradient-${r}`]) {
133
- switch (r) {
103
+ a[`--un-gradient-${e}`] = b.bracket.cssvar(o);
104
+ if (a[`--un-gradient-${e}`]) {
105
+ switch (e) {
134
106
  case "from":
135
107
  yield {
136
108
  ...a,
@@ -154,16 +126,16 @@ function Z() {
154
126
  yield { ...a };
155
127
  break;
156
128
  }
157
- for (const t of Object.values(Y))
158
- yield t;
129
+ for (const n of Object.values(Q))
130
+ yield n;
159
131
  yield {
160
- [n.selector]: (t) => t,
161
- ...s
132
+ [t.selector]: (n) => n,
133
+ ...c
162
134
  };
163
135
  }
164
136
  };
165
137
  }
166
- const T = {
138
+ const S = {
167
139
  l: ["-left"],
168
140
  r: ["-right"],
169
141
  t: ["-top"],
@@ -182,205 +154,224 @@ const T = {
182
154
  block: ["-block-start", "-block-end"],
183
155
  inline: ["-inline-start", "-inline-end"]
184
156
  };
185
- function V([, r = "", o], e) {
186
- if (r in T) {
187
- const { colorData: n, cssVariable: a } = j(o ?? "", e.theme);
188
- if (n) {
189
- const c = T[r].map((s) => oo(s)(n, e)).filter(J);
157
+ function k([, e = "", o], r) {
158
+ if (e in S) {
159
+ const { colorData: t, cssVariable: a } = w(o ?? "", r.theme);
160
+ if (t) {
161
+ const s = S[e].map((c) => X(c)(t, r)).filter(U);
190
162
  return [
191
- c.map((s) => s[0]).reduce((s, t) => (Object.assign(s, t), s), {}),
192
- ...c.flatMap((s) => s.slice(1)),
163
+ s.map((c) => c[0]).reduce((c, n) => (Object.assign(c, n), c), {}),
164
+ ...s.flatMap((c) => c.slice(1)),
193
165
  {
194
- [e.symbols.selector]: (s) => s,
166
+ [r.symbols.selector]: (c) => c,
195
167
  ...a
196
168
  }
197
169
  ];
198
170
  }
199
171
  }
200
172
  }
201
- function oo(r) {
202
- return (o, e) => {
203
- const n = H(o, `border${r}-color`, `border${r}`, e);
204
- if (n) {
205
- const a = n[0];
206
- return o?.color && !Object.values(F).includes(o.color) && !o.alpha && r && r !== "" && (a[`--un-border${r}-opacity`] = "var(--un-border-opacity)"), n;
173
+ function X(e) {
174
+ return (o, r) => {
175
+ const t = I(o, `border${e}-color`, `border${e}`, r);
176
+ if (t) {
177
+ const a = t[0];
178
+ return o?.color && !Object.values(E).includes(o.color) && !o.alpha && e && e !== "" && (a[`--un-border${e}-opacity`] = "var(--un-border-opacity)"), t;
207
179
  }
208
180
  };
209
181
  }
210
- const ro = {
182
+ const Y = {
211
183
  "mask-image": "var(--un-mask-linear), var(--un-mask-radial), var(--un-mask-conic)",
212
184
  "mask-composite": "intersect"
213
- }, z = {
185
+ }, j = {
214
186
  t: ["top"],
215
187
  b: ["bottom"],
216
188
  l: ["left"],
217
189
  r: ["right"],
218
190
  x: ["left", "right"],
219
191
  y: ["top", "bottom"]
220
- }, L = "linear-gradient(#fff, #fff)";
221
- function P([r, o = "", e, n], a) {
222
- const c = { ...ro }, s = [];
223
- if (s.push(...["linear", "radial", "conic"].map((t) => f(`--un-mask-${t}`, { initialValue: L }))), o in z) {
224
- c["--un-mask-linear"] = "var(--un-mask-left), var(--un-mask-right), var(--un-mask-bottom), var(--un-mask-top)";
225
- for (const t of z[o]) {
226
- c[`--un-mask-${t}`] = `linear-gradient(to ${t}, var(--un-mask-${t}-from-color) var(--un-mask-${t}-from-position), var(--un-mask-${t}-to-color) var(--un-mask-${t}-to-position))`, R(n) != null ? (I("spacing"), c[`--un-mask-${t}-${e}-position`] = `calc(var(--spacing) * ${x.bracket.cssvar.fraction.number(n)})`) : c[`--un-mask-${t}-${e}-position`] = x.bracket.cssvar.fraction.rem(n);
227
- const i = p(`--un-mask-${t}-${e}-color`, D("colors"))([r, n], a);
228
- if (i) {
229
- const [u, ...$] = i;
230
- Object.assign(c, u), s.push(...$);
192
+ }, D = "linear-gradient(#fff, #fff)";
193
+ function R([e, o = "", r, t], a) {
194
+ const s = { ...Y }, c = [];
195
+ if (c.push(...["linear", "radial", "conic"].map((n) => m(`--un-mask-${n}`, { initialValue: D }))), o in j) {
196
+ s["--un-mask-linear"] = "var(--un-mask-left), var(--un-mask-right), var(--un-mask-bottom), var(--un-mask-top)";
197
+ for (const n of j[o]) {
198
+ s[`--un-mask-${n}`] = `linear-gradient(to ${n}, var(--un-mask-${n}-from-color) var(--un-mask-${n}-from-position), var(--un-mask-${n}-to-color) var(--un-mask-${n}-to-position))`, M(t) != null ? (z("spacing"), s[`--un-mask-${n}-${r}-position`] = `calc(var(--spacing) * ${b.bracket.cssvar.fraction.number(t)})`) : s[`--un-mask-${n}-${r}-position`] = b.bracket.cssvar.fraction.rem(t);
199
+ const l = u(`--un-mask-${n}-${r}-color`, x("colors"))([e, t], a);
200
+ if (l) {
201
+ const [i, ...p] = l;
202
+ Object.assign(s, i), c.push(...p);
231
203
  }
232
- s.push(...["from", "to"].flatMap((u) => [
233
- f(`--un-mask-${t}-${u}-position`, { syntax: "<length-percentage>", initialValue: u === "from" ? "0%" : "100%" }),
234
- f(`--un-mask-${t}-${u}-color`, { syntax: "<color>", initialValue: u === "from" ? "black" : "transparent" })
204
+ c.push(...["from", "to"].flatMap((i) => [
205
+ m(`--un-mask-${n}-${i}-position`, { syntax: "<length-percentage>", initialValue: i === "from" ? "0%" : "100%" }),
206
+ m(`--un-mask-${n}-${i}-color`, { syntax: "<color>", initialValue: i === "from" ? "black" : "transparent" })
235
207
  ]));
236
208
  }
237
- s.push(...["top", "right", "bottom", "left"].map((t) => f(`--un-mask-${t}`, { initialValue: L })));
209
+ c.push(...["top", "right", "bottom", "left"].map((n) => m(`--un-mask-${n}`, { initialValue: D })));
238
210
  } else {
239
- if (e == null)
240
- o === "radial" ? (c["--un-mask-radial"] = "radial-gradient(var(--un-mask-radial-stops, var(--un-mask-radial-size)))", c["--un-mask-radial-size"] = x.bracket.cssvar.rem(n)) : (c[`--un-mask-${o}`] = `${o}-gradient(var(--un-mask-${o}-stops, var(--un-mask-${o}-position)))`, c[`--un-mask-${o}-position`] = R(n) ? `calc(1deg * ${x.bracket.cssvar.number(n)})` : x.bracket.cssvar.fraction(n));
211
+ if (r == null)
212
+ o === "radial" ? (s["--un-mask-radial"] = "radial-gradient(var(--un-mask-radial-stops, var(--un-mask-radial-size)))", s["--un-mask-radial-size"] = b.bracket.cssvar.rem(t)) : (s[`--un-mask-${o}`] = `${o}-gradient(var(--un-mask-${o}-stops, var(--un-mask-${o}-position)))`, s[`--un-mask-${o}-position`] = M(t) ? `calc(1deg * ${b.bracket.cssvar.number(t)})` : b.bracket.cssvar.fraction(t));
241
213
  else {
242
- const t = {
214
+ const n = {
243
215
  linear: "",
244
216
  radial: "var(--un-mask-radial-shape) var(--un-mask-radial-size) at ",
245
217
  conic: "from "
246
218
  };
247
- c[`--un-mask-${o}-stops`] = `${t[o]}var(--un-mask-${o}-position), var(--un-mask-${o}-from-color) var(--un-mask-${o}-from-position), var(--un-mask-${o}-to-color) var(--un-mask-${o}-to-position)`, c[`--un-mask-${o}`] = `${o}-gradient(var(--un-mask-${o}-stops))`;
248
- const i = p(`--un-mask-${o}-${e}-color`, D("colors"))([r, n], a);
249
- if (i) {
250
- const [u, ...$] = i;
251
- Object.assign(c, u), s.push(...$);
219
+ s[`--un-mask-${o}-stops`] = `${n[o]}var(--un-mask-${o}-position), var(--un-mask-${o}-from-color) var(--un-mask-${o}-from-position), var(--un-mask-${o}-to-color) var(--un-mask-${o}-to-position)`, s[`--un-mask-${o}`] = `${o}-gradient(var(--un-mask-${o}-stops))`;
220
+ const l = u(`--un-mask-${o}-${r}-color`, x("colors"))([e, t], a);
221
+ if (l) {
222
+ const [i, ...p] = l;
223
+ Object.assign(s, i), c.push(...p);
252
224
  }
253
225
  }
254
- o === "radial" && s.push(f("--un-mask-radial-shape", { initialValue: "ellipse" }), f("--un-mask-radial-size", { initialValue: "farthest-corner" })), s.push(...["from", "to"].flatMap((t) => [
255
- f(`--un-mask-${o}-position`, { initialValue: o === "radial" ? "center" : "0deg" }),
256
- f(`--un-mask-${o}-${t}-position`, { syntax: "<length-percentage>", initialValue: t === "from" ? "0%" : "100%" }),
257
- f(`--un-mask-${o}-${t}-color`, { syntax: "<color>", initialValue: t === "from" ? "black" : "transparent" })
226
+ o === "radial" && c.push(m("--un-mask-radial-shape", { initialValue: "ellipse" }), m("--un-mask-radial-size", { initialValue: "farthest-corner" })), c.push(...["from", "to"].flatMap((n) => [
227
+ m(`--un-mask-${o}-position`, { initialValue: o === "radial" ? "center" : "0deg" }),
228
+ m(`--un-mask-${o}-${n}-position`, { syntax: "<length-percentage>", initialValue: n === "from" ? "0%" : "100%" }),
229
+ m(`--un-mask-${o}-${n}-color`, { syntax: "<color>", initialValue: n === "from" ? "black" : "transparent" })
258
230
  ]));
259
231
  }
260
- return [c, ...s];
232
+ return [s, ...c];
261
233
  }
262
- function E(r) {
263
- return (o, e) => {
264
- const n = D(r);
265
- return p(`--un-${n}-color`, n)(o, e);
234
+ function T(e) {
235
+ return (o, r) => {
236
+ const t = x(e);
237
+ return u(`--un-${t}-color`, t)(o, r);
266
238
  };
267
239
  }
268
- const eo = [
240
+ function Z(e, o) {
241
+ const r = e?.split(/-\d+-?/)[0];
242
+ if (P(r))
243
+ return;
244
+ const t = {};
245
+ let a = !1;
246
+ for (const s of y) {
247
+ const c = f(`${r}-${s}`, o)?.cssColor;
248
+ c ? t[s] = c : a = !0;
249
+ }
250
+ if (a)
251
+ try {
252
+ const s = f(r, o)?.color || f(`[${r}]`, o)?.color, c = s ? L(s) : void 0;
253
+ if (c && C.valid(c)) {
254
+ const n = C.theme(c);
255
+ for (const l of y)
256
+ if (!t[l]) {
257
+ const i = v({ type: "hex", components: [n[l]], alpha: 1 });
258
+ t[l] = i;
259
+ }
260
+ }
261
+ } catch (s) {
262
+ console.error(`[unocss-preset-margicolor] get ${r} theme fail, please use another color.`), console.error(s);
263
+ }
264
+ for (const s of y)
265
+ t[s] = v(t[s]);
266
+ return t;
267
+ }
268
+ function _(e, o, r, t) {
269
+ const a = o.match(/.*-(\d+)/)?.[1], s = {};
270
+ let c = f(o, t)?.color;
271
+ if (!c && !a && (c = f(`[${o}]`, t)?.color), c)
272
+ s[`--mc-${e}-color`] = c;
273
+ else if (a) {
274
+ const { originDepth: l, beforeDepth: i, afterDepth: p } = V(a);
275
+ r[i] && r[p] && (s[`--mc-${e}-color`] = H({
276
+ originDepth: l,
277
+ beforeDepth: i,
278
+ beforeComponents: r[i].components,
279
+ afterComponents: r[p].components
280
+ }));
281
+ }
282
+ const n = G(e, r);
283
+ return Object.assign(s, n);
284
+ }
285
+ function K(e, o, r = {}) {
286
+ if (f(e, r)?.color) {
287
+ console.error(`[unocss-preset-margicolor] The color name '${e}' has been configured in the theme, please use the another name.`);
288
+ return;
289
+ }
290
+ const [t] = o.split(/[:/]/), a = Z(t, r);
291
+ if (a)
292
+ return _(e, t, a, r);
293
+ }
294
+ function oo(e) {
295
+ return e.colors ? [{
296
+ getCSS: ({ theme: o }) => {
297
+ const r = {};
298
+ for (const t in e.colors)
299
+ Object.assign(r, K(t, e.colors[t], o));
300
+ return `
301
+ :root {
302
+ ${Object.entries(r).map(([t, a]) => `${t}: ${a};`).join(`
303
+ `)}
304
+ }
305
+ `;
306
+ }
307
+ }] : [];
308
+ }
309
+ const ro = [
269
310
  // common style colors
270
- [/^(?:color|c)-mc-(.+)$/, p("color", "text"), { autocomplete: "(color|c)-mc-$colors" }],
271
- [/^text-(?:color-)?mc-(.+)$/, p("color", "text"), { autocomplete: "(text|text-color)-mc-$colors" }],
272
- [/^outline-(?:color-)?mc-(.+)$/, p("outline-color", "outline"), { autocomplete: "(outline|outline-color)-mc-$colors" }],
273
- [/^accent-mc-(.+)$/, p("accent-color", "accent"), { autocomplete: "accent-mc-$colors" }],
274
- [/^caret-mc-(.+)$/, p("caret-color", "caret"), { autocomplete: "caret-mc-$colors" }],
275
- [/^bg-mc-(.+)$/, p("background-color", "bg"), { autocomplete: "bg-mc-$colors" }],
311
+ [/^(?:color|c)-mc-(.+)$/, u("color", "text"), { autocomplete: "(color|c)-mc-$colors" }],
312
+ [/^text-(?:color-)?mc-(.+)$/, u("color", "text"), { autocomplete: "(text|text-color)-mc-$colors" }],
313
+ [/^outline-(?:color-)?mc-(.+)$/, u("outline-color", "outline"), { autocomplete: "(outline|outline-color)-mc-$colors" }],
314
+ [/^accent-mc-(.+)$/, u("accent-color", "accent"), { autocomplete: "accent-mc-$colors" }],
315
+ [/^caret-mc-(.+)$/, u("caret-color", "caret"), { autocomplete: "caret-mc-$colors" }],
316
+ [/^bg-mc-(.+)$/, u("background-color", "bg"), { autocomplete: "bg-mc-$colors" }],
276
317
  // from https://github.com/unocss/unocss/blob/main/packages-presets/preset-wind4/src/rules/decoration.ts
277
- [/^(?:underline|decoration)-mc-(.+)$/, (r, o) => {
278
- const e = p("text-decoration-color", "line")(r, o);
279
- if (e) {
280
- const n = e[0];
281
- return n["-webkit-text-decoration-color"] = n["text-decoration-color"], e;
318
+ [/^(?:underline|decoration)-mc-(.+)$/, (e, o) => {
319
+ const r = u("text-decoration-color", "line")(e, o);
320
+ if (r) {
321
+ const t = r[0];
322
+ return t["-webkit-text-decoration-color"] = t["text-decoration-color"], r;
282
323
  }
283
324
  }, { autocomplete: "(underline|decoration)-mc-$colors" }],
284
325
  // from https://github.com/unocss/unocss/blob/main/packages-presets/preset-wind4/src/rules/divide.ts
285
- [/^divide-mc-(.+)$/, function* (r, o) {
286
- const e = p("border-color", "divide")(r, o);
287
- e && (yield {
288
- [o.symbols.variants]: [A(r[0])],
289
- ...e[0]
290
- }, yield e[1]);
326
+ [/^divide-mc-(.+)$/, function* (e, o) {
327
+ const r = u("border-color", "divide")(e, o);
328
+ r && (yield {
329
+ [o.symbols.variants]: [q(e[0])],
330
+ ...r[0]
331
+ }, yield r[1]);
291
332
  }, { autocomplete: "divide-mc-$colors" }],
292
- [/^(?:filter-)?drop-shadow-color-mc-(.+)$/, p("--un-drop-shadow-color", "drop-shadow"), { autocomplete: "divide-mc-$colors" }],
293
- [/^\$ placeholder-mc-(.+)$/, p("color", "placeholder"), { autocomplete: "placeholder-mc-$colors" }],
294
- [/^ring-mc-(.+)$/, p("--un-ring-color", "ring"), { autocomplete: "ring-mc-$colors" }],
295
- [/^inset-ring-mc-(.+)$/, p("--un-inset-ring-color", "inset-ring"), { autocomplete: "inset-ring-mc-$colors" }],
296
- [/^ring-offset-mc-(.+)$/, p("--un-ring-offset-color", "ring-offset"), { autocomplete: "ring-offset-mc-$colors" }],
297
- [/^shadow-mc-(.+)$/, E("shadow"), { autocomplete: ["shadow-$colors", "shadow-mc-$shadow"] }],
298
- [/^inset-shadow-mc(.+)$/, E("insetShadow"), { autocomplete: ["inset-shadow-mc-$colors", "inset-shadow-mc-$insetShadow"] }],
299
- [/^fill-mc-(.+)$/, p("fill", "fill"), { autocomplete: "fill-mc-$colors" }],
300
- [/^stroke-mc-(.+)$/, p("stroke", "stroke"), { autocomplete: "stroke-mc-$colors" }],
301
- [/^text-stroke-mc-(.+)$/, p("-webkit-text-stroke-color", "text-stroke"), { autocomplete: "text-stroke-mc-$colors" }],
302
- [/^text-shadow-(?:color-)?mc-(.+)$/, p("--un-text-shadow-color", "text-shadow"), { autocomplete: ["text-shadow(-color)?-mc-$colors"] }],
303
- [/^mask-(linear|radial|conic)-(from|to)-mc-(.+)$/, P, { autocomplete: ["mask-(linear|radial|conic)-(from|to)-mc-$colors"] }],
304
- [/^mask-([trblxy])-(from|to)-mc-(.+)$/, P, { autocomplete: ["mask-(x|y|t|b|l|r)-(from|to)-mc-$colors"] }],
333
+ [/^(?:filter-)?drop-shadow-color-mc-(.+)$/, u("--un-drop-shadow-color", "drop-shadow"), { autocomplete: "divide-mc-$colors" }],
334
+ [/^\$ placeholder-mc-(.+)$/, u("color", "placeholder"), { autocomplete: "placeholder-mc-$colors" }],
335
+ [/^ring-mc-(.+)$/, u("--un-ring-color", "ring"), { autocomplete: "ring-mc-$colors" }],
336
+ [/^inset-ring-mc-(.+)$/, u("--un-inset-ring-color", "inset-ring"), { autocomplete: "inset-ring-mc-$colors" }],
337
+ [/^ring-offset-mc-(.+)$/, u("--un-ring-offset-color", "ring-offset"), { autocomplete: "ring-offset-mc-$colors" }],
338
+ [/^shadow-mc-(.+)$/, T("shadow"), { autocomplete: ["shadow-$colors", "shadow-mc-$shadow"] }],
339
+ [/^inset-shadow-mc(.+)$/, T("insetShadow"), { autocomplete: ["inset-shadow-mc-$colors", "inset-shadow-mc-$insetShadow"] }],
340
+ [/^fill-mc-(.+)$/, u("fill", "fill"), { autocomplete: "fill-mc-$colors" }],
341
+ [/^stroke-mc-(.+)$/, u("stroke", "stroke"), { autocomplete: "stroke-mc-$colors" }],
342
+ [/^text-stroke-mc-(.+)$/, u("-webkit-text-stroke-color", "text-stroke"), { autocomplete: "text-stroke-mc-$colors" }],
343
+ [/^text-shadow-(?:color-)?mc-(.+)$/, u("--un-text-shadow-color", "text-shadow"), { autocomplete: ["text-shadow(-color)?-mc-$colors"] }],
344
+ [/^mask-(linear|radial|conic)-(from|to)-mc-(.+)$/, R, { autocomplete: ["mask-(linear|radial|conic)-(from|to)-mc-$colors"] }],
345
+ [/^mask-([trblxy])-(from|to)-mc-(.+)$/, R, { autocomplete: ["mask-(x|y|t|b|l|r)-(from|to)-mc-$colors"] }],
305
346
  // border style colors
306
- [/^(?:border|b)-()(?:color-)?mc-(.+)$/, V, { autocomplete: ["(border|b)-mc-$colors", "(border|b)-<directions>-mc-$colors"] }],
307
- [/^(?:border|b)-([xy])-(?:color-)?mc-(.+)$/, V],
308
- [/^(?:border|b)-([rltbse])-(?:color-)?mc-(.+)$/, V],
309
- [/^(?:border|b)-(block|inline)-(?:color-)?mc-(.+)$/, V],
310
- [/^(?:border|b)-([bi][se])-(?:color-)?mc-(.+)$/, V],
347
+ [/^(?:border|b)-()(?:color-)?mc-(.+)$/, k, { autocomplete: ["(border|b)-mc-$colors", "(border|b)-<directions>-mc-$colors"] }],
348
+ [/^(?:border|b)-([xy])-(?:color-)?mc-(.+)$/, k],
349
+ [/^(?:border|b)-([rltbse])-(?:color-)?mc-(.+)$/, k],
350
+ [/^(?:border|b)-(block|inline)-(?:color-)?mc-(.+)$/, k],
351
+ [/^(?:border|b)-([bi][se])-(?:color-)?mc-(.+)$/, k],
311
352
  // bg gradient color
312
- [/^(from|via|to|stops)-mc-(.+)$/, Z()]
313
- ], to = [
314
- [/^mc-(.+)$/, no]
353
+ [/^(from|via|to|stops)-mc-(.+)$/, W()]
354
+ ], eo = [
355
+ [/^mc-(.+)$/, to]
315
356
  ];
316
- function no([, r], { theme: o }) {
317
- const e = {}, [n, a] = r.split("_");
318
- if (!a)
319
- return;
320
- if (y(n, o)?.color) {
321
- console.error(`[unocss-preset-margicolor][mc-${r}] The color name '${n}' has been configured in the theme, please use the another name.`);
322
- return;
323
- }
324
- const [c] = a.split(/[:/]/), s = c.match(/.*-(\d+)/)?.[1], t = c?.split(/-\d+-?/)[0];
325
- if (!t || !Number.isNaN(Number(t))) {
326
- console.error(`[unocss-preset-margicolor][mc-${r}] The color '${t}' is invalid.`);
327
- return;
328
- }
329
- const i = {};
330
- let u = !1;
331
- for (const l of M) {
332
- const m = y(`${t}-${l}`, o)?.cssColor;
333
- m ? i[l] = m : u = !0;
334
- }
335
- if (u)
336
- try {
337
- const l = y(t, o)?.color, m = l ? K(l) : void 0;
338
- if (m && N.valid(m)) {
339
- const b = N.theme(m);
340
- for (const d of M)
341
- if (!i[d]) {
342
- const g = C({ type: "hex", components: [b[d]], alpha: 1 });
343
- i[d] = g;
344
- }
345
- }
346
- } catch (l) {
347
- console.error(`[unocss-preset-margicolor] Error parsing ${r}: get ${t} theme fail, please use another color.`), console.error(l);
348
- }
349
- for (const l of M)
350
- i[l] = C(i[l]);
351
- const $ = y(c, o);
352
- if ($?.color)
353
- e[`--mc-${n}-color`] = $.color;
354
- else if (s) {
355
- const { originDepth: l, beforeDepth: m, afterDepth: b } = O(s);
356
- if (i[m] && i[b]) {
357
- const d = (l - m) / 100, g = Array.from({ length: 3 }).map((k, v) => {
358
- const w = i[m].components, S = i[b].components, _ = h(w[v]) + (h(S[v]) - h(w[v])) * d;
359
- return `${Math.round(_ * 1e3) / 1e3}`;
360
- });
361
- e[`--mc-${n}-color`] = `oklch(${g.join(" ")})`;
362
- }
363
- }
364
- for (const l of M)
365
- if (i[l]) {
366
- const m = i[l].components;
367
- e[`--mc-${n}-${l}-l`] = Math.round(h(m[0]) * 1e3) / 1e3, e[`--mc-${n}-${l}-c`] = Math.round(h(m[1]) * 1e3) / 1e3, e[`--mc-${n}-${l}-h`] = Math.round(h(m[2]) * 1e3) / 1e3;
368
- }
369
- return e;
357
+ function to([, e], { theme: o }) {
358
+ const r = e.indexOf("_"), t = e.substring(0, r), a = e.substring(r + 1);
359
+ return a ? K(t, a, o) : void 0;
370
360
  }
371
- const ao = [
372
- eo,
373
- to
361
+ const no = [
362
+ ro,
363
+ eo
374
364
  ].flat();
375
- function uo(r) {
365
+ function mo(e = {}) {
376
366
  return {
377
367
  name: "unocss-preset-magicolor",
378
368
  layer: "unocss-preset-magicolor",
379
369
  layers: { "unocss-preset-magicolor": -100 },
380
- rules: ao
370
+ rules: no,
371
+ preflights: oo(e)
381
372
  };
382
373
  }
383
374
  export {
384
- uo as default,
385
- uo as presetMagicolor
375
+ mo as default,
376
+ mo as presetMagicolor
386
377
  };
@@ -1 +1,12 @@
1
- export declare function updateMagicColor(): void;
1
+ /**
2
+ * Modify the value of the color variable
3
+ * @param params params Parameter object
4
+ * @param params.name Color name
5
+ * @param params.color Color
6
+ * @param params.dom params.dom Target element, modifying the entire page theme when passing `document.documentElement`
7
+ */
8
+ export declare function updateMagicColor(params: {
9
+ name: string;
10
+ color: string;
11
+ dom?: HTMLElement;
12
+ }): void;
@@ -1,5 +1,5 @@
1
1
  import { Preset } from 'unocss';
2
2
  import { PresetMcOptions } from './types';
3
- export declare function presetMagicolor(_options?: PresetMcOptions): Preset;
3
+ export declare function presetMagicolor(options?: PresetMcOptions): Preset;
4
4
  export * from './types';
5
5
  export default presetMagicolor;
@@ -0,0 +1,3 @@
1
+ import { Preflight } from 'unocss';
2
+ import { PresetMcOptions } from '../types';
3
+ export declare function preflights(options: PresetMcOptions): Preflight[];
@@ -0,0 +1,6 @@
1
+ export * from './bg-gradient';
2
+ export * from './border';
3
+ export * from './mask';
4
+ export * from './shadow';
5
+ export * from './theme-colors';
6
+ export * from './utilities';
@@ -0,0 +1,10 @@
1
+ import { Theme } from '@unocss/preset-wind4';
2
+ import { CSSObject } from 'unocss';
3
+ /**
4
+ * resolve color variable
5
+ * @param name color name
6
+ * @param hue `color-depth`: color can be theme color or css color
7
+ * @param theme unocss theme
8
+ * @returns css variables
9
+ */
10
+ export declare function resolveColorVariable(name: string, hue: string, theme?: Theme): CSSObject | undefined;
@@ -1,12 +1,5 @@
1
1
  import { Theme } from '@unocss/preset-wind4';
2
2
  import { CSSObject, CSSValueInput, RuleContext } from 'unocss';
3
- import { ThemeKey } from '../typing';
4
- export declare const themeMetaList: ThemeKey[];
5
- export declare function resolveDepth(no: string): {
6
- originDepth: number;
7
- beforeDepth: number;
8
- afterDepth: number;
9
- };
10
3
  export declare function parseMagicColor(body: string, theme: Theme): {
11
4
  colorData: {
12
5
  opacity: string | undefined;
@@ -1,3 +1,5 @@
1
1
  /** PresetMcOptions */
2
2
  export interface PresetMcOptions {
3
+ /** global colors variables */
4
+ colors?: Record<string, string>;
3
5
  }
@@ -0,0 +1,20 @@
1
+ import { CSSColorValue } from '@unocss/preset-wind4/utils';
2
+ import { CSSObject } from 'unocss';
3
+ import { ThemeKey } from '../typing';
4
+ export declare const themeMetaList: ThemeKey[];
5
+ export declare function resolveDepth(no: string): {
6
+ originDepth: keyof import('magic-color').ThemeMetas;
7
+ beforeDepth: keyof import('magic-color').ThemeMetas;
8
+ afterDepth: keyof import('magic-color').ThemeMetas;
9
+ };
10
+ /**
11
+ * is invalid color
12
+ */
13
+ export declare function isInvalidColor(color?: string): boolean;
14
+ export declare function countDiffColor(params: {
15
+ originDepth: number;
16
+ beforeDepth: number;
17
+ beforeComponents: (string | number)[];
18
+ afterComponents: (string | number)[];
19
+ }): string;
20
+ export declare function parseOklchVariable(name: string, themeMetaColors: Partial<Record<ThemeKey, CSSColorValue>>): CSSObject;
@@ -1,6 +1,2 @@
1
- export * from './bg-gradient';
2
- export * from './border';
3
- export * from './mask';
4
- export * from './shadow';
1
+ export * from './common';
5
2
  export * from './transforms';
6
- export * from './utilities';
@@ -3,3 +3,5 @@ import { CSSColorValue } from '@unocss/preset-wind4/utils';
3
3
  export declare function toNum(value?: string | number): number;
4
4
  /** to oklch color */
5
5
  export declare function toOklch(cssColor?: CSSColorValue): CSSColorValue | undefined;
6
+ /** Math.round(num * 1000) / 1000 */
7
+ export declare function roundNum(num: number): number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unocss-preset-magicolor",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
5
  "description": "Unocss预设",
6
6
  "author": "nxy",
7
7
  "license": "MIT",
@@ -26,12 +26,12 @@
26
26
  "dist"
27
27
  ],
28
28
  "dependencies": {
29
- "@unocss/preset-wind4": "66.5.4",
29
+ "@unocss/preset-wind4": "~66.5.5",
30
30
  "culori": "^4.0.2",
31
31
  "magic-color": "^2.1.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/culori": "^4.0.1",
35
- "unocss": "^66.5.4"
35
+ "unocss": "^66.5.5"
36
36
  }
37
37
  }
File without changes
File without changes
File without changes