😎 Give your xaringan slides some style
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

135 lines
4.8KB

  1. `%||%` <- function(x, y) if (is.null(x)) y else x
  2. #' @title Generate lighter or darker version of a color
  3. #' @description Produces a linear blend of the color with white or black.
  4. #' @param color_hex A character string representing a hex color
  5. #' @param strength The "strength" of the blend with white or black,
  6. #' 0 low to 1 high.
  7. #' @name lighten_darken_color
  8. NULL
  9. #' @rdname lighten_darken_color
  10. #' @export
  11. lighten_color <- function(color_hex, strength = 0.7) {
  12. stopifnot(strength >= 0 && strength <= 1)
  13. color_rgb <- col2rgb(color_hex)[, 1]
  14. color_rgb <- (1 - strength) * color_rgb + strength * 255
  15. rgb(color_rgb[1], color_rgb[2], color_rgb[3], maxColorValue = 255)
  16. }
  17. #' @rdname lighten_darken_color
  18. #' @export
  19. darken_color <- function(color_hex, strength = 0.8) {
  20. stopifnot(strength >= 0 && strength <= 1)
  21. color_rgb <- col2rgb(color_hex)[, 1]
  22. color_rgb <- (1 - strength) * color_rgb
  23. rgb(color_rgb[1], color_rgb[2], color_rgb[3], maxColorValue = 255)
  24. }
  25. #' @title Add alpha to hex color
  26. #' @inheritParams lighten_darken_color
  27. #' @param opacity Desired opacity of the output color
  28. #' @export
  29. apply_alpha <- function(color_hex, opacity = 0.5) {
  30. paste0(color_hex, as.hexmode(round(255*opacity, 0)))
  31. }
  32. adjust_value_color <- function(color_hex, strength = 0.5) {
  33. color_hsv <- rgb2hsv(col2rgb(color_hex))[, 1]
  34. color_hsv['v'] <- strength
  35. hsv(color_hsv[1], color_hsv[2], color_hsv[3])
  36. }
  37. #' Choose dark or light color
  38. #'
  39. #' Takes a color input as `x` and returns either the black or white color (or
  40. #' expression) if dark or light text should be used over the input color for
  41. #' best contrast. Follows W3C Recommendations.
  42. #'
  43. #' @references <https://stackoverflow.com/a/3943023/2022615>
  44. #' @param x The background color
  45. #' @param black Text or foreground color, e.g. "#222" or
  46. #' `substitute(darken_color(x, 0.8))`, if black text provides the best contrast.
  47. #' @param white Text or foreground color or expression, e.g. "#EEE" or
  48. #' `substitute(lighten_color(x, 0.8))`, if white text provides the best contrast.
  49. #' @export
  50. choose_dark_or_light <- function(x, black = "#000", white = "#FFF") {
  51. # x = color_hex
  52. # black <- substitute(black)
  53. # white <- substitute(white)
  54. color_rgb <- col2rgb(x)[, 1]
  55. # from https://stackoverflow.com/a/3943023/2022615
  56. color_rgb <- color_rgb / 255
  57. color_rgb[color_rgb <= 0.03928] <- color_rgb[color_rgb <= 0.03928]/12.92
  58. color_rgb[color_rgb > 0.03928] <- ((color_rgb[color_rgb > 0.03928] + 0.055)/1.055)^2.4
  59. lum <- t(c(0.2126, 0.7152, 0.0722)) %*% color_rgb
  60. if (lum[1, 1] > 0.179) eval(black) else eval(white)
  61. }
  62. #' @keywords internal
  63. call_write_xaringan_theme <- function() {
  64. paste0("write_xaringan_theme(",
  65. paste(template_variables$variable, collapse = ", "),
  66. ")")
  67. }
  68. #' Specify Google Font
  69. #'
  70. #' Builds Google Fonts URL from family name. Extra weights are given in the
  71. #' `...` parameters. Languages can be specified in `langauges` and must one or
  72. #' more of the language codes as given by `google_language_codes()`.
  73. #'
  74. #' @examples
  75. #' google_font("Josefin Sans", "400", "400i", "600i", "700")
  76. #' google_font("Josefin Sans", languages = c("latin-ext", "vietnamese"))
  77. #' @param family Font family
  78. #' @param ... Font weights to include, example "400", "400i"
  79. #' @param languages Font languages to include (dependent on the font.) See
  80. #' [google_language_codes()].
  81. #' @export
  82. google_font <- function(family, ..., languages = NULL) {
  83. base = "https://fonts.googleapis.com/css?family="
  84. weights <- if (length(list(...))) paste(c(...), collapse = ",")
  85. languages <- if (!is.null(languages)) paste(google_language_codes(languages), collapse = ",")
  86. structure(list(
  87. family = family,
  88. weights = weights,
  89. languages = languages,
  90. url = paste0(
  91. base, stringr::str_replace_all(family, " ", "+"),
  92. if (!is.null(weights)) paste0(":", weights),
  93. if (!is.null(languages)) paste0("&subset=", languages)
  94. )
  95. ), class = "google_font")
  96. }
  97. #' @title List Valid Google Language Codes
  98. #' @seealso [google_font()]
  99. #' @param x Vector of potential Google language codes
  100. #' @export
  101. google_language_codes <- function(
  102. x = c("latin", "latin-ext", "sinhala", "greek", "hebrew", "vietnamese",
  103. "cyrillic", "cyrillic-ext", "devanagari", "arabic", "khmer",
  104. "tamil", "greek-ext", "thai", "bengali", "gujarati", "oriya",
  105. "malayalam", "gurmukhi", "kannada", "telugu", "myanmar")
  106. ) {
  107. unique(match.arg(x, several.ok = TRUE))
  108. }
  109. print.google_font <- function(x) {
  110. cat(
  111. "Family: ", x$family,
  112. if (!is.null(x$weights)) paste("\nWeights:", x$weights),
  113. if (!is.null(x$languages)) paste("\nLangs: ", x$languages),
  114. "\nURL: ", x$url
  115. )
  116. }
  117. quote_elements_w_spaces <- function(x) {
  118. x <- stringr::str_split(x, ", ?")[[1]]
  119. has_space <- stringr::str_detect(x, "\\w \\w")
  120. not_quoted <- stringr::str_detect(x, "^\\w.+\\w$")
  121. x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'")
  122. paste(x, collapse = ", ")
  123. }