styimat 1.0.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/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 王小玗
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the " Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,317 @@
1
+ # styimat
2
+
3
+ 一个功能强大的 CSS 变量预处理库,支持嵌套规则、Lab/LCH 颜色空间转换和 Display P3 广色域。
4
+
5
+ ## 特性
6
+
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
+ ## 安装
16
+
17
+ ### NPM
18
+
19
+ ```bash
20
+ npm install styimat
21
+ ```
22
+
23
+ ### CDN (unpkg)
24
+
25
+ ```html
26
+ <script src="https://unpkg.com/styimat"></script>
27
+ ```
28
+
29
+ ### CDN (jsdelivr)
30
+
31
+ ```html
32
+ <script src="https://cdn.jsdelivr.net/npm/styimat/styimat.min.js"></script>
33
+ ```
34
+
35
+ ## 快速开始
36
+
37
+ ### 浏览器中使用
38
+
39
+ ```html
40
+ <!-- 在 HTML 中直接使用 -->
41
+ <style e>
42
+ $primary-color: lab(54.7 77.9 80.1);
43
+ $spacing: 1rem;
44
+
45
+ .button {
46
+ background-color: $primary-color;
47
+ padding: $spacing;
48
+
49
+ &:hover {
50
+ background-color: lab(44.7 67.9 70.1);
51
+ }
52
+ }
53
+ </style>
54
+
55
+ <script src="https://cdn.jsdelivr.net/npm/styimat/styimat.min.js"></script>
56
+ ```
57
+
58
+ ### Node.js 中使用
59
+
60
+ ```javascript
61
+ const styimat = require('styimat');
62
+
63
+ const css = `
64
+ $primary: lab(54.7 77.9 80.1);
65
+ .button { background-color: $primary; }
66
+ `;
67
+
68
+ const result = styimat.convert(css);
69
+ console.log(result);
70
+ ```
71
+
72
+ ### 命令行使用
73
+
74
+ ```bash
75
+ # 基本用法
76
+ npx styimat input.css output.css
77
+
78
+ # 输出到 stdout
79
+ npx styimat input.css
80
+
81
+ # 使用管道
82
+ npx styimat < input.css > output.css
83
+ cat input.css | npx styimat > output.css
84
+
85
+ # 从 stdin 读取,输出到 stdout
86
+ npx styimat
87
+
88
+ # 全局安装后使用
89
+ npm install -g styimat
90
+ styimat input.css output.css
91
+ ```
92
+
93
+ ## 语法指南
94
+
95
+ ### 变量定义
96
+
97
+ ```css
98
+ /* 全局变量 */
99
+ $primary-color: lab(54.7 77.9 80.1);
100
+ $spacing: 1rem;
101
+ $font-family: 'Inter', sans-serif;
102
+
103
+ /* 局部变量(在规则块内) */
104
+ .container {
105
+ $local-var: 20px;
106
+ margin: $local-var;
107
+ }
108
+ ```
109
+
110
+ ### Lab 颜色语法
111
+
112
+ ```css
113
+ /* 标准 lab() 函数 */
114
+ .element {
115
+ color: lab(54.7 77.9 80.1);
116
+ background: lab(87.8 -79.3 80.8 / 0.5); /* 带透明度 */
117
+ }
118
+
119
+ /* 十六进制简写 */
120
+ .element {
121
+ color: lab#8CFF80; /* L=8C, A=FF, B=80 */
122
+ }
123
+ ```
124
+
125
+ ### LCH 颜色语法
126
+
127
+ ```css
128
+ /* 标准 lch() 函数 */
129
+ .element {
130
+ color: lch(54.7 78.9 38.5);
131
+ background: lch(87.8 79.3 180 / 30%); /* 带透明度百分比 */
132
+ }
133
+
134
+ /* 十六进制简写 */
135
+ .element {
136
+ color: lch#8CFF90; /* L=8C, C=FF, H=90 */
137
+ }
138
+ ```
139
+
140
+ ## 十六进制颜色语法详解
141
+
142
+ ### Lab 十六进制格式:`lab#L16A16B16`
143
+ Lab 颜色使用 `lab#` 前缀,后跟 6 个十六进制字符:
144
+ - 前 2 位:L(明度),范围 00-FF,映射到 0-100
145
+ - 中间 2 位:A(红绿轴),范围 00-FF,映射到 -192 到 192
146
+ - 后 2 位:B(黄蓝轴),范围 00-FF,映射到 -192 到 192
147
+
148
+ 示例:`lab#8CFF80` 表示:
149
+ - L = 0x8C = 140 → 140/255 × 100 ≈ 55
150
+ - A = 0xFF = 255 → (255-128) × 1.5 ≈ 190.5
151
+ - B = 0x80 = 128 → (128-128) × 1.5 = 0
152
+
153
+ ### LCH 十六进制格式:`lch#L16C16H`
154
+ LCH 颜色使用 `lch#` 前缀,后跟 6 个字符(最后1-3位是十进制色相值):
155
+ - 前 2 位:L(明度),范围 00-FF,映射到 0-100
156
+ - 中间 2 位:C(色度),范围 00-FF,映射到 0-150
157
+ - 后 1-3 位:H(色相),范围 0-999,映射到 0-360度
158
+
159
+ 示例:`lch#8CFF090` 表示:
160
+ - L = 0x8C = 140 → 140/255 × 100 ≈ 55
161
+ - C = 0xFF = 255 → 255/255 × 150 = 150
162
+ - H = 090 → 090/100 × 360 = 324度
163
+
164
+ ### 嵌套规则
165
+
166
+ ```css
167
+ /* Sass 风格的嵌套 */
168
+ .container {
169
+ padding: 1rem;
170
+
171
+ .header {
172
+ font-size: 2rem;
173
+ }
174
+
175
+ @media (min-width: 768px) {
176
+ padding: 2rem;
177
+ }
178
+ }
179
+ ```
180
+
181
+ ### 嵌套变量
182
+
183
+ ```css
184
+ /* 变量可以引用其他变量 */
185
+ $base-color: lab(54.7 77.9 80.1);
186
+ $text-color: lch(20 30 270);
187
+ $border-color: $base-color;
188
+
189
+ .element {
190
+ color: $text-color;
191
+ border: 1px solid $border-color;
192
+ }
193
+ ```
194
+
195
+ ## API 文档
196
+
197
+ ### JavaScript API
198
+
199
+ ```javascript
200
+ const styimat = require('styimat');
201
+
202
+ // 转换 CSS
203
+ const css = styimat.convert(cssString, config);
204
+
205
+ // 应用到页面
206
+ styimat.apply(cssString, config);
207
+
208
+ // 配置全局设置
209
+ styimat.config({
210
+ rootSelector: ':root',
211
+ variablePrefix: '--',
212
+ convertLabToRGB: true,
213
+ convertLchToRGB: true,
214
+ enableP3: true,
215
+ indentSize: 2,
216
+ enableNesting: true
217
+ });
218
+
219
+ // 颜色工具
220
+ const rgb = styimat.colorUtils.labToRGB(54.7, 77.9, 80.1);
221
+ const p3 = styimat.colorUtils.labToP3(54.7, 77.9, 80.1);
222
+ const lab = styimat.colorUtils.lchToLab(54.7, 78.9, 38.5);
223
+ ```
224
+
225
+ ### 配置选项
226
+
227
+ | 选项 | 类型 | 默认值 | 描述 |
228
+ |------|------|--------|------|
229
+ | `rootSelector` | string | `':root'` | CSS 变量定义的根选择器 |
230
+ | `variablePrefix` | string | `'--'` | CSS 变量前缀 |
231
+ | `preserveOriginal` | boolean | `false` | 是否保留原始 `<style e>` 标签 |
232
+ | `indentSize` | number | `2` | 嵌套规则的缩进大小 |
233
+ | `enableNesting` | boolean | `true` | 是否启用嵌套规则 |
234
+ | `autoProcessStyleTags` | boolean | `true` | 是否自动处理页面中的 style 标签 |
235
+ | `styleTagAttribute` | string | `'e'` | 标识需要处理的 style 标签属性 |
236
+ | `convertLabToRGB` | boolean | `true` | 是否转换 Lab 颜色为 RGB |
237
+ | `convertLchToRGB` | boolean | `true` | 是否转换 LCH 颜色为 RGB |
238
+ | `enableP3` | boolean | `true` | 是否启用 Display P3 广色域支持 |
239
+
240
+ ## 示例
241
+
242
+ ### 完整示例
243
+
244
+ ```css
245
+ /* 定义颜色变量 */
246
+ $brand-primary: lab(54.7 77.9 80.1);
247
+ $brand-secondary: lch(44.7 67.9 210);
248
+ $text-primary: lch(20 30 270);
249
+ $spacing-unit: 1rem;
250
+ $border-radius: 0.5rem;
251
+
252
+ /* 使用变量和嵌套 */
253
+ .button {
254
+ background-color: $brand-primary;
255
+ color: white;
256
+ padding: $spacing-unit calc($spacing-unit * 2);
257
+ border-radius: $border-radius;
258
+ border: 2px solid lab(44.7 67.9 70.1);
259
+
260
+ &:hover {
261
+ background-color: lab(64.7 87.9 90.1);
262
+ transform: translateY(-2px);
263
+ }
264
+
265
+ &.large {
266
+ padding: calc($spacing-unit * 1.5) calc($spacing-unit * 3);
267
+ font-size: 1.25rem;
268
+ }
269
+ }
270
+
271
+ /* 媒体查询中的嵌套 */
272
+ .container {
273
+ max-width: 1200px;
274
+ margin: 0 auto;
275
+
276
+ @media (max-width: 768px) {
277
+ padding: 0 $spacing-unit;
278
+ }
279
+ }
280
+ ```
281
+
282
+ ### 颜色混合示例
283
+
284
+ ```css
285
+ /* 使用十六进制语法创建调色板 */
286
+ $primary: lab#8CFF80;
287
+ $secondary: lch#8CFF180;
288
+ $accent: lch#6CFF270;
289
+
290
+ /* 创建变体 */
291
+ $primary-light: lab#B4FFA0;
292
+ $primary-dark: lab#64FF60;
293
+ $primary-transparent: lab(55 190 0 / 0.8);
294
+
295
+ .theme {
296
+ --color-primary: var(--primary);
297
+ --color-secondary: var(--secondary);
298
+ --color-accent: var(--accent);
299
+
300
+ .card {
301
+ background: linear-gradient(135deg, $primary, $secondary);
302
+ border: 1px solid $accent;
303
+ }
304
+ }
305
+ ```
306
+
307
+ ## 许可证
308
+
309
+ MIT © 王小玗 2025
310
+
311
+ ## 贡献
312
+
313
+ 欢迎提交 Issue 和 Pull Request。
314
+
315
+ ## 支持
316
+
317
+ 如果发现 bug 或有功能建议,请在 GitHub 仓库中创建 Issue。
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env node
2
+
3
+ // 1. 只做一件事,做好
4
+ // 2. 输入 stdin,输出 stdout
5
+ // 3. 错误输出到 stderr
6
+ // 4. 安静模式可用
7
+ // 5. 返回正确的退出码
8
+
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+
12
+ function main() {
13
+ const args = process.argv.slice(2);
14
+
15
+ let inputContent = '';
16
+ let outputFile = null;
17
+
18
+ // 处理参数
19
+ if (args.length === 0) {
20
+ // 从 stdin 读取
21
+ inputContent = fs.readFileSync(0, 'utf8'); // 文件描述符 0 = stdin
22
+ } else if (args.length === 1) {
23
+ // 从文件读取,输出到 stdout
24
+ const inputFile = args[0];
25
+ inputContent = fs.readFileSync(inputFile, 'utf8');
26
+ } else if (args.length === 2) {
27
+ // 从文件读取,输出到文件
28
+ const inputFile = args[0];
29
+ outputFile = args[1];
30
+ inputContent = fs.readFileSync(inputFile, 'utf8');
31
+ } else {
32
+ // 参数过多,显示用法
33
+ console.error('用法:');
34
+ console.error(' huecss-converter # 从 stdin 读取,输出到 stdout');
35
+ console.error(' huecss-converter input.css # 从文件读取,输出到 stdout');
36
+ console.error(' huecss-converter input.css output.css # 从文件读取,输出到文件');
37
+ process.exit(1);
38
+ }
39
+
40
+ // 检查是否在相同目录
41
+ try {
42
+ // 尝试从多个位置加载 styimat.js
43
+ const possiblePaths = [
44
+ path.join(__dirname, 'styimat.js'),
45
+ path.join(process.cwd(), 'styimat.js'),
46
+ path.join(process.cwd(), 'node_modules', 'styimat', 'styimat.js')
47
+ ];
48
+
49
+ let huecssModule = null;
50
+ for (const modulePath of possiblePaths) {
51
+ try {
52
+ if (fs.existsSync(modulePath)) {
53
+ huecssModule = require(modulePath);
54
+ break;
55
+ }
56
+ } catch (e) {
57
+ // 继续尝试下一个路径
58
+ }
59
+ }
60
+
61
+ if (!huecssModule) {
62
+ throw new Error('找不到 styimat.js 模块');
63
+ }
64
+
65
+ // 获取转换函数
66
+ const huecss = huecssModule.huecss || huecssModule;
67
+
68
+ if (typeof huecss.convert !== 'function') {
69
+ throw new Error('styimat.convert 不是函数');
70
+ }
71
+
72
+ // 执行转换
73
+ const outputContent = huecss.convert(inputContent);
74
+
75
+ // 输出结果
76
+ if (outputFile) {
77
+ fs.writeFileSync(outputFile, outputContent, 'utf8');
78
+ } else {
79
+ process.stdout.write(outputContent + '\n');
80
+ }
81
+
82
+ } catch (error) {
83
+ console.error('错误:', error.message);
84
+ process.exit(1);
85
+ }
86
+ }
87
+
88
+ // 如果是直接运行
89
+ main();
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "styimat",
3
+ "version": "1.0.0",
4
+ "description": "A powerful CSS variable preprocessor with nested selectors, Lab/LCH color spaces conversion and modern color features.",
5
+ "keywords": [
6
+ "css",
7
+ "preprocessor",
8
+ "css-variables",
9
+ "nested-css",
10
+ "lab-color",
11
+ "lch-color",
12
+ "color-conversion",
13
+ "styimat",
14
+ "color",
15
+ "css-in-js"
16
+ ],
17
+ "license": "MIT",
18
+ "author": "王小玗",
19
+ "type": "commonjs",
20
+ "main": "styimat.min.js",
21
+ "browser": "styimat.min.js",
22
+ "unpkg": "styimat.min.js",
23
+ "jsdelivr": "styimat.min.js",
24
+ "files": [
25
+ "styimat.js",
26
+ "styimat.min.js",
27
+ "LICENSE",
28
+ "README.md",
29
+ "convert-styimat.js"
30
+ ],
31
+ "bin": {
32
+ "styimat": "convert-styimat.js"
33
+ },
34
+ "scripts": {
35
+ "test": "echo \"No tests yet\" && exit 0",
36
+ "build:min": "npx terser styimat.js -o styimat.min.js --comments '/^!/'",
37
+ "prepare": "npm run build:min",
38
+ "prepublishOnly": "npm run build:min && echo 'Built successfully!'"
39
+ }
40
+ }