Calculates the relative luminance of a color according to WCAG 2.0 guidelines.
Relative luminance is the relative brightness of any point in a colorspace,
normalized to 0 for darkest black and 1 for lightest white. This value is
commonly used for calculating color contrast ratios for accessibility.
Parameters
hexCode: string
A 3 or 6 character hexadecimal color code. The leading # is optional.
Returns number
The relative luminance value as a number between 0 (black) and 1 (white).
Throws
If hexCode isn't a 3- or 6-digit hex color.
Example
// Pure red calculateRelativeLuminance('#ff0000'); // => 0.2126
Example
// Pure black calculateRelativeLuminance('#000000'); // => 0
Example
// Pure white calculateRelativeLuminance('#ffffff'); // => 1
Example
// Using 3-digit shorthand calculateRelativeLuminance('000'); // => 0
Calculates the relative luminance of a color according to WCAG 2.0 guidelines.
Relative luminance is the relative brightness of any point in a colorspace, normalized to 0 for darkest black and 1 for lightest white. This value is commonly used for calculating color contrast ratios for accessibility.