@pbroom/color-kitContrast

relativeLuminance

Returns the WCAG 2.1 relative luminance of a color, from 0 (black) to 1 (white).

functionimport { relativeLuminance } from '@pbroom/color-kit';metrics.ts:131
function relativeLuminance(
  color: Color,
  options?: ContrastOptions,
): number;

Measured from the unrounded sRGB channels, clipped to the display gamut (options.gamut, default sRGB). Pass { precision: '8bit' } to measure the 8-bit hex value instead. Allocation-free.

Parameters

colorColor
Color to measure.
options?ContrastOptions
Display gamut and channel precision.

Returns

numberRelative luminance in [0, 1].

Throws

  • When options.gamut is not 'srgb' or 'display-p3' (including the removed 'p3' spelling).

Example

TypeScript
import { parse, relativeLuminance } from '@pbroom/color-kit';

relativeLuminance(parse('#777')); // → ≈ 0.1845

See also