styimat 1.4.0 → 1.6.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.
Files changed (4) hide show
  1. package/README.md +263 -24
  2. package/package.json +1 -1
  3. package/styimat.js +1451 -1380
  4. package/styimat.min.js +154 -126
package/README.md CHANGED
@@ -1,37 +1,52 @@
1
1
  # styimat
2
+ [![Stars](https://gitee.com/wxy6987/styimat/badge/star.svg?theme=white)](https://gitee.com/wxy6987/styimat/stargazers)
3
+ [![Forks](https://gitee.com/wxy6987/styimat/badge/fork.svg?theme=white)](https://gitee.com/wxy6987/styimat/members)
2
4
 
3
- 一个功能强大的 CSS 变量预处理库,支持嵌套规则、Lab/LCH 颜色空间转换和 Display P3 广色域。
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
- - 🎨 **Lab/LCH 颜色支持** - 将 Lab 和 LCH 颜色转换为 CSS 兼容的 RGB
8
- - 🌈 **广色域显示** - 自动检测并支持 Display P3 广色域显示器
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
- ### NPM
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 (unpkg)
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
- ```html
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,136 @@ 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. HTMLElement的新方法
217
+ HTMLElement由此库新增了`cssVar`属性,可以设置、获取css变量
218
+
219
+ ```javascript
220
+ ele.cssVar(prop, value); // 设置prop变量为value
221
+ ele.cssVar.prop = value; // 设置prop变量为value
222
+ ele.cssVar[prop] = value; // 设置prop变量为value
223
+ ele.cssVar(prop); // 获取prop变量的值
224
+ ele.cssVar.prop; // 获取prop变量的值
225
+ ele.cssVar[prop] // 获取prop变量的值
226
+
227
+ ```
228
+
229
+ ### 4. 混合使用示例
230
+
231
+ ```javascript
232
+ // 先配置,然后使用模板标签
233
+ styimat.config({ enableP3: true });
234
+
235
+ const designSystem = styimat`
236
+ /* 设计系统变量 */
237
+ $primary: lab#8CFF80;
238
+ $secondary: lch#6CFF180;
239
+ $spacing-unit: 1rem;
240
+
241
+ /* 组件样式 */
242
+ .btn {
243
+ padding: $spacing-unit;
244
+ background: $primary;
245
+
246
+ &.secondary {
247
+ background: $secondary;
248
+ }
249
+ }
250
+ `;
251
+
252
+ // 应用到页面
253
+ styimat.apply(designSystem);
254
+ ```
255
+
111
256
  ## 语法指南
112
257
 
113
258
  ### 变量定义
@@ -125,6 +270,24 @@ $font-family: 'Inter', sans-serif;
125
270
  }
126
271
  ```
127
272
 
273
+ ### Math 计算语法
274
+
275
+ ```css
276
+ /* math() 函数 */
277
+ .element {
278
+ width: math(100% / 3);
279
+ height: math(200px + 30px);
280
+ padding: math(2rem * 1.5);
281
+ margin: math(calc(100vh - 200px) / 2);
282
+ }
283
+
284
+ /* 支持单位运算 */
285
+ .container {
286
+ font-size: math(16px * 1.125); /* 18px */
287
+ gap: math(24px + 1rem); /* 支持混合单位 */
288
+ }
289
+ ```
290
+
128
291
  ### Lab 颜色语法
129
292
 
130
293
  ```css
@@ -188,10 +351,18 @@ LCH 颜色使用 `lch#` 前缀,后跟 6 个字符(最后1-3位是十进制
188
351
 
189
352
  .header {
190
353
  font-size: 2rem;
354
+
355
+ .title {
356
+ color: lab(20 40 60);
357
+ }
191
358
  }
192
359
 
193
360
  @media (min-width: 768px) {
194
361
  padding: 2rem;
362
+
363
+ .header {
364
+ font-size: 3rem;
365
+ }
195
366
  }
196
367
  }
197
368
  ```
@@ -203,10 +374,12 @@ LCH 颜色使用 `lch#` 前缀,后跟 6 个字符(最后1-3位是十进制
203
374
  $base-color: lab(54.7 77.9 80.1);
204
375
  $text-color: lch(20 30 270);
205
376
  $border-color: $base-color;
377
+ $padding-large: math(2rem * 1.5);
206
378
 
207
379
  .element {
208
380
  color: $text-color;
209
381
  border: 1px solid $border-color;
382
+ padding: $padding-large;
210
383
  }
211
384
  ```
212
385
 
@@ -230,14 +403,23 @@ styimat.config({
230
403
  convertLabToRGB: true,
231
404
  convertLchToRGB: true,
232
405
  enableP3: true,
406
+ enableMath: true,
407
+ mathPrecision: 6,
233
408
  indentSize: 2,
234
409
  enableNesting: true
235
410
  });
236
411
 
412
+ // 数学工具
413
+ styimat.math.evaluate('100px / 2'); // "50px"
414
+ styimat.math.evaluate('(16px * 1.5) + 4px'); // "28px"
415
+
237
416
  // 颜色工具
238
417
  const rgb = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
239
418
  const p3 = styimat.colorUtils.labToP3(54.7, 77.9, 80.1);
240
419
  const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
420
+
421
+ // 解析颜色
422
+ const colorInfo = styimat.colorUtils.parseColor('lab(54.7 77.9 80.1 / 0.8)');
241
423
  ```
242
424
 
243
425
  ### 配置选项
@@ -254,6 +436,8 @@ const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
254
436
  | `convertLabToRGB` | boolean | `true` | 是否转换 Lab 颜色为 RGB |
255
437
  | `convertLchToRGB` | boolean | `true` | 是否转换 LCH 颜色为 RGB |
256
438
  | `enableP3` | boolean | `true` | 是否启用 Display P3 广色域支持 |
439
+ | `enableMath` | boolean | `true` | 是否启用 `math()` 函数增强 |
440
+ | `mathPrecision` | number | `6` | 数学计算的精度 |
257
441
 
258
442
  ## 示例
259
443
 
@@ -271,7 +455,7 @@ $border-radius: 0.5rem;
271
455
  .button {
272
456
  background-color: $brand-primary;
273
457
  color: white;
274
- padding: $spacing-unit calc($spacing-unit * 2);
458
+ padding: $spacing-unit math($spacing-unit * 2);
275
459
  border-radius: $border-radius;
276
460
  border: 2px solid lab(44.7 67.9 70.1);
277
461
 
@@ -281,8 +465,8 @@ $border-radius: 0.5rem;
281
465
  }
282
466
 
283
467
  &.large {
284
- padding: calc($spacing-unit * 1.5) calc($spacing-unit * 3);
285
- font-size: 1.25rem;
468
+ padding: math($spacing-unit * 1.5) math($spacing-unit * 3);
469
+ font-size: math(1rem * 1.25);
286
470
  }
287
471
  }
288
472
 
@@ -293,6 +477,7 @@ $border-radius: 0.5rem;
293
477
 
294
478
  @media (max-width: 768px) {
295
479
  padding: 0 $spacing-unit;
480
+ font-size: math(16px * 0.9);
296
481
  }
297
482
  }
298
483
  ```
@@ -318,18 +503,72 @@ $primary-transparent: lab(55 190 0 / 0.8);
318
503
  .card {
319
504
  background: linear-gradient(135deg, $primary, $secondary);
320
505
  border: 1px solid $accent;
506
+ width: math(100% / 3);
507
+ }
508
+ }
509
+ ```
510
+
511
+ ### Math() 函数高级示例
512
+
513
+ ```css
514
+ /* 复杂数学计算 */
515
+ .grid {
516
+ --columns: 12;
517
+ --gap: 1rem;
518
+
519
+ .col-4 {
520
+ width: math(calc(100% / var(--columns)) * 4);
521
+ }
522
+
523
+ .col-6 {
524
+ width: math(calc(100% / var(--columns)) * 6 - var(--gap));
321
525
  }
322
526
  }
527
+
528
+ .responsive {
529
+ font-size: math(clamp(16px, 2vw + 8px, 24px));
530
+ padding: math(max(1rem, 2vh));
531
+ }
532
+
533
+ .aspect-ratio {
534
+ height: math(100vw / 16 * 9); /* 16:9 宽高比 */
535
+ }
323
536
  ```
324
537
 
538
+ ## Git 仓库
539
+
540
+ - **主仓库**: [https://gitee.com/wxy6987/styimat](https://gitee.com/wxy6987/styimat)
541
+ - **ZIP 下载**: [https://gitee.com/wxy6987/styimat/repository/archive/main.zip](https://gitee.com/wxy6987/styimat/repository/archive/main.zip)
542
+ - **Issues**: [问题反馈](https://gitee.com/wxy6987/styimat/issues)
543
+ - **Pull Requests**: [贡献代码](https://gitee.com/wxy6987/styimat/pulls)
544
+
545
+ ## 贡献指南
546
+
547
+ 欢迎贡献代码!请遵循以下步骤:
548
+
549
+ 1. **Fork 本仓库**
550
+ 2. **创建特性分支**
551
+ ```bash
552
+ git checkout -b feature/AmazingFeature
553
+ ```
554
+ 3. **提交更改**
555
+ ```bash
556
+ git commit -m 'Add some AmazingFeature'
557
+ ```
558
+ 4. **推送到分支**
559
+ ```bash
560
+ git push origin feature/AmazingFeature
561
+ ```
562
+ 5. **提交 Pull Request**
563
+
325
564
  ## 许可证
326
565
 
327
566
  MIT © 王小玗 2025
328
567
 
329
- ## 贡献
568
+ ## 支持
330
569
 
331
- 欢迎提交 Issue Pull Request
570
+ 如果发现 bug 或有功能建议,请在 [Gitee Issues](https://gitee.com/wxy6987/styimat/issues) 中创建 Issue
332
571
 
333
- ## 支持
572
+ ---
334
573
 
335
- 如果发现 bug 或有功能建议,请在 GitHub 仓库中创建 Issue。
574
+ **如果这个项目对你有帮助,请给个 [![Star](https://gitee.com/wxy6987/styimat/badge/star.svg?theme=white)](https://gitee.com/wxy6987/styimat)**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "styimat",
3
- "version": "1.4.0",
3
+ "version": "1.6.0",
4
4
  "description": "一个高效的CSS变量预处理库,支持Lab/LCH颜色空间自动转换、嵌套选择器和Display P3广色域,让现代CSS开发更简洁强大。",
5
5
  "keywords": [
6
6
  "css",