styimat 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +250 -24
- package/package.json +1 -1
- package/styimat.js +65 -1
- package/styimat.min.js +29 -1
package/README.md
CHANGED
|
@@ -1,37 +1,52 @@
|
|
|
1
1
|
# styimat
|
|
2
|
+
[](https://gitee.com/wxy6987/styimat/stargazers)
|
|
3
|
+
[](https://gitee.com/wxy6987/styimat/members)
|
|
2
4
|
|
|
3
|
-
一个功能强大的 CSS 变量预处理库,支持嵌套规则、Lab/LCH
|
|
5
|
+
一个功能强大的 CSS 变量预处理库,支持嵌套规则、Lab/LCH 颜色空间转换、Display P3 广色域和增强的数学计算功能。
|
|
4
6
|
|
|
5
|
-
##
|
|
7
|
+
## 安装方式
|
|
8
|
+
|
|
9
|
+
### 1. Git 克隆(推荐)
|
|
10
|
+
```bash
|
|
11
|
+
# 克隆主仓库
|
|
12
|
+
git clone https://gitee.com/wxy6987/styimat.git
|
|
6
13
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
- 🔧 **变量系统** - 类似 Sass 的变量系统,支持作用域变量
|
|
10
|
-
- 🏗️ **嵌套规则** - 支持 Sass 风格的嵌套选择器
|
|
11
|
-
- 📱 **十六进制语法** - 支持 `lab#L16A16B16` 和 `lch#L16C16H` 简洁语法
|
|
12
|
-
- ⚡ **轻量高效** - 无依赖,压缩后仅约 20KB
|
|
13
|
-
- 🔄 **多种使用方式** - 浏览器、Node.js、命令行均可使用
|
|
14
|
+
# 进入项目目录
|
|
15
|
+
cd styimat
|
|
14
16
|
|
|
15
|
-
|
|
17
|
+
# 安装依赖
|
|
18
|
+
npm install
|
|
19
|
+
```
|
|
16
20
|
|
|
17
|
-
###
|
|
21
|
+
### 2. 直接下载 ZIP
|
|
22
|
+
- [点击下载最新版本](https://gitee.com/wxy6987/styimat/repository/archive/main.zip)
|
|
18
23
|
|
|
24
|
+
### 3. NPM 安装
|
|
19
25
|
```bash
|
|
20
26
|
npm install styimat
|
|
21
27
|
```
|
|
22
28
|
|
|
23
|
-
### CDN
|
|
24
|
-
|
|
29
|
+
### 4. CDN 引入
|
|
25
30
|
```html
|
|
31
|
+
<!-- unpkg -->
|
|
26
32
|
<script src="https://unpkg.com/styimat"></script>
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
### CDN (jsdelivr)
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
<!-- jsdelivr -->
|
|
32
35
|
<script src="https://cdn.jsdelivr.net/npm/styimat/styimat.min.js"></script>
|
|
33
36
|
```
|
|
34
37
|
|
|
38
|
+
## 特性
|
|
39
|
+
|
|
40
|
+
- **Lab/LCH 颜色支持** - 将 Lab 和 LCH 颜色转换为 CSS 兼容的 RGB
|
|
41
|
+
- **广色域显示** - 自动检测并支持 Display P3 广色域显示器
|
|
42
|
+
- **变量系统** - 类似 Sass 的变量系统,支持作用域变量
|
|
43
|
+
-️ **嵌套规则** - 支持 Sass 风格的嵌套选择器
|
|
44
|
+
- **十六进制语法** - 支持 `lab#L16A16B16` 和 `lch#L16C16H` 简洁语法
|
|
45
|
+
- **增强数学计算** - 支持 `math()` 函数和复杂数学表达式
|
|
46
|
+
- **轻量高效** - 无依赖,压缩后仅约 20KB
|
|
47
|
+
- **多种使用方式** - 浏览器、Node.js、命令行均可使用
|
|
48
|
+
- **灵活API** - 支持函数调用、模板标签、对象配置等多种用法
|
|
49
|
+
|
|
35
50
|
## 快速开始
|
|
36
51
|
|
|
37
52
|
### 浏览器中使用
|
|
@@ -108,6 +123,123 @@ npm install -g styimat
|
|
|
108
123
|
styimat input.css output.css
|
|
109
124
|
```
|
|
110
125
|
|
|
126
|
+
## 高级用法
|
|
127
|
+
|
|
128
|
+
### 1. 作为函数直接调用
|
|
129
|
+
|
|
130
|
+
`styimat` 本身是一个可调用的函数,支持多种调用方式:
|
|
131
|
+
|
|
132
|
+
```javascript
|
|
133
|
+
// 方式1:直接传入CSS字符串
|
|
134
|
+
const css1 = styimat(`
|
|
135
|
+
$primary: lab(54.7 77.9 80.1);
|
|
136
|
+
.button { color: $primary; }
|
|
137
|
+
`);
|
|
138
|
+
|
|
139
|
+
// 方式2:传入配置对象(返回函数本身,可以链式调用)
|
|
140
|
+
styimat({
|
|
141
|
+
enableP3: true,
|
|
142
|
+
enableMath: true,
|
|
143
|
+
mathPrecision: 8
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// 方式3:无参数调用(自动处理页面中所有标记的<style>标签)
|
|
147
|
+
styimat();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
### 2. 作为模板标签使用
|
|
151
|
+
|
|
152
|
+
`styimat` 可以作为ES6模板字符串的标签函数,提供更简洁的语法:
|
|
153
|
+
|
|
154
|
+
```javascript
|
|
155
|
+
// 基本模板标签用法
|
|
156
|
+
const css = styimat`
|
|
157
|
+
$primary: lab(54.7 77.9 80.1);
|
|
158
|
+
$secondary: lch(44.7 67.9 210);
|
|
159
|
+
|
|
160
|
+
.button {
|
|
161
|
+
background: $primary;
|
|
162
|
+
color: white;
|
|
163
|
+
|
|
164
|
+
&:hover {
|
|
165
|
+
background: $secondary;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
`;
|
|
169
|
+
|
|
170
|
+
// 模板标签支持插值
|
|
171
|
+
const theme = 'dark';
|
|
172
|
+
const cssWithVars = styimat`
|
|
173
|
+
$primary: ${theme === 'dark' ? 'lab(30 40 50)' : 'lab(70 20 -10)'};
|
|
174
|
+
|
|
175
|
+
body {
|
|
176
|
+
background: $primary;
|
|
177
|
+
}
|
|
178
|
+
`;
|
|
179
|
+
|
|
180
|
+
// 复杂的插值示例
|
|
181
|
+
const spacing = 1.5;
|
|
182
|
+
const cssComplex = styimat`
|
|
183
|
+
$base-spacing: ${spacing}rem;
|
|
184
|
+
$large-spacing: math(${spacing} * 2);
|
|
185
|
+
|
|
186
|
+
.container {
|
|
187
|
+
padding: $base-spacing;
|
|
188
|
+
margin: $large-spacing;
|
|
189
|
+
}
|
|
190
|
+
`;
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### 3. 作为对象方法调用
|
|
194
|
+
|
|
195
|
+
除了函数调用,`styimat` 还提供了对象方法:
|
|
196
|
+
|
|
197
|
+
```javascript
|
|
198
|
+
// 1. 转换CSS
|
|
199
|
+
const converted = styimat.convert(cssString, config);
|
|
200
|
+
|
|
201
|
+
// 2. 应用到页面
|
|
202
|
+
const styleElement = styimat.apply(cssString, config);
|
|
203
|
+
|
|
204
|
+
// 3. 配置全局设置
|
|
205
|
+
styimat.config({
|
|
206
|
+
enableP3: true,
|
|
207
|
+
enableMath: true,
|
|
208
|
+
indentSize: 2
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
// 4. 使用工具方法
|
|
212
|
+
const mathResult = styimat.math.evaluate('100px / 2'); // "50px"
|
|
213
|
+
const rgbColor = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
### 4. 混合使用示例
|
|
217
|
+
|
|
218
|
+
```javascript
|
|
219
|
+
// 先配置,然后使用模板标签
|
|
220
|
+
styimat.config({ enableP3: true });
|
|
221
|
+
|
|
222
|
+
const designSystem = styimat`
|
|
223
|
+
/* 设计系统变量 */
|
|
224
|
+
$primary: lab#8CFF80;
|
|
225
|
+
$secondary: lch#6CFF180;
|
|
226
|
+
$spacing-unit: 1rem;
|
|
227
|
+
|
|
228
|
+
/* 组件样式 */
|
|
229
|
+
.btn {
|
|
230
|
+
padding: $spacing-unit;
|
|
231
|
+
background: $primary;
|
|
232
|
+
|
|
233
|
+
&.secondary {
|
|
234
|
+
background: $secondary;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
`;
|
|
238
|
+
|
|
239
|
+
// 应用到页面
|
|
240
|
+
styimat.apply(designSystem);
|
|
241
|
+
```
|
|
242
|
+
|
|
111
243
|
## 语法指南
|
|
112
244
|
|
|
113
245
|
### 变量定义
|
|
@@ -125,6 +257,24 @@ $font-family: 'Inter', sans-serif;
|
|
|
125
257
|
}
|
|
126
258
|
```
|
|
127
259
|
|
|
260
|
+
### Math 计算语法
|
|
261
|
+
|
|
262
|
+
```css
|
|
263
|
+
/* math() 函数 */
|
|
264
|
+
.element {
|
|
265
|
+
width: math(100% / 3);
|
|
266
|
+
height: math(200px + 30px);
|
|
267
|
+
padding: math(2rem * 1.5);
|
|
268
|
+
margin: math(calc(100vh - 200px) / 2);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/* 支持单位运算 */
|
|
272
|
+
.container {
|
|
273
|
+
font-size: math(16px * 1.125); /* 18px */
|
|
274
|
+
gap: math(24px + 1rem); /* 支持混合单位 */
|
|
275
|
+
}
|
|
276
|
+
```
|
|
277
|
+
|
|
128
278
|
### Lab 颜色语法
|
|
129
279
|
|
|
130
280
|
```css
|
|
@@ -188,10 +338,18 @@ LCH 颜色使用 `lch#` 前缀,后跟 6 个字符(最后1-3位是十进制
|
|
|
188
338
|
|
|
189
339
|
.header {
|
|
190
340
|
font-size: 2rem;
|
|
341
|
+
|
|
342
|
+
.title {
|
|
343
|
+
color: lab(20 40 60);
|
|
344
|
+
}
|
|
191
345
|
}
|
|
192
346
|
|
|
193
347
|
@media (min-width: 768px) {
|
|
194
348
|
padding: 2rem;
|
|
349
|
+
|
|
350
|
+
.header {
|
|
351
|
+
font-size: 3rem;
|
|
352
|
+
}
|
|
195
353
|
}
|
|
196
354
|
}
|
|
197
355
|
```
|
|
@@ -203,10 +361,12 @@ LCH 颜色使用 `lch#` 前缀,后跟 6 个字符(最后1-3位是十进制
|
|
|
203
361
|
$base-color: lab(54.7 77.9 80.1);
|
|
204
362
|
$text-color: lch(20 30 270);
|
|
205
363
|
$border-color: $base-color;
|
|
364
|
+
$padding-large: math(2rem * 1.5);
|
|
206
365
|
|
|
207
366
|
.element {
|
|
208
367
|
color: $text-color;
|
|
209
368
|
border: 1px solid $border-color;
|
|
369
|
+
padding: $padding-large;
|
|
210
370
|
}
|
|
211
371
|
```
|
|
212
372
|
|
|
@@ -230,14 +390,23 @@ styimat.config({
|
|
|
230
390
|
convertLabToRGB: true,
|
|
231
391
|
convertLchToRGB: true,
|
|
232
392
|
enableP3: true,
|
|
393
|
+
enableMath: true,
|
|
394
|
+
mathPrecision: 6,
|
|
233
395
|
indentSize: 2,
|
|
234
396
|
enableNesting: true
|
|
235
397
|
});
|
|
236
398
|
|
|
399
|
+
// 数学工具
|
|
400
|
+
styimat.math.evaluate('100px / 2'); // "50px"
|
|
401
|
+
styimat.math.evaluate('(16px * 1.5) + 4px'); // "28px"
|
|
402
|
+
|
|
237
403
|
// 颜色工具
|
|
238
404
|
const rgb = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
|
|
239
405
|
const p3 = styimat.colorUtils.labToP3(54.7, 77.9, 80.1);
|
|
240
406
|
const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
|
|
407
|
+
|
|
408
|
+
// 解析颜色
|
|
409
|
+
const colorInfo = styimat.colorUtils.parseColor('lab(54.7 77.9 80.1 / 0.8)');
|
|
241
410
|
```
|
|
242
411
|
|
|
243
412
|
### 配置选项
|
|
@@ -254,6 +423,8 @@ const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
|
|
|
254
423
|
| `convertLabToRGB` | boolean | `true` | 是否转换 Lab 颜色为 RGB |
|
|
255
424
|
| `convertLchToRGB` | boolean | `true` | 是否转换 LCH 颜色为 RGB |
|
|
256
425
|
| `enableP3` | boolean | `true` | 是否启用 Display P3 广色域支持 |
|
|
426
|
+
| `enableMath` | boolean | `true` | 是否启用 `math()` 函数增强 |
|
|
427
|
+
| `mathPrecision` | number | `6` | 数学计算的精度 |
|
|
257
428
|
|
|
258
429
|
## 示例
|
|
259
430
|
|
|
@@ -271,7 +442,7 @@ $border-radius: 0.5rem;
|
|
|
271
442
|
.button {
|
|
272
443
|
background-color: $brand-primary;
|
|
273
444
|
color: white;
|
|
274
|
-
padding: $spacing-unit
|
|
445
|
+
padding: $spacing-unit math($spacing-unit * 2);
|
|
275
446
|
border-radius: $border-radius;
|
|
276
447
|
border: 2px solid lab(44.7 67.9 70.1);
|
|
277
448
|
|
|
@@ -281,8 +452,8 @@ $border-radius: 0.5rem;
|
|
|
281
452
|
}
|
|
282
453
|
|
|
283
454
|
&.large {
|
|
284
|
-
padding:
|
|
285
|
-
font-size: 1.
|
|
455
|
+
padding: math($spacing-unit * 1.5) math($spacing-unit * 3);
|
|
456
|
+
font-size: math(1rem * 1.25);
|
|
286
457
|
}
|
|
287
458
|
}
|
|
288
459
|
|
|
@@ -293,6 +464,7 @@ $border-radius: 0.5rem;
|
|
|
293
464
|
|
|
294
465
|
@media (max-width: 768px) {
|
|
295
466
|
padding: 0 $spacing-unit;
|
|
467
|
+
font-size: math(16px * 0.9);
|
|
296
468
|
}
|
|
297
469
|
}
|
|
298
470
|
```
|
|
@@ -318,18 +490,72 @@ $primary-transparent: lab(55 190 0 / 0.8);
|
|
|
318
490
|
.card {
|
|
319
491
|
background: linear-gradient(135deg, $primary, $secondary);
|
|
320
492
|
border: 1px solid $accent;
|
|
493
|
+
width: math(100% / 3);
|
|
321
494
|
}
|
|
322
495
|
}
|
|
323
496
|
```
|
|
324
497
|
|
|
498
|
+
### Math() 函数高级示例
|
|
499
|
+
|
|
500
|
+
```css
|
|
501
|
+
/* 复杂数学计算 */
|
|
502
|
+
.grid {
|
|
503
|
+
--columns: 12;
|
|
504
|
+
--gap: 1rem;
|
|
505
|
+
|
|
506
|
+
.col-4 {
|
|
507
|
+
width: math(calc(100% / var(--columns)) * 4);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
.col-6 {
|
|
511
|
+
width: math(calc(100% / var(--columns)) * 6 - var(--gap));
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
.responsive {
|
|
516
|
+
font-size: math(clamp(16px, 2vw + 8px, 24px));
|
|
517
|
+
padding: math(max(1rem, 2vh));
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
.aspect-ratio {
|
|
521
|
+
height: math(100vw / 16 * 9); /* 16:9 宽高比 */
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
## Git 仓库
|
|
526
|
+
|
|
527
|
+
- **主仓库**: [https://gitee.com/wxy6987/styimat](https://gitee.com/wxy6987/styimat)
|
|
528
|
+
- **ZIP 下载**: [https://gitee.com/wxy6987/styimat/repository/archive/main.zip](https://gitee.com/wxy6987/styimat/repository/archive/main.zip)
|
|
529
|
+
- **Issues**: [问题反馈](https://gitee.com/wxy6987/styimat/issues)
|
|
530
|
+
- **Pull Requests**: [贡献代码](https://gitee.com/wxy6987/styimat/pulls)
|
|
531
|
+
|
|
532
|
+
## 贡献指南
|
|
533
|
+
|
|
534
|
+
欢迎贡献代码!请遵循以下步骤:
|
|
535
|
+
|
|
536
|
+
1. **Fork 本仓库**
|
|
537
|
+
2. **创建特性分支**
|
|
538
|
+
```bash
|
|
539
|
+
git checkout -b feature/AmazingFeature
|
|
540
|
+
```
|
|
541
|
+
3. **提交更改**
|
|
542
|
+
```bash
|
|
543
|
+
git commit -m 'Add some AmazingFeature'
|
|
544
|
+
```
|
|
545
|
+
4. **推送到分支**
|
|
546
|
+
```bash
|
|
547
|
+
git push origin feature/AmazingFeature
|
|
548
|
+
```
|
|
549
|
+
5. **提交 Pull Request**
|
|
550
|
+
|
|
325
551
|
## 许可证
|
|
326
552
|
|
|
327
553
|
MIT © 王小玗 2025
|
|
328
554
|
|
|
329
|
-
##
|
|
555
|
+
## 支持
|
|
330
556
|
|
|
331
|
-
|
|
557
|
+
如果发现 bug 或有功能建议,请在 [Gitee Issues](https://gitee.com/wxy6987/styimat/issues) 中创建 Issue。
|
|
332
558
|
|
|
333
|
-
|
|
559
|
+
---
|
|
334
560
|
|
|
335
|
-
|
|
561
|
+
**如果这个项目对你有帮助,请给个 [](https://gitee.com/wxy6987/styimat)**
|
package/package.json
CHANGED
package/styimat.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* MIT License
|
|
3
|
+
* Copyright (c) 2025 王小玗
|
|
4
|
+
*/
|
|
1
5
|
/**
|
|
2
6
|
* CSS 变量预处理库 - 增强版
|
|
3
7
|
* 支持 CSS 变量预处理、嵌套选择器和嵌套变量
|
|
@@ -1449,6 +1453,66 @@
|
|
|
1449
1453
|
}
|
|
1450
1454
|
}
|
|
1451
1455
|
};
|
|
1456
|
+
|
|
1457
|
+
|
|
1458
|
+
// 创建一个可调用的主函数
|
|
1459
|
+
const styimat = function(...args) {
|
|
1460
|
+
// 检查是否是模板字符串调用(标签函数)
|
|
1461
|
+
if (args.length > 1 || (args[0] && args[0].raw)) {
|
|
1462
|
+
// 处理模板字符串
|
|
1463
|
+
return handleTemplateTag(...args);
|
|
1464
|
+
}
|
|
1465
|
+
|
|
1466
|
+
// 获取第一个参数
|
|
1467
|
+
const firstArg = args[0];
|
|
1468
|
+
|
|
1469
|
+
// 如果传入CSS文本,则编译并返回结果
|
|
1470
|
+
if (typeof firstArg === 'string') {
|
|
1471
|
+
return convert(firstArg, { ...defaultConfig, ...args[1] });
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
// 如果传入配置对象,则应用配置
|
|
1475
|
+
if (typeof firstArg === 'object' && firstArg !== null) {
|
|
1476
|
+
defaultConfig = { ...defaultConfig, ...firstArg };
|
|
1477
|
+
return styimat;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// 如果没有参数,执行自动处理
|
|
1481
|
+
if (args.length === 0) {
|
|
1482
|
+
return apply();
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
return styimat;
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
// 处理模板字符串的函数
|
|
1489
|
+
function handleTemplateTag(strings, ...values) {
|
|
1490
|
+
// 拼接模板字符串
|
|
1491
|
+
let cssText = strings[0];
|
|
1492
|
+
|
|
1493
|
+
for (let i = 0; i < values.length; i++) {
|
|
1494
|
+
// 处理插值(支持字符串、数字、函数等)
|
|
1495
|
+
const value = values[i];
|
|
1496
|
+
let result = '';
|
|
1497
|
+
|
|
1498
|
+
if (typeof value === 'function') {
|
|
1499
|
+
result = value();
|
|
1500
|
+
} else if (Array.isArray(value)) {
|
|
1501
|
+
result = value.join(' ');
|
|
1502
|
+
} else {
|
|
1503
|
+
result = String(value != null ? value : '');
|
|
1504
|
+
}
|
|
1505
|
+
|
|
1506
|
+
cssText += result + strings[i + 1];
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
// 使用convert函数处理
|
|
1510
|
+
return convert(cssText, defaultConfig);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
// 将API的所有方法复制到主函数上
|
|
1514
|
+
Object.assign(styimat, api);
|
|
1515
|
+
Object.setPrototypeOf(styimat, Function.prototype);
|
|
1452
1516
|
|
|
1453
1517
|
// 自动初始化
|
|
1454
1518
|
if (typeof window !== 'undefined') {
|
|
@@ -1472,5 +1536,5 @@
|
|
|
1472
1536
|
});
|
|
1473
1537
|
}
|
|
1474
1538
|
|
|
1475
|
-
return
|
|
1539
|
+
return styimat;
|
|
1476
1540
|
}));
|
package/styimat.min.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* MIT License
|
|
3
|
+
* Copyright (c) 2025 王小玗
|
|
4
|
+
*/
|
|
1
5
|
/**
|
|
2
6
|
* CSS 变量预处理库 - 增强版
|
|
3
7
|
* 支持 CSS 变量预处理、嵌套选择器和嵌套变量
|
|
@@ -482,5 +486,29 @@ parseColor:function(colorString){try{
|
|
|
482
486
|
const labMatch=colorString.match(/lab\(\s*([\d.]+)(%?)\s+([\d.-]+)\s+([\d.-]+)(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(labMatch){let L=parseFloat(labMatch[1]);const A=parseFloat(labMatch[3]);const B=parseFloat(labMatch[4]);const alpha=labMatch[5]?labMatch[5].includes("%")?parseFloat(labMatch[5])/100:parseFloat(labMatch[5]):null;const colorStr=generateColorString(L,A,B,defaultConfig,alpha);return{L:L,A:A,B:B,alpha:alpha,rgb:preciseLabToRGB(L,A,B),p3:labToP3(L,A,B),colorString:colorStr}}
|
|
483
487
|
// 尝试解析lch()函数格式
|
|
484
488
|
const lchMatch=colorString.match(/lch\(\s*([\d.]+)(%?)\s+([\d.]+)\s+([\d.]+)(deg)?(?:\s*\/\s*([\d.%]+))?\s*\)/i);if(lchMatch){let L=parseFloat(lchMatch[1]);const C=parseFloat(lchMatch[3]);let H=parseFloat(lchMatch[4]);const alpha=lchMatch[6]?lchMatch[6].includes("%")?parseFloat(lchMatch[6])/100:parseFloat(lchMatch[6]):null;const lab=lchToLab(L,C,H);const colorStr=generateColorString(lab.L,lab.a,lab.b,defaultConfig,alpha);return{L:L,C:C,H:H,alpha:alpha,lab:lab,rgb:preciseLabToRGB(lab.L,lab.a,lab.b),p3:labToP3(lab.L,lab.a,lab.b),colorString:colorStr}}return null}catch(error){console.warn("无法解析颜色:",colorString,error);return null}}}};
|
|
489
|
+
// 创建一个可调用的主函数
|
|
490
|
+
const styimat=function(...args){
|
|
491
|
+
// 检查是否是模板字符串调用(标签函数)
|
|
492
|
+
if(args.length>1||args[0]&&args[0].raw){
|
|
493
|
+
// 处理模板字符串
|
|
494
|
+
return handleTemplateTag(...args)}
|
|
495
|
+
// 获取第一个参数
|
|
496
|
+
const firstArg=args[0];
|
|
497
|
+
// 如果传入CSS文本,则编译并返回结果
|
|
498
|
+
if(typeof firstArg==="string"){return convert(firstArg,{...defaultConfig,...args[1]})}
|
|
499
|
+
// 如果传入配置对象,则应用配置
|
|
500
|
+
if(typeof firstArg==="object"&&firstArg!==null){defaultConfig={...defaultConfig,...firstArg};return styimat}
|
|
501
|
+
// 如果没有参数,执行自动处理
|
|
502
|
+
if(args.length===0){return apply()}return styimat};
|
|
503
|
+
// 处理模板字符串的函数
|
|
504
|
+
function handleTemplateTag(strings,...values){
|
|
505
|
+
// 拼接模板字符串
|
|
506
|
+
let cssText=strings[0];for(let i=0;i<values.length;i++){
|
|
507
|
+
// 处理插值(支持字符串、数字、函数等)
|
|
508
|
+
const value=values[i];let result="";if(typeof value==="function"){result=value()}else if(Array.isArray(value)){result=value.join(" ")}else{result=String(value!=null?value:"")}cssText+=result+strings[i+1]}
|
|
509
|
+
// 使用convert函数处理
|
|
510
|
+
return convert(cssText,defaultConfig)}
|
|
511
|
+
// 将API的所有方法复制到主函数上
|
|
512
|
+
Object.assign(styimat,api);Object.setPrototypeOf(styimat,Function.prototype);
|
|
485
513
|
// 自动初始化
|
|
486
|
-
if(typeof window!=="undefined"){apply();Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const element=this;return new Proxy({},{get(target,prop){const varName=prop.startsWith("--")?prop:`--${prop}`;return element.style.getPropertyValue(varName)},set(target,prop,value){const varName=prop.startsWith("--")?prop:`--${prop}`;element.style.setProperty(varName,value);return true}})}})}return
|
|
514
|
+
if(typeof window!=="undefined"){apply();Object.defineProperty(window.HTMLElement.prototype,"cssVar",{get(){const element=this;return new Proxy({},{get(target,prop){const varName=prop.startsWith("--")?prop:`--${prop}`;return element.style.getPropertyValue(varName)},set(target,prop,value){const varName=prop.startsWith("--")?prop:`--${prop}`;element.style.setProperty(varName,value);return true}})}})}return styimat});
|