| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/contrast_ratio.R |
| 3 |
\name{contrast_ratio} |
| 4 |
\alias{contrast_ratio} |
| 5 |
\title{W3C Contrast Ratio} |
| 6 |
\usage{ |
| 7 |
contrast_ratio( |
| 8 |
col, |
| 9 |
col2 = "white", |
| 10 |
algorithm = c("WCAG", "APCA"), |
| 11 |
plot = FALSE, |
| 12 |
border = FALSE, |
| 13 |
cex = 2, |
| 14 |
off = 0.05, |
| 15 |
mar = rep(0.5, 4), |
| 16 |
digits = 2L, |
| 17 |
... |
| 18 |
) |
| 19 |
} |
| 20 |
\arguments{ |
| 21 |
\item{col, col2}{vectors of any of the three kind of R colors, i.e., either a color |
| 22 |
name (an element of \code{\link[grDevices]{colors}}), a hexadecimal string |
| 23 |
of the form \code{"#rrggbb"} or \code{"#rrggbbaa"} (see |
| 24 |
\code{\link[grDevices]{rgb}}), or an integer \code{i} meaning |
| 25 |
\code{palette()[i]}. Both can be vectors and are recycled as necessary.} |
| 26 |
|
| 27 |
\item{algorithm}{character specifying whether the established standard \code{"WCAG"} 2.1 algorithm |
| 28 |
should be used or the improved \code{"APCA"} 0.98G-4g algorithm, still under development.} |
| 29 |
|
| 30 |
\item{plot}{logical indicating whether the contrast ratios should also be |
| 31 |
visualized by simple color swatches. Can also be a vector of length 2, indicating |
| 32 |
whether the foreground color should be visualized on the background color and/or |
| 33 |
the background color on the foreground color.} |
| 34 |
|
| 35 |
\item{border}{logical or color specification for the borders around the color swatches (only |
| 36 |
used if \code{plot = TRUE}). The default is \code{FALSE} which is equivalent to |
| 37 |
\code{"transparent"}. If \code{TRUE} the border is drawn in the same color as the text in the |
| 38 |
rectangle.} |
| 39 |
|
| 40 |
\item{cex}{numeric. Size of the text in the color color swatches (only if \code{plot = TRUE}).} |
| 41 |
|
| 42 |
\item{off}{numeric. Vertical offset between the different color swatches (only if \code{plot = TRUE}). |
| 43 |
Can also be of length 2 giving both vertical and horizontal offsets, respectively.} |
| 44 |
|
| 45 |
\item{mar}{numeric. Size of the margins around the color swatches (only if \code{plot = TRUE}).} |
| 46 |
|
| 47 |
\item{digits}{numeric. Number of digits for the contrast ratios displayed in the color swatches |
| 48 |
(only if \code{plot = TRUE})} |
| 49 |
|
| 50 |
\item{\dots}{further arguments passed to the plot of the color swatches (only if \code{plot = TRUE}).} |
| 51 |
} |
| 52 |
\value{ |
| 53 |
A numeric vector with the contrast ratios is returned (invisibly, if \code{plot} is \code{TRUE}). |
| 54 |
} |
| 55 |
\description{ |
| 56 |
Compute (and visualize) the contrast ratio of pairs of colors, as defined by the World Wide |
| 57 |
Web Consortium (W3C). |
| 58 |
} |
| 59 |
\details{ |
| 60 |
The W3C Content Accessibility Guidelines (WCAG) recommend a contrast |
| 61 |
ratio of at least 4.5 for the color of regular text on the background color, and |
| 62 |
a ratio of at least 3 for large text. See \url{https://www.w3.org/TR/WCAG22/#contrast-minimum}. |
| 63 |
|
| 64 |
The contrast ratio is defined in \url{https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio} |
| 65 |
as \code{(L1 + 0.05) / (L2 + 0.05)} where \code{L1} and \code{L2} are the relative luminances |
| 66 |
(see \url{https://www.w3.org/TR/WCAG22/#dfn-relative-luminance}) of the lighter and darker |
| 67 |
colors, respectively. The relative luminances are weighted sums of scaled sRGB coordinates: |
| 68 |
\code{0.2126 * R + 0.7152 * G + 0.0722 * B} where each of \code{R}, \code{G}, and \code{B} |
| 69 |
is defined as \code{ifelse(RGB <= 0.03928, RGB/12.92, ((RGB + 0.055)/1.055)^2.4)} based on |
| 70 |
the \code{RGB} coordinates between 0 and 1. |
| 71 |
|
| 72 |
For use in the next major revision of the WCAG a new advanced perceptual contrast algorithm |
| 73 |
(APCA) has been proposed by Somers (2022), see also Muth (2022) for more background and details. |
| 74 |
APCA is still under development, here version 0.98G-4g is implemented. Unlike the standard WCAG |
| 75 |
algorithm, APCA takes into account which color is the text and which is the background. Hence |
| 76 |
for the APCA algorithm a matrix with normal and reverse polarity is returned. An absolute |
| 77 |
value of 45 is "sort of" like a WCAG ratio of 3, 60 is "sort of" like 4.5. |
| 78 |
} |
| 79 |
\examples{ |
| 80 |
# check contrast ratio of default palette on white background |
| 81 |
contrast_ratio(palette(), "white") |
| 82 |
|
| 83 |
# visualize contrast ratio of default palette on white and black background |
| 84 |
contrast_ratio(palette(), "white", plot = TRUE) |
| 85 |
contrast_ratio(palette()[-1], "black", plot = TRUE) |
| 86 |
|
| 87 |
# APCA algorithm |
| 88 |
contrast_ratio(palette(), "white", algorithm = "APCA") |
| 89 |
contrast_ratio(palette(), "white", algorithm = "APCA", plot = TRUE, digits = 0) |
| 90 |
} |
| 91 |
\references{ |
| 92 |
W3C (2024). \dQuote{Web Content Accessibility Guidelines (WCAG) 2.2.} |
| 93 |
\url{https://www.w3.org/TR/WCAG22/} |
| 94 |
|
| 95 |
Somers A (2022). \dQuote{Advanced Perceptual Contrast Algorithm.} |
| 96 |
\url{https://github.com/Myndex/SAPC-APCA} |
| 97 |
|
| 98 |
Muth LC (2022). \dQuote{It's Time for a More Sophisticated Color Contrast Check for Data Visualizations.} |
| 99 |
Datawrapper Blog. \url{https://www.datawrapper.de/blog/color-contrast-check-data-vis-wcag-apca/} |
| 100 |
} |
| 101 |
\seealso{ |
| 102 |
\code{\link[colorspace]{desaturate}} |
| 103 |
} |
| 104 |
\keyword{color} |