alpha: Modify color transparency

View source: R/manip_alpha.R

alphaR Documentation

Modify color transparency

Description

Modify color transparency, vectorized in both x and alpha (when both are vectors, they must be the same length).

Usage

alpha(x, alpha = 0.5)

Arguments

x

vector of colors specified as hex strings or named R colors.

alpha

transparency, number in [0, 1]; 0 means fully transparent, 1 means fully opaque.

Details

Most other operations in chroma do not support alpha channels (because the underlying library, chroma.js, does not output 8 digits hex color specifications, which describe r, g, b, and a channels). Therefore, any manipulation of transparency must be done last.

Examples

as.hex("red")
alpha("red")

show_col(c("red", alpha("red")))

# Vectorised in both x and alpha, as long as the lengths are compatible
alpha(c("red", "green", "blue"), 0.5)
alpha("red", c(0.2, 0.5, 0.7))
alpha(c("red", "green", "blue"), c(0.2, 0.5, 0.7))
alpha(c("red", "green", "blue", "purple"), c(0.2, 0.5))
## Not run: alpha(c("red", "green", "blue", "purple"), c(0.2, 0.5, 0.7))
# = fails because arguments have incompatible lengths (x = 4, alpha = 3)
## End(Not run)
# Beware, other functions do not support alpha channels
mix(alpha("red"), alpha("blue"))
mix("red", "blue")
# = both are the same, and fully opaque. Use alpha last
alpha(mix("red", "blue"))

jiho/chroma documentation built on Nov. 26, 2022, 2:39 a.m.