@tenphi/tasty 0.13.1 → 0.14.1
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 +155 -51
- package/dist/config.d.ts +13 -1
- package/dist/config.js +5 -1
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +5 -3
- package/dist/core/index.js +4 -3
- package/dist/debug.d.ts +26 -141
- package/dist/debug.js +356 -635
- package/dist/debug.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +4 -3
- package/dist/parser/classify.js +2 -1
- package/dist/parser/classify.js.map +1 -1
- package/dist/parser/parser.js +1 -1
- package/dist/plugins/okhsl-plugin.js +2 -275
- package/dist/plugins/okhsl-plugin.js.map +1 -1
- package/dist/plugins/types.d.ts +1 -1
- package/dist/properties/index.js +2 -15
- package/dist/properties/index.js.map +1 -1
- package/dist/ssr/format-property.js +9 -7
- package/dist/ssr/format-property.js.map +1 -1
- package/dist/styles/color.js +9 -5
- package/dist/styles/color.js.map +1 -1
- package/dist/styles/createStyle.js +24 -21
- package/dist/styles/createStyle.js.map +1 -1
- package/dist/styles/index.js +1 -1
- package/dist/styles/predefined.js +1 -1
- package/dist/styles/predefined.js.map +1 -1
- package/dist/styles/types.d.ts +19 -4
- package/dist/tasty.d.ts +9 -9
- package/dist/tasty.js +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils/color-math.d.ts +46 -0
- package/dist/utils/color-math.js +749 -0
- package/dist/utils/color-math.js.map +1 -0
- package/dist/utils/color-space.d.ts +5 -0
- package/dist/utils/color-space.js +229 -0
- package/dist/utils/color-space.js.map +1 -0
- package/dist/utils/colors.js +3 -1
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/mod-attrs.js +1 -1
- package/dist/utils/mod-attrs.js.map +1 -1
- package/dist/utils/process-tokens.d.ts +3 -13
- package/dist/utils/process-tokens.js +18 -98
- package/dist/utils/process-tokens.js.map +1 -1
- package/dist/utils/styles.d.ts +2 -15
- package/dist/utils/styles.js +22 -217
- package/dist/utils/styles.js.map +1 -1
- package/docs/PIPELINE.md +519 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +16 -6
- package/docs/comparison.md +16 -9
- package/docs/configuration.md +26 -3
- package/docs/debug.md +152 -339
- package/docs/design-system.md +1 -1
- package/docs/dsl.md +3 -1
- package/docs/getting-started.md +29 -13
- package/docs/injector.md +2 -2
- package/docs/methodology.md +3 -1
- package/docs/runtime.md +59 -9
- package/docs/ssr.md +3 -3
- package/docs/styles.md +1 -1
- package/docs/tasty-static.md +13 -2
- package/package.json +4 -3
- package/dist/utils/hsl-to-rgb.js +0 -38
- package/dist/utils/hsl-to-rgb.js.map +0 -1
- package/dist/utils/okhsl-to-rgb.js +0 -296
- package/dist/utils/okhsl-to-rgb.js.map +0 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Lru } from "../parser/lru.js";
|
|
2
|
+
import { okhslToSrgb } from "../utils/color-math.js";
|
|
2
3
|
|
|
3
4
|
//#region src/plugins/okhsl-plugin.ts
|
|
4
5
|
/**
|
|
@@ -12,283 +13,9 @@ import { Lru } from "../parser/lru.js";
|
|
|
12
13
|
* okhsl(240.5deg 50% 50%)
|
|
13
14
|
* okhsl(0.25turn 50% 50%)
|
|
14
15
|
* okhsl(1.57rad 50% 50%)
|
|
15
|
-
*
|
|
16
|
-
* Conversion math inlined from @texel/color to avoid external dependencies.
|
|
17
16
|
*/
|
|
18
17
|
const conversionCache = new Lru(500);
|
|
19
|
-
const OKLab_to_LMS_M = [
|
|
20
|
-
[
|
|
21
|
-
1,
|
|
22
|
-
.3963377773761749,
|
|
23
|
-
.2158037573099136
|
|
24
|
-
],
|
|
25
|
-
[
|
|
26
|
-
1,
|
|
27
|
-
-.1055613458156586,
|
|
28
|
-
-.0638541728258133
|
|
29
|
-
],
|
|
30
|
-
[
|
|
31
|
-
1,
|
|
32
|
-
-.0894841775298119,
|
|
33
|
-
-1.2914855480194092
|
|
34
|
-
]
|
|
35
|
-
];
|
|
36
|
-
const LMS_to_linear_sRGB_M = [
|
|
37
|
-
[
|
|
38
|
-
4.076741636075959,
|
|
39
|
-
-3.307711539258062,
|
|
40
|
-
.2309699031821041
|
|
41
|
-
],
|
|
42
|
-
[
|
|
43
|
-
-1.2684379732850313,
|
|
44
|
-
2.6097573492876878,
|
|
45
|
-
-.3413193760026569
|
|
46
|
-
],
|
|
47
|
-
[
|
|
48
|
-
-.004196076138675526,
|
|
49
|
-
-.703418617935936,
|
|
50
|
-
1.7076146940746113
|
|
51
|
-
]
|
|
52
|
-
];
|
|
53
|
-
const OKLab_to_linear_sRGB_coefficients = [
|
|
54
|
-
[[-1.8817030993265873, -.8093650129914302], [
|
|
55
|
-
1.19086277,
|
|
56
|
-
1.76576728,
|
|
57
|
-
.59662641,
|
|
58
|
-
.75515197,
|
|
59
|
-
.56771245
|
|
60
|
-
]],
|
|
61
|
-
[[1.8144407988010998, -1.194452667805235], [
|
|
62
|
-
.73956515,
|
|
63
|
-
-.45954404,
|
|
64
|
-
.08285427,
|
|
65
|
-
.12541073,
|
|
66
|
-
-.14503204
|
|
67
|
-
]],
|
|
68
|
-
[[.13110757611180954, 1.813339709266608], [
|
|
69
|
-
1.35733652,
|
|
70
|
-
-.00915799,
|
|
71
|
-
-1.1513021,
|
|
72
|
-
-.50559606,
|
|
73
|
-
.00692167
|
|
74
|
-
]]
|
|
75
|
-
];
|
|
76
|
-
const TAU = 2 * Math.PI;
|
|
77
18
|
const clamp = (value, min, max) => Math.max(Math.min(value, max), min);
|
|
78
|
-
const constrainAngle = (angle) => (angle % 360 + 360) % 360;
|
|
79
|
-
const sRGBLinearToGamma = (val) => {
|
|
80
|
-
const sign = val < 0 ? -1 : 1;
|
|
81
|
-
const abs = Math.abs(val);
|
|
82
|
-
return abs > .0031308 ? sign * (1.055 * Math.pow(abs, 1 / 2.4) - .055) : 12.92 * val;
|
|
83
|
-
};
|
|
84
|
-
const dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
85
|
-
const transform = (input, matrix) => [
|
|
86
|
-
dot3(input, matrix[0]),
|
|
87
|
-
dot3(input, matrix[1]),
|
|
88
|
-
dot3(input, matrix[2])
|
|
89
|
-
];
|
|
90
|
-
const cubed3 = (lms) => [
|
|
91
|
-
lms[0] * lms[0] * lms[0],
|
|
92
|
-
lms[1] * lms[1] * lms[1],
|
|
93
|
-
lms[2] * lms[2] * lms[2]
|
|
94
|
-
];
|
|
95
|
-
const OKLabToLinearSRGB = (lab) => {
|
|
96
|
-
return transform(cubed3(transform(lab, OKLab_to_LMS_M)), LMS_to_linear_sRGB_M);
|
|
97
|
-
};
|
|
98
|
-
const K1 = .206;
|
|
99
|
-
const K2 = .03;
|
|
100
|
-
const K3 = (1 + K1) / (1 + K2);
|
|
101
|
-
const toeInv = (x) => (x ** 2 + K1 * x) / (K3 * (x + K2));
|
|
102
|
-
const dotXY = (a, b) => a[0] * b[0] + a[1] * b[1];
|
|
103
|
-
const computeMaxSaturationOKLC = (a, b) => {
|
|
104
|
-
const okCoeff = OKLab_to_linear_sRGB_coefficients;
|
|
105
|
-
const lmsToRgb = LMS_to_linear_sRGB_M;
|
|
106
|
-
const tmp2 = [a, b];
|
|
107
|
-
const tmp3 = [
|
|
108
|
-
0,
|
|
109
|
-
a,
|
|
110
|
-
b
|
|
111
|
-
];
|
|
112
|
-
let chnlCoeff;
|
|
113
|
-
let chnlLMS;
|
|
114
|
-
if (dotXY(okCoeff[0][0], tmp2) > 1) {
|
|
115
|
-
chnlCoeff = okCoeff[0][1];
|
|
116
|
-
chnlLMS = lmsToRgb[0];
|
|
117
|
-
} else if (dotXY(okCoeff[1][0], tmp2) > 1) {
|
|
118
|
-
chnlCoeff = okCoeff[1][1];
|
|
119
|
-
chnlLMS = lmsToRgb[1];
|
|
120
|
-
} else {
|
|
121
|
-
chnlCoeff = okCoeff[2][1];
|
|
122
|
-
chnlLMS = lmsToRgb[2];
|
|
123
|
-
}
|
|
124
|
-
const [k0, k1, k2, k3, k4] = chnlCoeff;
|
|
125
|
-
const [wl, wm, ws] = chnlLMS;
|
|
126
|
-
let sat = k0 + k1 * a + k2 * b + k3 * (a * a) + k4 * a * b;
|
|
127
|
-
const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
|
|
128
|
-
const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
|
|
129
|
-
const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
|
|
130
|
-
const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
|
|
131
|
-
const l_ = 1 + sat * kl;
|
|
132
|
-
const m_ = 1 + sat * km;
|
|
133
|
-
const s_ = 1 + sat * ks;
|
|
134
|
-
const l = l_ * l_ * l_;
|
|
135
|
-
const m = m_ * m_ * m_;
|
|
136
|
-
const s = s_ * s_ * s_;
|
|
137
|
-
const lds = 3 * kl * (l_ * l_);
|
|
138
|
-
const mds = 3 * km * (m_ * m_);
|
|
139
|
-
const sds = 3 * ks * (s_ * s_);
|
|
140
|
-
const lds2 = 6 * (kl * kl) * l_;
|
|
141
|
-
const mds2 = 6 * (km * km) * m_;
|
|
142
|
-
const sds2 = 6 * (ks * ks) * s_;
|
|
143
|
-
const f = wl * l + wm * m + ws * s;
|
|
144
|
-
const f1 = wl * lds + wm * mds + ws * sds;
|
|
145
|
-
const f2 = wl * lds2 + wm * mds2 + ws * sds2;
|
|
146
|
-
sat = sat - f * f1 / (f1 * f1 - .5 * f * f2);
|
|
147
|
-
return sat;
|
|
148
|
-
};
|
|
149
|
-
const findCuspOKLCH = (a, b) => {
|
|
150
|
-
const S_cusp = computeMaxSaturationOKLC(a, b);
|
|
151
|
-
const rgb_at_max = OKLabToLinearSRGB([
|
|
152
|
-
1,
|
|
153
|
-
S_cusp * a,
|
|
154
|
-
S_cusp * b
|
|
155
|
-
]);
|
|
156
|
-
const L_cusp = Math.cbrt(1 / Math.max(Math.max(rgb_at_max[0], rgb_at_max[1]), Math.max(rgb_at_max[2], 0)));
|
|
157
|
-
return [L_cusp, L_cusp * S_cusp];
|
|
158
|
-
};
|
|
159
|
-
const findGamutIntersectionOKLCH = (a, b, l1, c1, l0, cusp) => {
|
|
160
|
-
const lmsToRgb = LMS_to_linear_sRGB_M;
|
|
161
|
-
const tmp3 = [
|
|
162
|
-
0,
|
|
163
|
-
a,
|
|
164
|
-
b
|
|
165
|
-
];
|
|
166
|
-
const floatMax = Number.MAX_VALUE;
|
|
167
|
-
let t;
|
|
168
|
-
const dotYZ = (mat, vec) => mat[1] * vec[1] + mat[2] * vec[2];
|
|
169
|
-
const dotXYZ = (vec, x, y, z) => vec[0] * x + vec[1] * y + vec[2] * z;
|
|
170
|
-
if ((l1 - l0) * cusp[1] - (cusp[0] - l0) * c1 <= 0) {
|
|
171
|
-
const denom = c1 * cusp[0] + cusp[1] * (l0 - l1);
|
|
172
|
-
t = denom === 0 ? 0 : cusp[1] * l0 / denom;
|
|
173
|
-
} else {
|
|
174
|
-
const denom = c1 * (cusp[0] - 1) + cusp[1] * (l0 - l1);
|
|
175
|
-
t = denom === 0 ? 0 : cusp[1] * (l0 - 1) / denom;
|
|
176
|
-
const dl = l1 - l0;
|
|
177
|
-
const dc = c1;
|
|
178
|
-
const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);
|
|
179
|
-
const km = dotYZ(OKLab_to_LMS_M[1], tmp3);
|
|
180
|
-
const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);
|
|
181
|
-
const ldt_ = dl + dc * kl;
|
|
182
|
-
const mdt_ = dl + dc * km;
|
|
183
|
-
const sdt_ = dl + dc * ks;
|
|
184
|
-
const L = l0 * (1 - t) + t * l1;
|
|
185
|
-
const C = t * c1;
|
|
186
|
-
const l_ = L + C * kl;
|
|
187
|
-
const m_ = L + C * km;
|
|
188
|
-
const s_ = L + C * ks;
|
|
189
|
-
const l = l_ * l_ * l_;
|
|
190
|
-
const m = m_ * m_ * m_;
|
|
191
|
-
const s = s_ * s_ * s_;
|
|
192
|
-
const ldt = 3 * ldt_ * l_ * l_;
|
|
193
|
-
const mdt = 3 * mdt_ * m_ * m_;
|
|
194
|
-
const sdt = 3 * sdt_ * s_ * s_;
|
|
195
|
-
const ldt2 = 6 * ldt_ * ldt_ * l_;
|
|
196
|
-
const mdt2 = 6 * mdt_ * mdt_ * m_;
|
|
197
|
-
const sdt2 = 6 * sdt_ * sdt_ * s_;
|
|
198
|
-
const r_ = dotXYZ(lmsToRgb[0], l, m, s) - 1;
|
|
199
|
-
const r1 = dotXYZ(lmsToRgb[0], ldt, mdt, sdt);
|
|
200
|
-
const r2 = dotXYZ(lmsToRgb[0], ldt2, mdt2, sdt2);
|
|
201
|
-
const ur = r1 / (r1 * r1 - .5 * r_ * r2);
|
|
202
|
-
let tr = -r_ * ur;
|
|
203
|
-
const g_ = dotXYZ(lmsToRgb[1], l, m, s) - 1;
|
|
204
|
-
const g1 = dotXYZ(lmsToRgb[1], ldt, mdt, sdt);
|
|
205
|
-
const g2 = dotXYZ(lmsToRgb[1], ldt2, mdt2, sdt2);
|
|
206
|
-
const ug = g1 / (g1 * g1 - .5 * g_ * g2);
|
|
207
|
-
let tg = -g_ * ug;
|
|
208
|
-
const b_ = dotXYZ(lmsToRgb[2], l, m, s) - 1;
|
|
209
|
-
const b1 = dotXYZ(lmsToRgb[2], ldt, mdt, sdt);
|
|
210
|
-
const b2 = dotXYZ(lmsToRgb[2], ldt2, mdt2, sdt2);
|
|
211
|
-
const ub = b1 / (b1 * b1 - .5 * b_ * b2);
|
|
212
|
-
let tb = -b_ * ub;
|
|
213
|
-
tr = ur >= 0 ? tr : floatMax;
|
|
214
|
-
tg = ug >= 0 ? tg : floatMax;
|
|
215
|
-
tb = ub >= 0 ? tb : floatMax;
|
|
216
|
-
t += Math.min(tr, Math.min(tg, tb));
|
|
217
|
-
}
|
|
218
|
-
return t;
|
|
219
|
-
};
|
|
220
|
-
const computeSt = (cusp) => {
|
|
221
|
-
const l = cusp[0];
|
|
222
|
-
const c = cusp[1];
|
|
223
|
-
return [c / l, c / (1 - l)];
|
|
224
|
-
};
|
|
225
|
-
const computeStMid = (a, b) => {
|
|
226
|
-
return [.11516993 + 1 / (7.4477897 + 4.1590124 * b + a * (-2.19557347 + 1.75198401 * b + a * (-2.13704948 - 10.02301043 * b + a * (-4.24894561 + 5.38770819 * b + 4.69891013 * a)))), .11239642 + 1 / (1.6132032 - .68124379 * b + a * (.40370612 + .90148123 * b + a * (-.27087943 + .6122399 * b + a * (.00299215 - .45399568 * b - .14661872 * a))))];
|
|
227
|
-
};
|
|
228
|
-
const getCs = (L, a, b, cusp) => {
|
|
229
|
-
const cMax = findGamutIntersectionOKLCH(a, b, L, 1, L, cusp);
|
|
230
|
-
const stMax = computeSt(cusp);
|
|
231
|
-
const k = cMax / Math.min(L * stMax[0], (1 - L) * stMax[1]);
|
|
232
|
-
const stMid = computeStMid(a, b);
|
|
233
|
-
let ca = L * stMid[0];
|
|
234
|
-
let cb = (1 - L) * stMid[1];
|
|
235
|
-
const cMid = .9 * k * Math.sqrt(Math.sqrt(1 / (1 / ca ** 4 + 1 / cb ** 4)));
|
|
236
|
-
ca = L * .4;
|
|
237
|
-
cb = (1 - L) * .8;
|
|
238
|
-
return [
|
|
239
|
-
Math.sqrt(1 / (1 / ca ** 2 + 1 / cb ** 2)),
|
|
240
|
-
cMid,
|
|
241
|
-
cMax
|
|
242
|
-
];
|
|
243
|
-
};
|
|
244
|
-
const OKHSLToOKLab = (hsl) => {
|
|
245
|
-
let h = hsl[0];
|
|
246
|
-
const s = hsl[1];
|
|
247
|
-
const l = hsl[2];
|
|
248
|
-
const L = toeInv(l);
|
|
249
|
-
let a = 0;
|
|
250
|
-
let b = 0;
|
|
251
|
-
h = constrainAngle(h) / 360;
|
|
252
|
-
if (L !== 0 && L !== 1 && s !== 0) {
|
|
253
|
-
const a_ = Math.cos(TAU * h);
|
|
254
|
-
const b_ = Math.sin(TAU * h);
|
|
255
|
-
const [c0, cMid, cMax] = getCs(L, a_, b_, findCuspOKLCH(a_, b_));
|
|
256
|
-
const mid = .8;
|
|
257
|
-
const midInv = 1.25;
|
|
258
|
-
let t, k0, k1, k2;
|
|
259
|
-
if (s < mid) {
|
|
260
|
-
t = midInv * s;
|
|
261
|
-
k0 = 0;
|
|
262
|
-
k1 = mid * c0;
|
|
263
|
-
k2 = 1 - k1 / cMid;
|
|
264
|
-
} else {
|
|
265
|
-
t = 5 * (s - .8);
|
|
266
|
-
k0 = cMid;
|
|
267
|
-
k1 = .2 * cMid ** 2 * 1.25 ** 2 / c0;
|
|
268
|
-
k2 = 1 - k1 / (cMax - cMid);
|
|
269
|
-
}
|
|
270
|
-
const c = k0 + t * k1 / (1 - k2 * t);
|
|
271
|
-
a = c * a_;
|
|
272
|
-
b = c * b_;
|
|
273
|
-
}
|
|
274
|
-
return [
|
|
275
|
-
L,
|
|
276
|
-
a,
|
|
277
|
-
b
|
|
278
|
-
];
|
|
279
|
-
};
|
|
280
|
-
const okhslToSRGB = (h, s, l) => {
|
|
281
|
-
const linearRGB = OKLabToLinearSRGB(OKHSLToOKLab([
|
|
282
|
-
h,
|
|
283
|
-
s,
|
|
284
|
-
l
|
|
285
|
-
]));
|
|
286
|
-
return [
|
|
287
|
-
clamp(sRGBLinearToGamma(linearRGB[0]), 0, 1),
|
|
288
|
-
clamp(sRGBLinearToGamma(linearRGB[1]), 0, 1),
|
|
289
|
-
clamp(sRGBLinearToGamma(linearRGB[2]), 0, 1)
|
|
290
|
-
];
|
|
291
|
-
};
|
|
292
19
|
/**
|
|
293
20
|
* Parse an angle value with optional unit.
|
|
294
21
|
* Supports: deg, turn, rad, or unitless (treated as degrees).
|
|
@@ -330,7 +57,7 @@ const okhslFunc = (groups) => {
|
|
|
330
57
|
const h = parseAngle(tokens[0]);
|
|
331
58
|
const s = parsePercentage(tokens[1]);
|
|
332
59
|
const l = parsePercentage(tokens[2]);
|
|
333
|
-
const [r, g, b] =
|
|
60
|
+
const [r, g, b] = okhslToSrgb(h, clamp(s, 0, 1), clamp(l, 0, 1));
|
|
334
61
|
const format = (n) => {
|
|
335
62
|
const pct = n * 100;
|
|
336
63
|
return parseFloat(pct.toFixed(1)).toString() + "%";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"okhsl-plugin.js","names":[],"sources":["../../src/plugins/okhsl-plugin.ts"],"sourcesContent":["/**\n * OKHSL Plugin for Tasty\n *\n * Converts OKHSL color syntax to RGB notation.\n * Supports angle units: deg, turn, rad, or unitless (degrees).\n *\n * Examples:\n * okhsl(240.5 50% 50%)\n * okhsl(240.5deg 50% 50%)\n * okhsl(0.25turn 50% 50%)\n * okhsl(1.57rad 50% 50%)\n *\n * Conversion math inlined from @texel/color to avoid external dependencies.\n */\n\nimport { Lru } from '../parser/lru';\n\nimport type { StyleDetails } from '../parser/types';\nimport type { TastyPlugin, TastyPluginFactory } from './types';\n\n// Cache for OKHSL to RGB conversions\nconst conversionCache = new Lru<string, string>(500);\n\n// ============================================================================\n// Conversion Matrices (from texel-color)\n// ============================================================================\n\nconst OKLab_to_LMS_M: [number, number, number][] = [\n [1.0, 0.3963377773761749, 0.2158037573099136],\n [1.0, -0.1055613458156586, -0.0638541728258133],\n [1.0, -0.0894841775298119, -1.2914855480194092],\n];\n\nconst LMS_to_linear_sRGB_M: [number, number, number][] = [\n [4.076741636075959, -3.307711539258062, 0.2309699031821041],\n [-1.2684379732850313, 2.6097573492876878, -0.3413193760026569],\n [-0.004196076138675526, -0.703418617935936, 1.7076146940746113],\n];\n\nconst OKLab_to_linear_sRGB_coefficients: [\n [[number, number], number[]],\n [[number, number], number[]],\n [[number, number], number[]],\n] = [\n [\n [-1.8817030993265873, -0.8093650129914302],\n [1.19086277, 1.76576728, 0.59662641, 0.75515197, 0.56771245],\n ],\n [\n [1.8144407988010998, -1.194452667805235],\n [0.73956515, -0.45954404, 0.08285427, 0.12541073, -0.14503204],\n ],\n [\n [0.13110757611180954, 1.813339709266608],\n [1.35733652, -0.00915799, -1.1513021, -0.50559606, 0.00692167],\n ],\n];\n\n// ============================================================================\n// Math Utilities\n// ============================================================================\n\nconst TAU = 2 * Math.PI;\n\nconst clamp = (value: number, min: number, max: number): number =>\n Math.max(Math.min(value, max), min);\n\nconst constrainAngle = (angle: number): number => ((angle % 360) + 360) % 360;\n\nconst copySign = (to: number, from: number): number =>\n Math.sign(to) === Math.sign(from) ? to : -to;\n\nconst _spow = (base: number, exp: number): number =>\n copySign(Math.abs(base) ** exp, base);\n\n// ============================================================================\n// sRGB Gamma Conversion\n// ============================================================================\n\nconst sRGBLinearToGamma = (val: number): number => {\n const sign = val < 0 ? -1 : 1;\n const abs = Math.abs(val);\n return abs > 0.0031308\n ? sign * (1.055 * Math.pow(abs, 1 / 2.4) - 0.055)\n : 12.92 * val;\n};\n\n// ============================================================================\n// Matrix Operations\n// ============================================================================\n\ntype Vec3 = [number, number, number];\n\nconst dot3 = (a: Vec3, b: Vec3): number =>\n a[0] * b[0] + a[1] * b[1] + a[2] * b[2];\n\nconst transform = (input: Vec3, matrix: Vec3[]): Vec3 => [\n dot3(input, matrix[0]),\n dot3(input, matrix[1]),\n dot3(input, matrix[2]),\n];\n\nconst cubed3 = (lms: Vec3): Vec3 => [\n lms[0] * lms[0] * lms[0],\n lms[1] * lms[1] * lms[1],\n lms[2] * lms[2] * lms[2],\n];\n\n// ============================================================================\n// OKLab Conversion\n// ============================================================================\n\nconst OKLabToLinearSRGB = (lab: Vec3): Vec3 => {\n const lms = transform(lab, OKLab_to_LMS_M);\n const cubedLms = cubed3(lms);\n return transform(cubedLms, LMS_to_linear_sRGB_M);\n};\n\n// ============================================================================\n// OKHSL to OKLab Conversion (from texel-color)\n// ============================================================================\n\nconst K1 = 0.206;\nconst K2 = 0.03;\nconst K3 = (1.0 + K1) / (1.0 + K2);\n\nconst _toe = (x: number): number =>\n 0.5 *\n (K3 * x - K1 + Math.sqrt((K3 * x - K1) * (K3 * x - K1) + 4 * K2 * K3 * x));\n\nconst toeInv = (x: number): number => (x ** 2 + K1 * x) / (K3 * (x + K2));\n\nconst dotXY = (a: [number, number], b: [number, number]): number =>\n a[0] * b[0] + a[1] * b[1];\n\nconst computeMaxSaturationOKLC = (a: number, b: number): number => {\n const okCoeff = OKLab_to_linear_sRGB_coefficients;\n const lmsToRgb = LMS_to_linear_sRGB_M;\n const tmp2: [number, number] = [a, b];\n const tmp3: Vec3 = [0, a, b];\n\n let chnlCoeff: number[];\n let chnlLMS: Vec3;\n\n if (dotXY(okCoeff[0][0], tmp2) > 1) {\n chnlCoeff = okCoeff[0][1];\n chnlLMS = lmsToRgb[0];\n } else if (dotXY(okCoeff[1][0], tmp2) > 1) {\n chnlCoeff = okCoeff[1][1];\n chnlLMS = lmsToRgb[1];\n } else {\n chnlCoeff = okCoeff[2][1];\n chnlLMS = lmsToRgb[2];\n }\n\n const [k0, k1, k2, k3, k4] = chnlCoeff;\n const [wl, wm, ws] = chnlLMS;\n\n let sat = k0 + k1 * a + k2 * b + k3 * (a * a) + k4 * a * b;\n\n const dotYZ = (mat: Vec3, vec: Vec3): number =>\n mat[1] * vec[1] + mat[2] * vec[2];\n\n const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);\n const km = dotYZ(OKLab_to_LMS_M[1], tmp3);\n const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);\n\n const l_ = 1.0 + sat * kl;\n const m_ = 1.0 + sat * km;\n const s_ = 1.0 + sat * ks;\n\n const l = l_ * l_ * l_;\n const m = m_ * m_ * m_;\n const s = s_ * s_ * s_;\n\n const lds = 3.0 * kl * (l_ * l_);\n const mds = 3.0 * km * (m_ * m_);\n const sds = 3.0 * ks * (s_ * s_);\n\n const lds2 = 6.0 * (kl * kl) * l_;\n const mds2 = 6.0 * (km * km) * m_;\n const sds2 = 6.0 * (ks * ks) * s_;\n\n const f = wl * l + wm * m + ws * s;\n const f1 = wl * lds + wm * mds + ws * sds;\n const f2 = wl * lds2 + wm * mds2 + ws * sds2;\n\n sat = sat - (f * f1) / (f1 * f1 - 0.5 * f * f2);\n\n return sat;\n};\n\nconst findCuspOKLCH = (a: number, b: number): [number, number] => {\n const _lmsToRgb = LMS_to_linear_sRGB_M;\n const S_cusp = computeMaxSaturationOKLC(a, b);\n\n const lab: Vec3 = [1, S_cusp * a, S_cusp * b];\n const rgb_at_max = OKLabToLinearSRGB(lab);\n\n const L_cusp = Math.cbrt(\n 1 /\n Math.max(\n Math.max(rgb_at_max[0], rgb_at_max[1]),\n Math.max(rgb_at_max[2], 0.0),\n ),\n );\n const C_cusp = L_cusp * S_cusp;\n\n return [L_cusp, C_cusp];\n};\n\nconst findGamutIntersectionOKLCH = (\n a: number,\n b: number,\n l1: number,\n c1: number,\n l0: number,\n cusp: [number, number],\n): number => {\n const lmsToRgb = LMS_to_linear_sRGB_M;\n const tmp3: Vec3 = [0, a, b];\n const floatMax = Number.MAX_VALUE;\n\n let t: number;\n\n const dotYZ = (mat: Vec3, vec: Vec3): number =>\n mat[1] * vec[1] + mat[2] * vec[2];\n const dotXYZ = (vec: Vec3, x: number, y: number, z: number): number =>\n vec[0] * x + vec[1] * y + vec[2] * z;\n\n if ((l1 - l0) * cusp[1] - (cusp[0] - l0) * c1 <= 0.0) {\n const denom = c1 * cusp[0] + cusp[1] * (l0 - l1);\n t = denom === 0 ? 0 : (cusp[1] * l0) / denom;\n } else {\n const denom = c1 * (cusp[0] - 1.0) + cusp[1] * (l0 - l1);\n t = denom === 0 ? 0 : (cusp[1] * (l0 - 1.0)) / denom;\n\n const dl = l1 - l0;\n const dc = c1;\n\n const kl = dotYZ(OKLab_to_LMS_M[0], tmp3);\n const km = dotYZ(OKLab_to_LMS_M[1], tmp3);\n const ks = dotYZ(OKLab_to_LMS_M[2], tmp3);\n\n const ldt_ = dl + dc * kl;\n const mdt_ = dl + dc * km;\n const sdt_ = dl + dc * ks;\n\n const L = l0 * (1.0 - t) + t * l1;\n const C = t * c1;\n\n const l_ = L + C * kl;\n const m_ = L + C * km;\n const s_ = L + C * ks;\n\n const l = l_ * l_ * l_;\n const m = m_ * m_ * m_;\n const s = s_ * s_ * s_;\n\n const ldt = 3 * ldt_ * l_ * l_;\n const mdt = 3 * mdt_ * m_ * m_;\n const sdt = 3 * sdt_ * s_ * s_;\n\n const ldt2 = 6 * ldt_ * ldt_ * l_;\n const mdt2 = 6 * mdt_ * mdt_ * m_;\n const sdt2 = 6 * sdt_ * sdt_ * s_;\n\n const r_ = dotXYZ(lmsToRgb[0], l, m, s) - 1;\n const r1 = dotXYZ(lmsToRgb[0], ldt, mdt, sdt);\n const r2 = dotXYZ(lmsToRgb[0], ldt2, mdt2, sdt2);\n\n const ur = r1 / (r1 * r1 - 0.5 * r_ * r2);\n let tr = -r_ * ur;\n\n const g_ = dotXYZ(lmsToRgb[1], l, m, s) - 1;\n const g1 = dotXYZ(lmsToRgb[1], ldt, mdt, sdt);\n const g2 = dotXYZ(lmsToRgb[1], ldt2, mdt2, sdt2);\n\n const ug = g1 / (g1 * g1 - 0.5 * g_ * g2);\n let tg = -g_ * ug;\n\n const b_ = dotXYZ(lmsToRgb[2], l, m, s) - 1;\n const b1 = dotXYZ(lmsToRgb[2], ldt, mdt, sdt);\n const b2 = dotXYZ(lmsToRgb[2], ldt2, mdt2, sdt2);\n\n const ub = b1 / (b1 * b1 - 0.5 * b_ * b2);\n let tb = -b_ * ub;\n\n tr = ur >= 0.0 ? tr : floatMax;\n tg = ug >= 0.0 ? tg : floatMax;\n tb = ub >= 0.0 ? tb : floatMax;\n\n t += Math.min(tr, Math.min(tg, tb));\n }\n\n return t;\n};\n\nconst computeSt = (cusp: [number, number]): [number, number] => {\n const l = cusp[0];\n const c = cusp[1];\n return [c / l, c / (1 - l)];\n};\n\nconst computeStMid = (a: number, b: number): [number, number] => {\n const s =\n 0.11516993 +\n 1.0 /\n (7.4477897 +\n 4.1590124 * b +\n a *\n (-2.19557347 +\n 1.75198401 * b +\n a *\n (-2.13704948 -\n 10.02301043 * b +\n a * (-4.24894561 + 5.38770819 * b + 4.69891013 * a))));\n\n const t =\n 0.11239642 +\n 1.0 /\n (1.6132032 -\n 0.68124379 * b +\n a *\n (0.40370612 +\n 0.90148123 * b +\n a *\n (-0.27087943 +\n 0.6122399 * b +\n a * (0.00299215 - 0.45399568 * b - 0.14661872 * a))));\n\n return [s, t];\n};\n\nconst getCs = (\n L: number,\n a: number,\n b: number,\n cusp: [number, number],\n): [number, number, number] => {\n const cMax = findGamutIntersectionOKLCH(a, b, L, 1, L, cusp);\n const stMax = computeSt(cusp);\n\n const k = cMax / Math.min(L * stMax[0], (1 - L) * stMax[1]);\n\n const stMid = computeStMid(a, b);\n\n let ca = L * stMid[0];\n let cb = (1.0 - L) * stMid[1];\n const cMid =\n 0.9 * k * Math.sqrt(Math.sqrt(1.0 / (1.0 / ca ** 4 + 1.0 / cb ** 4)));\n\n ca = L * 0.4;\n cb = (1.0 - L) * 0.8;\n const c0 = Math.sqrt(1.0 / (1.0 / ca ** 2 + 1.0 / cb ** 2));\n\n return [c0, cMid, cMax];\n};\n\nconst OKHSLToOKLab = (hsl: Vec3): Vec3 => {\n let h = hsl[0];\n const s = hsl[1];\n const l = hsl[2];\n\n const L = toeInv(l);\n let a = 0;\n let b = 0;\n\n h = constrainAngle(h) / 360.0;\n\n if (L !== 0.0 && L !== 1.0 && s !== 0) {\n const a_ = Math.cos(TAU * h);\n const b_ = Math.sin(TAU * h);\n\n const cusp = findCuspOKLCH(a_, b_);\n const Cs = getCs(L, a_, b_, cusp);\n const [c0, cMid, cMax] = Cs;\n\n const mid = 0.8;\n const midInv = 1.25;\n let t: number, k0: number, k1: number, k2: number;\n\n if (s < mid) {\n t = midInv * s;\n k0 = 0.0;\n k1 = mid * c0;\n k2 = 1.0 - k1 / cMid;\n } else {\n t = 5 * (s - 0.8);\n k0 = cMid;\n k1 = (0.2 * cMid ** 2 * 1.25 ** 2) / c0;\n k2 = 1.0 - k1 / (cMax - cMid);\n }\n\n const c = k0 + (t * k1) / (1.0 - k2 * t);\n\n a = c * a_;\n b = c * b_;\n }\n\n return [L, a, b];\n};\n\n// ============================================================================\n// OKHSL to sRGB Conversion\n// ============================================================================\n\nconst okhslToSRGB = (h: number, s: number, l: number): Vec3 => {\n // h: 0-360, s: 0-1, l: 0-1\n const oklab = OKHSLToOKLab([h, s, l]);\n const linearRGB = OKLabToLinearSRGB(oklab);\n\n // Apply gamma correction and clamp\n return [\n clamp(sRGBLinearToGamma(linearRGB[0]), 0, 1),\n clamp(sRGBLinearToGamma(linearRGB[1]), 0, 1),\n clamp(sRGBLinearToGamma(linearRGB[2]), 0, 1),\n ];\n};\n\n// ============================================================================\n// Parsing Utilities\n// ============================================================================\n\n/**\n * Parse an angle value with optional unit.\n * Supports: deg, turn, rad, or unitless (treated as degrees).\n */\nconst parseAngle = (value: string): number => {\n const match = value.match(/^([+-]?\\d*\\.?\\d+)(deg|turn|rad)?$/);\n if (!match) return 0;\n\n const num = parseFloat(match[1]);\n const unit = match[2];\n\n switch (unit) {\n case 'turn':\n return num * 360;\n case 'rad':\n return (num * 180) / Math.PI;\n case 'deg':\n default:\n return num;\n }\n};\n\n/**\n * Parse a percentage value (e.g., \"50%\") to a 0-1 range.\n */\nconst parsePercentage = (value: string): number => {\n const match = value.match(/^([+-]?\\d*\\.?\\d+)%?$/);\n if (!match) return 0;\n\n const num = parseFloat(match[1]);\n // If the value includes %, divide by 100, otherwise assume 0-1 range\n return value.includes('%') ? num / 100 : num;\n};\n\n// ============================================================================\n// Plugin Implementation\n// ============================================================================\n\n/**\n * The okhsl function handler for tasty parser.\n * Receives parsed style groups and returns an RGB color string.\n */\nconst okhslFunc = (groups: StyleDetails[]): string => {\n // We expect a single group with 3 values: H, S, L\n // and an optional slash-separated alpha part\n if (groups.length === 0 || groups[0].all.length < 3) {\n console.warn('[okhsl] Expected 3 values (H S L), got:', groups);\n return 'rgb(0% 0% 0%)';\n }\n\n const group = groups[0];\n const tokens = group.all;\n\n // Alpha is in the second slash-separated part (e.g., okhsl(240 50% 50% / .5))\n const alpha =\n group.parts.length > 1 && group.parts[1].all.length > 0\n ? group.parts[1].output\n : undefined;\n\n // Create cache key from input tokens\n const cacheKey = tokens.slice(0, 3).join(' ') + (alpha ? ` / ${alpha}` : '');\n const cached = conversionCache.get(cacheKey);\n if (cached) return cached;\n\n const h = parseAngle(tokens[0]);\n const s = parsePercentage(tokens[1]);\n const l = parsePercentage(tokens[2]);\n\n const [r, g, b] = okhslToSRGB(h, clamp(s, 0, 1), clamp(l, 0, 1));\n\n const format = (n: number): string => {\n const pct = n * 100;\n return parseFloat(pct.toFixed(1)).toString() + '%';\n };\n\n const result = alpha\n ? `rgb(${format(r)} ${format(g)} ${format(b)} / ${alpha})`\n : `rgb(${format(r)} ${format(g)} ${format(b)})`;\n\n conversionCache.set(cacheKey, result);\n return result;\n};\n\n/**\n * OKHSL Plugin for Tasty.\n *\n * Adds support for the `okhsl()` color function in tasty styles.\n *\n * @example\n * ```ts\n * import { configure } from '@tenphi/tasty';\n * import { okhslPlugin } from '@tenphi/tasty';\n *\n * configure({\n * plugins: [okhslPlugin()],\n * });\n *\n * // Now you can use okhsl in styles:\n * const Box = tasty({\n * styles: {\n * fill: 'okhsl(240 50% 50%)',\n * },\n * });\n * ```\n */\nexport const okhslPlugin: TastyPluginFactory = (): TastyPlugin => ({\n name: 'okhsl',\n funcs: {\n okhsl: okhslFunc,\n },\n});\n\n// Export the raw function for direct use if needed\nexport { okhslFunc };\n"],"mappings":";;;;;;;;;;;;;;;;;AAqBA,MAAM,kBAAkB,IAAI,IAAoB,IAAI;AAMpD,MAAM,iBAA6C;CACjD;EAAC;EAAK;EAAoB;EAAmB;CAC7C;EAAC;EAAK;EAAqB;EAAoB;CAC/C;EAAC;EAAK;EAAqB;EAAoB;CAChD;AAED,MAAM,uBAAmD;CACvD;EAAC;EAAmB;EAAoB;EAAmB;CAC3D;EAAC;EAAqB;EAAoB;EAAoB;CAC9D;EAAC;EAAuB;EAAoB;EAAmB;CAChE;AAED,MAAM,oCAIF;CACF,CACE,CAAC,qBAAqB,mBAAoB,EAC1C;EAAC;EAAY;EAAY;EAAY;EAAY;EAAW,CAC7D;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;AACrC,QAAO;;;;;;;;;;;;;;;;;;;;;;;;AAyBT,MAAa,qBAAsD;CACjE,MAAM;CACN,OAAO,EACL,OAAO,WACR;CACF"}
|
|
1
|
+
{"version":3,"file":"okhsl-plugin.js","names":[],"sources":["../../src/plugins/okhsl-plugin.ts"],"sourcesContent":["/**\n * OKHSL Plugin for Tasty\n *\n * Converts OKHSL color syntax to RGB notation.\n * Supports angle units: deg, turn, rad, or unitless (degrees).\n *\n * Examples:\n * okhsl(240.5 50% 50%)\n * okhsl(240.5deg 50% 50%)\n * okhsl(0.25turn 50% 50%)\n * okhsl(1.57rad 50% 50%)\n */\n\nimport { Lru } from '../parser/lru';\nimport { okhslToSrgb } from '../utils/color-math';\n\nimport type { StyleDetails } from '../parser/types';\nimport type { TastyPlugin, TastyPluginFactory } from './types';\n\nconst conversionCache = new Lru<string, string>(500);\n\nconst clamp = (value: number, min: number, max: number): number =>\n Math.max(Math.min(value, max), min);\n\n/**\n * Parse an angle value with optional unit.\n * Supports: deg, turn, rad, or unitless (treated as degrees).\n */\nconst parseAngle = (value: string): number => {\n const match = value.match(/^([+-]?\\d*\\.?\\d+)(deg|turn|rad)?$/);\n if (!match) return 0;\n\n const num = parseFloat(match[1]);\n const unit = match[2];\n\n switch (unit) {\n case 'turn':\n return num * 360;\n case 'rad':\n return (num * 180) / Math.PI;\n case 'deg':\n default:\n return num;\n }\n};\n\n/**\n * Parse a percentage value (e.g., \"50%\") to a 0-1 range.\n */\nconst parsePercentage = (value: string): number => {\n const match = value.match(/^([+-]?\\d*\\.?\\d+)%?$/);\n if (!match) return 0;\n\n const num = parseFloat(match[1]);\n return value.includes('%') ? num / 100 : num;\n};\n\n/**\n * The okhsl function handler for tasty parser.\n * Receives parsed style groups and returns an RGB color string.\n */\nconst okhslFunc = (groups: StyleDetails[]): string => {\n if (groups.length === 0 || groups[0].all.length < 3) {\n console.warn('[okhsl] Expected 3 values (H S L), got:', groups);\n return 'rgb(0% 0% 0%)';\n }\n\n const group = groups[0];\n const tokens = group.all;\n\n const alpha =\n group.parts.length > 1 && group.parts[1].all.length > 0\n ? group.parts[1].output\n : undefined;\n\n const cacheKey = tokens.slice(0, 3).join(' ') + (alpha ? ` / ${alpha}` : '');\n const cached = conversionCache.get(cacheKey);\n if (cached) return cached;\n\n const h = parseAngle(tokens[0]);\n const s = parsePercentage(tokens[1]);\n const l = parsePercentage(tokens[2]);\n\n const [r, g, b] = okhslToSrgb(h, clamp(s, 0, 1), clamp(l, 0, 1));\n\n const format = (n: number): string => {\n const pct = n * 100;\n return parseFloat(pct.toFixed(1)).toString() + '%';\n };\n\n const result = alpha\n ? `rgb(${format(r)} ${format(g)} ${format(b)} / ${alpha})`\n : `rgb(${format(r)} ${format(g)} ${format(b)})`;\n\n conversionCache.set(cacheKey, result);\n return result;\n};\n\n/**\n * OKHSL Plugin for Tasty.\n *\n * Adds support for the `okhsl()` color function in tasty styles.\n *\n * @example\n * ```ts\n * import { configure } from '@tenphi/tasty';\n * import { okhslPlugin } from '@tenphi/tasty';\n *\n * configure({\n * plugins: [okhslPlugin()],\n * });\n *\n * // Now you can use okhsl in styles:\n * const Box = tasty({\n * styles: {\n * fill: 'okhsl(240 50% 50%)',\n * },\n * });\n * ```\n */\nexport const okhslPlugin: TastyPluginFactory = (): TastyPlugin => ({\n name: 'okhsl',\n funcs: {\n okhsl: okhslFunc,\n },\n});\n\nexport { okhslFunc };\n"],"mappings":";;;;;;;;;;;;;;;;AAmBA,MAAM,kBAAkB,IAAI,IAAoB,IAAI;AAEpD,MAAM,SAAS,OAAe,KAAa,QACzC,KAAK,IAAI,KAAK,IAAI,OAAO,IAAI,EAAE,IAAI;;;;;AAMrC,MAAM,cAAc,UAA0B;CAC5C,MAAM,QAAQ,MAAM,MAAM,oCAAoC;AAC9D,KAAI,CAAC,MAAO,QAAO;CAEnB,MAAM,MAAM,WAAW,MAAM,GAAG;AAGhC,SAFa,MAAM,IAEnB;EACE,KAAK,OACH,QAAO,MAAM;EACf,KAAK,MACH,QA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rC,QAAO;;;;;;;;;;;;;;;;;;;;;;;;AAyBT,MAAa,qBAAsD;CACjE,MAAM;CACN,OAAO,EACL,OAAO,WACR;CACF"}
|
package/dist/plugins/types.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ interface TastyPlugin {
|
|
|
33
33
|
* Design tokens injected as CSS custom properties on `:root`.
|
|
34
34
|
* Values are parsed through the Tasty DSL. Supports state maps.
|
|
35
35
|
* - `$name` → `--name` CSS custom property
|
|
36
|
-
* - `#name` → `--name-color` and `--name-color-
|
|
36
|
+
* - `#name` → `--name-color` and `--name-color-{colorSpace}`
|
|
37
37
|
*/
|
|
38
38
|
tokens?: ConfigTokens;
|
|
39
39
|
/** Predefined tokens replaced during style parsing (`$name` or `#name`) */
|
package/dist/properties/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { RE_NUMBER, RE_RAW_UNIT } from "../parser/const.js";
|
|
2
|
+
import { colorInitialValueToComponents } from "../utils/color-space.js";
|
|
2
3
|
|
|
3
4
|
//#region src/properties/index.ts
|
|
4
5
|
const PROPERTIES_KEY = "@properties";
|
|
@@ -137,20 +138,6 @@ function getEffectiveDefinition(token, userDefinition) {
|
|
|
137
138
|
isValid: true
|
|
138
139
|
};
|
|
139
140
|
}
|
|
140
|
-
/**
|
|
141
|
-
* Convert a color initialValue to an RGB string for the companion `-rgb` property.
|
|
142
|
-
* Used by SSR to emit `@property --name-color-rgb { syntax: "<number>+"; ... }`.
|
|
143
|
-
*/
|
|
144
|
-
function colorInitialValueToRgb(initialValue) {
|
|
145
|
-
if (initialValue == null) return "0 0 0";
|
|
146
|
-
const val = String(initialValue).trim().toLowerCase();
|
|
147
|
-
if (val === "transparent" || val === "rgba(0,0,0,0)" || val === "") return "0 0 0";
|
|
148
|
-
if (val === "white") return "255 255 255";
|
|
149
|
-
if (val === "black") return "0 0 0";
|
|
150
|
-
const rgbMatch = val.match(/^rgba?\(\s*(\d+)[,\s]+(\d+)[,\s]+(\d+)/);
|
|
151
|
-
if (rgbMatch) return `${rgbMatch[1]} ${rgbMatch[2]} ${rgbMatch[3]}`;
|
|
152
|
-
return "0 0 0";
|
|
153
|
-
}
|
|
154
141
|
const UNIT_TO_SYNTAX = {};
|
|
155
142
|
const LENGTH_UNITS = [
|
|
156
143
|
"px",
|
|
@@ -232,5 +219,5 @@ function inferSyntaxFromValue(value) {
|
|
|
232
219
|
}
|
|
233
220
|
|
|
234
221
|
//#endregion
|
|
235
|
-
export {
|
|
222
|
+
export { extractLocalProperties, getEffectiveDefinition, hasLocalProperties, inferSyntaxFromValue, normalizePropertyDefinition, parsePropertyToken };
|
|
236
223
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../../src/properties/index.ts"],"sourcesContent":["/**\n * Properties Utilities\n *\n * Utilities for extracting and processing CSS @property definitions in styles.\n * Unlike keyframes, properties are permanent once registered and don't need cleanup.\n *\n * Property names use tasty token syntax:\n * - `$name` for regular properties → `--name`\n * - `#name` for color properties → `--name-color` (auto-sets syntax: '<color>')\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport { RE_NUMBER, RE_RAW_UNIT } from '../parser/const';\nimport type { Styles } from '../styles/types';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst PROPERTIES_KEY = '@properties';\n\n/**\n * Valid CSS custom property name pattern (after the -- prefix).\n * Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.\n */\nconst VALID_PROPERTY_NAME_PATTERN = /^[a-z_][a-z0-9-_]*$/i;\n\n// ============================================================================\n// Validation Functions\n// ============================================================================\n\n/**\n * Validate a CSS custom property name (the part after --).\n * Returns true if the name is valid for use as a CSS custom property.\n */\nexport function isValidPropertyName(name: string): boolean {\n return VALID_PROPERTY_NAME_PATTERN.test(name);\n}\n\n/**\n * Result of parsing a property token.\n */\nexport interface ParsedPropertyToken {\n /** The CSS custom property name (e.g., '--my-prop') */\n cssName: string;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n// ============================================================================\n// Extraction Functions\n// ============================================================================\n\n/**\n * Check if styles object has local @properties definition.\n * Fast path: single property lookup.\n */\nexport function hasLocalProperties(styles: Styles): boolean {\n return PROPERTIES_KEY in styles;\n}\n\n/**\n * Extract local @properties from styles object.\n * Returns null if no local properties (fast path).\n */\nexport function extractLocalProperties(\n styles: Styles,\n): Record<string, PropertyDefinition> | null {\n const properties = styles[PROPERTIES_KEY];\n if (!properties || typeof properties !== 'object') {\n return null;\n }\n return properties as Record<string, PropertyDefinition>;\n}\n\n// ============================================================================\n// Token Parsing Functions\n// ============================================================================\n\n/**\n * Parse a property token name and return the CSS property name and whether it's a color.\n * Supports tasty token syntax and validates the property name.\n *\n * Token formats:\n * - `$name` → { cssName: '--name', isColor: false }\n * - `#name` → { cssName: '--name-color', isColor: true }\n * - `--name` → { cssName: '--name', isColor: false } (legacy, auto-detect color by suffix)\n * - `name` → { cssName: '--name', isColor: false } (legacy)\n *\n * @param token - The property token to parse\n * @returns Parsed result with cssName, isColor, isValid, and optional error\n */\nexport function parsePropertyToken(token: string): ParsedPropertyToken {\n if (!token || typeof token !== 'string') {\n return {\n cssName: '',\n isColor: false,\n isValid: false,\n error: 'Property token must be a non-empty string',\n };\n }\n\n let name: string;\n let isColor: boolean;\n\n if (token.startsWith('$')) {\n // Regular property token: $name → --name\n name = token.slice(1);\n isColor = false;\n } else if (token.startsWith('#')) {\n // Color property token: #name → --name-color\n name = token.slice(1);\n isColor = true;\n } else if (token.startsWith('--')) {\n // Legacy format with -- prefix\n name = token.slice(2);\n isColor = token.endsWith('-color');\n } else {\n // Legacy format without prefix\n name = token;\n isColor = token.endsWith('-color');\n }\n\n // Validate the name\n if (!name) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: 'Property name cannot be empty',\n };\n }\n\n if (!isValidPropertyName(name)) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: `Invalid property name \"${name}\". Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.`,\n };\n }\n\n // Build the CSS custom property name\n // For #name tokens, we add -color suffix\n // For legacy formats (--name-color or name-color), the name already includes -color\n let cssName: string;\n if (token.startsWith('#')) {\n // Color token: #name → --name-color\n cssName = `--${name}-color`;\n } else {\n // All other formats: just add -- prefix\n cssName = `--${name}`;\n }\n\n return {\n cssName,\n isColor,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Normalization Functions\n// ============================================================================\n\n/**\n * Normalize a property name to the CSS custom property format.\n *\n * @deprecated Use parsePropertyToken instead for proper token handling\n */\nexport function normalizePropertyName(name: string): string {\n const result = parsePropertyToken(name);\n return result.isValid ? result.cssName : `--${name}`;\n}\n\n/**\n * Normalize a property definition to a consistent string representation.\n * Used for comparing definitions to detect type conflicts.\n *\n * Only `syntax` and `inherits` are compared — `initialValue` is intentionally\n * excluded because different components may set different defaults for the\n * same typed property (e.g. auto-inferred `0px` vs explicit `6px`).\n *\n * Keys are sorted alphabetically to ensure consistent comparison:\n * { inherits: true, syntax: '<color>' } === { syntax: '<color>', inherits: true }\n */\nexport function normalizePropertyDefinition(def: PropertyDefinition): string {\n const normalized: Record<string, unknown> = {};\n\n if (def.inherits !== undefined) {\n normalized.inherits = def.inherits;\n }\n if (def.syntax !== undefined) {\n normalized.syntax = def.syntax;\n }\n\n return JSON.stringify(normalized);\n}\n\n/**\n * Result of getEffectiveDefinition.\n */\nexport interface EffectiveDefinitionResult {\n /** The CSS custom property name */\n cssName: string;\n /** The effective property definition */\n definition: PropertyDefinition;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n/**\n * Get the effective property definition for a token.\n * For color tokens (#name), auto-sets syntax to '<color>' and defaults initialValue to 'transparent'.\n *\n * @param token - Property token ($name, #name, --name, or plain name)\n * @param userDefinition - User-provided definition options\n * @returns Effective definition with cssName, definition, isValid, and optional error\n */\nexport function getEffectiveDefinition(\n token: string,\n userDefinition: PropertyDefinition,\n): EffectiveDefinitionResult {\n const parsed = parsePropertyToken(token);\n\n if (!parsed.isValid) {\n return {\n cssName: '',\n definition: userDefinition,\n isColor: false,\n isValid: false,\n error: parsed.error,\n };\n }\n\n if (parsed.isColor) {\n // Color properties have fixed syntax and default initialValue\n return {\n cssName: parsed.cssName,\n definition: {\n syntax: '<color>', // Always '<color>' for color tokens, cannot be overridden\n inherits: userDefinition.inherits, // Allow inherits to be customized\n initialValue: userDefinition.initialValue ?? 'transparent', // Default to transparent\n },\n isColor: true,\n isValid: true,\n };\n }\n\n // Regular properties use the definition as-is\n return {\n cssName: parsed.cssName,\n definition: userDefinition,\n isColor: false,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Color Utilities\n// ============================================================================\n\n/**\n * Convert a color initialValue to an RGB string for the companion `-rgb` property.\n * Used by SSR to emit `@property --name-color-rgb { syntax: \"<number>+\"; ... }`.\n */\nexport function colorInitialValueToRgb(\n initialValue: string | number | undefined,\n): string {\n if (initialValue == null) return '0 0 0';\n\n const val = String(initialValue).trim().toLowerCase();\n\n if (val === 'transparent' || val === 'rgba(0,0,0,0)' || val === '') {\n return '0 0 0';\n }\n\n // Named color: white\n if (val === 'white') return '255 255 255';\n if (val === 'black') return '0 0 0';\n\n // rgb(R G B) or rgb(R, G, B) — extract components\n const rgbMatch = val.match(/^rgba?\\(\\s*(\\d+)[,\\s]+(\\d+)[,\\s]+(\\d+)/);\n if (rgbMatch) {\n return `${rgbMatch[1]} ${rgbMatch[2]} ${rgbMatch[3]}`;\n }\n\n // Fallback for any other value\n return '0 0 0';\n}\n\n// ============================================================================\n// Value Type Inference\n// ============================================================================\n\n/**\n * Result of inferring a CSS @property syntax from a value.\n */\nexport interface InferredSyntax {\n syntax: string;\n initialValue: string;\n}\n\nconst UNIT_TO_SYNTAX: Record<string, InferredSyntax> = {};\n\nconst LENGTH_UNITS = [\n 'px',\n 'em',\n 'rem',\n 'vw',\n 'vh',\n 'vmin',\n 'vmax',\n 'ch',\n 'ex',\n 'cap',\n 'ic',\n 'lh',\n 'rlh',\n 'svw',\n 'svh',\n 'lvw',\n 'lvh',\n 'dvw',\n 'dvh',\n 'cqw',\n 'cqh',\n 'cqi',\n 'cqb',\n 'cqmin',\n 'cqmax',\n];\n\nconst ANGLE_UNITS = ['deg', 'rad', 'grad', 'turn'];\nconst TIME_UNITS = ['ms', 's'];\n\nfor (const u of LENGTH_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<length-percentage>', initialValue: '0px' };\n}\nUNIT_TO_SYNTAX['%'] = { syntax: '<length-percentage>', initialValue: '0px' };\nfor (const u of ANGLE_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<angle>', initialValue: '0deg' };\n}\nfor (const u of TIME_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<time>', initialValue: '0s' };\n}\n\n/**\n * Infer CSS @property syntax from a concrete value.\n * Detects numeric types: \\<number\\>, \\<length-percentage\\>, \\<angle\\>, \\<time\\>.\n * Length and percentage values both map to \\<length-percentage\\> for maximum flexibility.\n * Color properties are handled separately via the #name token convention\n * (--name-color gets \\<color\\> syntax automatically in getEffectiveDefinition).\n *\n * @param value - The CSS value to infer from (e.g. '10px', '1', '45deg')\n * @returns Inferred syntax and initial value, or null if not inferable\n */\nexport function inferSyntaxFromValue(value: string): InferredSyntax | null {\n if (!value || typeof value !== 'string') return null;\n\n const trimmed = value.trim();\n if (!trimmed) return null;\n\n if (RE_NUMBER.test(trimmed)) {\n // Bare zero is ambiguous (could be <length>, <angle>, <percentage>, etc.)\n if (parseFloat(trimmed) === 0) return null;\n return { syntax: '<number>', initialValue: '0' };\n }\n\n const unitMatch = trimmed.match(RE_RAW_UNIT);\n if (unitMatch) {\n const unit = unitMatch[2];\n const mapping = UNIT_TO_SYNTAX[unit];\n if (mapping) return mapping;\n }\n\n return null;\n}\n"],"mappings":";;;AAmBA,MAAM,iBAAiB;;;;;AAMvB,MAAM,8BA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ACd,gBAAe,KAAK;CAAE,QAAQ;CAAU,cAAc;CAAM;;;;;;;;;;;AAa9D,SAAgB,qBAAqB,OAAsC;AACzE,KAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;CAEhD,MAAM,UAAU,MAAM,MAAM;AAC5B,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,UAAU,KAAK,QAAQ,EAAE;AAE3B,MAAI,WAAW,QAAQ,KAAK,EAAG,QAAO;AACtC,SAAO;GAAE,QAAQ;GAAY,cAAc;GAAK;;CAGlD,MAAM,YAAY,QAAQ,MAAM,YAAY;AAC5C,KAAI,WAAW;EAEb,MAAM,UAAU,eADH,UAAU;AAEvB,MAAI,QAAS,QAAO;;AAGtB,QAAO"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/properties/index.ts"],"sourcesContent":["/**\n * Properties Utilities\n *\n * Utilities for extracting and processing CSS @property definitions in styles.\n * Unlike keyframes, properties are permanent once registered and don't need cleanup.\n *\n * Property names use tasty token syntax:\n * - `$name` for regular properties → `--name`\n * - `#name` for color properties → `--name-color` (auto-sets syntax: '<color>')\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport { RE_NUMBER, RE_RAW_UNIT } from '../parser/const';\nimport type { Styles } from '../styles/types';\n\n// ============================================================================\n// Constants\n// ============================================================================\n\nconst PROPERTIES_KEY = '@properties';\n\n/**\n * Valid CSS custom property name pattern (after the -- prefix).\n * Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.\n */\nconst VALID_PROPERTY_NAME_PATTERN = /^[a-z_][a-z0-9-_]*$/i;\n\n// ============================================================================\n// Validation Functions\n// ============================================================================\n\n/**\n * Validate a CSS custom property name (the part after --).\n * Returns true if the name is valid for use as a CSS custom property.\n */\nexport function isValidPropertyName(name: string): boolean {\n return VALID_PROPERTY_NAME_PATTERN.test(name);\n}\n\n/**\n * Result of parsing a property token.\n */\nexport interface ParsedPropertyToken {\n /** The CSS custom property name (e.g., '--my-prop') */\n cssName: string;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n// ============================================================================\n// Extraction Functions\n// ============================================================================\n\n/**\n * Check if styles object has local @properties definition.\n * Fast path: single property lookup.\n */\nexport function hasLocalProperties(styles: Styles): boolean {\n return PROPERTIES_KEY in styles;\n}\n\n/**\n * Extract local @properties from styles object.\n * Returns null if no local properties (fast path).\n */\nexport function extractLocalProperties(\n styles: Styles,\n): Record<string, PropertyDefinition> | null {\n const properties = styles[PROPERTIES_KEY];\n if (!properties || typeof properties !== 'object') {\n return null;\n }\n return properties as Record<string, PropertyDefinition>;\n}\n\n// ============================================================================\n// Token Parsing Functions\n// ============================================================================\n\n/**\n * Parse a property token name and return the CSS property name and whether it's a color.\n * Supports tasty token syntax and validates the property name.\n *\n * Token formats:\n * - `$name` → { cssName: '--name', isColor: false }\n * - `#name` → { cssName: '--name-color', isColor: true }\n * - `--name` → { cssName: '--name', isColor: false } (legacy, auto-detect color by suffix)\n * - `name` → { cssName: '--name', isColor: false } (legacy)\n *\n * @param token - The property token to parse\n * @returns Parsed result with cssName, isColor, isValid, and optional error\n */\nexport function parsePropertyToken(token: string): ParsedPropertyToken {\n if (!token || typeof token !== 'string') {\n return {\n cssName: '',\n isColor: false,\n isValid: false,\n error: 'Property token must be a non-empty string',\n };\n }\n\n let name: string;\n let isColor: boolean;\n\n if (token.startsWith('$')) {\n // Regular property token: $name → --name\n name = token.slice(1);\n isColor = false;\n } else if (token.startsWith('#')) {\n // Color property token: #name → --name-color\n name = token.slice(1);\n isColor = true;\n } else if (token.startsWith('--')) {\n // Legacy format with -- prefix\n name = token.slice(2);\n isColor = token.endsWith('-color');\n } else {\n // Legacy format without prefix\n name = token;\n isColor = token.endsWith('-color');\n }\n\n // Validate the name\n if (!name) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: 'Property name cannot be empty',\n };\n }\n\n if (!isValidPropertyName(name)) {\n return {\n cssName: '',\n isColor,\n isValid: false,\n error: `Invalid property name \"${name}\". Must start with a letter or underscore, followed by letters, digits, hyphens, or underscores.`,\n };\n }\n\n // Build the CSS custom property name\n // For #name tokens, we add -color suffix\n // For legacy formats (--name-color or name-color), the name already includes -color\n let cssName: string;\n if (token.startsWith('#')) {\n // Color token: #name → --name-color\n cssName = `--${name}-color`;\n } else {\n // All other formats: just add -- prefix\n cssName = `--${name}`;\n }\n\n return {\n cssName,\n isColor,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Normalization Functions\n// ============================================================================\n\n/**\n * Normalize a property name to the CSS custom property format.\n *\n * @deprecated Use parsePropertyToken instead for proper token handling\n */\nexport function normalizePropertyName(name: string): string {\n const result = parsePropertyToken(name);\n return result.isValid ? result.cssName : `--${name}`;\n}\n\n/**\n * Normalize a property definition to a consistent string representation.\n * Used for comparing definitions to detect type conflicts.\n *\n * Only `syntax` and `inherits` are compared — `initialValue` is intentionally\n * excluded because different components may set different defaults for the\n * same typed property (e.g. auto-inferred `0px` vs explicit `6px`).\n *\n * Keys are sorted alphabetically to ensure consistent comparison:\n * { inherits: true, syntax: '<color>' } === { syntax: '<color>', inherits: true }\n */\nexport function normalizePropertyDefinition(def: PropertyDefinition): string {\n const normalized: Record<string, unknown> = {};\n\n if (def.inherits !== undefined) {\n normalized.inherits = def.inherits;\n }\n if (def.syntax !== undefined) {\n normalized.syntax = def.syntax;\n }\n\n return JSON.stringify(normalized);\n}\n\n/**\n * Result of getEffectiveDefinition.\n */\nexport interface EffectiveDefinitionResult {\n /** The CSS custom property name */\n cssName: string;\n /** The effective property definition */\n definition: PropertyDefinition;\n /** Whether this is a color property */\n isColor: boolean;\n /** Whether the token was valid */\n isValid: boolean;\n /** Error message if invalid */\n error?: string;\n}\n\n/**\n * Get the effective property definition for a token.\n * For color tokens (#name), auto-sets syntax to '<color>' and defaults initialValue to 'transparent'.\n *\n * @param token - Property token ($name, #name, --name, or plain name)\n * @param userDefinition - User-provided definition options\n * @returns Effective definition with cssName, definition, isValid, and optional error\n */\nexport function getEffectiveDefinition(\n token: string,\n userDefinition: PropertyDefinition,\n): EffectiveDefinitionResult {\n const parsed = parsePropertyToken(token);\n\n if (!parsed.isValid) {\n return {\n cssName: '',\n definition: userDefinition,\n isColor: false,\n isValid: false,\n error: parsed.error,\n };\n }\n\n if (parsed.isColor) {\n // Color properties have fixed syntax and default initialValue\n return {\n cssName: parsed.cssName,\n definition: {\n syntax: '<color>', // Always '<color>' for color tokens, cannot be overridden\n inherits: userDefinition.inherits, // Allow inherits to be customized\n initialValue: userDefinition.initialValue ?? 'transparent', // Default to transparent\n },\n isColor: true,\n isValid: true,\n };\n }\n\n // Regular properties use the definition as-is\n return {\n cssName: parsed.cssName,\n definition: userDefinition,\n isColor: false,\n isValid: true,\n };\n}\n\n// ============================================================================\n// Color Utilities\n// ============================================================================\n\nexport { colorInitialValueToComponents } from '../utils/color-space';\n\n// ============================================================================\n// Value Type Inference\n// ============================================================================\n\n/**\n * Result of inferring a CSS @property syntax from a value.\n */\nexport interface InferredSyntax {\n syntax: string;\n initialValue: string;\n}\n\nconst UNIT_TO_SYNTAX: Record<string, InferredSyntax> = {};\n\nconst LENGTH_UNITS = [\n 'px',\n 'em',\n 'rem',\n 'vw',\n 'vh',\n 'vmin',\n 'vmax',\n 'ch',\n 'ex',\n 'cap',\n 'ic',\n 'lh',\n 'rlh',\n 'svw',\n 'svh',\n 'lvw',\n 'lvh',\n 'dvw',\n 'dvh',\n 'cqw',\n 'cqh',\n 'cqi',\n 'cqb',\n 'cqmin',\n 'cqmax',\n];\n\nconst ANGLE_UNITS = ['deg', 'rad', 'grad', 'turn'];\nconst TIME_UNITS = ['ms', 's'];\n\nfor (const u of LENGTH_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<length-percentage>', initialValue: '0px' };\n}\nUNIT_TO_SYNTAX['%'] = { syntax: '<length-percentage>', initialValue: '0px' };\nfor (const u of ANGLE_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<angle>', initialValue: '0deg' };\n}\nfor (const u of TIME_UNITS) {\n UNIT_TO_SYNTAX[u] = { syntax: '<time>', initialValue: '0s' };\n}\n\n/**\n * Infer CSS @property syntax from a concrete value.\n * Detects numeric types: \\<number\\>, \\<length-percentage\\>, \\<angle\\>, \\<time\\>.\n * Length and percentage values both map to \\<length-percentage\\> for maximum flexibility.\n * Color properties are handled separately via the #name token convention\n * (--name-color gets \\<color\\> syntax automatically in getEffectiveDefinition).\n *\n * @param value - The CSS value to infer from (e.g. '10px', '1', '45deg')\n * @returns Inferred syntax and initial value, or null if not inferable\n */\nexport function inferSyntaxFromValue(value: string): InferredSyntax | null {\n if (!value || typeof value !== 'string') return null;\n\n const trimmed = value.trim();\n if (!trimmed) return null;\n\n if (RE_NUMBER.test(trimmed)) {\n // Bare zero is ambiguous (could be <length>, <angle>, <percentage>, etc.)\n if (parseFloat(trimmed) === 0) return null;\n return { syntax: '<number>', initialValue: '0' };\n }\n\n const unitMatch = trimmed.match(RE_RAW_UNIT);\n if (unitMatch) {\n const unit = unitMatch[2];\n const mapping = UNIT_TO_SYNTAX[unit];\n if (mapping) return mapping;\n }\n\n return null;\n}\n"],"mappings":";;;;AAmBA,MAAM,iBAAiB;;;;;AAMvB,MAAM,8BAA8B;;;;;AAUpC,SAAgB,oBAAoB,MAAuB;AACzD,QAAO,4BAA4B,KAAK,KAAK;;;;;;AAyB/C,SAAgB,mBAAmB,QAAyB;AAC1D,QAAO,kBAAkB;;;;;;AAO3B,SAAgB,uBACd,QAC2C;CAC3C,MAAM,aAAa,OAAO;AAC1B,KAAI,CAAC,cAAc,OAAO,eAAe,SACvC,QAAO;AAET,QAAO;;;;;;;;;;;;;;;AAoBT,SAAgB,mBAAmB,OAAoC;AACrE,KAAI,CAAC,SAAS,OAAO,UAAU,SAC7B,QAAO;EACL,SAAS;EACT,SAAS;EACT,SAAS;EACT,OAAO;EACR;CAGH,IAAI;CACJ,IAAI;AAEJ,KAAI,MAAM,WAAW,IAAI,EAAE;AAEz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ACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACD;AAED,MAAM,cAAc;CAAC;CAAO;CAAO;CAAQ;CAAO;AAClD,MAAM,aAAa,CAAC,MAAM,IAAI;AAE9B,KAAK,MAAM,KAAK,aACd,gBAAe,KAAK;CAAE,QAAQ;CAAuB,cAAc;CAAO;AAE5E,eAAe,OAAO;CAAE,QAAQ;CAAuB,cAAc;CAAO;AAC5E,KAAK,MAAM,KAAK,YACd,gBAAe,KAAK;CAAE,QAAQ;CAAW,cAAc;CAAQ;AAEjE,KAAK,MAAM,KAAK,WACd,gBAAe,KAAK;CAAE,QAAQ;CAAU,cAAc;CAAM;;;;;;;;;;;AAa9D,SAAgB,qBAAqB,OAAsC;AACzE,KAAI,CAAC,SAAS,OAAO,UAAU,SAAU,QAAO;CAEhD,MAAM,UAAU,MAAM,MAAM;AAC5B,KAAI,CAAC,QAAS,QAAO;AAErB,KAAI,UAAU,KAAK,QAAQ,EAAE;AAE3B,MAAI,WAAW,QAAQ,KAAK,EAAG,QAAO;AACtC,SAAO;GAAE,QAAQ;GAAY,cAAc;GAAK;;CAGlD,MAAM,YAAY,QAAQ,MAAM,YAAY;AAC5C,KAAI,WAAW;EAEb,MAAM,UAAU,eADH,UAAU;AAEvB,MAAI,QAAS,QAAO;;AAGtB,QAAO"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { colorInitialValueToComponents, getColorSpaceSuffix, getComponentPropertySyntax } from "../utils/color-space.js";
|
|
2
|
+
import { getEffectiveDefinition } from "../properties/index.js";
|
|
2
3
|
import { parseStyle } from "../utils/styles.js";
|
|
3
4
|
|
|
4
5
|
//#region src/ssr/format-property.ts
|
|
@@ -7,7 +8,7 @@ import { parseStyle } from "../utils/styles.js";
|
|
|
7
8
|
*
|
|
8
9
|
* Returns the full `@property --name { ... }` text, or empty string
|
|
9
10
|
* if the token is invalid. For color properties, also returns
|
|
10
|
-
* the companion
|
|
11
|
+
* the companion component property.
|
|
11
12
|
*/
|
|
12
13
|
function formatPropertyCSS(token, definition) {
|
|
13
14
|
const result = getEffectiveDefinition(token, definition);
|
|
@@ -15,12 +16,13 @@ function formatPropertyCSS(token, definition) {
|
|
|
15
16
|
const rules = [];
|
|
16
17
|
rules.push(buildPropertyRule(result.cssName, result.definition));
|
|
17
18
|
if (result.isColor) {
|
|
18
|
-
const
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
const suffix = getColorSpaceSuffix();
|
|
20
|
+
const componentCssName = `${result.cssName}-${suffix}`;
|
|
21
|
+
const componentInitial = colorInitialValueToComponents(result.definition.initialValue);
|
|
22
|
+
rules.push(buildPropertyRule(componentCssName, {
|
|
23
|
+
syntax: getComponentPropertySyntax(),
|
|
22
24
|
inherits: result.definition.inherits,
|
|
23
|
-
initialValue:
|
|
25
|
+
initialValue: componentInitial
|
|
24
26
|
}));
|
|
25
27
|
}
|
|
26
28
|
return rules.join("\n");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"format-property.js","names":[],"sources":["../../src/ssr/format-property.ts"],"sourcesContent":["/**\n * Format @property CSS rules for SSR output.\n *\n * Replicates the CSS construction from StyleInjector.property()\n * but returns a CSS string instead of inserting into the DOM.\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport {
|
|
1
|
+
{"version":3,"file":"format-property.js","names":[],"sources":["../../src/ssr/format-property.ts"],"sourcesContent":["/**\n * Format @property CSS rules for SSR output.\n *\n * Replicates the CSS construction from StyleInjector.property()\n * but returns a CSS string instead of inserting into the DOM.\n */\n\nimport type { PropertyDefinition } from '../injector/types';\nimport { getEffectiveDefinition } from '../properties';\nimport {\n colorInitialValueToComponents,\n getColorSpaceSuffix,\n getComponentPropertySyntax,\n} from '../utils/color-space';\nimport type { StyleValue } from '../utils/styles';\nimport { parseStyle } from '../utils/styles';\n\n/**\n * Format a single @property rule as a CSS string.\n *\n * Returns the full `@property --name { ... }` text, or empty string\n * if the token is invalid. For color properties, also returns\n * the companion component property.\n */\nexport function formatPropertyCSS(\n token: string,\n definition: PropertyDefinition,\n): string {\n const result = getEffectiveDefinition(token, definition);\n if (!result.isValid) return '';\n\n const rules: string[] = [];\n\n rules.push(buildPropertyRule(result.cssName, result.definition));\n\n if (result.isColor) {\n const suffix = getColorSpaceSuffix();\n const componentCssName = `${result.cssName}-${suffix}`;\n const componentInitial = colorInitialValueToComponents(\n result.definition.initialValue,\n );\n rules.push(\n buildPropertyRule(componentCssName, {\n syntax: getComponentPropertySyntax(),\n inherits: result.definition.inherits,\n initialValue: componentInitial,\n }),\n );\n }\n\n return rules.join('\\n');\n}\n\nfunction buildPropertyRule(\n cssName: string,\n definition: PropertyDefinition,\n): string {\n const parts: string[] = [];\n\n if (definition.syntax != null) {\n let syntax = String(definition.syntax).trim();\n if (!/^['\"]/u.test(syntax)) syntax = `\"${syntax}\"`;\n parts.push(`syntax: ${syntax};`);\n }\n\n const inherits = definition.inherits ?? true;\n parts.push(`inherits: ${inherits ? 'true' : 'false'};`);\n\n if (definition.initialValue != null) {\n let initialValueStr: string;\n if (typeof definition.initialValue === 'number') {\n initialValueStr = String(definition.initialValue);\n } else {\n initialValueStr = parseStyle(\n definition.initialValue as StyleValue,\n ).output;\n }\n parts.push(`initial-value: ${initialValueStr};`);\n }\n\n const declarations = parts.join(' ').trim();\n return `@property ${cssName} { ${declarations} }`;\n}\n"],"mappings":";;;;;;;;;;;;AAwBA,SAAgB,kBACd,OACA,YACQ;CACR,MAAM,SAAS,uBAAuB,OAAO,WAAW;AACxD,KAAI,CAAC,OAAO,QAAS,QAAO;CAE5B,MAAM,QAAkB,EAAE;AAE1B,OAAM,KAAK,kBAAkB,OAAO,SAAS,OAAO,WAAW,CAAC;AAEhE,KAAI,OAAO,SAAS;EAClB,MAAM,SAAS,qBAAqB;EACpC,MAAM,mBAAmB,GAAG,OAAO,QAAQ,GAAG;EAC9C,MAAM,mBAAmB,8BACvB,OAAO,WAAW,aACnB;AACD,QAAM,KACJ,kBAAkB,kBAAkB;GAClC,QAAQ,4BAA4B;GACpC,UAAU,OAAO,WAAW;GAC5B,cAAc;GACf,CAAC,CACH;;AAGH,QAAO,MAAM,KAAK,KAAK;;AAGzB,SAAS,kBACP,SACA,YACQ;CACR,MAAM,QAAkB,EAAE;AAE1B,KAAI,WAAW,UAAU,MAAM;EAC7B,IAAI,SAAS,OAAO,WAAW,OAAO,CAAC,MAAM;AAC7C,MAAI,CAAC,SAAS,KAAK,OAAO,CAAE,UAAS,IAAI,OAAO;AAChD,QAAM,KAAK,WAAW,OAAO,GAAG;;CAGlC,MAAM,WAAW,WAAW,YAAY;AACxC,OAAM,KAAK,aAAa,WAAW,SAAS,QAAQ,GAAG;AAEvD,KAAI,WAAW,gBAAgB,MAAM;EACnC,IAAI;AACJ,MAAI,OAAO,WAAW,iBAAiB,SACrC,mBAAkB,OAAO,WAAW,aAAa;MAEjD,mBAAkB,WAChB,WAAW,aACZ,CAAC;AAEJ,QAAM,KAAK,kBAAkB,gBAAgB,GAAG;;AAIlD,QAAO,aAAa,QAAQ,KADP,MAAM,KAAK,IAAI,CAAC,MAAM,CACG"}
|
package/dist/styles/color.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { getColorSpaceSuffix } from "../utils/color-space.js";
|
|
1
2
|
import { parseColor } from "../utils/styles.js";
|
|
2
|
-
import {
|
|
3
|
+
import { convertColorChainToComponentChain } from "./createStyle.js";
|
|
3
4
|
|
|
4
5
|
//#region src/styles/color.ts
|
|
5
6
|
function colorStyle({ color }) {
|
|
@@ -10,10 +11,13 @@ function colorStyle({ color }) {
|
|
|
10
11
|
let name = "";
|
|
11
12
|
if (match) name = match[1];
|
|
12
13
|
const styles = { color };
|
|
13
|
-
if (name && name !== "current")
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
if (name && name !== "current") {
|
|
15
|
+
const suffix = getColorSpaceSuffix();
|
|
16
|
+
Object.assign(styles, {
|
|
17
|
+
"--current-color": color,
|
|
18
|
+
[`--current-color-${suffix}`]: convertColorChainToComponentChain(color)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
17
21
|
return styles;
|
|
18
22
|
}
|
|
19
23
|
colorStyle.__lookupStyles = ["color"];
|
package/dist/styles/color.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"color.js","names":[],"sources":["../../src/styles/color.ts"],"sourcesContent":["import { parseColor } from '../utils/styles';\n\nimport {
|
|
1
|
+
{"version":3,"file":"color.js","names":[],"sources":["../../src/styles/color.ts"],"sourcesContent":["import { getColorSpaceSuffix } from '../utils/color-space';\nimport { parseColor } from '../utils/styles';\n\nimport { convertColorChainToComponentChain } from './createStyle';\n\nexport function colorStyle({ color }: { color?: string | boolean }) {\n if (!color) return;\n\n if (color === true) color = 'currentColor';\n\n if (\n typeof color === 'string' &&\n (color.startsWith('#') || color.startsWith('(#'))\n ) {\n color = parseColor(color).color || color;\n }\n\n const match = color.match(/var\\(--(.+?)-color/);\n let name = '';\n\n if (match) {\n name = match[1];\n }\n\n const styles = {\n color: color,\n };\n\n if (name && name !== 'current') {\n const suffix = getColorSpaceSuffix();\n Object.assign(styles, {\n '--current-color': color,\n [`--current-color-${suffix}`]: convertColorChainToComponentChain(color),\n });\n }\n\n return styles;\n}\n\ncolorStyle.__lookupStyles = ['color'];\n"],"mappings":";;;;;AAKA,SAAgB,WAAW,EAAE,SAAuC;AAClE,KAAI,CAAC,MAAO;AAEZ,KAAI,UAAU,KAAM,SAAQ;AAE5B,KACE,OAAO,UAAU,aAChB,MAAM,WAAW,IAAI,IAAI,MAAM,WAAW,KAAK,EAEhD,SAAQ,WAAW,MAAM,CAAC,SAAS;CAGrC,MAAM,QAAQ,MAAM,MAAM,qBAAqB;CAC/C,IAAI,OAAO;AAEX,KAAI,MACF,QAAO,MAAM;CAGf,MAAM,SAAS,EACN,OACR;AAED,KAAI,QAAQ,SAAS,WAAW;EAC9B,MAAM,SAAS,qBAAqB;AACpC,SAAO,OAAO,QAAQ;GACpB,mBAAmB;IAClB,mBAAmB,WAAW,kCAAkC,MAAM;GACxE,CAAC;;AAGJ,QAAO;;AAGT,WAAW,iBAAiB,CAAC,QAAQ"}
|