-
Notifications
You must be signed in to change notification settings - Fork 8
/
hex_color.R
28 lines (24 loc) · 843 Bytes
/
hex_color.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#' Create color in hexadecimal
#'
#' @description Create hexadecimal type color name with transparency
#'
#' @param color a hexadecimal or named color e.g. '#1874CD' or 'dodgerblue3'
#' @param alpha transparency factor. Numeric value between 0 and 1
#'
#' @examples
#' \dontrun{
#' color <- hex_color(color = 'dodgerblue3', alpha = 0.5)
#' }
#' @export
hex_color <- function(color = NULL, alpha = 1) {
if (!all(is.numeric(alpha) & alpha >= 0 & alpha <= 1)) {
stop('Argument \"alpha\" must be a number between 0 and 1')
}
color <- grDevices::col2rgb(col = color)
color <- grDevices::rgb(red = color[1, ],
green = color[2, ],
blue = color[3, ],
alpha = 255 * alpha,
maxColorValue = 255)
return(color)
} # End hex_colors