#' Check the color
#'
#' Check whether a vector element is a valid color.
#'
#' @param x a any type color string or a javascript color object specifying.
#' @return Return a logic vector indicating whether the color is valid.
#' @export
#' @examples
#' is_valid_color("blue")
#' x <- "{ h: 0.5, s: 0.5, l: 0.5 }"
#' is_valid_color(x)
#' y <- c("{ r: 1, g: 0, b: 0 }", "{ h: 0.5, s: 0.5, v: 0.5 }")
#' is_valid_color(y)
#' is_valid_color(c(NA, "red"))
#' is_valid_color("not a color")
is_valid_color <- function(x) {
cmd <- ifelse(is_js_object(x),
paste0("tinycolor(", x, ").isValid()"),
paste0("tinycolor(\"", x, "\").isValid()"))
out <- v8_eval(cmd)
out <- ifelse(out == "TRUE", TRUE, FALSE)
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.