check_colors: Checking colours for colour blindness and legibility

check_colorsR Documentation

Checking colours for colour blindness and legibility

Description

These functions score a set of colours rather than the plot that uses them, so that one palette can be compared with another.

check_separation() reports how far apart the colours are, taking the worst case over normal vision and each type of colour blindness, so that a palette is only credited for a difference that every viewer can see.

check_contrast() reports whether text can be read on a ground.

Usage

check_separation(colors, background = NULL)

check_contrast(colors, background = NULL)

Arguments

colors

One or more colours, given as hexcodes or as names R knows.

background

Optionally, a colour to include in the comparison, so that a colour too pale or too dark to be seen against it is not counted as distinct. By default the current theme's background is used.

Details

The two functions answer different questions, and a palette needs both answered. check_separation() asks whether two marks can be told apart, and check_contrast() asks whether text can be read on what it sits on. simulate_colorblind() asks a third question, which is whether either survives a photocopier.

Distances are Euclidean distances in CIELAB space, the same measure match_color() uses. As a rule of thumb, a distance below 10 means two colours are easily confused, 10 to 25 means they are separable but close, and above 25 means they are comfortably distinct. Ratios are those of WCAG 2.1, which asks for at least 4.5 for body text and at least 3 for large text and for graphical objects.

Colour blindness affects about 8% of men and 0.5% of women, and the worst case is taken over deuteranopia, protanopia and tritanopia as well as normal vision.

Value

check_separation() returns a square matrix of worst-case distances, with the colours as its dimnames and a missing diagonal, so that min(x, na.rm = TRUE) gives the closest pair. A "grey" attribute holds the same matrix as seen in greyscale.

check_contrast() returns a square matrix of WCAG contrast ratios, shaped the same way.

References

World Wide Web Consortium. 2018. Web Content Accessibility Guidelines (WCAG) 2.1. https://www.w3.org/TR/WCAG21/

See Also

check_layout(), which scores a drawing rather than a palette.

Other themes: list_fonts(), theme_colorblind, theme_medium, theme_set

Examples

# How well does the current theme's palette separate five categories?
check_separation(ag_qualitative(5))
# The closest pair in it
min(check_separation(ag_qualitative(5)), na.rm = TRUE)
# And the closest pair once it is printed in greyscale
min(attr(check_separation(ag_qualitative(5)), "grey"), na.rm = TRUE)
# A red and a green that only look different to some viewers
check_separation(c("#B7352D", "#627313"))[1, 2]
# Can the current theme's ink be read on its ground?
check_contrast(ag_ink())[1, 2]

autograph documentation built on Nov. 16, 2026, 5:08 p.m.