Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Last active October 20, 2023 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rich-iannone/55ffa2cf293313e70468ca8447dd3d97 to your computer and use it in GitHub Desktop.
Save rich-iannone/55ffa2cf293313e70468ca8447dd3d97 to your computer and use it in GitHub Desktop.
This gist generates a gt table that compares text color choices from the WCAG and APCA contrast methods. Implemented in the `data_color()` function of the gt package, where the background is set first (based on cell data) and the color of the overlaid text is determined from the color contrast algorithm.
library(gt)
library(tidyverse)
color_sequence <- seq_along(grDevices::colors())
dplyr::tibble(
x11c = grDevices::colors(),
wcag = color_sequence,
apca = color_sequence
) %>%
gt(rowname_col = "x11c") %>%
data_color(
columns = wcag,
colors = scales::col_numeric(
palette = grDevices::colors(),
domain = NULL
),
contrast_algo = "wcag"
) %>%
data_color(
columns = apca,
colors = scales::col_numeric(
palette = grDevices::colors(),
domain = NULL
),
contrast_algo = "apca"
) %>%
cols_width(
stub() ~ "180px",
everything() ~ "100px"
) %>%
cols_align(
align = "center",
columns = c(wcag, apca)
) %>%
text_transform(
locations = cells_body(columns = c(wcag, apca)),
fn = function(x) "ABCDEFG"
) %>%
tab_header(
title = "Color Contrast Computation Comparison",
subtitle = "Text color choices from the WCAG and APCA contrast methods."
) %>%
cols_label(
wcag = "WCAG",
apca = "APCA"
) %>%
opt_stylize(style = 6, color = "gray")
@rich-iannone
Copy link
Author

color_contrast_wcag_apca

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment