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

685 satır
22KB

  1. #' A Plot Theme for ggplot2 by xaringanthemer
  2. #'
  3. #' Creates [ggplot2] themes to match the xaringanthemer theme used in the
  4. #' [xaringan] slides that seamlessly matches the "normal" slide colors and
  5. #' styles.
  6. #'
  7. #' @param text_color Color for text and foreground, inherits from `text_color`
  8. #' @param background_color Color for background, inherits from
  9. #' `background_color`
  10. #' @param accent_color Color for titles and accents, inherits from
  11. #' `header_color`
  12. #' @param accent_secondary_color Color for secondary accents, inherits from
  13. #' `text_bold_color`
  14. #' @inheritDotParams theme_xaringan_base
  15. #'
  16. #' @examples
  17. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  18. #' # Set xaringanthemer theme but save to tempfile
  19. #' style_duo_accent(outfile = tempfile())
  20. #'
  21. #' library(ggplot2)
  22. #' ggplot(iris) +
  23. #' aes(Petal.Length, Petal.Width) +
  24. #' geom_point() +
  25. #' theme_xaringan()
  26. #' }
  27. #'
  28. #' @return A ggplot2 theme
  29. #' @family xaringanthemer ggplot2 themes
  30. #' @export
  31. theme_xaringan <- function(
  32. text_color = NULL,
  33. background_color = NULL,
  34. accent_color = NULL,
  35. accent_secondary_color = NULL,
  36. ...
  37. ) {
  38. requires_xaringanthemer_env()
  39. requires_package(fn = "xaringan_theme")
  40. background_color <- background_color %||% xaringanthemer_env$background_color
  41. text_color <- text_color %||% xaringanthemer_env$text_color
  42. accent_color <- accent_color %||% xaringanthemer_env$header_color
  43. accent_secondary_color <- accent_secondary_color %||% xaringanthemer_env$text_bold_color %||% accent_color
  44. theme_xaringan_base(text_color, background_color,
  45. accent_color = accent_color,
  46. accent_secondary_color = accent_secondary_color,
  47. ...)
  48. }
  49. #' An Inverse Plot Theme for ggplot2 by xaringanthemer
  50. #'
  51. #' A [ggplot2] xaringanthemer plot theme to seamlessly match the "inverse"
  52. #' [xaringan] slide colors and styles as styled by [xaringanthemer].
  53. #'
  54. #' @param text_color Color for text and foreground, inherits from `text_color`
  55. #' @param background_color Color for background, inherits from
  56. #' `background_color`
  57. #' @param accent_color Color for titles and accents, inherits from
  58. #' `header_color`
  59. #' @param accent_secondary_color Color for secondary accents, inherits from
  60. #' `text_bold_color`
  61. #' @inheritDotParams theme_xaringan_base
  62. #'
  63. #' @examples
  64. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  65. #' # Set xaringanthemer theme but save to tempfile
  66. #' style_duo_accent(outfile = tempfile())
  67. #'
  68. #' library(ggplot2)
  69. #' ggplot(iris) +
  70. #' aes(Petal.Length, Petal.Width) +
  71. #' geom_point() +
  72. #' theme_xaringan()
  73. #' }
  74. #'
  75. #' @return A ggplot2 theme
  76. #' @family xaringanthemer ggplot2 themes
  77. #' @export
  78. theme_xaringan_inverse <- function(
  79. text_color = NULL,
  80. background_color = NULL,
  81. accent_color = NULL,
  82. accent_secondary_color = NULL,
  83. ...
  84. ) {
  85. requires_xaringanthemer_env()
  86. requires_package(fn = "xaringan_theme")
  87. background_color <- background_color %||% xaringanthemer_env$inverse_background_color
  88. text_color <- text_color %||% xaringanthemer_env$inverse_text_color
  89. accent_color <- accent_color %||% xaringanthemer_env$inverse_header_color
  90. accent_secondary_color <- accent_secondary_color %||% accent_color
  91. theme_xaringan_base(text_color, background_color,
  92. accent_color = accent_color,
  93. accent_secondary_color = accent_secondary_color,
  94. ...)
  95. }
  96. #' The ggplot2 xaringanthemer base plot theme
  97. #'
  98. #' Provides a base plot theme for [ggplot2] to match the [xaringan] slide theme
  99. #' created by [xaringanthemer]. The theme is designed to create a general plot
  100. #' style from two colors, a `background_color` and a `text_color` (or foreground
  101. #' color). Also accepts an `accent_color` and an `accent_secondary_color` that are
  102. #' [xaringanthemer] is not required for the base theme. Use
  103. #' [theme_xaringan()] or [theme_xaringan_inverse()] in xaringan slides styled by
  104. #' xaringanthemer for a plot theme that matches the slide style.
  105. #'
  106. #' @param text_color Color for text and foreground
  107. #' @param background_color Color for background
  108. #' @param accent_color Color for titles and accents, inherits from
  109. #' `header_color` or `text_color`. Used for the `title` base setting in
  110. #' [ggplot2::theme()], and additionally for setting the `color` or `fill` of
  111. #' [ggplot2] geom defaults.
  112. #' @param accent_secondary_color Color for secondary accents, inherits from
  113. #' `text_bold_color` or `accent_color`. Used only when setting [ggplot2] geom
  114. #' defaults.
  115. #' @param set_ggplot_defaults Should defaults be set for [ggplot2] _geoms_?
  116. #' Defaults to TRUE. To restore ggplot's defaults, or the previously set geom
  117. #' defaults, see [theme_xaringan_restore_defaults()].
  118. #' @param text_font Font to use for text elements, passed to
  119. #' [sysfonts::font_add_google()], if available and `text_font_use_google` is
  120. #' `TRUE`. Inherits from `text_font_family`.
  121. #' @param text_font_use_google Is `text_font` available on [Google
  122. #' Fonts](https://fonts.google.com)?
  123. #' @param text_font_size Base text font size, inherits from `text_font_size`, or
  124. #' defaults to 11.
  125. #' @param title_font Font to use for title elements, passed to
  126. #' [sysfonts::font_add_google()], if available and `title_font_use_google` is
  127. #' `TRUE`. Inherits from `title_font_family`.
  128. #' @param title_font_use_google Is `title_font` available on [Google
  129. #' Fonts](https://fonts.google.com)?
  130. #' @param title_font_size Base text font size, inherits from `title_font_size`,
  131. #' or defaults to 14.
  132. #' @param ... Ignored
  133. #'
  134. #' @examples
  135. #' if (requireNamespace("ggplot2", quietly = TRUE)) {
  136. #' library(ggplot2)
  137. #' ggplot(iris) +
  138. #' aes(Petal.Length, Petal.Width) +
  139. #' geom_point() +
  140. #' theme_xaringan_base(
  141. #' text_color = "#e1e5f2",
  142. #' background_color = "#021c25",
  143. #' accent_color = "#1f7a8c",
  144. #' set_ggplot_defaults = TRUE) +
  145. #' labs(title = "Basic Iris Plot",
  146. #' subtitle = "+ theme_xaringan_base()",
  147. #' caption = "{xaringanthemer}")
  148. #'
  149. #' ggplot(iris) +
  150. #' aes(Petal.Length, Petal.Width) +
  151. #' geom_point() +
  152. #' theme_xaringan_base(
  153. #' text_color = "#021c25",
  154. #' background_color = "#e1e5f2",
  155. #' accent_color = "#1f7a8c",
  156. #' set_ggplot_defaults = TRUE) +
  157. #' labs(title = "Basic Iris Plot",
  158. #' subtitle = "+ theme_xaringan_base()",
  159. #' caption = "{xaringanthemer}")
  160. #' }
  161. #'
  162. #' @return A ggplot2 theme
  163. #' @family xaringanthemer ggplot2 themes
  164. #' @export
  165. theme_xaringan_base <- function(
  166. text_color,
  167. background_color,
  168. ...,
  169. set_ggplot_defaults = TRUE,
  170. accent_color = NULL,
  171. accent_secondary_color = NULL,
  172. text_font = NULL,
  173. text_font_use_google = NULL,
  174. text_font_size = NULL,
  175. title_font = NULL,
  176. title_font_use_google = NULL,
  177. title_font_size = NULL
  178. ) {
  179. blend <- color_blender(text_color, background_color)
  180. text_font_size <- text_font_size %||% web_to_point(xaringanthemer_env$text_font_size, scale = 1.25) %||% 11
  181. title_font_size <- title_font_size %||% web_to_point(xaringanthemer_env$header_h3_font_size, scale = 0.8) %||% 14
  182. text_font <- if (!is.null(text_font)) {
  183. register_font(text_font, identical(text_font_use_google, TRUE))
  184. } else get_theme_font("text")
  185. title_font <- if (!is.null(title_font)) {
  186. register_font(title_font, identical(title_font_use_google, TRUE))
  187. } else get_theme_font("header")
  188. text_font %||% "sans"
  189. title_font %||% "sans"
  190. if (set_ggplot_defaults) {
  191. accent_color <- accent_color %||% xaringanthemer_env$header_color %||% text_color
  192. accent_secondary_color <- accent_secondary_color %||% xaringanthemer_env$text_bold_color %||% accent_color
  193. theme_xaringan_set_defaults(text_color, background_color, accent_color, accent_secondary_color)
  194. }
  195. ggplot2::theme(
  196. line = ggplot2::element_line(color = blend(0.2)),
  197. rect = ggplot2::element_rect(fill = background_color),
  198. text = ggplot2::element_text(
  199. color = blend(0.1),
  200. family = text_font,
  201. size = text_font_size),
  202. title = ggplot2::element_text(
  203. color = accent_color,
  204. family = title_font,
  205. size = title_font_size),
  206. plot.background = ggplot2::element_rect(
  207. fill = background_color,
  208. color = background_color),
  209. panel.background = ggplot2::element_rect(
  210. fill = background_color,
  211. color = background_color),
  212. panel.grid.major = ggplot2::element_line(
  213. color = blend(0.8),
  214. inherit.blank = TRUE),
  215. panel.grid.minor = ggplot2::element_line(
  216. color = blend(0.9),
  217. inherit.blank = TRUE),
  218. axis.title = ggplot2::element_text(size = title_font_size * 0.8),
  219. axis.ticks = ggplot2::element_line(color = blend(0.8)),
  220. axis.text = ggplot2::element_text(color = blend(0.4)),
  221. plot.caption = ggplot2::element_text(
  222. size = text_font_size * 0.8,
  223. color = blend(0.3))
  224. )
  225. }
  226. #' Set and Restore ggplot2 geom Defaults
  227. #'
  228. #' Set [ggplot2] _geom_ defaults to match [theme_xaringan()] with
  229. #' `theme_xaringan_set_defaults()` and restore the standard or previously-set
  230. #' defaults with `theme_xaringan_restore_defaults()`. By default,
  231. #' `theme_xaringan_set_defaults()` is run with [theme_xaringan()] or
  232. #' [theme_xaringan_inverse()].
  233. #'
  234. #' @family xaringanthemer ggplot2 themes
  235. #' @inheritParams theme_xaringan
  236. #' @return Invisibly returns a list of the current ggplot2 geom defaults
  237. #' @export
  238. theme_xaringan_set_defaults <- function(
  239. text_color = NULL,
  240. background_color = NULL,
  241. accent_color = text_color,
  242. accent_secondary_color = accent_color,
  243. text_family = NULL
  244. ) {
  245. requires_package("ggplot2")
  246. blend <- color_blender(text_color, background_color)
  247. xaringan_theme_defaults <- list(
  248. "line" = list(color = text_color),
  249. "vline" = list(color = accent_secondary_color),
  250. "hline" = list(color = accent_secondary_color),
  251. "abline" = list(color = accent_secondary_color),
  252. "segment" = list(color = text_color),
  253. "bar" = list(fill = accent_color),
  254. "col" = list(fill = accent_color),
  255. "boxplot" = list(color = text_color),
  256. "contour" = list(color = text_color),
  257. "density" = list(color = text_color,
  258. fill = text_color,
  259. alpha = 0.1),
  260. "dotplot" = list(color = accent_color),
  261. "errorbarh" = list(color = text_color),
  262. "crossbar" = list(color = text_color),
  263. "errorbar" = list(color = text_color),
  264. "linerange" = list(color = text_color),
  265. "pointrange" = list(color = text_color),
  266. "map" = list(color = text_color),
  267. "path" = list(color = text_color),
  268. "line" = list(color = text_color),
  269. "step" = list(color = text_color),
  270. "point" = list(color = accent_color),
  271. "polygon" = list(color = accent_color,
  272. fill = accent_color),
  273. "quantile" = list(color = text_color),
  274. "rug" = list(color = blend(0.5)),
  275. "segment" = list(color = text_color),
  276. "smooth" = list(fill = blend(0.75),
  277. color = accent_secondary_color),
  278. "spoke" = list(color = text_color),
  279. "label" = list(color = text_color,
  280. family = text_family %||% get_theme_font("text")),
  281. "text" = list(color = text_color,
  282. family = text_family %||% get_theme_font("text")),
  283. "rect" = list(fill = text_color),
  284. "tile" = list(fill = text_color),
  285. "violin" = list(fill = text_color),
  286. "sf" = list(color = text_color)
  287. )
  288. geom_names <- setNames(nm = names(xaringan_theme_defaults))
  289. previous_defaults <- lapply(
  290. geom_names,
  291. function(geom) safely_set_geom(geom, xaringan_theme_defaults[[geom]])
  292. )
  293. if (is.null(xaringanthemer_env$old_ggplot_defaults)) {
  294. xaringanthemer_env$old_ggplot_defaults <- previous_defaults
  295. }
  296. invisible(previous_defaults)
  297. }
  298. #' @describeIn theme_xaringan_set_defaults Restore previous or standard [ggplot2] _geom_ defaults.
  299. #' @inheritParams theme_xaringan
  300. #' @return Invisibly returns a list of the current ggplot2 geom defaults
  301. #' @export
  302. xaringan_theme_restore_defaults <- function() {
  303. requires_package("ggplot2")
  304. requires_xaringanthemer_env()
  305. if (is.null(xaringanthemer_env$old_ggplot_defaults)) return(invisible())
  306. old_default <- xaringanthemer_env$old_ggplot_defaults
  307. old_default_not_std <- vapply(old_default, function(x) length(x) > 0, logical(1))
  308. old_default <- old_default[old_default_not_std]
  309. restore_default <- utils::modifyList(xaringanthemer_env$std_ggplot_defaults, old_default)
  310. geom_names <- setNames(nm = names(restore_default))
  311. previous_defaults <- lapply(
  312. geom_names,
  313. function(geom) safely_set_geom(geom, restore_default[[geom]])
  314. )
  315. invisible(previous_defaults)
  316. }
  317. safely_set_geom <- function(geom, new) {
  318. tryCatch({
  319. ggplot2::update_geom_defaults(geom, new)
  320. },
  321. error = function(e) invisible(),
  322. warning = function(w) invisible())
  323. }
  324. # Color Scales ------------------------------------------------------------
  325. #' Xaringan Themer ggplot2 Scales
  326. #'
  327. #' Color and fill scales for discrete and continuous values, created using the
  328. #' primary accent color of the xaringanthemer styles.
  329. #'
  330. #' @param ... Arguments passed on to the appropriate scale function, one of
  331. #' [colorspace::scale_color_discrete_sequential],
  332. #' [colorspace::scale_color_continuous_sequential],
  333. #' [colorspace::scale_fill_discrete_sequential], or
  334. #' [colorspace::scale_fill_continuous_sequential].
  335. #' @param color A color value, in hex, to override the default color. Otherwise,
  336. #' the primary color of the resulting scale is chosen from the xaringanthemer
  337. #' slide styles.
  338. #' @param inverse If `color` is not supplied and `inverse = TRUE`, a primary
  339. #' color is chosen to work well with the inverse slide styles, namely the
  340. #' value of `inverse_header_color`
  341. #' @param direction Direction of the discrete scale. Use values less than 0 to
  342. #' reverse the direction, e.g. `direction = -1`.
  343. #' @inheritParams colorspace::scale_color_continuous_sequential
  344. #' @param aes_type The type of aesthetic to which the scale is being applied.
  345. #' One of "color", "colour", or "fill".
  346. #' @name scale_xaringan
  347. NULL
  348. #' @rdname scale_xaringan
  349. #' @export
  350. scale_xaringan_discrete <- function(
  351. aes_type = c("color", "colour", "fill"),
  352. ...,
  353. color = NULL,
  354. direction = 1,
  355. inverse = FALSE
  356. ) {
  357. aes_type <- match.arg(aes_type)
  358. color <- hex2HCL(get_theme_accent_color(color, inverse))
  359. if (direction >= 0) {
  360. if (aes_type %in% c("color", "colour")) {
  361. colorspace::scale_color_discrete_sequential(
  362. c1 = color[1, "C"], l1 = color[1, "L"], h1 = color[1, "H"], ...)
  363. } else {
  364. colorspace::scale_fill_discrete_sequential(
  365. c1 = color[1, "C"], l1 = color[1, "L"], h1 = color[1, "H"], ...)
  366. }
  367. } else {
  368. if (aes_type %in% c("color", "colour")) {
  369. colorspace::scale_color_discrete_sequential(
  370. c2 = color[1, "C"], l2 = color[1, "L"], h2 = color[1, "H"], ...)
  371. } else {
  372. colorspace::scale_fill_discrete_sequential(
  373. c2 = color[1, "C"], l2 = color[1, "L"], h2 = color[1, "H"], ...)
  374. }
  375. }
  376. }
  377. #' @rdname scale_xaringan
  378. #' @export
  379. scale_xaringan_fill_discrete <- function(
  380. ..., color = NULL, direction = 1, inverse = FALSE
  381. ) {
  382. scale_xaringan_discrete(
  383. "fill", ..., color = color, direction = direction, inverse = inverse
  384. )
  385. }
  386. #' @rdname scale_xaringan
  387. #' @export
  388. scale_xaringan_color_discrete <- function(
  389. ..., color = NULL, direction = 1, inverse = FALSE
  390. ) {
  391. scale_xaringan_discrete(
  392. "color", ..., color = color, direction = direction, inverse = inverse
  393. )
  394. }
  395. #' @rdname scale_xaringan
  396. #' @export
  397. scale_xaringan_colour_discrete <- scale_xaringan_color_discrete
  398. #' @rdname scale_xaringan
  399. #' @export
  400. scale_xaringan_continuous <- function(
  401. aes_type = c("color", "colour", "fill"),
  402. ..., color = NULL, begin = 0, end = 1, inverse = FALSE
  403. ) {
  404. aes_type <- match.arg(aes_type)
  405. color <- hex2HCL(get_theme_accent_color(color, inverse))
  406. scale_fn <- switch(
  407. aes_type,
  408. colour = , color = colorspace::scale_color_continuous_sequential,
  409. fill = colorspace::scale_fill_continuous_sequential
  410. )
  411. scale_fn(
  412. c1 = color[1, "C"],
  413. l1 = color[1, "L"],
  414. h1 = color[1, "H"],
  415. begin = begin, end = end,
  416. ...
  417. )
  418. }
  419. #' @rdname scale_xaringan
  420. #' @export
  421. scale_xaringan_fill_continuous <- function(
  422. ..., color = NULL, direction = 1, inverse = FALSE
  423. ) {
  424. scale_xaringan_continuous(
  425. "fill", ..., color = color, direction = direction, inverse = inverse
  426. )
  427. }
  428. #' @rdname scale_xaringan
  429. #' @export
  430. scale_xaringan_color_continuous <- function(
  431. ..., color = NULL, direction = 1, inverse = FALSE
  432. ) {
  433. scale_xaringan_continuous(
  434. "color", ..., color = color, direction = direction, inverse = inverse
  435. )
  436. }
  437. #' @rdname scale_xaringan
  438. #' @export
  439. scale_xaringan_colour_continuous <- scale_xaringan_color_continuous
  440. get_theme_accent_color <- function(color = NULL, inverse = FALSE) {
  441. color <-
  442. if (!inverse) {
  443. color %||%
  444. xaringanthemer_env[["header_color"]] %||%
  445. xaringanthemer_env[["text_color"]]
  446. } else {
  447. color %||% xaringanthemer_env[["inverse_header_color"]]
  448. }
  449. if (is.null(color)) {
  450. stop(
  451. call. = FALSE,
  452. "No color provided and no default available. ",
  453. "Have you forgotten to use a style function to set the xaringan theme?"
  454. )
  455. }
  456. color
  457. }
  458. blend_colors <- function(x, y, alpha = 0.5) {
  459. x <- colorspace::hex2RGB(x)
  460. y <- colorspace::hex2RGB(y)
  461. z <- colorspace::mixcolor(alpha, x, y)
  462. colorspace::hex(z)
  463. }
  464. color_blender <- function(x, y) function(alpha = 0.5) blend_colors(x, y, alpha)
  465. hex2HCL <- function(x) {
  466. colorspace::coords(as(colorspace::hex2RGB(x), "polarLUV"))
  467. }
  468. # Fonts -------------------------------------------------------------------
  469. get_theme_font <- function(element = c("text", "header", "code")) {
  470. element <- match.arg(element)
  471. element_family <- paste0(element, "_font_family")
  472. element_google <- paste0(element, "_font_google")
  473. element_url <- paste0(element, "_font_url")
  474. family <- xaringanthemer_env[[element_family]]
  475. is_google_font <- !is.null(xaringanthemer_env[[element_google]]) ||
  476. grepl("fonts.google", xaringanthemer_env[[element_url]], fixed = TRUE)
  477. register_font(family, google = is_google_font, fn = sys.calls()[[max(1, length(sys.calls()) - 1)]])
  478. }
  479. register_font <- function(
  480. family,
  481. google = TRUE,
  482. fn = sys.calls()[[max(1, length(sys.calls()) - 1)]][[1]],
  483. ...
  484. ) {
  485. if (is.null(family)) return(NULL)
  486. family <- gsub("['\"]", "", family)
  487. if (!identical(xaringanthemer_env$showtext_auto, TRUE)) {
  488. if (requires_package(pkg = "showtext", fn, required = FALSE)) {
  489. showtext::showtext_auto()
  490. } else return(family)
  491. xaringanthemer_env$showtext_auto <- TRUE
  492. }
  493. if (!requires_package(pkg = "sysfonts", fn, required = FALSE)) {
  494. return(family)
  495. } else if (!family %in% sysfonts::font_families()) {
  496. is_default_font <- family %in% c(
  497. "Roboto", "Source Code Pro", "Yanone Kaffeesatz"
  498. )
  499. if (identical(google, TRUE) || is_default_font) {
  500. tryCatch(
  501. sysfonts::font_add_google(family, ...),
  502. error = function(e) warning(e$message),
  503. warning = function(w) warning(w$message)
  504. )
  505. } else {
  506. warning(paste(
  507. "Please manually register fonts not served by Google Fonts.",
  508. "See `sysfonts::font_add()` for more information."))
  509. }
  510. }
  511. family
  512. }
  513. requires_package <- function(pkg = "ggplot2", fn = "", required = TRUE) {
  514. raise <- if (required) stop else warning
  515. if (!requireNamespace(pkg, quietly = TRUE)) {
  516. msg <- paste0(
  517. "`", pkg, "` is ",
  518. if (required) "required " else "suggested ",
  519. if (fn != "") paste0("by ", fn, "() ")[1],
  520. "but is not installed."
  521. )
  522. raise(msg, call. = FALSE)
  523. invisible(FALSE)
  524. }
  525. invisible(TRUE)
  526. }
  527. requires_xaringanthemer_env <- function() {
  528. if (!exists("xaringanthemer_env") || is.null(xaringanthemer_env$header_color)) {
  529. stop("Please call a xaringanthemer theme function first.")
  530. }
  531. }
  532. #' Get the Value of xaringanthemer Style Setting
  533. #'
  534. #' A helper function to retrieve the value of style settings as set by a
  535. #' xaringanthemer style function, for use in plotting and other circumstances.
  536. #'
  537. #' @section Style Settings:
  538. #' Style settings used by xaringanthemer include:
  539. #'
  540. #' - `background_color`
  541. #' - `background_image`
  542. #' - `background_position`
  543. #' - `background_size`
  544. #' - `blockquote_left_border_color`
  545. #' - `code_font_family`
  546. #' - `code_font_family_fallback`
  547. #' - `code_font_google`
  548. #' - `code_font_size`
  549. #' - `code_font_url`
  550. #' - `code_highlight_color`
  551. #' - `code_inline_background_color`
  552. #' - `code_inline_color`
  553. #' - `code_inline_font_size`
  554. #' - `extra_css`
  555. #' - `extra_fonts`
  556. #' - `footnote_color`
  557. #' - `footnote_font_size`
  558. #' - `footnote_position_bottom`
  559. #' - `header_background_auto`
  560. #' - `header_background_color`
  561. #' - `header_background_content_padding_top`
  562. #' - `header_background_ignore_classes`
  563. #' - `header_background_padding`
  564. #' - `header_background_text_color`
  565. #' - `header_color`
  566. #' - `header_font_family`
  567. #' - `header_font_google`
  568. #' - `header_font_url`
  569. #' - `header_font_weight`
  570. #' - `header_h1_font_size`
  571. #' - `header_h2_font_size`
  572. #' - `header_h3_font_size`
  573. #' - `inverse_background_color`
  574. #' - `inverse_header_color`
  575. #' - `inverse_text_color`
  576. #' - `inverse_text_shadow`
  577. #' - `left_column_selected_color`
  578. #' - `left_column_subtle_color`
  579. #' - `link_color`
  580. #' - `padding`
  581. #' - `table_border_color`
  582. #' - `table_row_border_color`
  583. #' - `table_row_even_background_color`
  584. #' - `text_bold_color`
  585. #' - `text_color`
  586. #' - `text_font_base`
  587. #' - `text_font_family`
  588. #' - `text_font_family_fallback`
  589. #' - `text_font_google`
  590. #' - `text_font_size`
  591. #' - `text_font_url`
  592. #' - `text_font_weight`
  593. #' - `text_slide_number_color`
  594. #' - `text_slide_number_font_size`
  595. #' - `title_slide_background_color`
  596. #' - `title_slide_background_image`
  597. #' - `title_slide_background_position`
  598. #' - `title_slide_background_size`
  599. #' - `title_slide_text_color`
  600. #'
  601. #' @param setting A xaringanthemer style setting
  602. #' @export
  603. theme_xaringan_get_value <- function(setting) {
  604. requires_xaringanthemer_env()
  605. if (length(setting) > 1) {
  606. xaringanthemer_env[setting]
  607. } else {
  608. xaringanthemer_env[[setting]]
  609. }
  610. }
  611. web_to_point <- function(x, px_per_em = 16, scale = 1) {
  612. if (is.null(x)) return(NULL)
  613. if (grepl("pt$", x)) {
  614. return(as.numeric(sub("pt$", "", x)))
  615. } else if (grepl("px$", x)) {
  616. x <- as.numeric(sub("px$", "", x))
  617. return(x * 0.75)
  618. } else if (grepl("em$", x)) {
  619. x <- as.numeric(sub("em$", "", x))
  620. return(x * px_per_em * 0.75)
  621. } else {
  622. return()
  623. }
  624. }