R/custom_color_pallete.R

Defines functions clrs

Documented in clrs

#' Custom color palette
#'
#' @param x A numeric, character or \code{NULL}. If numeric a color of a given index is returned.
#' If character a color of a given name is returned. If \code{NULL} a vector of all colors in the pallete is returned.
#' @return A named vector of colors
#' @references Two of the colors (blue and green) copied from Doug Bolton's code.
#' @export

clrs <- function(x = NULL) {
  list_of_colors <- c(blue = "#2c7fb8",
                      green = "#31a354",
                      orange = "#fdae61")

  if(is.null(x)) {output = list_of_colors}

  if(is.numeric(x)) {
    #check input
    if (length(x)==1) {
      if (x > length(list_of_colors)) stop("Color does not exist")
    } else {
      if (length(x) > length(list_of_colors)) stop("Too many colors chosen")
    }
    output <- list_of_colors[x]
  }

  if(is.character(x)) {
    #check input
    if (length(x) > length(list_of_colors)) stop("Too many colors chosen")
    #check if color name exist in the list of colors
    if (!(x %in% names(list_of_colors))) stop("Color does not exist in pallete")

    output <- list_of_colors[x]
  }

  return(output)
}
ptompalski/UsefulRFunctions documentation built on May 26, 2019, 11:32 a.m.