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

112 lines
3.8KB

  1. #' @title Generate lighter or darker version of a color
  2. #' @description Produces a linear blend of the color with white or black.
  3. #' @param color_hex A character string representing a hex color
  4. #' @param strength The "strength" of the blend with white or black,
  5. #' where 0 is entirely the original color and 1 is entirely white
  6. #' (`lighten_color()`) or black (`darken_color()`).
  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 (hex)
  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 = "#000000", white = "#FFFFFF") {
  54. if (is_light_color(x)) eval(black) else eval(white)
  55. }
  56. is_light_color <- function(x) {
  57. # this function returns TRUE if the given color
  58. # is light-colored and requires dark text
  59. color_rgb <- col2rgb(x)[, 1]
  60. # from https://stackoverflow.com/a/3943023/2022615
  61. color_rgb <- color_rgb / 255
  62. color_rgb[color_rgb <= 0.03928] <- color_rgb[color_rgb <= 0.03928] / 12.92
  63. color_rgb[color_rgb > 0.03928] <- ((color_rgb[color_rgb > 0.03928] + 0.055) / 1.055)^2.4
  64. lum <- t(c(0.2126, 0.7152, 0.0722)) %*% color_rgb
  65. lum[1, 1] > 0.179
  66. }
  67. prepare_colors <- function(colors = NULL) {
  68. if (is.null(colors) || length(colors) < 1) return(NULL)
  69. if (is.null(names(colors))) {
  70. stop(
  71. "`colors` must have names corresponding to valid CSS classes",
  72. call. = FALSE
  73. )
  74. }
  75. if (any(grepl("\\s", names(colors)))) {
  76. stop(
  77. "Color names in `colors` must be valid CSS classes",
  78. " and cannot contain spaces",
  79. call. = FALSE)
  80. }
  81. if (any(grepl("[^[:alpha:]_-]", names(colors)))) {
  82. stop("Color names in `colors` must be valid CSS classes", call. = FALSE)
  83. }
  84. whisker::iteratelist(colors, "color_name")
  85. }
  86. full_length_hex <- function(x) {
  87. if (!grepl("^#", x) || grepl("[^#0-9a-fA-F]", x)) {
  88. stop(paste0('"', x, '" is not a hexadecimal color'))
  89. }
  90. x <- sub("^#", "", x)
  91. if (nchar(x) == 3) {
  92. x <- strsplit(x, character(0))[[1]]
  93. x <- rep(x, each = 2)
  94. x <- paste(x, collapse = "")
  95. } else if (nchar(x) != 6) {
  96. stop(paste0('"', x, '" is not a hexadecimal color'))
  97. }
  98. paste0("#", x)
  99. }