Last active
October 20, 2023 15:31
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
Author
rich-iannone
commented
Nov 22, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment