Skip to content
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

Transform certain X11 colors with no CSS equivalent #787

Merged
merged 30 commits into from
Jun 22, 2021

Conversation

rich-iannone
Copy link
Member

This PR intercepts named colors just before sass compilation and replaces X11 color names with no CSS color equivalent with a hexadecimal color. This should likely be done in more places where colors serve as inputs (e.g., cell_fill()) being careful to only apply the transformation in the HTML context.

The X11 color names that don't work as CSS color names are the shades of gray (e.g., gray1, gray85, grey50, etc.). The ones that do work but yield small-to-large differences between X11 and CSS are the numbered variants in some colors (e.g., Azure2, etc.).

Fortunately, the gt package has a utility function to handle the conversion from X11 color name to hexadecimal color code (html_color()). This is used only in those cases where X11 colors need transformation.

Fixes: #712

@rich-iannone
Copy link
Member Author

rich-iannone commented Jun 9, 2021

Open question:

  • what happens if using CSS colors with no equivalent in X11/R (grDevices::col2rgb() will probably fail)

@rich-iannone
Copy link
Member Author

Named CSS3 colors are now detected and normalized to hexadecimal values before hitting grDevices::col2rgb(). This means that nine color names not in the X11/R color set (e.g., crimson, indigo, silver, etc.) will now work.

@rich-iannone rich-iannone marked this pull request as ready for review June 10, 2021 01:09
* master:
  Fix for possible column name duplication when generating summary rows (#792)
  Change the `data` arg to `.data` to avoid partial matching issues (#772)
R/data_color.R Outdated
# translate certain color names (CSS exclusive names) if necessary
if (!all(grepl("^#", colors))) {

named_colors <- tolower(colors[!grepl("^#", colors)])
Copy link
Member

Choose a reason for hiding this comment

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

Simplify conditional logic (both 378 and 382) by reusing the variable you'll need anyway.

R/data_color.R Outdated
if (any(named_colors %in% css_exclusive_color_names())) {

colors <-
vapply(
Copy link
Member

Choose a reason for hiding this comment

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

Vectorize please

R/data_color.R Outdated
colors[is_css_excl_named] <-
names(
css_exclusive_color_names()[
match(tolower(colors[is_css_excl_named]), css_exclusive_color_names())
Copy link
Member

Choose a reason for hiding this comment

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

Switch css_exclusive_color_names's names to values and values to names

R/data_color.R Outdated
Comment on lines 412 to 414
unname(
css_exclusive_colors()[
match(colors[is_css_excl_named], names(css_exclusive_colors()))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
unname(
css_exclusive_colors()[
match(colors[is_css_excl_named], names(css_exclusive_colors()))
unname(css_exclusive_colors()[colors[is_css_excl_named]])

@rich-iannone rich-iannone merged commit 3175567 into master Jun 22, 2021
@rich-iannone rich-iannone deleted the x11-colors-in-html branch June 22, 2021 20:19
rich-iannone added a commit that referenced this pull request Jun 22, 2021
* master:
  Transform certain X11 colors with no CSS equivalent (#787)
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.

Some colours in *.background.color statements within tab_options result in a Sass error
2 participants