Skip to content

Colours

react-smart-copy/color is for design-system docs, colour pickers, theme editors and design tools. It reads and writes every CSS Color 4 notation, without dependencies. About 6.5 kB in total; parseColor with formatColor is about 5.3 kB.

Copy a colour in the format your code uses. Rich-text apps also get a swatch. The last one is a Display P3 colour, outside what most screens show.

Pick a colour to copy it.

The copied value appears here.

Paste any colour: rgb(37 99 235), hsl(221 83% 53%) or color: royalblue;. It goes in as hex.

Nothing pasted yet.

Contrast

The quick brown fox jumps over the lazy dog.

The colour, in the code's format

colorPayload copies a colour as hex, rgb(), hsl(), oklch() or Display P3, with a swatch for Docs and Slack. Wide-gamut colours are mapped the way CSS specifies, keeping their hue.

pasteAsColor takes any CSS colour, even a declaration copied from developer tools, and inserts it in the field's format. contrastRatio is the WCAG 2 ratio, never rounded up.

import { colorPayload, contrastRatio, pasteAsColor } from 'react-smart-copy/color';

copy(() => colorPayload(token.value, { format: 'oklch' }));

<input onPaste={(event) => pasteAsColor(event, { format: 'hex' })} />

contrastRatio('#767676', '#fff'); // 4.54
import { useCopy } from "react-smart-copy";
import { colorPayload, pasteAsColor } from "react-smart-copy/color";
const { copy } = useCopy();
<button onClick={() => copy(() => colorPayload(token.value, { format: "oklch" }))}>Copy</button>
// Text: "oklch(62.31% 0.18801 259.81)". Docs, Notion, Slack and mail also get a swatch.
<input value={hex} onChange={…} onPaste={(event) => pasteAsColor(event, { format: "hex" })} />
// Pasting "rgb(59 130 246)" or "background: #3b82f6;" inserts "#3b82f6".

Every format, both ways. parseColor reads hex (3, 4, 6 and 8 digits), the 148 named colours and transparent, rgb() and hsl() (both syntaxes), hwb(), lab(), lch(), oklab(), oklch(), and color() in srgb, srgb-linear, display-p3, xyz, xyz-d65 and xyz-d50, with every angle unit, percentages and none. It returns null for anything else and never throws. formatColor(color, format) writes hex, rgb, hsl, hwb, oklch, oklab, lab, lch or p3. Its default precision brings every 8-bit colour back exactly; this was checked on 836,056 colours.

Wide gamut done right. A Display P3 or OKLCH colour outside sRGB stays exact until a format needs it in sRGB. Then it is gamut-mapped as CSS Color 4 specifies: lightness and hue are kept and chroma is reduced, so a vivid P3 red becomes the closest sRGB red, not an orange. gamut: "clip" clips each channel instead, as most browsers draw. inGamut and toGamut are exported.

Pasting. readColor(text) reads the one colour in pasted text. That includes a declaration copied from a stylesheet or developer tools: color: #3b82f6;, --brand: oklch(…) !important;. bareHex: true also reads 3b82f6; it is off by default, because words like “decade” are hex. pasteAsColor is a paste handler for colour fields that keeps the browser’s undo.

Contrast. contrastRatio(text, background) is the WCAG 2 ratio. Translucent colours are drawn the way a browser draws them, and the ratio is never rounded, so 4.499 fails. contrastLevel(ratio, { large }) gives AAA, AA or fail.

import { contrastLevel, contrastRatio, formatColor, parseColor } from "react-smart-copy/color";
formatColor("color(display-p3 1 0 0)", "hex"); // "#ff0b0c": mapped, not clipped
formatColor("#3b82f6", "hsl", { legacy: true }); // "hsl(217.22, 91.22%, 59.8%)"
contrastLevel(contrastRatio("#767676", "#fff")); // "AA" (4.54)
parseColor("hsl(1turn 100% 50% / 50%)"); // { r: 1, g: 0, b: 0, alpha: 0.5 }
Option (formatColor, colorPayload) Default Notes
format "hex" colorPayload only
gamut "map" "clip"
legacy false comma syntax for rgb and hsl
uppercase false hex digits
precision per format decimal places
swatch true colorPayload only

Accuracy is property-tested against culori for every notation and format. In Chromium, Firefox and WebKit, colours are also checked against the browser’s own CSS parser.