| #' @param strength The "strength" of the blend with white or black, | #' @param strength The "strength" of the blend with white or black, | ||||
| #' where 0 is entirely the original color and 1 is entirely white | #' where 0 is entirely the original color and 1 is entirely white | ||||
| #' (`lighten_color()`) or black (`darken_color()`). | #' (`lighten_color()`) or black (`darken_color()`). | ||||
| #' @examples | |||||
| #' blue <- "#0e6ba8" | |||||
| #' blue_light <- lighten_color(blue, strength = 0.33) | |||||
| #' blue_dark <- darken_color(blue, strength = 0.33) | |||||
| #' | |||||
| #' if (requireNamespace("scales", quietly = TRUE)) { | |||||
| #' scales::show_col(c(blue_light, blue, blue_dark)) | |||||
| #' } | |||||
| #' | |||||
| #' @name lighten_darken_color | #' @name lighten_darken_color | ||||
| NULL | NULL | ||||
| #' and appending to the hex color. | #' and appending to the hex color. | ||||
| #' @inheritParams lighten_darken_color | #' @inheritParams lighten_darken_color | ||||
| #' @param opacity Desired opacity of the output color | #' @param opacity Desired opacity of the output color | ||||
| #' @examples | |||||
| #' blue <- "#0e6ba8" | |||||
| #' blue_transparent <- apply_alpha(blue) | |||||
| #' | |||||
| #' if (requireNamespace("scales", quietly = TRUE)) { | |||||
| #' scales::show_col(c(blue, blue_transparent)) | |||||
| #' } | |||||
| #' | |||||
| #' @export | #' @export | ||||
| apply_alpha <- function(color_hex, opacity = 0.5) { | apply_alpha <- function(color_hex, opacity = 0.5) { | ||||
| paste0(color_hex, as.hexmode(round(255 * opacity, 0))) | paste0(color_hex, as.hexmode(round(255 * opacity, 0))) | ||||
| #' `substitute(darken_color(x, 0.8))`, if black text provides the best contrast. | #' `substitute(darken_color(x, 0.8))`, if black text provides the best contrast. | ||||
| #' @param white Text or foreground color or expression, e.g. "#EEE" or | #' @param white Text or foreground color or expression, e.g. "#EEE" or | ||||
| #' `substitute(lighten_color(x, 0.8))`, if white text provides the best contrast. | #' `substitute(lighten_color(x, 0.8))`, if white text provides the best contrast. | ||||
| #' @examples | |||||
| #' light_green <- "#c4d6b0" | |||||
| #' contrast_green <- choose_dark_or_light(light_green) | |||||
| #' dark_purple <- "#381d2a" | |||||
| #' contrast_purple <- choose_dark_or_light(dark_purple) | |||||
| #' | |||||
| #' if (requireNamespace("scales", quietly = TRUE)) { | |||||
| #' scales::show_col(c(light_green, contrast_green, dark_purple, contrast_purple)) | |||||
| #' } | |||||
| #' | |||||
| #' @export | #' @export | ||||
| choose_dark_or_light <- function(x, black = "#000000", white = "#FFFFFF") { | choose_dark_or_light <- function(x, black = "#000000", white = "#FFFFFF") { | ||||
| if (is_light_color(x)) eval(black) else eval(white) | if (is_light_color(x)) eval(black) else eval(white) |
| #' | #' | ||||
| #' @param setting A xaringanthemer style setting | #' @param setting A xaringanthemer style setting | ||||
| #' @inheritParams theme_xaringan | #' @inheritParams theme_xaringan | ||||
| #' @examples | |||||
| #' # Create a xaringanthemer style in a temporary file for this example | |||||
| #' xaringan_themer_css <- tempfile("xaringan-themer", fileext = ".css") | |||||
| #' | |||||
| #' style_solarized_light(outfile = xaringan_themer_css) | |||||
| #' | |||||
| #' theme_xaringan_get_value("text_color") | |||||
| #' theme_xaringan_get_value("background_color") | |||||
| #' theme_xaringan_get_value("header_color") | |||||
| #' theme_xaringan_get_value("text_bold_color") | |||||
| #' | |||||
| #' @export | #' @export | ||||
| theme_xaringan_get_value <- function(setting, css_file = NULL) { | theme_xaringan_get_value <- function(setting, css_file = NULL) { | ||||
| requires_xaringanthemer_env(css_file = css_file) | requires_xaringanthemer_env(css_file = css_file) |
| converting opacity in the \verb{[0, 1]} range to hex in the \verb{[0, 255]} range | converting opacity in the \verb{[0, 1]} range to hex in the \verb{[0, 255]} range | ||||
| and appending to the hex color. | and appending to the hex color. | ||||
| } | } | ||||
| \examples{ | |||||
| blue <- "#0e6ba8" | |||||
| blue_transparent <- apply_alpha(blue) | |||||
| if (requireNamespace("scales", quietly = TRUE)) { | |||||
| scales::show_col(c(blue, blue_transparent)) | |||||
| } | |||||
| } |
| Takes a color input as \code{x} and returns either the black or white color (or | Takes a color input as \code{x} and returns either the black or white color (or | ||||
| expression) if dark or light text should be used over the input color for | expression) if dark or light text should be used over the input color for | ||||
| best contrast. Follows W3C Recommendations. | best contrast. Follows W3C Recommendations. | ||||
| } | |||||
| \examples{ | |||||
| light_green <- "#c4d6b0" | |||||
| contrast_green <- choose_dark_or_light(light_green) | |||||
| dark_purple <- "#381d2a" | |||||
| contrast_purple <- choose_dark_or_light(dark_purple) | |||||
| if (requireNamespace("scales", quietly = TRUE)) { | |||||
| scales::show_col(c(light_green, contrast_green, dark_purple, contrast_purple)) | |||||
| } | |||||
| } | } | ||||
| \references{ | \references{ | ||||
| \url{https://stackoverflow.com/a/3943023/2022615} | \url{https://stackoverflow.com/a/3943023/2022615} |
| \description{ | \description{ | ||||
| Produces a linear blend of the color with white or black. | Produces a linear blend of the color with white or black. | ||||
| } | } | ||||
| \examples{ | |||||
| blue <- "#0e6ba8" | |||||
| blue_light <- lighten_color(blue, strength = 0.33) | |||||
| blue_dark <- darken_color(blue, strength = 0.33) | |||||
| if (requireNamespace("scales", quietly = TRUE)) { | |||||
| scales::show_col(c(blue_light, blue, blue_dark)) | |||||
| } | |||||
| } |
| } | } | ||||
| } | } | ||||
| \examples{ | |||||
| # Create a xaringanthemer style in a temporary file for this example | |||||
| xaringan_themer_css <- tempfile("xaringan-themer", fileext = ".css") | |||||
| style_solarized_light(outfile = xaringan_themer_css) | |||||
| theme_xaringan_get_value("text_color") | |||||
| theme_xaringan_get_value("background_color") | |||||
| theme_xaringan_get_value("header_color") | |||||
| theme_xaringan_get_value("text_bold_color") | |||||
| } |