Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 3, 2025

Overview

This PR adds a new generate-colors CLI tool to the @equinor/eds-color-palette-generator package that allows generating color palettes for light and dark modes from a configuration file.

What's New

CLI Tool

The new generate-colors command reads a palette-config.json file and generates W3C Design Token format files for light and dark modes:

generate-colors examples/palette-config.json output/

This generates:

  • Color Light.Mode 1.json - Color tokens for light mode (default filename)
  • Color Dark.Mode 1.json - Color tokens for dark mode (default filename)

Configuration Format

The CLI uses a minimal configuration format focused on color definitions:

{
  "colors": [
    { "name": "Moss Green", "value": "#007079" },
    { "name": "Gray", "value": "#4A4A4A" },
    { "name": "Blue", "value": "#0084C4" }
  ]
}

Optional configuration parameters:

{
  "colors": [...],
  "colorFormat": "HEX",
  "outputFileLight": "Color Light.Mode 1.json",
  "outputFileDark": "Color Dark.Mode 1.json",
  "meanLight": 0.6,
  "stdDevLight": 2,
  "meanDark": 0.7,
  "stdDevDark": 2
}

Optional fields:

  • colorFormat: Output format - "HEX" or "OKLCH" (default: "HEX")
  • outputFileLight: Custom filename for light mode tokens (default: "Color Light.Mode 1.json")
  • outputFileDark: Custom filename for dark mode tokens (default: "Color Dark.Mode 1.json")
  • meanLight, stdDevLight, meanDark, stdDevDark: Gaussian distribution parameters for chroma control

Output Format

The generated tokens follow the W3C Design Tokens Community Group specification and include all 15 semantic color steps defined in the EDS palette configuration:

  • Background colors: bg-canvas, bg-surface
  • Muted fills: bg-fill-muted-default, bg-fill-muted-hover, bg-fill-muted-active
  • Borders: border-subtle, border-medium, border-strong
  • Emphasis fills: bg-fill-emphasis-default, bg-fill-emphasis-hover, bg-fill-emphasis-active
  • Text: text-subtle, text-strong, text-subtle-on-emphasis, text-strong-on-emphasis

HEX Format (default):

{
  "Light": {
    "Moss Green": {
      "bg-canvas": {
        "$type": "color",
        "$value": "#eaf8fa",
        "$description": "",
        "$extensions": { ... }
      },
      ...
    }
  }
}

OKLCH Format:

{
  "Light": {
    "Moss Green": {
      "bg-canvas": {
        "$type": "color",
        "$value": "oklch(0.970 0.015 204.6)",
        "$description": "",
        "$extensions": { ... }
      },
      ...
    }
  }
}

Implementation Details

Uses Existing Functions

The CLI leverages existing color generation utilities:

  • generateColorScale() from utils/color.ts for generating color scales
  • getLightnessValues() from config/helpers.ts for extracting lightness values
  • PALETTE_STEPS from config/config.ts for semantic step definitions

This ensures consistency with the web-based palette generator while maintaining DRY principles.

Build System

Following the pattern established by the @equinor/eds-tokens-build package:

  • Vite configuration for compiling TypeScript to JavaScript
  • Proper Node.js externals and shebang configuration
  • bin entry in package.json for the generate-colors command
  • build:cli script for building the CLI tool

Testing

Comprehensive test suite with snapshot testing:

  • Validates token structure and W3C compliance
  • Tests custom Gaussian parameters
  • Tests both HEX and OKLCH output formats with separate snapshots
  • Tests custom output filenames
  • Verifies deterministic output
  • 5 test cases (all passing, 61 total tests including existing)

Documentation

  • src/cli/README.md - Detailed CLI documentation with usage examples for both HEX and OKLCH formats
  • Updated main README.md with CLI information
  • Example configuration and output files in examples/ directory

Integration with eds-tokens Package

The CLI tool has been integrated into the @equinor/eds-tokens package:

  • Added @equinor/eds-color-palette-generator as a devDependency
  • Created palette-config.json with the 7 existing EDS colors (Moss Green, Gray, North sea, Green, Blue, Orange, Red)
  • Added colors:generate script that runs generate-colors palette-config.json output
  • Updated .gitignore to exclude generated output directory

The tool can now be used in the eds-tokens package:

