File: alphablend.R

package info (click to toggle)
r-cran-vim 6.2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,556 kB
  • sloc: cpp: 141; sh: 12; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,491 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
36
37
38
39
40
41
42
43
44
45
# ---------------------------------------
# Author: Andreas Alfons
#         Vienna University of Technology
# ---------------------------------------



#' Alphablending for colors
#' 
#' Convert colors to semitransparent colors.
#' 
#' 
#' @param col a vector specifying colors.
#' @param alpha a numeric vector containing the alpha values (between 0 and 1).
#' @param bg the background color to be used for alphablending.  This can be
#' used as a workaround for graphics devices that do not support
#' semitransparent colors.
#' @return a vector containing the semitransparent colors.
#' @author Andreas Alfons
#' @keywords color
#' @examples
#' 
#' alphablend("red", 0.6)
#' 
#' @export alphablend
alphablend <- function(col, alpha = NULL, bg = NULL) {
    if(length(alpha) == 0) col
    else {
        alpha <- rep(alpha, length.out=length(col))
        if(length(bg) == 0) {
            colrgb <- col2rgb(col)/255
            rgb(colrgb[1,], colrgb[2,], colrgb[3,], alpha)
        } else {
            if(any(alpha < 0 | alpha > 1)) {
                stop("values in 'alpha' must be between 0 and 1")
            }
            colrgb <- col2rgb(col)/255
            bgrgb <- as.vector(col2rgb(bg[1])/255)
            red <- alpha*colrgb[1,] + (1-alpha)*bgrgb[1]
            green <- alpha*colrgb[2,] + (1-alpha)*bgrgb[2]
            blue <- alpha*colrgb[3,] + (1-alpha)*bgrgb[3]
            mapply(rgb, red, green, blue)
        }
    }
}