| @@ -4,6 +4,15 @@ | |||
| #' @param strength The "strength" of the blend with white or black, | |||
| #' where 0 is entirely the original color and 1 is entirely white | |||
| #' (`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 | |||
| NULL | |||
| @@ -31,6 +40,14 @@ darken_color <- function(color_hex, strength = 0.8) { | |||
| #' and appending to the hex color. | |||
| #' @inheritParams lighten_darken_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 | |||
| apply_alpha <- function(color_hex, opacity = 0.5) { | |||
| paste0(color_hex, as.hexmode(round(255 * opacity, 0))) | |||
| @@ -54,6 +71,16 @@ adjust_value_color <- function(color_hex, strength = 0.5) { | |||
| #' `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 | |||
| #' `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 | |||
| choose_dark_or_light <- function(x, black = "#000000", white = "#FFFFFF") { | |||
| if (is_light_color(x)) eval(black) else eval(white) | |||
| @@ -950,6 +950,17 @@ requires_xaringanthemer_env <- function( | |||
| #' | |||
| #' @param setting A xaringanthemer style setting | |||
| #' @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 | |||
| theme_xaringan_get_value <- function(setting, css_file = NULL) { | |||
| requires_xaringanthemer_env(css_file = css_file) | |||
| @@ -16,3 +16,12 @@ Applies alpha (or opacity) to a color in hexadecimal form by | |||
| converting opacity in the \verb{[0, 1]} range to hex in the \verb{[0, 255]} range | |||
| 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)) | |||
| } | |||
| } | |||
| @@ -19,6 +19,17 @@ choose_dark_or_light(x, black = "#000000", white = "#FFFFFF") | |||
| 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 | |||
| 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{ | |||
| \url{https://stackoverflow.com/a/3943023/2022615} | |||
| @@ -20,3 +20,13 @@ where 0 is entirely the original color and 1 is entirely white | |||
| \description{ | |||
| 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)) | |||
| } | |||
| } | |||
| @@ -90,3 +90,15 @@ Style settings used by xaringanthemer include: | |||
| } | |||
| } | |||
| \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") | |||
| } | |||