R/get-format.R

#' Get color format
#'
#' Get the name of color format, Hex, Rgb, Hsl, Hsv, Name, and so on.
#'
#' @param x a any type color string or a javascript color object specifying.
#' @param na.rm a logical value indicating whether NA values should be stripped before the computation proceeds.
#'
#' @return Returns a string vector of the color format name.
#' @export
#' @examples
#' get_format("#F3A212") # "HEX"
#' get_format("#F3A21222") # “HEX8”
#' get_format("{ r: 1, g: 0, b: 0 }") # "RGB"
#' get_format("{ h: 0.5, s: 0.5, v: 0.5 }") # "HSL"
#' get_format("{ h: 0.5, s: 0.5, v: 0.5 }") # "HSV"
#' get_format('red') # "NAME"
get_format <- function(x, na.rm = FALSE) {
  if(all(is.na(x))) return(NA_character_)
  if(na.rm) x <- x[! is.na(x)]
  cmd <- ifelse(is_js_object(x),
                paste0("tinycolor(", x, ").getFormat()"),
                paste0("tinycolor(\"", x, "\").getFormat()"))
  out <- ifelse(is_valid_color(x), v8_eval(cmd), NA)
  return(out)
}
houyunhuang/tinycolor documentation built on June 6, 2019, 7:43 p.m.