# color2k: A whole theme from one color

Pick a color and let color2k work out the rest. Each of these three builders takes a color and returns the same fourteen tokens, for light or dark.

You can try all three on any color at [color2k.com/themes](https://color2k.com/themes/).

Each one also looks at what you gave it and makes a few decisions. It returns those too, as `why`.

**Soft** is a pastel wash. It tames a loud color with `desaturate` and lifts a nearly gray one with `saturate`. Once it is calmed, your color becomes the button, unless the label would not read on it or the button would melt into the page. Then the button gets a pastel of the same hue.

```js
import {
  adjustHue,
  desaturate,
  hasBadContrast,
  hsla,
  mix,
  parseToHsla,
  parseToRgba,
  readableColor,
  saturate,
  toHex,
  transparentize,
} from 'color2k';

function softTheme(color, dark = false) {
  // A see-through pick is flattened onto the page first, the way it would look.
  const solid = toHex(mix(dark ? '#000' : '#fff', toHex(color).slice(0, 7), parseToRgba(color)[3]));
  const [, saturation] = parseToHsla(solid);
  const vivid = saturation > 0.8;
  const faint = saturation < 0.35;
  // Tame a loud color and lift a quiet one, so the wash is always gentle.
  const calm = vivid ? desaturate(solid, 0.2) : faint ? saturate(solid, 0.3) : solid;
  const [hue, chroma] = parseToHsla(calm);
  const tone = (lightness, scale = 1) => toHex(hsla(hue, chroma * scale, lightness, 1));
  const bg = dark ? tone(0.13, 0.4) : tone(0.97);
  const text = dark ? tone(0.93, 0.7) : tone(0.2, 0.6);
  const onAccent = dark ? tone(0.15, 0.5) : text;
  // The button is your color, unless its label would not read on it or the
  // button would melt into the page.
  const clash = hasBadContrast(onAccent, 'aa', calm) || hasBadContrast(calm, 'decorative', bg);
  const accent = clash ? tone(dark ? 0.78 : 0.74) : toHex(calm);

  return {
    bg,
    surface: dark ? tone(0.18, 0.4) : '#ffffff',
    border: dark ? tone(0.28, 0.4) : tone(0.9, 0.8),
    text,
    muted: dark ? tone(0.74, 0.5) : tone(0.3, 0.5),
    onSurface: text,
    accent,
    accentHover: toHex(mix(accent, readableColor(onAccent), 0.15)),
    onAccent,
    soft: toHex(mix(accent, bg, 0.72)),
    onSoft: text,
    ring: transparentize(accent, 0.45),
    extra: toHex(adjustHue(accent, 40)),
    shadow: transparentize(accent, 0.5),
    why: { vivid, faint, clash },
  };
}

const light = softTheme('#ff8fb8');
const dark = softTheme('#ff8fb8', true);

light.accent; // '#f49abb'
light.why; // { vivid: true, faint: false, clash: false }
```

Result:

- light bg: #fdf1f6
- light surface: #ffffff
- light border: #f6d5e1
- light text: #4b1b2c
- light muted: #6b2e44
- light onSurface: #4b1b2c
- light accent: #f49abb
- light accentHover: #f6a9c5
- light onAccent: #4b1b2c
- light soft: #fad9e5
- light onSoft: #4b1b2c
- light ring: rgba(244, 154, 187, 0.55)
- light extra: #f4b59a
- light shadow: rgba(244, 154, 187, 0.5)
- light decision vivid: true
- light decision faint: false
- light decision clash: false
- dark bg: #2c171e
- dark surface: #3d1f2a
- dark border: #5e3141
- dark text: #f7e3ea
- dark muted: #d7a2b6
- dark onSurface: #f7e3ea
- dark accent: #f49abb
- dark accentHover: #f6a9c5
- dark onAccent: #361722
- dark soft: #643c4a
- dark onSoft: #f7e3ea
- dark ring: rgba(244, 154, 187, 0.55)
- dark extra: #f4b59a
- dark shadow: rgba(244, 154, 187, 0.5)
- dark decision vivid: true
- dark decision faint: false
- dark decision clash: false

**Serious** drains the color out. The page is built from grays that keep 8% of the hue. Your color is the link and the button, so it has to read on the page. If `hasBadContrast` says it doesn't, the theme darkens it a step at a time until it does. Then `readableColorIsBlack` picks the button label.

```js
import {
  darken,
  desaturate,
  hasBadContrast,
  hsla,
  lighten,
  mix,
  parseToHsla,
  parseToRgba,
  readableColor,
  readableColorIsBlack,
  toHex,
  transparentize,
} from 'color2k';

function seriousTheme(color, dark = false) {
  // A see-through pick is flattened onto the page first, the way it would look.
  const solid = toHex(mix(dark ? '#000' : '#fff', toHex(color).slice(0, 7), parseToRgba(color)[3]));
  const [hue] = parseToHsla(solid);
  const gray = (lightness) => toHex(desaturate(hsla(hue, 1, lightness, 1), 0.92));
  const bg = dark ? gray(0.1) : gray(0.98);
  const text = dark ? gray(0.93) : gray(0.13);
  // Links are set in your color, so it has to read on the page.
  const weak = hasBadContrast(solid, 'aa', bg);
  let accent = solid;
  for (let i = 0; i < 50 && hasBadContrast(accent, 'aa', bg); i++) {
    accent = toHex(dark ? lighten(accent, 0.02) : darken(accent, 0.02));
  }
  const darkLabel = readableColorIsBlack(accent);
  const onAccent = darkLabel ? '#000' : '#fff';

  return {
    bg,
    surface: dark ? gray(0.14) : '#ffffff',
    border: dark ? gray(0.24) : gray(0.88),
    text,
    muted: dark ? gray(0.68) : gray(0.4),
    onSurface: text,
    accent,
    accentHover: toHex(mix(accent, readableColor(onAccent), 0.15)),
    onAccent,
    soft: toHex(mix(accent, bg, 0.86)),
    onSoft: text,
    ring: transparentize(accent, 0.6),
    extra: dark ? gray(0.3) : gray(0.8),
    shadow: transparentize(text, 0.92),
    why: { weak, darkLabel },
  };
}

const light = seriousTheme('#ff8fb8');
const dark = seriousTheme('#ff8fb8', true);

light.accent; // '#e00052'
light.why; // { weak: true, darkLabel: false }
```

Result:

- light bg: #faf9fa
- light surface: #ffffff
- light border: #e3dee0
- light text: #241e20
- light muted: #6e5e64
- light onSurface: #241e20
- light accent: #e00052
- light accentHover: #be0046
- light onAccent: #fff
- light soft: #f6d6e2
- light onSoft: #241e20
- light ring: rgba(224, 0, 82, 0.4)
- light extra: #d0c8cb
- light shadow: rgba(36, 30, 32, 0.08)
- light decision weak: true
- light decision darkLabel: false
- dark bg: #1c1719
- dark surface: #272123
- dark border: #42383c
- dark text: #efeced
- dark muted: #b4a7ac
- dark onSurface: #efeced
- dark accent: #ff8fb8
- dark accentHover: #ffa0c3
- dark onAccent: #000
- dark soft: #3c282f
- dark onSoft: #efeced
- dark ring: rgba(255, 143, 184, 0.4)
- dark extra: #53464b
- dark shadow: rgba(239, 236, 237, 0.08)
- dark decision weak: false
- dark decision darkLabel: true

**Bold** uses your color as the page, after turning a muted one up with `saturate`. A light page gets black text, black cards and hard black shadows. A dark page gets white text and cards tinted from the page with `lighten`. The button is the hue next door, 40 degrees along the wheel, unless it would disappear on the card. Then it falls back to black or white.

```js
import {
  adjustHue,
  hasBadContrast,
  lighten,
  mix,
  parseToHsla,
  parseToRgba,
  readableColor,
  readableColorIsBlack,
  saturate,
  toHex,
  transparentize,
} from 'color2k';

function boldTheme(color, dark = false) {
  // A see-through pick is flattened onto the page first, the way it would look.
  const solid = toHex(mix(dark ? '#000' : '#fff', toHex(color).slice(0, 7), parseToRgba(color)[3]));
  const [, saturation] = parseToHsla(solid);
  const dull = saturation < 0.5;
  // Bold needs volume, so a muted color is turned up first.
  const brand = toHex(dull ? saturate(solid, 0.35) : solid);
  const bg = dark ? toHex(mix(brand, 'black', 0.82)) : brand;
  const light = readableColorIsBlack(bg);
  const text = light ? '#000' : '#fff';
  // A light page gets black cards. A dark page gets cards tinted from it.
  const tinted = !light;
  const surface = tinted ? toHex(lighten(bg, 0.14)) : '#000';
  const neighbor = toHex(adjustHue(brand, 40));
  // The button is the hue next door, unless it would disappear on the card.
  const lost = hasBadContrast(neighbor, 'readable', surface);
  const accent = lost ? readableColor(surface) : neighbor;
  const onAccent = readableColor(accent);

  return {
    bg,
    surface,
    border: text,
    text,
    muted: text,
    onSurface: readableColor(surface),
    accent,
    accentHover: toHex(mix(accent, readableColor(onAccent), 0.15)),
    onAccent,
    soft: text,
    onSoft: bg,
    ring: transparentize(accent, 0.4),
    extra: toHex(adjustHue(brand, 340)),
    shadow: light ? '#000' : accent,
    why: { dull, light, tinted, lost },
  };
}

const light = boldTheme('#ff8fb8');
const dark = boldTheme('#ff8fb8', true);

light.accent; // '#ffb08f'
light.why; // { dull: false, light: true, tinted: false, lost: false }
```

Result:

- light bg: #ff8fb8
- light surface: #000
- light border: #000
- light text: #000
- light muted: #000
- light onSurface: #fff
- light accent: #ffb08f
- light accentHover: #ffbca0
- light onAccent: #000
- light soft: #000
- light onSoft: #ff8fb8
- light ring: rgba(255, 176, 143, 0.6)
- light extra: #ff8fdd
- light shadow: #000
- light decision dull: false
- light decision light: true
- light decision tinted: false
- light decision lost: false
- dark bg: #2e1a21
- dark surface: #5b3341
- dark border: #fff
- dark text: #fff
- dark muted: #fff
- dark onSurface: #fff
- dark accent: #ffb08f
- dark accentHover: #ffbca0
- dark onAccent: #000
- dark soft: #fff
- dark onSoft: #2e1a21
- dark ring: rgba(255, 176, 143, 0.6)
- dark extra: #ff8fdd
- dark shadow: #ffb08f
- dark decision dull: false
- dark decision light: false
- dark decision tinted: true
- dark decision lost: false

The text pairs in all three (text on the page, text on a card, the button label, the chip, and the Serious link) hold WCAG AA for any color you pass, see-through ones included. The build sweeps about 1,300 colors through each one to check.

A theme is a plain object of strings, so putting it on the page is a loop:

```js
const tokens = { bg: '#fdf1f6', text: '#471f2e', accent: '#f584ae' };

for (const [name, value] of Object.entries(tokens)) {
  globalThis.document?.documentElement.style.setProperty(`--${name}`, value);
}
```
