Skip to content

relativeLuminance

Computes the WCAG 2.x relative luminance of a color: the perceived brightness from 0 (black) to 1 (white), after linearising the sRGB channels. The alpha channel is ignored: flatten a translucent color over its background first (see blendColors).

Available since v3.1.10

import { relativeLuminance } from '@helpers4/color';
// or, from the all-in-one package (same code, one install):
import { relativeLuminance } from 'helpers4/color';
relativeLuminance(color: RgbColor): number
ParameterTypeDescription
colorRgbColorAn sRGB color, channels 0-255

number — The relative luminance, between 0 and 1

Black is 0, white is 1, and green weighs the most of the three primaries.

relativeLuminance({ r: 0, g: 0, b: 0 }) // => 0
relativeLuminance({ r: 0, g: 255, b: 0 }) // => 0.7152

A background brighter than about 0.18 takes dark text better than white text.

relativeLuminance({ r: 255, g: 220, b: 0 }) > 0.18 // => true

An RGB(A) color with channels expressed as plain numbers, convenient for further conversion or manipulation.

interface RgbColor {
  a?: number;
  b: number;
  g: number;
  r: number;
}

View source on GitHub