😎 Give your xaringan slides some style
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

141 行
5.2KB

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