oklchtohex 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -36
- package/package.json +2 -8
- package/src/cli.js +0 -152
package/README.md
CHANGED
|
@@ -20,60 +20,48 @@ A build-time package is the most practical fix:
|
|
|
20
20
|
npm i -D oklchtohex
|
|
21
21
|
```
|
|
22
22
|
|
|
23
|
-
##
|
|
23
|
+
## JS API
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
```js
|
|
26
|
+
import { oklchToHex, replaceOklchInText, convertTailwindCssToHex } from "oklchtohex";
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
28
|
+
const hex = oklchToHex("oklch(70.4% 0.191 22.216)");
|
|
29
|
+
const css = replaceOklchInText("color: oklch(70.4% 0.191 22.216);");
|
|
30
|
+
const convertedTailwindCss = convertTailwindCssToHex(css);
|
|
29
31
|
```
|
|
30
32
|
|
|
31
|
-
|
|
33
|
+
### Conversion behavior
|
|
32
34
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
- Known Tailwind default variables like `--color-red-500` are replaced with exact Tailwind HEX palette values.
|
|
36
|
+
- Unknown variables and raw `oklch(...)` values use functional conversion.
|
|
37
|
+
- `gamut` option supports `clip` (default) and `fit`.
|
|
36
38
|
|
|
37
|
-
|
|
39
|
+
## Tailwind v4 build integration (package-only)
|
|
38
40
|
|
|
39
|
-
|
|
40
|
-
npx oklchtohex ./dist/app.css --stdout
|
|
41
|
-
```
|
|
41
|
+
Create a small Node script in your app, for example `scripts/convert-tailwind-to-hex.mjs`:
|
|
42
42
|
|
|
43
|
-
|
|
43
|
+
```js
|
|
44
|
+
import fs from "node:fs/promises";
|
|
45
|
+
import { convertTailwindCssToHex } from "oklchtohex";
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
- `--alpha auto|always|never`
|
|
48
|
-
defaults to `auto`.
|
|
49
|
-
- `--upper`
|
|
50
|
-
outputs uppercase HEX.
|
|
47
|
+
const inputPath = "./dist/tailwind.css";
|
|
48
|
+
const outputPath = "./dist/tailwind.css"; // in-place rewrite
|
|
51
49
|
|
|
52
|
-
|
|
50
|
+
const css = await fs.readFile(inputPath, "utf8");
|
|
51
|
+
const converted = convertTailwindCssToHex(css, { gamut: "clip" });
|
|
52
|
+
await fs.writeFile(outputPath, converted, "utf8");
|
|
53
|
+
```
|
|
53
54
|
|
|
54
|
-
|
|
55
|
+
Then call it in your app `package.json`:
|
|
55
56
|
|
|
56
57
|
```json
|
|
57
58
|
{
|
|
58
59
|
"scripts": {
|
|
59
60
|
"build:css": "tailwindcss -i ./src/input.css -o ./dist/tailwind.css",
|
|
60
|
-
"build:css:hex": "
|
|
61
|
+
"build:css:hex": "node ./scripts/convert-tailwind-to-hex.mjs",
|
|
61
62
|
"build": "npm run build:css && npm run build:css:hex"
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
```
|
|
65
66
|
|
|
66
|
-
This
|
|
67
|
-
|
|
68
|
-
If you want two files instead, output to `./dist/tailwind.hex.css` and link that file.
|
|
69
|
-
|
|
70
|
-
Note: some browsers still show `rgb(...)` in the "Computed" pane. The "Styles" pane reflects your source declaration format.
|
|
71
|
-
|
|
72
|
-
## JS API
|
|
73
|
-
|
|
74
|
-
```js
|
|
75
|
-
import { oklchToHex, replaceOklchInText } from "oklchtohex";
|
|
76
|
-
|
|
77
|
-
const hex = oklchToHex("oklch(70.4% 0.191 22.216)");
|
|
78
|
-
const css = replaceOklchInText("color: oklch(70.4% 0.191 22.216);");
|
|
79
|
-
```
|
|
67
|
+
This keeps the package library-only while still giving build-time conversion.
|
package/package.json
CHANGED
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "oklchtohex",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Convert OKLCH colors to HEX
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Convert OKLCH colors to HEX as a JavaScript package.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/converter.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./src/converter.js"
|
|
9
9
|
},
|
|
10
|
-
"bin": {
|
|
11
|
-
"oklchtohex": "./src/cli.js"
|
|
12
|
-
},
|
|
13
10
|
"files": [
|
|
14
11
|
"src",
|
|
15
12
|
"README.md"
|
|
16
13
|
],
|
|
17
|
-
"scripts": {
|
|
18
|
-
"check": "node ./src/cli.js --help"
|
|
19
|
-
},
|
|
20
14
|
"keywords": [
|
|
21
15
|
"oklch",
|
|
22
16
|
"hex",
|
package/src/cli.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import fs from "node:fs/promises";
|
|
4
|
-
import path from "node:path";
|
|
5
|
-
import process from "node:process";
|
|
6
|
-
import { oklchToHex, replaceOklchInText } from "./converter.js";
|
|
7
|
-
|
|
8
|
-
function printHelp() {
|
|
9
|
-
const help = `
|
|
10
|
-
oklchtohex
|
|
11
|
-
|
|
12
|
-
Convert OKLCH values to HEX, or rewrite an entire CSS file.
|
|
13
|
-
|
|
14
|
-
Usage:
|
|
15
|
-
oklchtohex --value "oklch(70.4% 0.191 22.216)"
|
|
16
|
-
oklchtohex ./dist/app.css -o ./dist/app.hex.css
|
|
17
|
-
oklchtohex ./dist/app.css --stdout
|
|
18
|
-
|
|
19
|
-
Options:
|
|
20
|
-
--value <oklch> Convert a single OKLCH value to HEX
|
|
21
|
-
-o, --output <file> Output file path for transformed CSS
|
|
22
|
-
--stdout Print transformed CSS to stdout
|
|
23
|
-
--gamut <fit|clip> Gamut strategy (default: clip)
|
|
24
|
-
--alpha <auto|always|never>
|
|
25
|
-
Include alpha in HEX (default: auto)
|
|
26
|
-
--upper Uppercase HEX output
|
|
27
|
-
-h, --help Show this help text
|
|
28
|
-
`;
|
|
29
|
-
process.stdout.write(help);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function parseArgs(argv) {
|
|
33
|
-
const parsed = {
|
|
34
|
-
input: undefined,
|
|
35
|
-
output: undefined,
|
|
36
|
-
value: undefined,
|
|
37
|
-
stdout: false,
|
|
38
|
-
gamut: "clip",
|
|
39
|
-
includeAlpha: "auto",
|
|
40
|
-
uppercase: false,
|
|
41
|
-
help: false
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
for (let i = 0; i < argv.length; i += 1) {
|
|
45
|
-
const arg = argv[i];
|
|
46
|
-
|
|
47
|
-
if (arg === "-h" || arg === "--help") {
|
|
48
|
-
parsed.help = true;
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
51
|
-
if (arg === "--value") {
|
|
52
|
-
parsed.value = argv[i + 1];
|
|
53
|
-
i += 1;
|
|
54
|
-
continue;
|
|
55
|
-
}
|
|
56
|
-
if (arg === "-o" || arg === "--output") {
|
|
57
|
-
parsed.output = argv[i + 1];
|
|
58
|
-
i += 1;
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
if (arg === "--stdout") {
|
|
62
|
-
parsed.stdout = true;
|
|
63
|
-
continue;
|
|
64
|
-
}
|
|
65
|
-
if (arg === "--gamut") {
|
|
66
|
-
parsed.gamut = argv[i + 1];
|
|
67
|
-
i += 1;
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (arg === "--alpha") {
|
|
71
|
-
parsed.includeAlpha = argv[i + 1];
|
|
72
|
-
i += 1;
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (arg === "--upper") {
|
|
76
|
-
parsed.uppercase = true;
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
if (!arg.startsWith("-") && !parsed.input) {
|
|
80
|
-
parsed.input = arg;
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
throw new Error(`Unknown argument: ${arg}`);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return parsed;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function normalizeIncludeAlpha(value) {
|
|
91
|
-
if (!value || value === "auto") {
|
|
92
|
-
return "auto";
|
|
93
|
-
}
|
|
94
|
-
if (value === "always") {
|
|
95
|
-
return true;
|
|
96
|
-
}
|
|
97
|
-
if (value === "never") {
|
|
98
|
-
return false;
|
|
99
|
-
}
|
|
100
|
-
throw new Error(`Invalid --alpha value: ${value}`);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function normalizeGamut(value) {
|
|
104
|
-
if (value === "fit" || value === "clip") {
|
|
105
|
-
return value;
|
|
106
|
-
}
|
|
107
|
-
throw new Error(`Invalid --gamut value: ${value}`);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function run() {
|
|
111
|
-
const args = parseArgs(process.argv.slice(2));
|
|
112
|
-
if (args.help) {
|
|
113
|
-
printHelp();
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const options = {
|
|
118
|
-
gamut: normalizeGamut(args.gamut),
|
|
119
|
-
includeAlpha: normalizeIncludeAlpha(args.includeAlpha),
|
|
120
|
-
uppercase: args.uppercase
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
if (args.value) {
|
|
124
|
-
const hex = oklchToHex(args.value, options);
|
|
125
|
-
process.stdout.write(`${hex}\n`);
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (!args.input) {
|
|
130
|
-
throw new Error("Missing input. Provide --value or a CSS file path.");
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const inputPath = path.resolve(process.cwd(), args.input);
|
|
134
|
-
const css = await fs.readFile(inputPath, "utf8");
|
|
135
|
-
const transformed = replaceOklchInText(css, options);
|
|
136
|
-
|
|
137
|
-
if (args.stdout || !args.output) {
|
|
138
|
-
process.stdout.write(transformed);
|
|
139
|
-
if (!transformed.endsWith("\n")) {
|
|
140
|
-
process.stdout.write("\n");
|
|
141
|
-
}
|
|
142
|
-
return;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const outputPath = path.resolve(process.cwd(), args.output);
|
|
146
|
-
await fs.writeFile(outputPath, transformed, "utf8");
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
run().catch((error) => {
|
|
150
|
-
process.stderr.write(`${error.message}\n`);
|
|
151
|
-
process.exit(1);
|
|
152
|
-
});
|