Engineering10 min read

Building a Color Engine That Designers Can Actually Work With

The engineering behind @dr/color-engine, a client-side color generation engine built for Designers Room. A look at OKLCH, color harmony, shade generation, overrides, contrast utilities, and versioning.

Quick answer: @dr/color-engine is a client-side color generation engine built for Designers Room. It generates structured color scales, semantic colors, and harmony-based palettes using OKLCH, while supporting manual overrides, locked shades, color conversions, and contrast utilities.

I wanted to build a color system generator for Designers Room. Not another tool that takes a HEX value and returns a handful of lighter and darker colors. I wanted something that could generate a structured color system, let designers control individual values, and keep those decisions intact when the palette changes.

Designers Room is a product of Anvik Labs, and @dr/color-engine is the engine behind its color system generation. I built it as a separate package because I didn't want the color generation logic tied directly to the interface. The UI should be able to change without having to rewrite the underlying color logic.

That separation also gave me a clearer engineering problem to solve: define a configuration, generate a palette from it, and make the result predictable enough that the same configuration always produces the same output.

Rendering diagram…

Where the color engine fits inside Designers Room

1. Separating configuration from generation

One of the first decisions was to keep the engine client-side. The backend doesn't need to calculate or store generated palettes. It only needs to store the configuration that describes how a palette should be generated.

That configuration contains the base colors, harmony mode, shade overrides, semantic overrides, locked values, and theme-related settings. The engine takes this configuration and produces a GeneratedPalette.

I preferred this approach because palette generation is deterministic. There is no reason to make a network request every time someone changes a color or adjusts a shade. The interface can generate the result immediately, while the backend remains responsible for persisting the user's configuration.

Rendering diagram…

Configuration-driven palette generation

This also makes the engine easier to test. I can pass a configuration into the generator and inspect the output without needing a database, API server, or browser interface.

2. Defining the color system

I settled on four main color scales: primary, secondary, accent, and neutral. Each scale contains eleven shades, from 50 to 950.

The idea was to give the designer a complete scale rather than a collection of unrelated colors. A primary color can be used for the main brand identity, secondary and accent colors can add variation, and the neutral scale provides the foundation for surfaces, borders, and text.

ScalePurpose
PrimaryThe main brand color and its tonal range
SecondaryA supporting color generated through the selected harmony
AccentAn additional color for emphasis and variation
NeutralA tinted grayscale scale for surfaces and structure

Alongside these scales, I added four semantic colors: success, warning, error, and info. These are handled separately because their meaning should remain recognizable regardless of the selected brand color.

Rendering diagram…

The generated color system

3. Why I chose OKLCH

The color space was an important decision. I didn't want to generate shades by simply mixing a color with white or black. That approach can produce uneven results, especially when the goal is to create a scale that feels visually consistent.

I used OKLCH as the main working color space. It gives me separate controls for lightness, chroma, and hue. That makes it easier to generate a tonal scale while keeping the color's character relatively consistent.

For each shade, the engine uses a predefined lightness target. The scale moves from very light at 50 to very dark at 950. Chroma follows a sine-shaped curve so saturation doesn't remain constant across every shade. I also apply a small lightness bias based on the source color.

ShadeTarget lightness
500.97
1000.94
2000.88
3000.81
4000.72
5000.63
6000.54
7000.45
8000.36
9000.28
9500.18

I used culori for color conversion and formatting. Chroma is clamped during generation to avoid values that produce problematic colors when converted into the target output space. This is a practical constraint, although it isn't the same as calculating the maximum available chroma for every hue and lightness combination.

Rendering diagram…

How an individual color scale is generated

I also had to account for hue drift. Secondary and accent colors can shift slightly across their tonal scales. Without that, some colors can feel flat or lose their character as they move from light to dark.

4. Generating color harmony

A color system shouldn't require the designer to manually choose every supporting color. I added harmony modes to generate secondary and accent base colors from the primary color.

The available modes are analogous, complementary, triadic, split-complementary, and monochrome. For example, analogous generation uses nearby hues. In the current implementation, the secondary and accent hues are offset by +30 and -30 degrees. Complementary generation places the secondary hue 180 degrees away from the primary.

I also made sure the designer isn't forced to use generated harmony. Secondary and accent base colors can be supplied manually. The harmony mode provides a starting point, not a restriction.

Harmony modeApproach
AnalogousUses nearby hues around the primary
ComplementaryUses an opposing hue
TriadicUses three hues distributed around the color wheel
Split-complementaryUses two hues around the complementary position
MonochromeUses variations of the same hue

Rendering diagram…

Harmony generation flow

5. Building a neutral scale

Neutral colors are easy to overlook in a palette generator, but they have a significant impact on how a design system feels. A neutral scale derived from the primary color can give the interface a subtle warm or cool character without making every surface look tinted.

I derive the neutral scale from the primary color, reduce its chroma, and allow a warm, cool, or neutral tint option. This keeps the neutral scale connected to the palette while making it less saturated than the main brand colors.

The current implementation reduces chroma but doesn't enforce a minimum chroma floor. That means some neutral scales can become nearly grayscale, depending on the source color and settings.

6. Semantic colors need their own rules

Success, warning, error, and info colors have a different job from brand colors. Their meaning needs to remain familiar, so I didn't generate them by simply rotating the primary hue.

Instead, I use fixed hue anchors: success at 150 degrees, warning at 88 degrees, error at 28 degrees, and info at 255 degrees. Lightness and chroma are derived in relation to the primary color, while a deterministic seed rotation helps vary the generated result. Manual semantic overrides are supported too.

Semantic roleHue anchor
Success150°
Warning88°
Error28°
Info255°

The important distinction is that the semantic role determines the general color direction, while the primary palette influences the generated color characteristics. A designer can still replace any semantic base color manually.

