makeTransparent: Make Transparent Colors

Description Usage Arguments Value Author(s) See Also Examples

View source: R/animatoR-functions-knitr.R

Description

Add transparency parameter (alpha) to colors

Usage

1
makeTransparent(x, alpha = 0.5)

Arguments

x

vector specifying colors or factor. Colors can be specified as numbers or character strings, see colors. Factors are also acceptable in which case the input will be transformed into level numbers.

alpha

numeric, transparency value rgb.

Value

A character vector with elements of 7 or 9 characters, "#" followed by the red, blue, green and optionally alpha values in hexadecimal (after rescaling to 0 ... 255). The optional alpha values range from 0 (fully transparent) to 255 (opaque).

Author(s)

Andrej Blejec andrej.blejec@nib.si

See Also

rgb for setting colors and colors for color names.

Examples

 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
# Transparent red
makeTransparent("red",0.5)
# Same thing
makeTransparent(2,0.5)
# Vector of names
makeTransparent(c("red","orange"),0.75)
# Vector of color numbers
makeTransparent(1:5)
# Factor levels can be used as color indicator
makeTransparent(factor(c("S","M","H","S")))
# Different transparency
makeTransparent(1:5,alpha=1:5)
#
# Define transparent version of base colors
#
trcols<-makeTransparent(c(1:8,0.1),alpha=0.75)
trcols <- data.frame(t(trcols),stringsAsFactors=FALSE)
names(trcols) <-
paste("tr",c("black","red","green","blue",
"cyan","magenta","yellow","grey","white"),sep="")
str(trcols)
attach(trcols)
trred
trgreen
detach()
# Plot colors
if(interactive()){
par(xpd=TRUE)
newplot(stamp=FALSE)
points(1:9,1:9,bg=unlist(trcols),col="grey",pch=21,cex=15)
text((1:9)+1.1,(1:9)-0.7,names(trcols),adj=0)
}

ablejec/animatoR documentation built on Feb. 21, 2020, 10:09 p.m.