😎 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.

119 lines
4.2KB

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