@andbc/pigment 0.0.2

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/LICENSE +21 -0
  2. package/README.md +77 -0
  3. package/dist/index.js +272 -0
  4. package/package.json +34 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 andbc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # pigment
2
+
3
+ CLI tool to convert colors between formats.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install -g @andbc/pigment
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```bash
14
+ pigment <color> --to <format> [--from <format>]
15
+ ```
16
+
17
+ ## Supported Formats
18
+
19
+ | Format | Example input | Example output |
20
+ |----------|--------------------------------------|------------------------------------------|
21
+ | `hex` | `#ff6600` / `#ff660080` | `#ff6600` / `#ff660080` |
22
+ | `rgb` | `rgb(255, 102, 0)` | `rgb(255, 102, 0)` |
23
+ | `hsl` | `hsl(24, 100%, 50%)` | `hsl(24, 100%, 50%)` |
24
+ | `glsl` | `vec3(1.0, 0.4, 0.0)` | `vec3(1.000, 0.400, 0.000)` |
25
+ | `oklch` | `oklch(65% 0.196 41)` | `oklch(69.58% 0.2043 43.49)` |
26
+ | `oklab` | `oklab(65% 0.1 0.1)` | `oklab(65.00% 0.1000 0.1000)` |
27
+ | `p3` | `color(display-p3 1 0.4 0)` | `color(display-p3 1.0000 0.4000 0.0000)` |
28
+
29
+ **GLSL:** outputs `vec3` when the source has no alpha channel, `vec4` when it does (e.g. 8-digit hex `#rrggbbaa`).
30
+
31
+ ## Examples
32
+
33
+ ```bash
34
+ # Hex to RGB (format auto-detected)
35
+ pigment "#ff6600" --to rgb
36
+ # rgb(255, 102, 0)
37
+
38
+ # Hex to OKLCH (format auto-detected)
39
+ pigment "#ff6600" --to oklch
40
+ # oklch(0.6958 0.2043 43.49)
41
+
42
+ # 8-digit hex to GLSL vec4 (with alpha, format auto-detected)
43
+ pigment "#ff660080" --to glsl
44
+ # vec4(1.000, 0.400, 0.000, 0.502)
45
+
46
+ # RGB to HSL (explicit --from)
47
+ pigment "rgb(255, 102, 0)" --from rgb --to hsl
48
+ # hsl(24, 100%, 50%)
49
+
50
+ # OKLCH to Display P3 (explicit --from)
51
+ pigment "oklch(0.65 0.196 41)" --from oklch --to p3
52
+ # color(display-p3 0.8588 0.3860 0.1713)
53
+
54
+ # GLSL to hex (explicit --from)
55
+ pigment "vec3(1.0, 0.4, 0.0)" --from glsl --to hex
56
+ # #ff6600
57
+ ```
58
+
59
+ ## Flags
60
+
61
+ | Flag | Alias | Description |
62
+ |----------|-------|-----------------------------------------|
63
+ | `--from` | `-f` | Input format (auto-detected if omitted) |
64
+ | `--to` | `-t` | Output format (required) |
65
+ | `--help` | `-h` | Show help |
66
+
67
+ ## Build
68
+
69
+ ```bash
70
+ yarn build
71
+ ```
72
+
73
+ Outputs to `dist/index.js`.
74
+
75
+ ## Acknowledgements
76
+
77
+ Color space conversions by [`@texel/color`](https://github.com/texel-org/color).
package/dist/index.js ADDED
@@ -0,0 +1,272 @@
1
+ // src/index.ts
2
+ import yargs from "yargs";
3
+
4
+ // src/converters.ts
5
+ import {
6
+ convert as colorConvert,
7
+ deserialize,
8
+ sRGB,
9
+ OKLab,
10
+ OKLCH,
11
+ DisplayP3
12
+ } from "@texel/color";
13
+ var FORMATS = ["hex", "rgb", "hsl", "glsl", "oklch", "oklab", "p3"];
14
+ function hslToSRGB(h, s, l) {
15
+ if (s === 0) return [l, l, l];
16
+ const hue2rgb = (p2, q2, t) => {
17
+ if (t < 0) t += 1;
18
+ if (t > 1) t -= 1;
19
+ if (t < 1 / 6) return p2 + (q2 - p2) * 6 * t;
20
+ if (t < 1 / 2) return q2;
21
+ if (t < 2 / 3) return p2 + (q2 - p2) * (2 / 3 - t) * 6;
22
+ return p2;
23
+ };
24
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
25
+ const p = 2 * l - q;
26
+ return [hue2rgb(p, q, h + 1 / 3), hue2rgb(p, q, h), hue2rgb(p, q, h - 1 / 3)];
27
+ }
28
+ function sRGBToHSL(r, g, b) {
29
+ const max = Math.max(r, g, b);
30
+ const min = Math.min(r, g, b);
31
+ const l = (max + min) / 2;
32
+ if (max === min) return [0, 0, l];
33
+ const d = max - min;
34
+ const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
35
+ let h = 0;
36
+ if (max === r) h = ((g - b) / d + (g < b ? 6 : 0)) / 6;
37
+ else if (max === g) h = ((b - r) / d + 2) / 6;
38
+ else h = ((r - g) / d + 4) / 6;
39
+ return [h, s, l];
40
+ }
41
+ function detectFormat(color2) {
42
+ const s = color2.trim().toLowerCase();
43
+ if (s.startsWith("#")) return "hex";
44
+ if (s.startsWith("rgba(") || s.startsWith("rgb(")) return "rgb";
45
+ if (s.startsWith("hsla(") || s.startsWith("hsl(")) return "hsl";
46
+ if (s.startsWith("vec3(") || s.startsWith("vec4(")) return "glsl";
47
+ if (s.startsWith("oklch(")) return "oklch";
48
+ if (s.startsWith("oklab(")) return "oklab";
49
+ if (s.startsWith("color(display-p3")) return "p3";
50
+ throw new Error(`Cannot detect format for: ${color2}`);
51
+ }
52
+ function parse(color2, from) {
53
+ switch (from) {
54
+ case "hex": {
55
+ const hex = color2.replace(/^#/, "");
56
+ const len = hex.length;
57
+ if (len === 3) {
58
+ return {
59
+ values: [
60
+ parseInt(hex[0] + hex[0], 16) / 255,
61
+ parseInt(hex[1] + hex[1], 16) / 255,
62
+ parseInt(hex[2] + hex[2], 16) / 255
63
+ ],
64
+ hasAlpha: false
65
+ };
66
+ }
67
+ if (len === 4) {
68
+ return {
69
+ values: [
70
+ parseInt(hex[0] + hex[0], 16) / 255,
71
+ parseInt(hex[1] + hex[1], 16) / 255,
72
+ parseInt(hex[2] + hex[2], 16) / 255,
73
+ parseInt(hex[3] + hex[3], 16) / 255
74
+ ],
75
+ hasAlpha: true
76
+ };
77
+ }
78
+ if (len === 6) {
79
+ return {
80
+ values: [
81
+ parseInt(hex.slice(0, 2), 16) / 255,
82
+ parseInt(hex.slice(2, 4), 16) / 255,
83
+ parseInt(hex.slice(4, 6), 16) / 255
84
+ ],
85
+ hasAlpha: false
86
+ };
87
+ }
88
+ if (len === 8) {
89
+ return {
90
+ values: [
91
+ parseInt(hex.slice(0, 2), 16) / 255,
92
+ parseInt(hex.slice(2, 4), 16) / 255,
93
+ parseInt(hex.slice(4, 6), 16) / 255,
94
+ parseInt(hex.slice(6, 8), 16) / 255
95
+ ],
96
+ hasAlpha: true
97
+ };
98
+ }
99
+ throw new Error(`Invalid hex color: ${color2}`);
100
+ }
101
+ case "rgb": {
102
+ const m = color2.match(/rgba?\(\s*([^)]+)\)/i);
103
+ if (!m) throw new Error(`Invalid rgb color: ${color2}`);
104
+ const parts = m[1].split(",").map((s) => s.trim());
105
+ const r = parseInt(parts[0]) / 255;
106
+ const g = parseInt(parts[1]) / 255;
107
+ const b = parseInt(parts[2]) / 255;
108
+ if (parts.length === 4) {
109
+ return { values: [r, g, b, parseFloat(parts[3])], hasAlpha: true };
110
+ }
111
+ return { values: [r, g, b], hasAlpha: false };
112
+ }
113
+ case "hsl": {
114
+ const m = color2.match(/hsla?\(\s*([^)]+)\)/i);
115
+ if (!m) throw new Error(`Invalid hsl color: ${color2}`);
116
+ const parts = m[1].split(",").map((s2) => s2.trim());
117
+ const h = parseFloat(parts[0]) / 360;
118
+ const s = parseFloat(parts[1]) / 100;
119
+ const l = parseFloat(parts[2]) / 100;
120
+ const rgb = hslToSRGB(h, s, l);
121
+ if (parts.length === 4) {
122
+ return { values: [...rgb, parseFloat(parts[3])], hasAlpha: true };
123
+ }
124
+ return { values: rgb, hasAlpha: false };
125
+ }
126
+ case "glsl": {
127
+ const m = color2.match(/vec([34])\(\s*([^)]+)\)/);
128
+ if (!m) throw new Error(`Invalid glsl color: ${color2}`);
129
+ const isVec4 = m[1] === "4";
130
+ const components = m[2].split(",").map((s) => parseFloat(s.trim()));
131
+ return { values: components, hasAlpha: isVec4 };
132
+ }
133
+ case "oklch": {
134
+ const result = deserialize(color2);
135
+ const srgb = colorConvert(
136
+ result.coords,
137
+ OKLCH,
138
+ sRGB
139
+ );
140
+ return { values: Array.from(srgb), hasAlpha: false };
141
+ }
142
+ case "oklab": {
143
+ const result = deserialize(color2);
144
+ const srgb = colorConvert(
145
+ result.coords,
146
+ OKLab,
147
+ sRGB
148
+ );
149
+ return { values: Array.from(srgb), hasAlpha: false };
150
+ }
151
+ case "p3": {
152
+ const result = deserialize(color2);
153
+ const srgb = colorConvert(
154
+ result.coords,
155
+ DisplayP3,
156
+ sRGB
157
+ );
158
+ return { values: Array.from(srgb), hasAlpha: false };
159
+ }
160
+ }
161
+ }
162
+ function convertToSpace(srgbValues, to) {
163
+ const alpha = srgbValues.length === 4 ? srgbValues[3] : void 0;
164
+ const rgb3 = [srgbValues[0], srgbValues[1], srgbValues[2]];
165
+ const converted = (() => {
166
+ switch (to) {
167
+ case "hex":
168
+ case "rgb":
169
+ case "glsl":
170
+ return Array.from(rgb3);
171
+ case "hsl":
172
+ return Array.from(sRGBToHSL(rgb3[0], rgb3[1], rgb3[2]));
173
+ case "oklch":
174
+ return Array.from(colorConvert(rgb3, sRGB, OKLCH));
175
+ case "oklab":
176
+ return Array.from(colorConvert(rgb3, sRGB, OKLab));
177
+ case "p3":
178
+ return Array.from(colorConvert(rgb3, sRGB, DisplayP3));
179
+ }
180
+ })();
181
+ if (alpha !== void 0) converted.push(alpha);
182
+ return converted;
183
+ }
184
+ function serialize(values, to, hasAlpha) {
185
+ const [c0, c1, c2, c3] = values;
186
+ switch (to) {
187
+ case "hex": {
188
+ const r = Math.round(c0 * 255).toString(16).padStart(2, "0");
189
+ const g = Math.round(c1 * 255).toString(16).padStart(2, "0");
190
+ const b = Math.round(c2 * 255).toString(16).padStart(2, "0");
191
+ if (hasAlpha && c3 !== void 0) {
192
+ const a = Math.round(c3 * 255).toString(16).padStart(2, "0");
193
+ return `#${r}${g}${b}${a}`;
194
+ }
195
+ return `#${r}${g}${b}`;
196
+ }
197
+ case "rgb": {
198
+ const r = Math.round(c0 * 255);
199
+ const g = Math.round(c1 * 255);
200
+ const b = Math.round(c2 * 255);
201
+ if (hasAlpha && c3 !== void 0) return `rgba(${r}, ${g}, ${b}, ${c3.toFixed(3)})`;
202
+ return `rgb(${r}, ${g}, ${b})`;
203
+ }
204
+ case "hsl": {
205
+ const h = Math.round(c0 * 360);
206
+ const s = Math.round(c1 * 100);
207
+ const l = Math.round(c2 * 100);
208
+ if (hasAlpha && c3 !== void 0) return `hsla(${h}, ${s}%, ${l}%, ${c3.toFixed(3)})`;
209
+ return `hsl(${h}, ${s}%, ${l}%)`;
210
+ }
211
+ case "glsl": {
212
+ const r = c0.toFixed(3);
213
+ const g = c1.toFixed(3);
214
+ const b = c2.toFixed(3);
215
+ if (hasAlpha && c3 !== void 0) return `vec4(${r}, ${g}, ${b}, ${c3.toFixed(3)})`;
216
+ return `vec3(${r}, ${g}, ${b})`;
217
+ }
218
+ case "oklch": {
219
+ const L = c0.toFixed(4);
220
+ const C = c1.toFixed(4);
221
+ const H = c2.toFixed(2);
222
+ if (hasAlpha && c3 !== void 0) return `oklch(${L} ${C} ${H} / ${c3.toFixed(3)})`;
223
+ return `oklch(${L} ${C} ${H})`;
224
+ }
225
+ case "oklab": {
226
+ const L = c0.toFixed(4);
227
+ const a = c1.toFixed(4);
228
+ const b = c2.toFixed(4);
229
+ if (hasAlpha && c3 !== void 0) return `oklab(${L} ${a} ${b} / ${c3.toFixed(3)})`;
230
+ return `oklab(${L} ${a} ${b})`;
231
+ }
232
+ case "p3": {
233
+ const r = c0.toFixed(4);
234
+ const g = c1.toFixed(4);
235
+ const b = c2.toFixed(4);
236
+ if (hasAlpha && c3 !== void 0) return `color(display-p3 ${r} ${g} ${b} / ${c3.toFixed(3)})`;
237
+ return `color(display-p3 ${r} ${g} ${b})`;
238
+ }
239
+ }
240
+ }
241
+
242
+ // src/index.ts
243
+ var argv = await yargs(process.argv.slice(2)).usage("$0 <color> [options]").positional("color", {
244
+ type: "string",
245
+ description: "Input color value"
246
+ }).option("from", {
247
+ alias: "f",
248
+ type: "string",
249
+ description: "Input format (auto-detected if omitted)",
250
+ choices: FORMATS
251
+ }).option("to", {
252
+ alias: "t",
253
+ type: "string",
254
+ description: "Output format",
255
+ choices: FORMATS,
256
+ demandOption: true
257
+ }).help().alias("help", "h").parseAsync();
258
+ var color = argv._[0];
259
+ if (!color) {
260
+ console.error("Missing required argument: color");
261
+ process.exit(1);
262
+ }
263
+ try {
264
+ const from = argv.from ?? detectFormat(color);
265
+ const { values, hasAlpha } = parse(color, from);
266
+ const converted = convertToSpace(values, argv.to);
267
+ const output = serialize(converted, argv.to, hasAlpha);
268
+ console.log(output);
269
+ } catch (e) {
270
+ console.error(e.message);
271
+ process.exit(1);
272
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@andbc/pigment",
3
+ "version": "0.0.2",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "A CLI tool to convert colors between different formats (HEX, RGB, HSL, etc.)",
8
+ "type": "module",
9
+ "bin": {
10
+ "pigment": "dist/index.js"
11
+ },
12
+ "files": [
13
+ "dist",
14
+ "bin"
15
+ ],
16
+ "main": "index.js",
17
+ "scripts": {
18
+ "watch": "tsup ./src/index.ts --format esm --watch",
19
+ "build": "tsc --noemit && tsup ./src/index.ts --format esm",
20
+ "prepublishOnly": "npm run build"
21
+ },
22
+ "author": "Andy Duboc",
23
+ "license": "MIT",
24
+ "devDependencies": {
25
+ "@types/node": "^25.0.8",
26
+ "@types/yargs": "^17.0.35",
27
+ "tsup": "^8.5.1",
28
+ "typescript": "^5.9.3"
29
+ },
30
+ "dependencies": {
31
+ "@texel/color": "^1.1.11",
32
+ "yargs": "17.7.2"
33
+ }
34
+ }