Calculates the WCAG contrast ratio between two colors.
The ratio runs from 1 (no contrast) to 21 (black on white), and the order of the colors
doesn't matter. WCAG AA asks for at least 4.5 for normal text and 3 for large text; AAA
asks for 7 and 4.5.
Parameters
foreground: string
A 3- or 6-digit hex color, such as the text color. The leading # is
optional.
background: string
A 3- or 6-digit hex color, such as the background color.
Returns number
The contrast ratio, from 1 to 21.
Throws
If either color isn't a 3- or 6-digit hex color.
Example
getContrastRatio('#000000', '#ffffff'); // 21 getContrastRatio('#777777', '#ffffff'); // 4.47..., just short of AA for normal text
Calculates the WCAG contrast ratio between two colors.
The ratio runs from 1 (no contrast) to 21 (black on white), and the order of the colors doesn't matter. WCAG AA asks for at least 4.5 for normal text and 3 for large text; AAA asks for 7 and 4.5.