#' Check two colors whether equal
#'
#' Functions to check whether two colors are equal.
#'
#' @param ... color strings, the length of `...` must be 2.
#'
#' @return Return a boolean indicating whether the two colors are equal.
#' @export
#' @examples
#' is_equals("red", "blue")
#' is_equals("red", "#F00")
#' col_mix2("red", "green")
#' col_mixn("red", "green", "blue")
#' col_random(3)
is_equals <- 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)
col <- ifelse(is_js_object(col),
paste0("tinycolor(", col, ")"),
paste0("tinycolor(\"", col, "\")"))
cmd <- paste0("tinycolor.equals(", col[1], ",", col[2], ")")
out <- ifelse(v8_eval(cmd) == "TRUE", TRUE, FALSE)
return(out)
}
#' Mix colors
#'
#' Mix two colors in a given proportion.
#'
#' @param ... color strings, the length of `...` must be 2.
#' @param amount proportion of the first color, from 0 to 100.
#'
#' @return Return mixed color.
#' @export
col_mix2 <- function(..., amount = 50) {
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)
col <- ifelse(is_js_object(col),
paste0("tinycolor(", col, ")"),
paste0("tinycolor(\"", col, "\")"))
cmd <- paste0("tinycolor.mix(", col[1], ",", col[2], ",", "amount = ", amount, ")")
out <- v8_eval(cmd)
return(to_hex_string(out))
}
#' Random color generation
#'
#' Randomly generate of a given number of colors.
#'
#' @param n the number of colors.
#'
#' @return Return hex color.
#' @export
col_random <- function(n = 1L) {
if(length(n) != 1)
stop("`n` must be length of 1.", call. = FALSE)
if(is.na(n)) return(NA)
if(!is.integer(n)) n <- as.integer(n)
cmd <- "tinycolor.random().toHexString()"
out <- replicate(n, v8_eval(cmd))
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.