chromonym 1.0.0 → 1.1.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 +165 -3
- package/dist/colorspaces/index.d.ts +4 -0
- package/dist/colorspaces/index.d.ts.map +1 -0
- package/dist/colorspaces/index.js +4 -0
- package/dist/colorspaces/index.js.map +1 -0
- package/dist/colorspaces/pantone.d.ts +919 -0
- package/dist/colorspaces/pantone.d.ts.map +1 -0
- package/dist/colorspaces/pantone.js +918 -0
- package/dist/colorspaces/pantone.js.map +1 -0
- package/dist/colorspaces/web.d.ts +156 -0
- package/dist/colorspaces/web.d.ts.map +1 -0
- package/dist/colorspaces/web.js +155 -0
- package/dist/colorspaces/web.js.map +1 -0
- package/dist/colorspaces/x11.d.ts +667 -0
- package/dist/colorspaces/x11.d.ts.map +1 -0
- package/dist/colorspaces/x11.js +666 -0
- package/dist/colorspaces/x11.js.map +1 -0
- package/dist/conversions/hex.d.ts +13 -0
- package/dist/conversions/hex.d.ts.map +1 -0
- package/dist/conversions/hex.js +39 -0
- package/dist/conversions/hex.js.map +1 -0
- package/dist/conversions/hsl.d.ts +12 -0
- package/dist/conversions/hsl.d.ts.map +1 -0
- package/dist/conversions/hsl.js +90 -0
- package/dist/conversions/hsl.js.map +1 -0
- package/dist/conversions/hsv.d.ts +12 -0
- package/dist/conversions/hsv.d.ts.map +1 -0
- package/dist/conversions/hsv.js +85 -0
- package/dist/conversions/hsv.js.map +1 -0
- package/dist/conversions/index.d.ts +6 -0
- package/dist/conversions/index.d.ts.map +1 -0
- package/dist/conversions/index.js +6 -0
- package/dist/conversions/index.js.map +1 -0
- package/dist/conversions/pantone.d.ts +14 -0
- package/dist/conversions/pantone.d.ts.map +1 -0
- package/dist/conversions/pantone.js +63 -0
- package/dist/conversions/pantone.js.map +1 -0
- package/dist/conversions/rgb.d.ts +13 -0
- package/dist/conversions/rgb.d.ts.map +1 -0
- package/dist/conversions/rgb.js +46 -0
- package/dist/conversions/rgb.js.map +1 -0
- package/dist/convert.d.ts +10 -0
- package/dist/convert.d.ts.map +1 -0
- package/dist/convert.js +51 -0
- package/dist/convert.js.map +1 -0
- package/dist/detectFormat.d.ts +9 -0
- package/dist/detectFormat.d.ts.map +1 -0
- package/dist/detectFormat.js +47 -0
- package/dist/detectFormat.js.map +1 -0
- package/dist/identify.d.ts +12 -0
- package/dist/identify.d.ts.map +1 -0
- package/dist/identify.js +44 -0
- package/dist/identify.js.map +1 -0
- package/dist/index.d.ts +14 -337
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -267
- package/dist/index.js.map +1 -1
- package/dist/math/euclideanDistance.d.ts +9 -0
- package/dist/math/euclideanDistance.d.ts.map +1 -0
- package/dist/math/euclideanDistance.js +13 -0
- package/dist/math/euclideanDistance.js.map +1 -0
- package/dist/math/index.d.ts +2 -0
- package/dist/math/index.d.ts.map +1 -0
- package/dist/math/index.js +4 -0
- package/dist/math/index.js.map +1 -0
- package/dist/resolve.d.ts +14 -0
- package/dist/resolve.d.ts.map +1 -0
- package/dist/resolve.js +49 -0
- package/dist/resolve.js.map +1 -0
- package/dist/types.d.ts +42 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +4 -0
- package/dist/types.js.map +1 -0
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -1,17 +1,166 @@
|
|
|
1
1
|
# chromonym
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/simiancraft/chromonym/actions/workflows/ci.yml)
|
|
4
|
+
[](https://codecov.io/gh/simiancraft/chromonym)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://github.com/semantic-release/semantic-release)
|
|
7
|
+
[](https://biomejs.dev)
|
|
4
8
|
|
|
5
|
-
|
|
9
|
+
Identify, resolve, and convert colors across CSS, X11, and Pantone colorspaces.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { identify, resolve, convert } from 'chromonym';
|
|
13
|
+
|
|
14
|
+
identify('#ff0000') // 'red'
|
|
15
|
+
resolve('crimson') // '#dc143c'
|
|
16
|
+
resolve('185 C', { colorspace: 'pantone' }) // '#e4002b'
|
|
17
|
+
convert('#ff0000', { format: 'RGB' }) // 'rgb(255, 0, 0)'
|
|
18
|
+
convert({ r: 255, g: 0, b: 0 }, { format: 'HSL' })// 'hsl(0, 100%, 50%)'
|
|
19
|
+
```
|
|
6
20
|
|
|
7
21
|
## Install
|
|
8
22
|
|
|
9
23
|
```sh
|
|
10
24
|
bun add chromonym
|
|
11
|
-
# or
|
|
12
25
|
npm install chromonym
|
|
13
26
|
```
|
|
14
27
|
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
Three primary functions. All accept an optional `opts` object; defaults fire when omitted.
|
|
31
|
+
|
|
32
|
+
### `identify(input, opts?)`
|
|
33
|
+
|
|
34
|
+
**Color → name.** Finds the nearest-match name in the chosen colorspace using Euclidean distance in RGB.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
function identify(
|
|
38
|
+
input: ColorInput,
|
|
39
|
+
opts?: { colorspace?: ColorspaceName },
|
|
40
|
+
): string | null
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
- **Default colorspace**: `'web'`.
|
|
44
|
+
- Returns the matched name, or `null` if input is unrecognized (`detectFormat` returns `'UNKNOWN'`).
|
|
45
|
+
- Ties are broken by the first entry encountered in the colorspace data.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
identify('#ff0000') // 'red'
|
|
49
|
+
identify([255, 0, 0]) // 'red'
|
|
50
|
+
identify({ r: 250, g: 20, b: 60 }) // 'crimson' (nearest)
|
|
51
|
+
identify('#ff0000', { colorspace: 'x11' }) // 'red' (or 'red1', X11 has numbered variants)
|
|
52
|
+
identify('#ff0000', { colorspace: 'pantone' }) // nearest Pantone C code, e.g. '185C'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### `resolve(name, opts?)`
|
|
56
|
+
|
|
57
|
+
**Name → color.** Normalizes the input (lowercases it, strips all non-alphanumeric characters), looks up the canonical key in the chosen colorspace, and returns the value in the chosen format.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
function resolve(
|
|
61
|
+
name: string,
|
|
62
|
+
opts?: { colorspace?: ColorspaceName; format?: ColorFormat },
|
|
63
|
+
): ColorValue | null
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- **Default colorspace**: `'web'`.
|
|
67
|
+
- **Default format**: `'HEX'`.
|
|
68
|
+
- Returns `null` if the normalized name isn't in the colorspace.
|
|
69
|
+
- Normalization lets users pass `'Alice Blue'`, `'alice-blue'`, `'ALICEBLUE'`, `'Pantone 185 C'` etc. — all resolved identically.
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
resolve('crimson') // '#dc143c'
|
|
73
|
+
resolve('Alice Blue') // '#f0f8ff'
|
|
74
|
+
resolve('alice-blue!') // '#f0f8ff'
|
|
75
|
+
resolve('185 C', { colorspace: 'pantone' }) // '#e4002b'
|
|
76
|
+
resolve('Pantone 185 C', { colorspace: 'pantone' })// '#e4002b' (same, normalized)
|
|
77
|
+
resolve('crimson', { format: 'RGB' }) // 'rgb(220, 20, 60)'
|
|
78
|
+
resolve('crimson', { format: 'RGBA' }) // { r: 220, g: 20, b: 60, a: 1 }
|
|
79
|
+
resolve('not-a-color') // null
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### `convert(input, opts?)`
|
|
83
|
+
|
|
84
|
+
**Color → color in a different format.** Detects the input format, normalizes to the canonical internal `Rgba` representation, and returns the value in the target format. Colorspace-independent.
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
function convert(
|
|
88
|
+
input: ColorInput,
|
|
89
|
+
opts?: { format?: ColorFormat },
|
|
90
|
+
): ColorValue
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- **Default format**: `'HEX'`.
|
|
94
|
+
- Throws if input format is unrecognized.
|
|
95
|
+
- Alpha information is preserved when converting into RGBA/RGB(A) string forms, and discarded for HEX (6-digit) / HSL / HSV / PANTONE output.
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
convert('#ff0000') // '#ff0000' (identity)
|
|
99
|
+
convert('#ff0000', { format: 'RGB' }) // 'rgb(255, 0, 0)'
|
|
100
|
+
convert('#ff0000', { format: 'RGBA' }) // { r: 255, g: 0, b: 0, a: 1 }
|
|
101
|
+
convert([255, 0, 0], { format: 'HEX' }) // '#ff0000'
|
|
102
|
+
convert('rgb(255, 0, 0)', { format: 'HSL' }) // 'hsl(0, 100%, 50%)'
|
|
103
|
+
convert({ h: 0, s: 100, l: 50 }, { format: 'HEX' })// '#ff0000'
|
|
104
|
+
convert('185 C', { format: 'HEX' }) // '#e4002b'
|
|
105
|
+
convert('#e4002b', { format: 'PANTONE' }) // '185C' (nearest Pantone C)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Colorspaces
|
|
109
|
+
|
|
110
|
+
| Name | Entries | Source |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `'web'` (default) | 148 | CSS Color Module Level 4 named colors |
|
|
113
|
+
| `'x11'` | 658 | X.Org `rgb.txt` (public domain) |
|
|
114
|
+
| `'pantone'` | 907 | Pantone Coated (C) — community approximations (not Pantone-licensed) |
|
|
115
|
+
|
|
116
|
+
Each colorspace is a pure data object: `Record<string, HexColor>`. Importable directly:
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
import { web, x11, pantone } from 'chromonym';
|
|
120
|
+
web.crimson // '#dc143c'
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## Formats
|
|
124
|
+
|
|
125
|
+
Format keys are SCREAMING_CAPS — they are used as dispatch keys internally.
|
|
126
|
+
|
|
127
|
+
| Key | Input accepted | Output shape |
|
|
128
|
+
|---|---|---|
|
|
129
|
+
| `'HEX'` | `'#rgb'`, `'#rrggbb'`, `'#rrggbbaa'` | `'#rrggbb'` string |
|
|
130
|
+
| `'RGB'` | `'rgb(r, g, b)'`, `[r, g, b]`, `{ r, g, b }` | `'rgb(r, g, b)'` string |
|
|
131
|
+
| `'RGBA'` | `'rgba(r, g, b, a)'`, `[r, g, b, a]`, `{ r, g, b, a }` | `{ r, g, b, a }` object |
|
|
132
|
+
| `'HSL'` | `'hsl(h, s%, l%)'`, `{ h, s, l }` | `'hsl(h, s%, l%)'` string |
|
|
133
|
+
| `'HSV'` | `'hsv(h, s%, v%)'`, `{ h, s, v }` | `'hsv(h, s%, v%)'` string |
|
|
134
|
+
| `'PANTONE'` | `'185 C'`, `'185C'`, `'Pantone 185 C'`, etc. | `'185C'`-style string |
|
|
135
|
+
|
|
136
|
+
The runtime set `COLOR_FORMATS` is exported for enumeration / membership checks.
|
|
137
|
+
|
|
138
|
+
## Utilities
|
|
139
|
+
|
|
140
|
+
- **`detectFormat(input): DetectedFormat`** — runtime format detection. Returns one of the format keys or `'UNKNOWN'`.
|
|
141
|
+
- **`COLOR_FORMATS: ReadonlySet<ColorFormat>`** — the Set of valid format keys.
|
|
142
|
+
- **Per-format converters** (tree-shakeable) — `hexToRgba`, `rgbaToHex`, `rgbToRgba`, `rgbaToRgb`, `hslToRgba`, `rgbaToHsl`, `hsvToRgba`, `rgbaToHsv`, `pantoneToRgba`, `rgbaToPantone`. Use these directly if you only need one conversion and want maximum tree-shaking.
|
|
143
|
+
|
|
144
|
+
## Tree-shaking
|
|
145
|
+
|
|
146
|
+
- `sideEffects: false` in `package.json`.
|
|
147
|
+
- All exports are named; no default export.
|
|
148
|
+
- Barrel uses explicit re-exports.
|
|
149
|
+
- Importing `{ resolve }` or `{ identify }` pulls in all three colorspaces (necessary for the string-keyed `colorspace` option). For strict tree-shaking of colorspace data, use the low-level converters and colorspace objects directly.
|
|
150
|
+
|
|
151
|
+
## Types
|
|
152
|
+
|
|
153
|
+
All types are re-exported from the root:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import type {
|
|
157
|
+
ColorInput, ColorValue, ColorFormat, ColorspaceName,
|
|
158
|
+
HexColor, Rgba, RgbInput, RgbaInput, HslInput, HsvInput, PantoneCode,
|
|
159
|
+
Colorspace,
|
|
160
|
+
WebColorName, X11ColorName, PantoneColorName,
|
|
161
|
+
} from 'chromonym';
|
|
162
|
+
```
|
|
163
|
+
|
|
15
164
|
## Development
|
|
16
165
|
|
|
17
166
|
```sh
|
|
@@ -22,6 +171,19 @@ bun test
|
|
|
22
171
|
bun run build
|
|
23
172
|
```
|
|
24
173
|
|
|
174
|
+
Regenerating colorspace data:
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
bun run scripts/generate-x11.ts # reads /usr/share/X11/rgb.txt
|
|
178
|
+
bun run scripts/generate-pantone.ts # reads color_library (MIT dependency)
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## Coverage
|
|
182
|
+
|
|
183
|
+
[](https://codecov.io/github/simiancraft/chromonym)
|
|
184
|
+
|
|
185
|
+
Each ring is a directory; each leaf is a file. Green is covered, red is gaps.
|
|
186
|
+
|
|
25
187
|
## License
|
|
26
188
|
|
|
27
189
|
MIT © [the-simian](https://github.com/the-simian)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/colorspaces/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,gBAAgB,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,KAAK,YAAY,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC/C,OAAO,EAAE,KAAK,YAAY,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/colorspaces/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,OAAO,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAqB,GAAG,EAAE,MAAM,OAAO,CAAC;AAC/C,OAAO,EAAqB,GAAG,EAAE,MAAM,OAAO,CAAC"}
|