#' Add transparency to a color
#'
#' @param color input color
#' @param trans degree of transparency - a numeric values between 0 and 255 where 255 is fully visible)
#' @importFrom grDevices col2rgb colorRampPalette
#' @details This function adds transparency to a color. It is originally by Tim Essington. Define transparency with an integer between 0 and 255, 0 being fully transparent and 255 being fully visible. Works with either color and trans a vector of equal length, or one of the two of length 1.
#' @return a hex code for the transparent version of \code{color}
#'
#' @examples
#' x <- add_trans("red", 100)
#' print(x)
#' @export
add_trans <- function(color, trans) {
if (length(color) != length(trans) & !any(c(length(color), length(trans)) == 1)) stop("Vector lengths not correct")
if (length(color) == 1 & length(trans) > 1) color <- rep(color, length(trans))
if (length(trans) == 1 & length(color) > 1) trans <- rep(trans, length(color))
num2hex <- function(x) {
hex <- unlist(strsplit("0123456789ABCDEF", split = ""))
return(paste(hex[(x - x %% 16) / 16 + 1], hex[x %% 16 + 1], sep = ""))
}
rgb <- rbind(col2rgb(color), trans)
res <- paste("#", apply(apply(rgb, 2, num2hex), 2, paste, collapse = ""), sep = "")
return(res)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.