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

75 lines
2.6KB

  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. #' Specify Google Font
  17. #'
  18. #' Builds Google Fonts URL from family name. Extra weights are given in the
  19. #' `...` parameters. Languages can be specified in `langauges` and must one or
  20. #' more of the language codes as given by `google_language_codes()`.
  21. #'
  22. #' @examples
  23. #' google_font("Josefin Sans", "400", "400i", "600i", "700")
  24. #' google_font("Josefin Sans", languages = c("latin-ext", "vietnamese"))
  25. #' @param family Font family
  26. #' @param ... Font weights to include, example "400", "400i"
  27. #' @param languages Font languages to include (dependent on the font.) See
  28. #' [google_language_codes()].
  29. #' @export
  30. google_font <- function(family, ..., languages = NULL) {
  31. base = "https://fonts.googleapis.com/css?family="
  32. weights <- if (length(list(...))) paste(c(...), collapse = ",")
  33. languages <- if (!is.null(languages)) paste(google_language_codes(languages), collapse = ",")
  34. structure(list(
  35. family = family,
  36. weights = weights,
  37. languages = languages,
  38. url = paste0(
  39. base, stringr::str_replace_all(family, " ", "+"),
  40. if (!is.null(weights)) paste0(":", weights),
  41. if (!is.null(languages)) paste0("&subset=", languages)
  42. )
  43. ), class = "google_font")
  44. }
  45. #' @export
  46. google_language_codes <- function(
  47. x = c("latin", "latin-ext", "sinhala", "greek", "hebrew", "vietnamese",
  48. "cyrillic", "cyrillic-ext", "devanagari", "arabic", "khmer",
  49. "tamil", "greek-ext", "thai", "bengali", "gujarati", "oriya",
  50. "malayalam", "gurmukhi", "kannada", "telugu", "myanmar")
  51. ) {
  52. unique(match.arg(x, several.ok = TRUE))
  53. }
  54. print.google_font <- function(x) {
  55. cat(
  56. "Family: ", x$family,
  57. if (!is.null(x$weights)) paste("\nWeights:", x$weights),
  58. if (!is.null(x$languages)) paste("\nLangs: ", x$languages),
  59. "\nURL: ", x$url
  60. )
  61. }
  62. quote_elements_w_spaces <- function(x) {
  63. x <- stringr::str_split(x, ", ?")[[1]]
  64. has_space <- stringr::str_detect(x, "\\w \\w")
  65. not_quoted <- stringr::str_detect(x, "^\\w.+\\w$")
  66. x[has_space & not_quoted] <- paste0("'", x[has_space & not_quoted], "'")
  67. paste(x, collapse = ", ")
  68. }