Przeglądaj źródła

Add examples to color functions

tags/v0.3.0
Garrick Aden-Buie 6 lat temu
rodzic
commit
5e55c5646c
6 zmienionych plików z 80 dodań i 0 usunięć
  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 Wyświetl plik

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

+ 11
- 0
R/ggplot2.R Wyświetl plik

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

+ 9
- 0
man/apply_alpha.Rd Wyświetl plik

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

}

+ 11
- 0
man/choose_dark_or_light.Rd Wyświetl plik

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}

+ 10
- 0
man/lighten_darken_color.Rd Wyświetl plik

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

}

+ 12
- 0
man/theme_xaringan_get_value.Rd Wyświetl plik

} }
} }


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

}

Ładowanie…
Anuluj
Zapisz