Colour
Perceptual colour maths: contrast, difference, conversion, and 31,900 colour names on a subpath.
The arithmetic behind haus's contrast checks and the measured values on the colour foundation page.
Install
npm install @hipuku/colourIt replaces haus-colour-utils and haus-colour-names, which are deprecated. It is not a drop-in:
the function names changed and the names moved to a subpath.
Usage
import { deltaE, wcagContrast, createNamedColourMatcher } from '@hipuku/colour'
import { colourNameEntries } from '@hipuku/colour/names'
deltaE('#4f84ba', '#4e83bd') // 0.66: below what a person can see
wcagContrast('#000', '#fff').ratio // 21
const nameColour = createNamedColourMatcher(colourNameEntries())
nameColour('#4f84ba')[0] // { name: 'Blue Vault', hex: '#4e83bd', distance: 0.7 }deltaE is CIEDE2000: a difference of about 1 is the smallest a person can see.
The names subpath
The 31,900 colour names are close to 1MB, so they are a separate import, @hipuku/colour/names, and
a bundler leaves them out unless you import it. It exports colourNames, colourNameEntries and
COLOUR_NAME_COUNT.
API
| Export | Signature |
|---|---|
toHex | (input: string) => string | null |
generateLightnessScale | (hex: string, options?: LightnessScaleOptions) => string[] |
wcagContrast | (foreground: string, background: string) => ContrastResult |
isLight | (hex: string) => boolean |
suggestTextColour | (backgroundHex: string, palette: string[]) => string |
clusterByPerceptualDistance | (hexes: string[], threshold?: number) => ColourCluster[] |
deltaE | (a: string, b: string) => number |
nearestNamedColour | (hex: string, topN?: number) => NamedColourMatch[] |
createNamedColourMatcher | (entries: NamedColourEntry[]) => (hex: string, topN?: number) => NamedColourMatch[] |
basicColourNames | NamedColourEntry[] |
oklch | (hex: string) => Oklch | null |
hueFamily | { (hex: string): HueFamilyName | null; (hex: string, families: readonly HueFamily[], neutralChroma?: number): string | null; } |
HUE_FAMILIES | readonly [{ readonly name: 'Red'; readonly min: 11; readonly max: 34; }, { readonly name: 'Orange'; readonly min: 34; readonly max: 72; }, { readonly name: 'Yellow'; readonly min: 72; readonly max: 111; }, { readonly name: 'Green'; readonly min: 111; readonly max: 170; }, { readonly name: 'Cyan'; readonly min: 170; readonly max: 208; }, { readonly name: 'Blue'; readonly min: 208; readonly max: 276; }, { readonly name: 'Purple'; readonly min: 276; readonly max: 338; }, { readonly name: 'Pink'; readonly min: 338; readonly max: 11; }] |
NEUTRAL_CHROMA | 0.03 |
analysePalette | (hexes: string[]) => PaletteAnalysis | null |
classifyDeltaE | (distance: number) => DeltaEBand |
NEAR_IDENTICAL_DELTA_E | 2.5 |
PALETTE_ACHROMATIC_CHROMA | 0.04 |
The full reference is the package README.