Compute contrast ratios between pairs of colours, using the WCAG 2.1 formula.
Value
contrast_ratio() returns a numeric vector of contrast ratios, of
the same length as colour1 and colour2.
Details
Contrast ratio is defined as \(\frac{L_1 + 0.05}{L_2 + 0.05}\) where \(L_1\) and \(L_2\) are the relative luminances of the lighter and darker colours being compared.
Contrast ratios range from 1 (no colour contrast) to 21 (maximum contrast). To meet WCAG 2.1 minimum (AA) criteria for accessibility, the contrast ratios between adjacent colours must be:
For text, at least 4.5
For large text (18pt or larger, or 14pt bold), at least 3
For non-text plot elements (e.g. patterns, lines, points, borders etc.), at least 3
To meet WCAG 2.1 enhanced (AAA) contrast requirements, generally a ratio above 7 is required.
See WCAG 2.1 success criteria 1.4.3 Contrast (Minimum), 1.4.6 Contrast (Enhanced) and 1.4.11 Non-text Contrast.