@cssints/gamut

Gamut and contrast colour maths for cssints, computed at build time. fit returns the nearest colour a gamut can show, readable a text colour that reaches a WCAG 2 contrast ratio on a background, and tones a lightness scale of one colour as DTCG data for createTokens. The package has no runtime, no DOM and no scope() member: like @cssints/ramp, its values are computed when the module is evaluated and are data after that, so they go under any token group and get @property rules, themes and check.contrast from cssints.

The idea is gamutant's: a gamut and contrast library that needs no JavaScript. This is the same set of decisions, made once for a design's colours instead of per element in CSS, with the colour maths of culori.

// tokens.ts
import { bg, check, cn, color, createTokens } from "cssints" with { type: "cssints" };
import { fit, readable, tones } from "@cssints/gamut";

export const t = createTokens({
  bg: { $type: "color", $value: "#ffffff" },
  ink: { $type: "color", $value: readable("#ffffff") }, // #767676, 4.54:1
  blue: tones("#2563eb", 9),
  vivid: { $type: "color", $value: fit("oklch(0.7 0.4 150)") }, // #00be58: inside sRGB
});

check.contrast(t.ink, t.bg, { min: 4.5 });
export const text = cn(color(t.ink), bg(t.bg));

The sheet of that file:

@layer _.k, _.a, _.b, _.c, _.d, _.e;
@property --bg {
  syntax: "<color>";
  inherits: true;
  initial-value: #ffffff;
}
@property --blue-1 {
  syntax: "<color>";
  inherits: true;
  initial-value: #e7effd;
}
/* … --blue-2 to --blue-8 … */
@property --blue-9 {
  syntax: "<color>";
  inherits: true;
  initial-value: #000211;
}
@property --ink {
  syntax: "<color>";
  inherits: true;
  initial-value: #767676;
}
@property --vivid {
  syntax: "<color>";
  inherits: true;
  initial-value: #00be58;
}
@layer _.a {
  ._od79qs {
    color: var(--ink);
  }
  ._q77new {
    background-color: var(--bg);
  }
}

The maths

Every colour is read and written in OKLCH, a perceptual space: lightness l from 0 to 1, chroma c from 0 (grey) up, hue h in degrees. Two operations on it do the work:

sRGB results are hex, the 8-bit rounding of an in-gamut colour. A Display P3 result is color(display-p3 r g b): a hex cannot hold a colour sRGB cannot show.

fit(color, options?)

The nearest colour the target gamut can show, in the target's syntax.

fit("oklch(0.7 0.4 150)"); // #00be58: sRGB by default
fit("oklch(0.7 0.4 150)", { target: "display-p3" }); // color(display-p3 0 0.7683 0.2674)
fit("oklch(0.7 0.4 150)", { method: "clip" }); // #00d600: the channels clipped, not the chroma
fit("#2563eb"); // #2563eb: already in sRGB, unchanged
fit("color(display-p3 1 0 0)"); // #ff3428: the nearest sRGB red

readable(bg, options?)

A text colour with at least ratio WCAG 2 contrast on bg (4.5 by default, body text): the background's hue and its chroma relative to the sRGB maximum, with the lightness nearest the background that passes, searched by bisection on the hex it outputs (32 steps). A colour with no hue (white, grey) gives a neutral text colour.

Background toward Result Ratio
#ffffff black #767676 4.54
#111111 white #7c7c7c 4.52
#2563eb white #e8f0fd 4.51
#f5f5f4 black #545353 7.03
#808080 black #242424 3.93
#808080 white #fefefe 3.92

tones(color, count)

count tones of one colour as a DTCG colour group for createTokens: OKLCH lightness from 0.95 down to 0.10, evenly, lightest first, steps "1" to "N". The hue is held and the chroma keeps the colour's share of the sRGB maximum at each lightness, so every tone is in sRGB by construction.

tones("#2563eb", 9);
1 2 3 4 5 6 7 8 9
#e7effd #b5ccf8 #83a9f3 #5084ef #1a5ae8 #1041ac #072973 #02123f #000211

tones("#eab308", 5): #ffedc6, #d4a206, #866502, #3f2e00, #050300. tones(c, 1) is the 0.95 tone alone. The steps are read as t.blue._1 … t.blue._9 in code (_, as cssints reads a key that starts with a digit), and as --blue-1 … --blue-9 in CSS.

Browser support

The package runs at build time, in Node (22 and later): no browser, no DOM, nothing shipped to the client. What it writes is plain colour values:

Limits

cd packages/gamut && bun run test        # the maths, then a token file through the engine (@property rules, check.contrast)
cd packages/gamut && bun run typecheck   # the type test (test/types/gamut.ts)

Credits

The idea, a gamut and contrast library that needs no JavaScript, is gamutant's (MIT). The chroma relative to the sRGB gamut is nutelch's idea (MIT, David Aerne), as in @cssints/ramp. The colour maths is culori (MIT, Dan Burzo), a dependency. No code is copied.