relativeLuminance
Returns the WCAG 2.1 relative luminance of a color, from 0 (black) to 1 (white).
import { relativeLuminance } from '@pbroom/color-kit';metrics.ts:131function 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
- options?
ContrastOptions - Display gamut and channel precision.
Returns
numberRelative luminance in [0, 1].
Throws
- When
options.gamutis not'srgb'or'display-p3'(including the removed'p3'spelling).
Example
import { parse, relativeLuminance } from '@pbroom/color-kit';
relativeLuminance(parse('#777')); // → ≈ 0.1845