7. Overrides and locked shades

This was one of the more important parts of the engine. Generating a palette is relatively straightforward. Keeping a designer's manual decisions intact while regenerating it is harder.

I separated overrides from locks. Overrides let the designer specify a value at different levels: an entire role base, an individual shade, or a semantic base color. Locks determine which existing values should be preserved during regeneration.

That distinction matters. A value can be manually overridden without necessarily being locked forever. A lock is specifically about protecting that value when the rest of the palette is regenerated.

Rendering diagram…

Regenerating a palette while preserving locked values

I implemented regenerateUnlocked() for this behavior. It generates the palette using the base configuration and the requested seed, then writes the generated values for unlocked entries into overrides. Locked entries retain their existing values.

This makes regeneration a controlled operation rather than a complete reset. The designer can experiment with a new base color without losing every adjustment made along the way.

8. Contrast utilities

Generating a visually consistent palette doesn't automatically make every color combination readable. I added contrast utilities so the engine can help evaluate foreground and background combinations.

The engine uses culori's WCAG contrast calculation. The current utilities include contrastRatio, contrastCheck, readableTextColor, and suggestFix. The default contrast check uses a 4.5:1 threshold.

suggestFix compares available shades from the color scales and the current semantic color against neutral surfaces to suggest alternatives. It is intended as a practical helper, not a complete accessibility solver.

I deliberately kept the first version focused. There is no APCA implementation, and palette generation doesn't automatically solve every shade for contrast. Those are separate problems that deserve their own design and testing.

Rendering diagram…

Contrast evaluation and suggestions

9. Color conversion and input handling

The engine also needs to work with the color formats designers already use. I added conversions between HEX, RGB, HSL, OKLCH, and HSV.

Internally, HEX values are normalized to lowercase six-digit values. The custom color picker works with HSV, while the color inspector uses OKLCH. This allows the interface to expose controls that make sense for each interaction without changing the engine's internal color representation.

One edge case I had to handle was grayscale. When saturation is zero, hue becomes undefined or loses practical meaning in HSV-based calculations. The conversion logic needs to account for that rather than assuming every color has a meaningful hue value.

I also added checks for invalid numeric input and conversion round-tripping. A color conversion should not silently produce broken values just because an input is outside the expected range.

10. Testing the engine

I didn't want to rely on a few visually pleasing palettes as proof that the engine worked. I focused on behavior that could be tested consistently.

  • The same configuration and seed should produce the same palette.
  • Changing the seed should produce a different generated result.
  • Locked values should survive regeneration.
  • Manual overrides should be respected.
  • Generated output should contain the documented keys.
  • Color conversions should support round-tripping.
  • Grayscale input should not break conversion logic.
  • Invalid numeric input should be handled safely.
  • Store behavior should cover undo, redo, normalization, locks, and base resets.

Determinism was particularly important. If two calls receive the same configuration and seed, they should produce the same output. Without that guarantee, debugging palette differences and reproducing user configurations becomes much harder.

11. Versioning the generation algorithm

There is one problem with a generator that becomes part of a product: its algorithm will probably change over time. A better generation algorithm might produce different shades from the same configuration. That can unexpectedly change palettes that users have already created.

I didn't want a future improvement to silently change existing user configurations. So I planned versioned generation rather than replacing the original algorithm in place.

The approach is to keep generatePalette() as the public entry point and dispatch internally based on the engine version. Existing configurations without an engine version continue to use v1. New configurations can use v2 through extensions.colorEngineVersion.

The v1 implementation stays frozen as generatePaletteV1(), with golden snapshots to help verify that its output doesn't change. This gives me room to improve the algorithm without breaking palettes created with the earlier version.

Rendering diagram…

Version-aware palette generation

The backend still only needs to store configuration data. It doesn't need to store generated shades or understand the internal generation algorithm. That responsibility stays inside the engine.

12. What is still missing

The first version is useful, but there are several areas I would improve before calling the engine complete.

  • Chroma is clamped rather than calculated relative to the maximum in-gamut chroma for each hue and lightness.
  • The primary base color is not guaranteed to match the 500 shade exactly.
  • Shade lightness values are fixed targets rather than values solved around contrast requirements.
  • There is no perceptual separation check between generated colors.
  • Semantic colors use fixed hue anchors rather than a more flexible semantic color model.
  • Neutral chroma has no minimum floor.
  • Dark mode is not yet treated as an independently generated palette.
  • APCA is not implemented.
  • Engine versioning needs to remain part of the configuration compatibility strategy.

I don't consider these small details that can all be fixed by adjusting a few numbers. Some of them require changes to how the generation algorithm works. That's why I want to keep the current version stable while exploring improvements separately.

13. What I want to improve in v2

For v2, I want to focus on improving the actual color generation rather than adding more controls to the interface. Better in-gamut chroma handling, more reliable perceptual spacing, and a stronger approach to light and dark palettes are areas I want to investigate.

The versioning structure gives me a way to do that without changing the output of existing configurations. Old configurations can continue using v1, while new configurations opt into v2. I can also compare the results through tests and visual inspection before making it the default.

Final thoughts

Building @dr/color-engine made me think about color generation as an engineering problem rather than just a visual one. The output has to look right, but it also needs to be deterministic, editable, testable, and compatible with configurations that users have already created.

The most important decision was keeping generation separate from persistence and the interface. That gave the engine a clear responsibility and made it easier to test each part independently.

There is still work to do, especially around perceptual accuracy and accessibility. But the current implementation gives Designers Room a foundation that can evolve without making every change a breaking change.

@dr/color-engine was built for Designers Room, a product of Anvik Labs. The goal isn't to generate the most colors. It's to give designers a color system they can understand, control, and keep working with.