#' Get color's luminance
#'
#' Get the luminance 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 perceived luminance of a color, from 0-1 as defined by \href{https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef}{Web Content Accessibility Guidelines (Version 2.0)}.
#' @export
#' @examples
#' get_luminance("{ r: 80, g: 45, b: 20 }")
#' get_luminance("blue")
#' get_luminance("HSL(0.2, 0.9, 0.1)")
get_luminance <- function(x, na.rm = FALSE) {
if(na.rm) x <- x[! is.na(x)]
if(all(is.na(x))) return(NA_real_)
cmd <- ifelse(is_js_object(x),
paste0("tinycolor(", x, ").getLuminance()"),
paste0("tinycolor(\"", x, "\").getLuminance()"))
out <- as.numeric(ifelse(is_valid_color(x), v8_eval(cmd), NA))
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.