| 1 |
#' W3C Contrast Ratio |
| 2 |
#' |
| 3 |
#' Compute (and visualize) the contrast ratio of pairs of colors, as defined by the World Wide |
| 4 |
#' Web Consortium (W3C). |
| 5 |
#' |
| 6 |
#' The W3C Content Accessibility Guidelines (WCAG) recommend a contrast |
| 7 |
#' ratio of at least 4.5 for the color of regular text on the background color, and |
| 8 |
#' a ratio of at least 3 for large text. See \url{https://www.w3.org/TR/WCAG22/#contrast-minimum}. |
| 9 |
#' |
| 10 |
#' The contrast ratio is defined in \url{https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio} |
| 11 |
#' as \code{(L1 + 0.05) / (L2 + 0.05)} where \code{L1} and \code{L2} are the relative luminances |
| 12 |
#' (see \url{https://www.w3.org/TR/WCAG22/#dfn-relative-luminance}) of the lighter and darker |
| 13 |
#' colors, respectively. The relative luminances are weighted sums of scaled sRGB coordinates: |
| 14 |
#' \code{0.2126 * R + 0.7152 * G + 0.0722 * B} where each of \code{R}, \code{G}, and \code{B} |
| 15 |
#' is defined as \code{ifelse(RGB <= 0.03928, RGB/12.92, ((RGB + 0.055)/1.055)^2.4)} based on |
| 16 |
#' the \code{RGB} coordinates between 0 and 1. |
| 17 |
#' |
| 18 |
#' For use in the next major revision of the WCAG a new advanced perceptual contrast algorithm |
| 19 |
#' (APCA) has been proposed by Somers (2022), see also Muth (2022) for more background and details. |
| 20 |
#' APCA is still under development, here version 0.98G-4g is implemented. Unlike the standard WCAG |
| 21 |
#' algorithm, APCA takes into account which color is the text and which is the background. Hence |
| 22 |
#' for the APCA algorithm a matrix with normal and reverse polarity is returned. An absolute |
| 23 |
#' value of 45 is "sort of" like a WCAG ratio of 3, 60 is "sort of" like 4.5. |
| 24 |
#' |
| 25 |
#' @param col,col2 vectors of any of the three kind of R colors, i.e., either a color |
| 26 |
#' name (an element of \code{\link[grDevices]{colors}}), a hexadecimal string |
| 27 |
#' of the form \code{"#rrggbb"} or \code{"#rrggbbaa"} (see |
| 28 |
#' \code{\link[grDevices]{rgb}}), or an integer \code{i} meaning |
| 29 |
#' \code{palette()[i]}. Both can be vectors and are recycled as necessary. |
| 30 |
#' @param algorithm character specifying whether the established standard \code{"WCAG"} 2.1 algorithm |
| 31 |
#' should be used or the improved \code{"APCA"} 0.98G-4g algorithm, still under development. |
| 32 |
#' @param plot logical indicating whether the contrast ratios should also be |
| 33 |
#' visualized by simple color swatches. Can also be a vector of length 2, indicating |
| 34 |
#' whether the foreground color should be visualized on the background color and/or |
| 35 |
#' the background color on the foreground color. |
| 36 |
#' @param border logical or color specification for the borders around the color swatches (only |
| 37 |
#' used if \code{plot = TRUE}). The default is \code{FALSE} which is equivalent to |
| 38 |
#' \code{"transparent"}. If \code{TRUE} the border is drawn in the same color as the text in the |
| 39 |
#' rectangle. |
| 40 |
#' @param cex numeric. Size of the text in the color color swatches (only if \code{plot = TRUE}). |
| 41 |
#' @param off numeric. Vertical offset between the different color swatches (only if \code{plot = TRUE}). |
| 42 |
#' Can also be of length 2 giving both vertical and horizontal offsets, respectively. |
| 43 |
#' @param mar numeric. Size of the margins around the color swatches (only if \code{plot = TRUE}). |
| 44 |
#' @param digits numeric. Number of digits for the contrast ratios displayed in the color swatches |
| 45 |
#' (only if \code{plot = TRUE}) |
| 46 |
#' @param \dots further arguments passed to the plot of the color swatches (only if \code{plot = TRUE}). |
| 47 |
#' |
| 48 |
#' @return A numeric vector with the contrast ratios is returned (invisibly, if \code{plot} is \code{TRUE}). |
| 49 |
#' @seealso \code{\link[colorspace]{desaturate}} |
| 50 |
#' @references W3C (2024). \dQuote{Web Content Accessibility Guidelines (WCAG) 2.2.} |
| 51 |
#' \url{https://www.w3.org/TR/WCAG22/} |
| 52 |
#' |
| 53 |
#' Somers A (2022). \dQuote{Advanced Perceptual Contrast Algorithm.} |
| 54 |
#' \url{https://github.com/Myndex/SAPC-APCA} |
| 55 |
#' |
| 56 |
#' Muth LC (2022). \dQuote{It's Time for a More Sophisticated Color Contrast Check for Data Visualizations.} |
| 57 |
#' Datawrapper Blog. \url{https://www.datawrapper.de/blog/color-contrast-check-data-vis-wcag-apca/} |
| 58 |
#' @keywords color |
| 59 |
#' @examples |
| 60 |
#' # check contrast ratio of default palette on white background |
| 61 |
#' contrast_ratio(palette(), "white") |
| 62 |
#' |
| 63 |
#' # visualize contrast ratio of default palette on white and black background |
| 64 |
#' contrast_ratio(palette(), "white", plot = TRUE) |
| 65 |
#' contrast_ratio(palette()[-1], "black", plot = TRUE) |
| 66 |
#' |
| 67 |
#' # APCA algorithm |
| 68 |
#' contrast_ratio(palette(), "white", algorithm = "APCA") |
| 69 |
#' contrast_ratio(palette(), "white", algorithm = "APCA", plot = TRUE, digits = 0) |
| 70 |
#' @export contrast_ratio |
| 71 |
#' @importFrom grDevices rgb |
| 72 |
#' @importFrom graphics par |
| 73 |
#' @importFrom graphics rect |
| 74 |
#' @importFrom graphics text |
| 75 |
|
| 76 |
contrast_ratio <- function(col, col2 = "white", algorithm = c("WCAG", "APCA"), |
| 77 |
plot = FALSE, border = FALSE, cex = 2, off = 0.05, mar = rep(0.5, 4), digits = 2L, ...) |
| 78 |
{ |
| 79 |
## https://www.w3.org/TR/WCAG22/#dfn-contrast-ratio |
| 80 |
if(length(col) < 1L || length(col2) < 1L) stop("both 'col' and 'col2' need to specify at least one color") |
| 81 |
|
| 82 |
## suitably recycle colors if necessary |
| 83 |
n <- max(c(length(col), length(col2))) |
| 84 |
if(!(length(col) == n && length(col2) == n)) { |
| 85 |
col <- rep_len(col, n) |
| 86 |
col2 <- rep_len(col2, n) |
| 87 |
} |
| 88 |
|
| 89 |
## compute relative luminances |
| 90 |
algorithm <- match.arg(algorithm, c("WCAG", "APCA")) |
| 91 |
lum <- relative_luminance(col, algorithm = algorithm) |
| 92 |
lum2 <- relative_luminance(col2, algorithm = algorithm) |
| 93 |
|
| 94 |
## compute contrast ratio |
| 95 |
if(algorithm == "WCAG") { |
| 96 |
ratio <- (lum + 0.05)/(lum2 + 0.05) |
| 97 |
ratio[ratio < 1] <- 1/ratio[ratio < 1] |
| 98 |
} |
| 99 |
if(algorithm == "APCA") { |
| 100 |
apca <- function(txt, bg) { |
| 101 |
## for normal polarity (bg luminance > txt luminance) |
| 102 |
ratio <- (bg^0.56 - txt^0.57) * 1.14 |
| 103 |
ratio <- ifelse(ratio < 0.1, 0, ratio - 0.027) |
| 104 |
## for reverse polarity |
| 105 |
rev <- bg <= txt |
| 106 |
ratio[rev] <- (bg[rev]^0.65 - txt[rev]^0.62) * 1.14 |
| 107 |
ratio[rev] <- ifelse(ratio[rev] > -0.1, 0, ratio[rev] + 0.027) |
| 108 |
## zero for essentially identical luminances |
| 109 |
ratio[abs(bg - txt) < 0.0005] <- 0 |
| 110 |
ratio <- ratio * 100 |
| 111 |
return(ratio) |
| 112 |
} |
| 113 |
ratio <- cbind("normal" = apca(lum, lum2), "reverse" = apca(lum2, lum)) |
| 114 |
} |
| 115 |
|
| 116 |
## optionally visualize |
| 117 |
plot <- rep_len(plot, 2L) |
| 118 |
if(any(plot)) { |
| 119 |
opar <- par(mar = mar) |
| 120 |
on.exit(par(opar)) |
| 121 |
if(identical(border, FALSE)) border <- "transparent" |
| 122 |
if(length(off) == 1L) off <- c(off, 0) |
| 123 |
plot(0, 0, xlim = c(0, sum(plot)), ylim = c(0, n), type = "n", axes = FALSE, xlab = "", ylab = "", ...) |
| 124 |
if(plot[1L]) { |
| 125 |
r <- format(round(if(algorithm == "WCAG") ratio else ratio[, 1L], digits = digits), nsmall = digits) |
| 126 |
rect(0L, n:1L - 1L, 1L - off[2L], n:1L - off[1L], col = col2, border = if(isTRUE(border)) col else border) |
| 127 |
text((1 - off[2L])/2, n:1L - (1 - (1 - off[1L])/2), r, cex = cex, col = col) |
| 128 |
} |
| 129 |
if(plot[2L]) { |
| 130 |
r <- format(round(if(algorithm == "WCAG") ratio else ratio[, 2L], digits = digits), nsmall = digits) |
| 131 |
rect(0L + plot[1L], n:1L - 1L, 1L - off[2L] + plot[1L], n:1L - off[1L], col = col, border = if(isTRUE(border)) col2 else border) |
| 132 |
text((1 - off[2L])/2 + plot[1L], n:1L - (1 - (1 - off[1L])/2), r, cex = cex, col = col2) |
| 133 |
} |
| 134 |
invisible(ratio) |
| 135 |
} else { |
| 136 |
return(ratio) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
relative_luminance <- function(col, algorithm = c("WCAG", "APCA")) { |
| 141 |
## raw sRGB coordinates |
| 142 |
rgb <- t(col2rgb(col))/255 |
| 143 |
|
| 144 |
## type of algorithm |
| 145 |
algorithm <- match.arg(algorithm, c("WCAG", "APCA")) |
| 146 |
|
| 147 |
## sRGB coefficients |
| 148 |
coef <- round(c(0.2126478133913640, 0.7151791475336150, 0.0721730390750208), |
| 149 |
digits = switch(algorithm, "WCAG" = 4, "APCA" = 16)) |
| 150 |
|
| 151 |
## relative luminance is essentially (rgb^2.4) %*% coef |
| 152 |
## but with slightly different adjustments for WCAG vs. APCA |
| 153 |
if(algorithm == "WCAG") { |
| 154 |
## https://www.w3.org/TR/WCAG22/#dfn-relative-luminance |
| 155 |
rgb[] <- ifelse(rgb <= 0.03928, rgb/12.92, ((rgb + 0.055)/1.055)^2.4) |
| 156 |
rlum <- as.numeric(rgb %*% coef) |
| 157 |
} else { |
| 158 |
## https://github.com/Myndex/SAPC-APCA |
| 159 |
rgb[] <- rgb^2.4 |
| 160 |
rlum <- as.numeric(rgb %*% coef) |
| 161 |
## black soft clamp |
| 162 |
clamp <- rlum <= 0.022 |
| 163 |
rlum[clamp] <- rlum[clamp] + (0.022 - rlum[clamp])^1.414 |
| 164 |
} |
| 165 |
|
| 166 |
return(rlum) |
| 167 |
} |