oklchtohex 0.4.0 → 0.4.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.
Files changed (2) hide show
  1. package/README.md +112 -46
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,18 +1,21 @@
1
1
  # oklchtohex
2
2
 
3
- `oklchtohex` helps teams using Tailwind v4 convert `oklch(...)` colors into HEX.
3
+ [![npm version](https://img.shields.io/npm/v/oklchtohex.svg)](https://www.npmjs.com/package/oklchtohex)
4
+ [![license](https://img.shields.io/npm/l/oklchtohex.svg)](https://www.npmjs.com/package/oklchtohex)
5
+ [![npm downloads](https://img.shields.io/npm/dm/oklchtohex.svg)](https://www.npmjs.com/package/oklchtohex)
4
6
 
5
- This is useful when:
6
- - clients compare implementation colors against Figma tokens shown in HEX
7
- - QA wants quick visual checks from DevTools
8
- - you want an optional HEX-only CSS artifact at build time
7
+ Convert CSS `oklch(...)` colors to HEX.
9
8
 
10
- ## Why this approach
9
+ Built for teams using Tailwind v4 who want HEX values in generated CSS for QA, client review, and design-tool matching.
11
10
 
12
- A build-time package is the most practical fix:
13
- - no Tailwind fork required
14
- - works for any CSS output (including generated Tailwind files)
15
- - can run in CI and produce a deterministic `*.hex.css` artifact
11
+ ## Features
12
+
13
+ - Converts `oklch(...)` to HEX (`#RRGGBB` / `#RRGGBBAA`)
14
+ - Works as a Vite plugin (auto-convert in `dev` and `build`)
15
+ - Works as a plain JS utility for custom build scripts
16
+ - Handles Tailwind default color variables with exact palette HEX values
17
+ - Handles Tailwind slash opacity utilities like `border-red-500/30`
18
+ - Handles custom theme colors like `border-aurora/40` when emitted via `var(--color-aurora)`
16
19
 
17
20
  ## Install
18
21
 
@@ -20,26 +23,9 @@ A build-time package is the most practical fix:
20
23
  npm i -D oklchtohex
21
24
  ```
22
25
 
23
- ## JS API
24
-
25
- ```js
26
- import { oklchToHex, replaceOklchInText, convertTailwindCssToHex } from "oklchtohex";
27
-
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);
31
- ```
26
+ ## Quick Start (Vite Plugin)
32
27
 
33
- ### Conversion behavior
34
-
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
- - Tailwind opacity utilities like `border-red-500/30` and custom theme colors like `border-aurora/40` are handled when emitted as `color-mix(... var(--color-*) ..., transparent)` and converted to 8-digit HEX when the source variable is resolvable.
38
- - `gamut` option supports `clip` (default) and `fit`.
39
-
40
- ## Vite plugin (auto on dev + build)
41
-
42
- Use the built-in plugin to auto-convert CSS during `vite dev` and `vite build`.
28
+ Add the plugin to `vite.config.js` or `vite.config.ts`:
43
29
 
44
30
  ```js
45
31
  import { defineConfig } from "vite";
@@ -49,52 +35,132 @@ import { oklchToHexVitePlugin } from "oklchtohex/vite";
49
35
  export default defineConfig({
50
36
  plugins: [
51
37
  react(),
52
- oklchToHexVitePlugin({
53
- gamut: "clip",
54
- convertDev: true,
55
- convertBuild: true
56
- })
38
+ oklchToHexVitePlugin()
57
39
  ]
58
40
  });
59
41
  ```
60
42
 
61
- ### Plugin options
43
+ Default behavior:
44
+
45
+ - Converts during `vite dev`
46
+ - Converts during `vite build`
47
+ - Uses `gamut: "clip"`
48
+
49
+ No changes are required to your existing `dev` or `build` commands.
50
+
51
+ ## Vite Plugin Options
52
+
53
+ ```js
54
+ oklchToHexVitePlugin({
55
+ gamut: "clip", // "clip" | "fit"
56
+ includeAlpha: "auto", // "auto" | "always" | "never"
57
+ uppercase: false, // boolean
58
+ onError: "preserve", // "preserve" | "throw"
59
+ convertDev: true, // boolean
60
+ convertBuild: true, // boolean
61
+ include: /\\.css$/, // RegExp | (id) => boolean
62
+ exclude: /legacy\\.css$/ // RegExp | (id) => boolean
63
+ })
64
+ ```
65
+
66
+ ## JS API
67
+
68
+ ```js
69
+ import {
70
+ oklchToHex,
71
+ replaceOklchInText,
72
+ convertTailwindCssToHex
73
+ } from "oklchtohex";
74
+
75
+ const hex = oklchToHex("oklch(63.7% 0.237 25.331)");
76
+ const css = replaceOklchInText(".btn { color: oklch(63.7% 0.237 25.331); }");
77
+ const converted = convertTailwindCssToHex(css);
78
+ ```
79
+
80
+ ### `oklchToHex(input, options?)`
81
+
82
+ - `input`: OKLCH string or `{ l, c, h, alpha? }`
83
+ - Returns: HEX string
84
+
85
+ Options:
62
86
 
63
87
  - `gamut`: `"clip"` (default) or `"fit"`
64
88
  - `includeAlpha`: `"auto"` (default), `"always"`, `"never"`
65
89
  - `uppercase`: `false` (default)
66
- - `onError`: `"preserve"` (default) or `"throw"`
67
- - `include`: `RegExp | (id) => boolean` (default matches CSS-like files)
68
- - `exclude`: `RegExp | (id) => boolean`
69
- - `convertDev`: `true` by default
70
- - `convertBuild`: `true` by default
71
90
 
72
- ## Tailwind v4 build integration (package-only)
91
+ ### `replaceOklchInText(text, options?)`
92
+
93
+ - Converts all `oklch(...)` values in a string
94
+ - Returns transformed text
95
+
96
+ Extra option:
97
+
98
+ - `onError`: `"preserve"` (default) keeps original token on parse/conversion errors
99
+
100
+ ### `convertTailwindCssToHex(css, options?)`
101
+
102
+ - Alias for `replaceOklchInText` with Tailwind-oriented behavior
73
103
 
74
- Create a small Node script in your app, for example `scripts/convert-tailwind-to-hex.mjs`:
104
+ ## Tailwind-Specific Behavior
105
+
106
+ - Known default variables like `--color-red-500` are replaced with exact Tailwind HEX values.
107
+ - Unknown/custom variables are converted from `oklch(...)` using the converter.
108
+ - `color-mix(in oklab, var(--color-*) <x>%, transparent)` is converted to HEX with alpha when the source variable resolves to HEX.
109
+ - This supports utilities such as `border-red-500/30` and custom ones like `border-aurora/40`.
110
+
111
+ ## Manual Build Script (Non-Plugin Flow)
112
+
113
+ If you do not want the Vite plugin, run conversion in a custom build script:
75
114
 
76
115
  ```js
116
+ // scripts/convert-css-to-hex.mjs
77
117
  import fs from "node:fs/promises";
78
118
  import { convertTailwindCssToHex } from "oklchtohex";
79
119
 
80
120
  const inputPath = "./dist/tailwind.css";
81
- const outputPath = "./dist/tailwind.css"; // in-place rewrite
121
+ const outputPath = "./dist/tailwind.css"; // in-place
82
122
 
83
123
  const css = await fs.readFile(inputPath, "utf8");
84
124
  const converted = convertTailwindCssToHex(css, { gamut: "clip" });
85
125
  await fs.writeFile(outputPath, converted, "utf8");
86
126
  ```
87
127
 
88
- Then call it in your app `package.json`:
128
+ Then wire it in `package.json`:
89
129
 
90
130
  ```json
91
131
  {
92
132
  "scripts": {
93
133
  "build:css": "tailwindcss -i ./src/input.css -o ./dist/tailwind.css",
94
- "build:css:hex": "node ./scripts/convert-tailwind-to-hex.mjs",
134
+ "build:css:hex": "node ./scripts/convert-css-to-hex.mjs",
95
135
  "build": "npm run build:css && npm run build:css:hex"
96
136
  }
97
137
  }
98
138
  ```
99
139
 
100
- This keeps the package library-only while still giving build-time conversion.
140
+ ## Notes
141
+
142
+ - DevTools `Styles` reflects your source CSS format (HEX after conversion).
143
+ - DevTools `Computed` may still show normalized `rgb(...)` in some browsers.
144
+ - Node.js `>=18` is required.
145
+
146
+ ## FAQ
147
+
148
+ ### Do I need to change my `vite dev` or `vite build` commands?
149
+
150
+ No. If you use `oklchToHexVitePlugin()` in `vite.config.*`, your existing commands stay the same.
151
+
152
+ ### Why do I still see `rgb(...)` in DevTools sometimes?
153
+
154
+ Browsers often normalize colors in the `Computed` panel. Check the `Styles` panel or generated CSS file to confirm HEX output.
155
+
156
+ ### Does this support Tailwind custom colors like `border-aurora/40`?
157
+
158
+ Yes. Custom theme variables are converted first, then opacity utilities emitted via `color-mix(... var(--color-*) ..., transparent)` are resolved to HEX with alpha.
159
+
160
+ ### Can I use this without Vite?
161
+
162
+ Yes. Use the JS API (`replaceOklchInText` / `convertTailwindCssToHex`) in any Node-based build step.
163
+
164
+ ### What if a color token cannot be parsed?
165
+
166
+ Default behavior is `onError: "preserve"`, which keeps the original token unchanged. Set `onError: "throw"` to fail fast.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oklchtohex",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "description": "Convert OKLCH colors to HEX as a JavaScript package.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",