Skip to content

Autocoloroptions - #1930

Merged
rich-iannone merged 9 commits into
rstudio:masterfrom
xx02al:autocoloroptions
Sep 17, 2025
Merged

rich-iannone merged 9 commits into
rstudio:masterfrom
xx02al:autocoloroptions

Conversation

@xx02al

@xx02al xx02al commented Dec 2, 2024 •

Copy link
Copy Markdown
Contributor

I have added light and dark autocolor_text color options. Often, tables are embedded in a context and in there "black" is not black, but another very dark color and analogously for white. Being able to design the tables in harmony is central.

The current implementation follows the previous design of {gt}, particularly data_color(), but/thus it is unattractive in that if autocolor = FALSE, now allready 3 arguments are irrelevant.

Additionally, I fixed a constant in the calculation of relative luminance WCAG (according to documentation).

@CLAassistant

CLAassistant commented Dec 6, 2024 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@xx02al

xx02al commented Feb 7, 2025

Copy link
Copy Markdown
Contributor Author

Hi @rich-iannone and @olivroy since it has been a while since I opened this pull request, and I would still like to be able to use this feature in various situations (i.e., tables I create), I would like to ask what you think about it!?

@olivroy olivroy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, good start! Sorry for the delay.

I think this is a good idea. Can't garantee that we merge it. I'd suggest adding some before / after (visual) examples of the improvement this PR brings.

We'd like to avoid new imports, so if you find a way to accomplish this without using farver, it would be great.

It is good that you added some tests.

Also, feel free to use gt from your PR `pak::pak("#1930") and report back if you see that something that is off.

You would need to add a news bullet as well that briefly describe the change https://style.tidyverse.org/news.html

Comment thread DESCRIPTION Outdated
cli (>= 3.6.3),
commonmark (>= 1.9.1),
dplyr (>= 1.1.4),
farver,

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor Author

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.

Comment thread R/data_color.R Outdated
#' default this is `"apca"` (Accessible Perceptual Contrast Algorithm) and the
#' alternative to this is `"wcag"` (Web Content Accessibility Guidelines).
#'
#' @param autocolor_light

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the pattern above and give a short description

For example, #' @param autocolor_light *Automatic light color*

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread R/data_color.R Outdated
#' `"white"` will be used (`#FFFFFF"`). Alpha channel values will be set to
#' 1.0 (fully opaque).
#'
#' @param autocolor_dark

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment thread R/data_color.R
Comment on lines +1310 to +1311
light_color <- farver::encode_colour(farver::decode_colour(light))
dark_color <- farver::encode_colour(farver::decode_colour(dark))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?
We don't use this pattern of encoding / decoding colors in the package..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See just above.

Comment thread R/utils_color_contrast.R
coef <- round(c(apca_coeffs$sRco, apca_coeffs$sGco, apca_coeffs$sBco), 4)

rgb[] <- ifelse(rgb <= 0.03928, rgb / 12.92, ((rgb + 0.055) / 1.055)^2.4)
rgb[] <- ifelse(rgb <= 0.04045, rgb / 12.92, ((rgb + 0.055) / 1.055)^2.4)

This comment was marked as resolved.

@xx02al

xx02al commented Feb 25, 2025

Copy link
Copy Markdown
Contributor Author

Here's an example, not because it is particularly aesthetically pleasing, but I think it serves the purpose of clearly pointing out differences. Run it with and without autocolor_light = "#FFE8D1".

exibble |>
  gt() |> 
  data_color(
    columns = c(fctr, datetime, group),
    method = "factor",
    rows = c(2, 4:6, 8),
    palette = "#725F0B",
    na_color = "#B6D7C7",
    autocolor_light = "#FFE8D1"
  ) |> 
  data_color(
    columns = c(date, currency),
    palette = "#6E6684",
    na_color = "#B6D7C7",
    autocolor_light = "#FFE8D1"
  ) |> 
  tab_options(
    table.background.color = "#004249",
    table.font.color.light = "#FFE8D1"
  )

@rich-iannone rich-iannone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@rich-iannone
rich-iannone merged commit e7f0b82 into rstudio:master Sep 17, 2025
11 checks passed
@xx02al
xx02al deleted the autocoloroptions branch September 17, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants