Skip to content

Commit f0cb91c

Browse files
Copilotmagnh
authored andcommitted
feat(eds-color-palette-generator): add CLI tool to generate color palettes (#4033)
Fixes: #4033
1 parent 284cc21 commit f0cb91c

22 files changed

+2764
-184
lines changed

packages/eds-color-palette-generator/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ node_modules/
1010

1111
# TypeScript
1212
*.tsbuildinfo
13+
14+
# Build output
15+
dist/
16+
output/

packages/eds-color-palette-generator/README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ An accessible color palette generator for the Equinor Design System. This tool c
1010
* **Light and dark mode support**: Separate configurations for optimal contrast in each mode
1111
* **Interactive configuration**: Adjust lightness values and Gaussian parameters in real-time
1212
* **Export/Import**: Save and share color palette configurations
13+
* **CLI tool**: Generate color tokens from configuration files
1314
* **About page**: Comprehensive documentation with interactive demos explaining how the generator works
1415

1516
## Getting Started
1617

18+
### Web Interface
19+
1720
First, run the development server:
1821

1922
```bash
@@ -24,6 +27,24 @@ Open [http://localhost:3000](http://localhost:3000) with your browser to see the
2427

2528
To learn how the generator works internally, visit the About page at [http://localhost:3000/about](http://localhost:3000/about).
2629

30+
### CLI Tool
31+
32+
Generate color tokens from a configuration file:
33+
34+
```bash
35+
generate-colors [configPath] [outputDir]
36+
```
37+
38+
See [src/cli/README.md](./src/cli/README.md) for detailed CLI documentation and examples.
39+
40+
**Example:**
41+
42+
```bash
43+
generate-colors examples/palette-config.json output/
44+
```
45+
46+
This will generate `color.tokens.light.json` and `color.tokens.dark.json` files in the output directory.
47+
2748
## How It Works
2849

2950
The generator uses a three-step process:
@@ -51,7 +72,7 @@ Color palettes can be configured through:
5172

5273
## Testing
5374

54-
Run tests with:
75+
Run unit tests with:
5576

5677
```bash
5778
pnpm test
@@ -63,6 +84,16 @@ Run end-to-end tests:
6384
pnpm test:e2e
6485
```
6586

87+
## Building
88+
89+
Build the CLI tool:
90+
91+
```bash
92+
pnpm build:cli
93+
```
94+
95+
This will compile the TypeScript CLI script into a distributable JavaScript file in the `dist/` directory.
96+
6697
## Documentation
6798

6899
* **[ABOUT_PAGE.md](./ABOUT_PAGE.md)**: Documentation for the About page and interactive components
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

packages/eds-color-palette-generator/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
"version": "0.1.0",
55
"private": true,
66
"type": "module",
7+
"bin": {
8+
"generate-colors": "./dist/cli/generate-colors.js"
9+
},
710
"scripts": {
811
"dev": "next dev --turbopack",
912
"build": "next build",
13+
"build:cli": "vite build",
1014
"start": "next start",
1115
"lint": "next lint",
1216
"test": "vitest",
@@ -41,6 +45,8 @@
4145
"tailwindcss": "^4.1.12",
4246
"tsx": "^4.20.5",
4347
"typescript": "^5.9.2",
48+
"vite": "^7.1.3",
49+
"vite-plugin-dts": "^3.9.1",
4450
"vitest": "^3.2.4"
4551
}
4652
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Generate Colors CLI
2+
3+
A CLI tool to generate color palettes for light and dark modes based on a configuration file.
4+
5+
## Installation
6+
7+
This tool is part of the `@equinor/eds-color-palette-generator` package.
8+
9+
## Usage
10+
11+
### Basic Usage
12+
13+
```bash
14+
generate-colors [configPath] [outputDir]
15+
```
16+
17+
**Arguments:**
18+
- `configPath` (optional): Path to the palette configuration JSON file. Defaults to `palette-config.json` in the current directory.
19+
- `outputDir` (optional): Output directory for the generated token files. Defaults to `output/` in the current directory.
20+
21+
**Example:**
22+
23+
```bash
24+
generate-colors examples/palette-config.json output/
25+
```
26+
27+
### Configuration File Format
28+
29+
The configuration file should be a JSON file with the following structure:
30+
31+
```json
32+
{
33+
"colors": [
34+
{ "name": "Moss Green", "value": "#007079" },
35+
{ "name": "Gray", "value": "#4A4A4A" },
36+
{ "name": "Blue", "value": "#0084C4" }
37+
],
38+
"colorFormat": "HEX",
39+
"outputFileLight": "Color Light.Mode 1.json",
40+
"outputFileDark": "Color Dark.Mode 1.json",
41+
"meanLight": 0.6,
42+
"stdDevLight": 2,
43+
"meanDark": 0.7,
44+
"stdDevDark": 2
45+
}
46+
```
47+
48+
**Required fields:**
49+
- `colors`: An array of color definitions, each with:
50+
- `name`: The name of the color
51+
- `value`: The hex color value
52+
53+
**Optional fields:**
54+
- `colorFormat`: Output color format - either `"HEX"` or `"OKLCH"` (default: `"HEX"`)
55+
- `outputFileLight`: Custom filename for light mode tokens (default: `"Color Light.Mode 1.json"`)
56+
- `outputFileDark`: Custom filename for dark mode tokens (default: `"Color Dark.Mode 1.json"`)
57+
- `meanLight`: Mean value for the Gaussian distribution in light mode (default: 0.6)
58+
- `stdDevLight`: Standard deviation for the Gaussian distribution in light mode (default: 2)
59+
- `meanDark`: Mean value for the Gaussian distribution in dark mode (default: 0.7)
60+
- `stdDevDark`: Standard deviation for the Gaussian distribution in dark mode (default: 2)
61+
62+
### Output
63+
64+
The tool generates two files with configurable names:
65+
- Default: `Color Light.Mode 1.json` - Color tokens for light mode
66+
- Default: `Color Dark.Mode 1.json` - Color tokens for dark mode
67+
68+
Both files follow the W3C Design Tokens Community Group format and include all the semantic color steps defined in the palette configuration.
69+
70+
#### Output Formats
71+
72+
**HEX Format (default):**
73+
```json
74+
{
75+
"Light": {
76+
"Moss Green": {
77+
"1": {
78+
"$type": "color",
79+
"$value": "#f5fefe",
80+
...
81+
}
82+
}
83+
}
84+
}
85+
```
86+
87+
**OKLCH Format:**
88+
```json
89+
{
90+
"Light": {
91+
"Moss Green": {
92+
"1": {
93+
"$type": "color",
94+
"$value": "oklch(0.970 0.015 204.6)",
95+
...
96+
}
97+
}
98+
}
99+
}
100+
```
101+
102+
## Example
103+
104+
See the `examples/` directory for a sample configuration file and generated output:
105+
- `examples/palette-config.json`: Sample configuration
106+
- `examples/color.tokens.light.json`: Generated light mode tokens
107+
- `examples/color.tokens.dark.json`: Generated dark mode tokens
108+
109+
## How It Works
110+
111+
The tool uses the existing `generateColorScale` function from the color utilities to create color scales for each color in the configuration. The lightness values and other configuration parameters are derived from the predefined `PALETTE_STEPS` configuration, ensuring consistency with the EDS color system.
112+
113+
The Gaussian distribution parameters (`mean` and `stdDev`) control the chroma (saturation) of colors at different lightness levels, creating natural-looking color scales that maintain the hue while varying lightness and saturation.

0 commit comments

Comments
 (0)