R/color-contrast-ratio.R

#' Get colors' contrast ratio
#'
#' Functions to get contrast ratio between two colors.
#'
#' @param ... colors string, the length of `...` must be 2.
#'
#' @return Return the contrast ratio of two colors. For the same colors, return 1. The value increases with the increase of contrast ratio.
#' @export
#' @examples
#' col_contrast_ratio("red", "white")
#' col_contrast_ratio("red", "#AB12CD")

col_contrast_ratio <- function(...) {
  col <- unname(unlist(list(...)))
  if(length(col) != 2)
    stop("The number of colors must be 2", call. = FALSE)
  if(any(is.na(col))) return(NA_real_)
  cmd <- paste0("tinycolor.readability(\"", col[1], "\",\"", col[2], "\")")
  out <- as.numeric(v8_eval(cmd))
  return(out)
}
houyunhuang/tinycolor documentation built on June 6, 2019, 7:43 p.m.