|
| 1 | +#' Tailwind CSS color palettes |
| 2 | +#' |
| 3 | +#' Tailwind CSS color palettes. |
| 4 | +#' |
| 5 | +#' @param palette Palette type. There are 22 available options: |
| 6 | +#' - `"slate"` |
| 7 | +#' - `"gray"` |
| 8 | +#' - `"zinc"` |
| 9 | +#' - `"neutral"` |
| 10 | +#' - `"stone"` |
| 11 | +#' - `"red"` |
| 12 | +#' - `"orange"` |
| 13 | +#' - `"amber"` |
| 14 | +#' - `"yellow"` |
| 15 | +#' - `"lime"` |
| 16 | +#' - `"green"` |
| 17 | +#' - `"emerald"` |
| 18 | +#' - `"teal"` |
| 19 | +#' - `"cyan"` |
| 20 | +#' - `"sky"` |
| 21 | +#' - `"blue"` |
| 22 | +#' - `"indigo"` |
| 23 | +#' - `"violet"` |
| 24 | +#' - `"purple"` |
| 25 | +#' - `"fuchsia"` |
| 26 | +#' - `"pink"` |
| 27 | +#' - `"rose"` |
| 28 | +#' @param n Number of individual colors to be generated. |
| 29 | +#' @param alpha Transparency level, a real number in (0, 1]. |
| 30 | +#' See `alpha` in [grDevices::rgb()] for details. |
| 31 | +#' @param reverse Logical. Should the order of the colors be reversed? |
| 32 | +#' |
| 33 | +#' @export rgb_tw3 |
| 34 | +#' |
| 35 | +#' @importFrom grDevices colorRamp rgb |
| 36 | +#' @importFrom scales manual_pal |
| 37 | +#' |
| 38 | +#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me> |
| 39 | +#' |
| 40 | +#' @references |
| 41 | +#' <https://tailwindcss.com/docs/customizing-colors> |
| 42 | +#' |
| 43 | +#' @examples |
| 44 | +#' library("scales") |
| 45 | +#' show_col(pal_tw3("rose")(10)) |
| 46 | +#' show_col(pal_tw3("rose", n = 30, alpha = 0.6, reverse = TRUE)(30)) |
| 47 | +rgb_tw3 <- function( |
| 48 | + palette = c( |
| 49 | + "slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", |
| 50 | + "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", |
| 51 | + "indigo", "violet", "purple", "fuchsia", "pink", "rose" |
| 52 | + ), n = 10, alpha = 1, reverse = FALSE) { |
| 53 | + palette <- match.arg(palette) |
| 54 | + |
| 55 | + if (alpha > 1L || alpha <= 0L) stop("alpha must be in (0, 1]") |
| 56 | + |
| 57 | + raw_cols <- ggsci_db$"tw3"[[palette]] |
| 58 | + func_cols <- colorRamp(raw_cols, space = "Lab", interpolate = "spline") |
| 59 | + mat_cols <- func_cols(seq(0L, 1L, length.out = n)) |
| 60 | + alpha_cols <- rgb( |
| 61 | + mat_cols[, 1L], mat_cols[, 2L], mat_cols[, 3L], |
| 62 | + alpha = alpha * 255L, maxColorValue = 255L |
| 63 | + ) |
| 64 | + |
| 65 | + if (reverse) alpha_cols <- rev(alpha_cols) |
| 66 | + |
| 67 | + alpha_cols |
| 68 | +} |
| 69 | + |
| 70 | +#' Tailwind CSS color palettes |
| 71 | +#' |
| 72 | +#' Tailwind CSS color palettes. |
| 73 | +#' |
| 74 | +#' @inheritParams rgb_tw3 |
| 75 | +#' |
| 76 | +#' @export pal_tw3 |
| 77 | +#' |
| 78 | +#' @importFrom scales manual_pal |
| 79 | +#' |
| 80 | +#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me> |
| 81 | +#' |
| 82 | +#' @examples |
| 83 | +#' library("scales") |
| 84 | +#' show_col(pal_tw3("rose")(10)) |
| 85 | +#' show_col(pal_tw3("rose", n = 30, alpha = 0.6, reverse = TRUE)(30)) |
| 86 | +pal_tw3 <- function( |
| 87 | + palette = c( |
| 88 | + "slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", |
| 89 | + "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", |
| 90 | + "indigo", "violet", "purple", "fuchsia", "pink", "rose" |
| 91 | + ), n = 10, alpha = 1, reverse = FALSE) { |
| 92 | + palette <- match.arg(palette) |
| 93 | + |
| 94 | + alpha_cols <- rgb_tw3(palette, n, alpha, reverse) |
| 95 | + manual_pal(unname(alpha_cols)) |
| 96 | +} |
| 97 | + |
| 98 | +#' Tailwind CSS color scales |
| 99 | +#' |
| 100 | +#' See [pal_tw3()] for details. |
| 101 | +#' |
| 102 | +#' @inheritParams pal_tw3 |
| 103 | +#' @param ... Additional parameters for [ggplot2::discrete_scale()]. |
| 104 | +#' |
| 105 | +#' @export scale_color_tw3 |
| 106 | +#' |
| 107 | +#' @importFrom ggplot2 scale_color_gradientn |
| 108 | +#' |
| 109 | +#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me> |
| 110 | +#' |
| 111 | +#' @rdname scale_tw3 |
| 112 | +#' |
| 113 | +#' @examples |
| 114 | +#' library("ggplot2") |
| 115 | +#' |
| 116 | +#' data("mtcars") |
| 117 | +#' cor <- abs(cor(mtcars)) |
| 118 | +#' cor_melt <- data.frame( |
| 119 | +#' Var1 = rep(seq_len(nrow(cor)), times = ncol(cor)), |
| 120 | +#' Var2 = rep(seq_len(ncol(cor)), each = nrow(cor)), |
| 121 | +#' value = as.vector(cor) |
| 122 | +#' ) |
| 123 | +#' |
| 124 | +#' ggplot( |
| 125 | +#' cor_melt, |
| 126 | +#' aes(x = Var1, y = Var2, fill = value) |
| 127 | +#' ) + |
| 128 | +#' geom_tile(colour = "black", size = 0.3) + |
| 129 | +#' theme_bw() + |
| 130 | +#' scale_fill_tw3("slate") |
| 131 | +scale_color_tw3 <- function( |
| 132 | + palette = c( |
| 133 | + "slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", |
| 134 | + "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", |
| 135 | + "indigo", "violet", "purple", "fuchsia", "pink", "rose" |
| 136 | + ), alpha = 1, reverse = FALSE, ...) { |
| 137 | + palette <- match.arg(palette) |
| 138 | + scale_color_gradientn( |
| 139 | + colours = rgb_tw3( |
| 140 | + palette, |
| 141 | + n = 512, alpha = alpha, reverse = reverse |
| 142 | + ), |
| 143 | + ... |
| 144 | + ) |
| 145 | +} |
| 146 | + |
| 147 | +#' @export scale_colour_tw3 |
| 148 | +#' @rdname scale_tw3 |
| 149 | +scale_colour_tw3 <- scale_color_tw3 |
| 150 | + |
| 151 | +#' @export scale_fill_tw3 |
| 152 | +#' @importFrom ggplot2 scale_fill_gradientn |
| 153 | +#' @rdname scale_tw3 |
| 154 | +scale_fill_tw3 <- function( |
| 155 | + palette = c( |
| 156 | + "slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", |
| 157 | + "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", |
| 158 | + "indigo", "violet", "purple", "fuchsia", "pink", "rose" |
| 159 | + ), alpha = 1, reverse = FALSE, ...) { |
| 160 | + palette <- match.arg(palette) |
| 161 | + scale_fill_gradientn( |
| 162 | + colours = rgb_tw3( |
| 163 | + palette, |
| 164 | + n = 512, alpha = alpha, reverse = reverse |
| 165 | + ), |
| 166 | + ... |
| 167 | + ) |
| 168 | +} |
0 commit comments