R/pal-sage-gradient.R

Defines functions sage_gradient_pal

Documented in sage_gradient_pal

#' Sage gradient palette
#'
#' Used for both continuous and binned scales, this function creates a gradient
#' of colors from the given high and low hex codes (or, by default, from the
#' "200" to "800" values for a chosen color option).
#'
#' @param option One of the Sage colors. See `names(sage_colors)` for a list.
#' @param low Optional starting value (if `NULL`, will default to the `"200"`
#'   value for the chosen color option)
#' @param high Optional ending value (if `NULL`, will default to the `"800"`
#'   value for the chosen color option)
#' @return A function that can be called on values between 0 and 1 to return the
#'   hex color of the gradient at that point in the range.
#' @export
#' @examples
#' sage_gradient_pal()(1)
#' sage_gradient_pal()(0.5)
#' sage_gradient_pal(
#'   option = "powder",
#'   low = sage_colors[["powder"]][["300"]],
#'   high = sage_colors[["powder"]][["700"]]
#' )(1)
sage_gradient_pal <- function(option = "royal", low = NULL, high = NULL) {
  if (!option %in% names(sage_colors)) {
    stop(
      paste(
        "`option` must be one of",
        paste(names(sage_colors), collapse = ", ")
      ),
      call. = FALSE
    )
  }
  palette <- sage_colors[[option]]
  if (is.null(low)) {
    low <- palette[["200"]]
  }
  if (is.null(high)) {
    high <- palette[["800"]]
  }
  scales::seq_gradient_pal(low, high, space = "Lab")
}
Sage-Bionetworks/sagethemes documentation built on Dec. 27, 2021, 2:09 p.m.