luminance
luminance measures how much light an sRGB color contributes. It uses the same channel weights as WCAG 2 contrast, so you can rank colors with the values a contrast calculation uses.
Example
import { colore } from 'colore-js';
import { luminance } from 'colore-js/fn';
colore('#fff').luminance(); // 1
luminance('#000'); // 0
Syntax
colore(color).luminance(): number
luminance(color: ColorInput): number
Parameters
Returns
Both APIs return a number. In-gamut sRGB ranges from 0 for black to 1 for white. The result uses color channels only. For a translucent value, resolve its displayed color over the actual background before measuring.
Throws
A ColorError when a color cannot be parsed.
Compare the luminance of RGB primaries
import { luminance } from 'colore-js/fn';
['#f00', '#0f0', '#00f'].map((color) => luminance(color));
// [0.2126, 0.7152, 0.0722]
The green primary contributes more luminance than red or blue. A pair’s accessibility depends on the difference in luminance between foreground and background, so use contrastRatio for that calculation.
Related
- contrastRatio to measure a pair
- Check color contrast in JavaScript to apply WCAG thresholds
- Color spaces and ranges for linear sRGB