Skip to content

Commit

Permalink
Merge pull request #48 from nanxstats/tw3
Browse files Browse the repository at this point in the history
Add Tailwind CSS v3 color scales
  • Loading branch information
nanxstats authored Jun 17, 2024
2 parents 0d4a833 + d410ea7 commit 749c4a9
Show file tree
Hide file tree
Showing 11 changed files with 650 additions and 34 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ export(pal_rickandmorty)
export(pal_simpsons)
export(pal_startrek)
export(pal_tron)
export(pal_tw3)
export(pal_uchicago)
export(pal_ucscgb)
export(rgb_bs5)
export(rgb_gsea)
export(rgb_material)
export(rgb_tw3)
export(scale_color_aaas)
export(scale_color_bmj)
export(scale_color_bs5)
Expand All @@ -49,6 +51,7 @@ export(scale_color_rickandmorty)
export(scale_color_simpsons)
export(scale_color_startrek)
export(scale_color_tron)
export(scale_color_tw3)
export(scale_color_uchicago)
export(scale_color_ucscgb)
export(scale_colour_aaas)
Expand All @@ -73,6 +76,7 @@ export(scale_colour_rickandmorty)
export(scale_colour_simpsons)
export(scale_colour_startrek)
export(scale_colour_tron)
export(scale_colour_tw3)
export(scale_colour_uchicago)
export(scale_colour_ucscgb)
export(scale_fill_aaas)
Expand All @@ -97,6 +101,7 @@ export(scale_fill_rickandmorty)
export(scale_fill_simpsons)
export(scale_fill_startrek)
export(scale_fill_tron)
export(scale_fill_tw3)
export(scale_fill_uchicago)
export(scale_fill_ucscgb)
importFrom(ggplot2,discrete_scale)
Expand Down
168 changes: 168 additions & 0 deletions R/continuous-tw3.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#' Tailwind CSS color palettes
#'
#' Tailwind CSS color palettes.
#'
#' @param palette Palette type. There are 22 available options:
#' - `"slate"`
#' - `"gray"`
#' - `"zinc"`
#' - `"neutral"`
#' - `"stone"`
#' - `"red"`
#' - `"orange"`
#' - `"amber"`
#' - `"yellow"`
#' - `"lime"`
#' - `"green"`
#' - `"emerald"`
#' - `"teal"`
#' - `"cyan"`
#' - `"sky"`
#' - `"blue"`
#' - `"indigo"`
#' - `"violet"`
#' - `"purple"`
#' - `"fuchsia"`
#' - `"pink"`
#' - `"rose"`
#' @param n Number of individual colors to be generated.
#' @param alpha Transparency level, a real number in (0, 1].
#' See `alpha` in [grDevices::rgb()] for details.
#' @param reverse Logical. Should the order of the colors be reversed?
#'
#' @export rgb_tw3
#'
#' @importFrom grDevices colorRamp rgb
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @references
#' <https://tailwindcss.com/docs/customizing-colors>
#'
#' @examples
#' library("scales")
#' show_col(pal_tw3("rose")(10))
#' show_col(pal_tw3("rose", n = 30, alpha = 0.6, reverse = TRUE)(30))
rgb_tw3 <- function(
palette = c(
"slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber",
"yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue",
"indigo", "violet", "purple", "fuchsia", "pink", "rose"
), n = 10, alpha = 1, reverse = FALSE) {
palette <- match.arg(palette)

if (alpha > 1L || alpha <= 0L) stop("alpha must be in (0, 1]")

raw_cols <- ggsci_db$"tw3"[[palette]]
func_cols <- colorRamp(raw_cols, space = "Lab", interpolate = "spline")
mat_cols <- func_cols(seq(0L, 1L, length.out = n))
alpha_cols <- rgb(
mat_cols[, 1L], mat_cols[, 2L], mat_cols[, 3L],
alpha = alpha * 255L, maxColorValue = 255L
)

if (reverse) alpha_cols <- rev(alpha_cols)

alpha_cols
}

#' Tailwind CSS color palettes
#'
#' Tailwind CSS color palettes.
#'
#' @inheritParams rgb_tw3
#'
#' @export pal_tw3
#'
#' @importFrom scales manual_pal
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @examples
#' library("scales")
#' show_col(pal_tw3("rose")(10))
#' show_col(pal_tw3("rose", n = 30, alpha = 0.6, reverse = TRUE)(30))
pal_tw3 <- function(
palette = c(
"slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber",
"yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue",
"indigo", "violet", "purple", "fuchsia", "pink", "rose"
), n = 10, alpha = 1, reverse = FALSE) {
palette <- match.arg(palette)

alpha_cols <- rgb_tw3(palette, n, alpha, reverse)
manual_pal(unname(alpha_cols))
}

#' Tailwind CSS color scales
#'
#' See [pal_tw3()] for details.
#'
#' @inheritParams pal_tw3
#' @param ... Additional parameters for [ggplot2::discrete_scale()].
#'
#' @export scale_color_tw3
#'
#' @importFrom ggplot2 scale_color_gradientn
#'
#' @author Nan Xiao | \email{[email protected]} | <https://nanx.me>
#'
#' @rdname scale_tw3
#'
#' @examples
#' library("ggplot2")
#'
#' data("mtcars")
#' cor <- abs(cor(mtcars))
#' cor_melt <- data.frame(
#' Var1 = rep(seq_len(nrow(cor)), times = ncol(cor)),
#' Var2 = rep(seq_len(ncol(cor)), each = nrow(cor)),
#' value = as.vector(cor)
#' )
#'
#' ggplot(
#' cor_melt,
#' aes(x = Var1, y = Var2, fill = value)
#' ) +
#' geom_tile(colour = "black", size = 0.3) +
#' theme_bw() +
#' scale_fill_tw3("slate")
scale_color_tw3 <- function(
palette = c(
"slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber",
"yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue",
"indigo", "violet", "purple", "fuchsia", "pink", "rose"
), alpha = 1, reverse = FALSE, ...) {
palette <- match.arg(palette)
scale_color_gradientn(
colours = rgb_tw3(
palette,
n = 512, alpha = alpha, reverse = reverse
),
...
)
}

#' @export scale_colour_tw3
#' @rdname scale_tw3
scale_colour_tw3 <- scale_color_tw3

#' @export scale_fill_tw3
#' @importFrom ggplot2 scale_fill_gradientn
#' @rdname scale_tw3
scale_fill_tw3 <- function(
palette = c(
"slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber",
"yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue",
"indigo", "violet", "purple", "fuchsia", "pink", "rose"
), alpha = 1, reverse = FALSE, ...) {
palette <- match.arg(palette)
scale_fill_gradientn(
colours = rgb_tw3(
palette,
n = 512, alpha = alpha, reverse = reverse
),
...
)
}
Loading

0 comments on commit 749c4a9

Please sign in to comment.