wcag color contrast
This module produces WCAG(link is external) contrast ratio(link is external)
measurement and scoring. It's an accessibility standard, intended to make sure websites have high enough contrast to be readable by everyone, including people with low vision(link is external) and age-related vision problems. This module is a low-level utility, useful for its implementation of the underlying math: to test a page, you'll want to use something like Google Chrome’s Lighthouse tools(link is external), or the axe extension(link is external) for Chrome and Firefox.
I (personally, speaking as Tom) have doubts about the universality, accuracy, and application of this particular standard(link is external). High contrast is a good value, but I argue that the standard is over-strict and doesn't precisely match human perception.
API
luminance
Get the contrast ratio between two relative luminance values
Parameters
a
number(link is external) luminance valueb
number(link is external) luminance value
Examples
luminance(1, 1); // = 1
Returns number(link is external) contrast ratio
rgb
Get a score for the contrast between two colors as rgb triplets
Parameters
Examples
rgb([0, 0, 0], [255, 255, 255]); // = 21
Returns number(link is external) contrast ratio
hex
Get a score for the contrast between two colors as hex strings
Parameters
a
string(link is external) hex valueb
string(link is external) hex value
Examples
hex('#000', '#fff'); // = 21
Returns number(link is external) contrast ratio
score
Get a textual score from a numeric contrast value
Parameters
contrast
number(link is external)
Examples
score(10); // = 'AAA'
Returns string(link is external) score