Last active
December 20, 2018 14:31
-
-
Save mages/5339689 to your computer and use it in GitHub Desktop.
How to change the alpha value of colours in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Add an alpha value to a colour | |
add.alpha <- function(col, alpha=1){ | |
if(missing(col)) | |
stop("Please provide a vector of colours.") | |
apply(sapply(col, col2rgb)/255, 2, | |
function(x) | |
rgb(x[1], x[2], x[3], alpha=alpha)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rgb
andcol2rgb
are both vectorized. Perhaps:add.alpha <- function(cols, alpha) rgb(t(col2rgb(cols)/255), alpha = alpha)