File: colors.R

package info (click to toggle)
r-cran-pkgmaker 0.32.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,192 kB
  • sloc: sh: 13; makefile: 2
file content (35 lines) | stat: -rw-r--r-- 896 bytes parent folder | download | duplicates (2)
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
29
30
31
32
33
34
35
# Color utilities
# 
# Author: Renaud Gaujoux
# Created: 30 Nov 2012
###############################################################################

#' Colour utilities
#' 
#' \code{alphacol} adds an alpha value to a colour specification and convert to 
#' a hexadecimal colour string.
#'  
#' @inheritParams grDevices::col2rgb
#' 
#' @return a character vector of color specifications.
#' @export
#' @examples
#' 
#' # Alphas
#' alphacol('red') # do nothing
#' alphacol('red', 10)
#' alphacol('#aabbcc', 5)
#' alphacol(4, 5)
#' 
alphacol <- function(col, alpha = FALSE){
    col <- as.hexmode(col2rgb(col, alpha))
    storage.mode(col) <- "character"
    if( !is.logical(alpha) ){
        if( alpha < 1 ) alpha <- alpha * 255
        alpha <- round(alpha)
        col['alpha', ] <- as.character(as.hexmode(alpha))
    }
	apply(col, 2, function(x) paste("#", paste(x, collapse=''), sep=''))
}