-
Notifications
You must be signed in to change notification settings - Fork 212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autocoloroptions #1930
base: master
Are you sure you want to change the base?
Autocoloroptions #1930
Changes from 4 commits
560f587
b30a83d
87f9fa3
71cc3e6
f503036
f340668
102ce26
cb9a6ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,8 @@ | |
#' text | ||
#' - text autocoloring: if colorizing the cell background, `data_color()` will | ||
#' automatically recolor the foreground text to provide the best contrast (can | ||
#' be deactivated with `autocolor_text = FALSE`) | ||
#' be deactivated with `autocolor_text = FALSE`; a light and dark color to be | ||
#' used can be specified with `autocolor_light` and `autocolor_dark`) | ||
#' | ||
#' `data_color()` won't fail with the default options used, but | ||
#' that won't typically provide you the type of colorization you really need. | ||
|
@@ -233,6 +234,22 @@ | |
#' default this is `"apca"` (Accessible Perceptual Contrast Algorithm) and the | ||
#' alternative to this is `"wcag"` (Web Content Accessibility Guidelines). | ||
#' | ||
#' @param autocolor_light | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow the pattern above and give a short description For example, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
#' | ||
#' `scalar<character>` // *default:* `"white"` | ||
#' | ||
#' The light color to use when `autocolor_text = TRUE`. By default the color | ||
#' `"white"` will be used (`#FFFFFF"`). Alpha channel values will be set to | ||
#' 1.0 (fully opaque). | ||
#' | ||
#' @param autocolor_dark | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
#' | ||
#' `scalar<character>` // *default:* `"black"` | ||
#' | ||
#' The dark color to use when `autocolor_text = TRUE`. By default the color | ||
#' `"black"` will be used (`#000000"`). Alpha channel values will be set to | ||
#' 1.0 (fully opaque). | ||
#' | ||
#' @param colors *[Deprecated] Color mapping function* | ||
#' | ||
#' `function` // *default:* `NULL` (`optional`) | ||
|
@@ -363,7 +380,9 @@ | |
#' setting `autocolor_text` to `FALSE`. The `contrast_algo` argument lets us | ||
#' choose between two color contrast algorithms: `"apca"` (*Accessible | ||
#' Perceptual Contrast Algorithm*, the default algo) and `"wcag"` (*Web Content | ||
#' Accessibility Guidelines*). | ||
#' Accessibility Guidelines*). `autocolor_light` and `autocolor_dark` allow for | ||
#' further customization, however, should only be used if you are sure that | ||
#' accessibility criteria are guaranteed. | ||
#' | ||
#' @section Examples: | ||
#' | ||
|
@@ -669,6 +688,8 @@ data_color <- function( | |
apply_to = c("fill", "text"), | ||
autocolor_text = TRUE, | ||
contrast_algo = c("apca", "wcag"), | ||
autocolor_light = "#FFFFFF", | ||
autocolor_dark = "#000000", | ||
colors = NULL | ||
) { | ||
|
||
|
@@ -679,7 +700,7 @@ data_color <- function( | |
direction <- rlang::arg_match0(direction, values = c("column", "row")) | ||
|
||
# Get the correct `method` value | ||
method <- | ||
method <- | ||
rlang::arg_match0( | ||
method, | ||
values = c("auto", "numeric", "bin", "quantile", "factor") | ||
|
@@ -1122,6 +1143,8 @@ data_color <- function( | |
color_vals <- | ||
ideal_fgnd_color( | ||
bgnd_color = color_vals, | ||
light = autocolor_light, | ||
dark = autocolor_dark, | ||
algo = contrast_algo | ||
) | ||
|
||
|
@@ -1268,8 +1291,10 @@ expand_short_hex <- function(colors) { | |
|
||
#' For a background color, which foreground color provides better contrast? | ||
#' | ||
#' The input for this function is a single color value in 'rgba()' format. The | ||
#' output is a single color value in #RRGGBB hexadecimal format | ||
#' The `bgnd_color` input for this function is a single color value in 'rgba()' | ||
#' format. The output is a single color value in #RRGGBB hexadecimal format. | ||
#' `light` and `dark` accepts every color(specification) that can be handled by | ||
#' {farver}. | ||
#' | ||
#' @noRd | ||
ideal_fgnd_color <- function( | ||
|
@@ -1282,6 +1307,9 @@ ideal_fgnd_color <- function( | |
# Get the correct `algo` value | ||
algo <- rlang::arg_match0(algo, values = c("apca", "wcag")) | ||
|
||
light_color <- farver::encode_colour(farver::decode_colour(light)) | ||
dark_color <- farver::encode_colour(farver::decode_colour(dark)) | ||
Comment on lines
+1325
to
+1326
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See just above. |
||
|
||
# Normalize color to hexadecimal color if it is in the 'rgba()' string format | ||
bgnd_color <- rgba_to_hex(colors = bgnd_color) | ||
|
||
|
@@ -1291,17 +1319,17 @@ ideal_fgnd_color <- function( | |
if (algo == "apca") { | ||
|
||
# Determine the ideal color for the chosen background color with APCA | ||
contrast_dark <- get_contrast_ratio(color_1 = dark, color_2 = bgnd_color, algo = "apca")[, 1] | ||
contrast_light <- get_contrast_ratio(color_1 = light, color_2 = bgnd_color, algo = "apca")[, 1] | ||
contrast_dark <- get_contrast_ratio(color_1 = dark_color, color_2 = bgnd_color, algo = "apca")[, 1] | ||
contrast_light <- get_contrast_ratio(color_1 = light_color, color_2 = bgnd_color, algo = "apca")[, 1] | ||
|
||
} else { | ||
|
||
# Determine the ideal color for the chosen background color with WCAG | ||
contrast_dark <- get_contrast_ratio(color_1 = dark, color_2 = bgnd_color, algo = "wcag") | ||
contrast_light <- get_contrast_ratio(color_1 = light, color_2 = bgnd_color, algo = "wcag") | ||
contrast_dark <- get_contrast_ratio(color_1 = dark_color, color_2 = bgnd_color, algo = "wcag") | ||
contrast_light <- get_contrast_ratio(color_1 = light_color, color_2 = bgnd_color, algo = "wcag") | ||
} | ||
|
||
ifelse(abs(contrast_dark) >= abs(contrast_light), dark, light) | ||
ifelse(abs(contrast_dark) >= abs(contrast_light), dark_color, light_color) | ||
} | ||
|
||
#' Convert colors in mixed formats (incl. rgba() strings) format to hexadecimal | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add this in Suggrsts instead? We cannot add another import due to CRAN restrictions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understand. Using {farver} to offer the user the possibility to specify the color in a range of different specifications (literally everything that {farver} can handle), as well as {farver} has the task and responsibility to convert precisely (remember also base R limitations) into the desired #RRGGBB hexadecimal format.
I have added it to “Suggests”, as has been done with other pkgs elsewhere in gt with packages that are only used in a very specific case. I think that is reasonable.