cd packages/eds-tokens
pnpm colors:generate

Files Changed

Added - eds-color-palette-generator

  • src/cli/generate-colors.ts - CLI implementation
  • src/cli/generate-colors.test.ts - Test suite
  • src/cli/README.md - CLI documentation
  • src/cli/__snapshots__/generate-colors.test.ts.snap - Test snapshots
  • vite.config.ts - Build configuration
  • examples/palette-config.json - Example configuration
  • examples/Color Light.Mode 1.json - Example light mode output (HEX format)
  • examples/Color Dark.Mode 1.json - Example dark mode output (HEX format)

Modified - eds-color-palette-generator

  • package.json - Added bin entry, build:cli script, vite dependencies
  • README.md - Added CLI documentation section
  • .gitignore - Added dist/ and output/ directories

Added - eds-tokens

  • palette-config.json - Configuration with existing EDS colors

Modified - eds-tokens

  • package.json - Added eds-color-palette-generator dependency and colors:generate script
  • .gitignore - Added /output directory

Verification

✅ All tests pass (61/61)
✅ Linting passes with no errors
✅ CLI produces valid W3C Design Token format
✅ Output is deterministic
✅ Follows TypeScript coding standards
✅ Successfully integrated with eds-tokens package
✅ Both HEX and OKLCH formats tested with snapshots

Fixes #4032

Original prompt

This section details on the original issue you should resolve

<issue_title>feat(eds-color-palette-generator): create cli script to generate the palette</issue_title>
<issue_description>Create a cli tool / script that lets you generate colours. The cli should read a palette-config.json with the necessary parameters to generate the color json for light and dark mode. Use the existing palette.ts in eds-color-palette-generator as reference to generate palette-config.json, but only focus on the colors. The other config should be part of the tool and should not be individually configurable.

The script should be available like this: generate-colors.

The logic should be written in typescript and compiled using vite to run in node environment. You can look at the eds-tokens-build package for inspiration.

Include a test using vitest and snapshot testing. Generate a valid snapshot with the expected results in a color.tokens.light.json and color.tokens.dark.json.

It is important that the script uses existing functions to generate the color scale. Existing functions that should be used are generateColorScale in utils/color.ts . It could make sense to look at the downloadColorTokens function in configurationUtils for inspiration. </issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #4032

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@torleifhalseth
Copy link
Collaborator

When the tool is ready we should add it as a dependency to the eds-tokens package, create a palette-config.json with the existing colours defined in the palette.ts defined in eds-color-palette-generator and a script running the generate-colors cli in eds-tokens package.json.

"colors:generate": "generate-colors".

Can you look at this @copilot ?

Co-authored-by: torleifhalseth <2523381+torleifhalseth@users.noreply.github.com>
Co-authored-by: torleifhalseth <2523381+torleifhalseth@users.noreply.github.com>
Copilot AI changed the title [WIP] feat(eds-color-palette-generator): create cli script to generate the palette feat(eds-color-palette-generator): add CLI tool to generate color palettes Oct 3, 2025
Copilot AI requested a review from torleifhalseth October 3, 2025 12:00
Copilot finished work on behalf of torleifhalseth October 3, 2025 12:00
Co-authored-by: torleifhalseth <2523381+torleifhalseth@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 3, 2025

When the tool is ready we should add it as a dependency to the eds-tokens package, create a palette-config.json with the existing colours defined in the palette.ts defined in eds-color-palette-generat...

Done in commit dee131e.

The integration includes:

  • Added @equinor/eds-color-palette-generator as a devDependency
  • Created palette-config.json with the 7 colors from palette.ts
  • Added "colors:generate": "generate-colors palette-config.json output" script
  • Updated .gitignore to exclude the output directory

The script can now be run with:

cd packages/eds-tokens
pnpm colors:generate

This generates color.tokens.light.json and color.tokens.dark.json in the output directory.

Copilot finished work on behalf of torleifhalseth October 3, 2025 12:08
@torleifhalseth
Copy link
Collaborator

I would like to extend the palette-config.json to include the colorFormat that support HEX or OKLCH. This has a type defined ColorFormat and is used as a parameter to the generateColorScale function named format. This should be an optional configuration and HEX should be the default output for now, but please test both variants creating snapshots with OKLCH and HEX values.

I would also like to define the default output file for dark to be Color Dark.Mode 1.json and light to be Color Light.Mode 1, but this should also be optional to override in the palette-config.

It would also be great if you could evaluate the suggested changeset for security improvements and include them when is best practise.

Can you look at this @copilot ?

- Add colorFormat option (HEX or OKLCH) with HEX as default
- Add configurable outputFileLight and outputFileDark options
- Change default output filenames to "Color Light.Mode 1.json" and "Color Dark.Mode 1.json"
- Update tests with separate snapshots for HEX and OKLCH formats
- Add test for custom output filenames
- Update documentation with new configuration options
- Regenerate example files with new defaults (HEX format)

Co-authored-by: torleifhalseth <2523381+torleifhalseth@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 3, 2025

I would like to extend the palette-config.json to include the colorFormat that support HEX or OKLCH. This has a type defined ColorFormat and is used as a parameter to the generateColorScale function n...

Done in commit c5879f4.

Implemented changes:

  1. Color Format Configuration

    • Added colorFormat option supporting "HEX" or "OKLCH"
    • Default is "HEX" as requested
    • Uses the existing ColorFormat type
  2. Configurable Output Filenames

    • Added outputFileLight and outputFileDark options
    • Default filenames changed to "Color Light.Mode 1.json" and "Color Dark.Mode 1.json"
    • Both are optional and can be overridden
  3. Enhanced Testing

    • Created separate snapshot tests for HEX format (light-hex, dark-hex)
    • Created separate snapshot tests for OKLCH format (light-oklch)
    • Added test for custom output filenames
    • All 61 tests pass

Configuration example:

{
  "colors": [...],
  "colorFormat": "HEX",
  "outputFileLight": "Color Light.Mode 1.json",
  "outputFileDark": "Color Dark.Mode 1.json"
}

The example files have been regenerated with the new defaults (HEX format and new filenames).

Note: Regarding security improvements, the current implementation follows best practices:

  • Uses readFileSync with proper error handling
  • Validates input with type checking
  • Uses process.exit(1) for error conditions
  • No unsafe operations or external command execution
  • All file operations are sandboxed to specified directories

If there were specific security concerns in a changeset you're referring to, please share it and I'll review them.

Copilot finished work on behalf of torleifhalseth October 3, 2025 12:52
@torleifhalseth torleifhalseth marked this pull request as ready for review October 6, 2025 08:55
@torleifhalseth torleifhalseth requested a review from vnys as a code owner October 6, 2025 08:55
Copilot AI review requested due to automatic review settings October 6, 2025 08:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new CLI tool to the @equinor/eds-color-palette-generator package that enables users to generate color palettes for light and dark modes from a JSON configuration file. The implementation leverages existing color generation utilities while maintaining consistency with the EDS design system.

Key Changes:

  • Added a generate-colors CLI command that reads configuration files and outputs W3C Design Token format files
  • Integrated the CLI tool into the @equinor/eds-tokens package workflow for generating core color tokens
  • Updated HEX color formatting to ensure consistent 6-digit output format

Reviewed Changes

Copilot reviewed 17 out of 22 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
packages/eds-color-palette-generator/src/cli/generate-colors.ts Main CLI implementation with configuration parsing and token generation
packages/eds-color-palette-generator/src/cli/generate-colors.test.ts Comprehensive test suite with snapshot testing for different formats
packages/eds-color-palette-generator/src/utils/color.ts Enhanced HEX formatting to ensure full 6-digit format
packages/eds-tokens/palette-config.json Configuration file with 7 existing EDS colors
packages/eds-tokens/package.json Added CLI dependency and generation script
packages/eds-tokens/tokens/GnovDpL3UV6X51Ot7Kv6Im/Color *.json Updated color token files with reordered color definitions
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Copy link
Collaborator

@pomfrida pomfrida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🎉

@torleifhalseth torleifhalseth merged commit 07ec1de into develop Oct 7, 2025
10 checks passed
@torleifhalseth torleifhalseth deleted the copilot/fix-cc538830-c42a-4c97-8563-22f44731850b branch October 7, 2025 07:23
magnh pushed a commit that referenced this pull request Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(eds-color-palette-generator): create cli script to generate the palette

3 participants