R/get-brightness.R

#' Get color's brightness
#'
#' Get the brightness of a color vector.
#'
#' @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 the brightness of the color, values in the range of 0 to 255.
#' @export
#' @examples
#' get_brightness("#F00")
#' get_brightness("{ r: 1, g: 0, b: 0 }")
#' get_brightness("{ h: 0.5, s: 0.5, v: 0.5 }")
get_brightness <- function(x, na.rm = FALSE) {
  if(na.rm) x <- x[! is.na(x)]
  cmd <- ifelse(is_js_object(x),
                paste0("tinycolor(", x, ").getBrightness()"),
                paste0("tinycolor(\"", x, "\").getBrightness()"))
  out <- as.numeric(v8_eval(cmd))
  return(out)
}
houyunhuang/tinycolor documentation built on June 6, 2019, 7:43 p.m.