😎 Give your xaringan slides some style
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 satır
3.0KB

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