Просмотр исходного кода

Add examples to color functions

tags/v0.3.0
Garrick Aden-Buie 6 лет назад
Родитель
Сommit
5e55c5646c
6 измененных файлов: 80 добавлений и 0 удалений
  1. +27
    -0
      R/color.R
  2. +11
    -0
      R/ggplot2.R
  3. +9
    -0
      man/apply_alpha.Rd
  4. +11
    -0
      man/choose_dark_or_light.Rd
  5. +10
    -0
      man/lighten_darken_color.Rd
  6. +12
    -0
      man/theme_xaringan_get_value.Rd

+ 27
- 0
R/color.R Просмотреть файл

@@ -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)

+ 11
- 0
R/ggplot2.R Просмотреть файл

@@ -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)

+ 9
- 0
man/apply_alpha.Rd Просмотреть файл

@@ -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))
}

}

+ 11
- 0
man/choose_dark_or_light.Rd Просмотреть файл

@@ -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}

+ 10
- 0
man/lighten_darken_color.Rd Просмотреть файл

@@ -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))
}

}

+ 12
- 0
man/theme_xaringan_get_value.Rd Просмотреть файл

@@ -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")

}

Загрузка…
Отмена
Сохранить