channel: Get or set a color channel

View source: R/manip_channel.R

channelR Documentation

Get or set a color channel

Description

Extract the value of a color channel or set it and therefore modify the color.

Usage

channel(x, model, channel)

channel(x, model, channel) <- value

Arguments

x

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

model

string defining the color model; valid models are cmyk, hcl, lch, hsi, hsl, hsv, lab, rgb, and rgba.

channel

string defining the channel within the color model.

value

the channel value to set; a number, the convention of which depends on the channel.

Value

For channel, a vector of channel values, the convention of which depend on the channel.

For channel <-, the updated object(s).

See Also

luminance for relative brightness, which is slightly different from the perceived brightness (i.e. the value of the L channel in L*a*b* or HCL).

Other color manipulation functions: average(), blend(), darken(), luminance(), mix(), saturate()

Examples

channel("pink", "rgb", "r")
channel(colors()[1:5], "hcl", "l")

# Colors along a HSV rainbow have very different lightness,
# which makes them inapropriate for continuous color scales
# In contrast, lightness should be constant along a HCL rainbow
HSV_rainbow <- hsv(h=seq(0, 360, length.out=7), s=1, v=1)
HCL_rainbow <- hcl(h=seq(0, 360, length.out=7), l=0.65)

show_col(HSV_rainbow, HCL_rainbow)
channel(HCL_rainbow, "hcl", "l")
# NB: the subtle changes are caused by the round-trip conversion to R's
#     internal sRGB representation of colors.
# make it more visual
plot(channel(HSV_rainbow, "hcl", "l"), col=HSV_rainbow,
     pch=19, cex=2, type="b", ylab="Lightness")
abline(h=0.65, lty="dotted")
points(channel(HCL_rainbow, "hcl", "l"), col=HCL_rainbow,
       pch=19, cex=2, type="b")

# Force a given lightness
x_orig <- x <- HSV_rainbow
channel(x, "hcl", "l") <- 0.5
show_col(x_orig, x)

# Make all colors equally saturated
x_orig <- x <- c("aliceblue", "aquamarine4", "coral", "blanchedalmond")
channel(x, "hsv", "s") <- 0.5
show_col(x_orig, x)

# Keep the lightness and saturation but change the hue
# (also called "colorizing")
x_orig <- x <- c("aliceblue", "aquamarine4", "coral", "blanchedalmond")
channel(x, "hsv", "h") <- 240  # make all blue
show_col(x_orig, x)

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