rgb: RGB color specification

View source: R/parse_rgb.R

rgbR Documentation

RGB color specification

Description

Create a vector of colors from red, green, and blue. This is a drop-in replacement for the rgb function, included with R in package grDevices.

Usage

rgb(
  red = 0,
  green = 0,
  blue = 0,
  alpha = NULL,
  names = NULL,
  maxColorValue = 1
)

rgba(red = 0, green = 0, blue = 0, alpha = 1)

Arguments

red, green, blue

color channels, numbers in [0, maxColorValue]. red can also be a matrix or data.frame containing the other components (green and blue for rgb, green, blue, and alpha for rgba)

alpha

transparency, number in [0, maxColorValue]; 0 means fully transparent, maxColorValue means fully opaque. See function alpha for another way to change the transparency after the fact.

names

character vector. The names for the resulting vector.

maxColorValue

number giving the maximum of the color values range, typically 1 or 255.

Details

The first argument can also be a data.frame or a matrix. In that case, its columns are considered as the color components, taken in order, and the other color components arguments are ignored.

When separate arguments are used for the color components and are vectors, values in shorter arguments are recycled to match the length of the longest argument. If the lengths are not compatible, an error is output.

rgba is a variant which forces maxColorValue to be 1 and requires four color components, including alphaallows to specify an alpha value as a color component (i.e. as the fourth column of the first argument when this argument is a matrix/data.frame).

RGB is how colors are displayed on a computer screen. However, this is not how colors are perceived by the human eye/brain. Other color spaces such as HCL and L*a*b* make it easier to create color palettes that are appropriate for human perception.

Value

A vector of colors specified as hex codes

See Also

parse_color for the general function to parse colors in various specifications (which this function calls internally) and convert_color to convert parsed colors to another model.

Other color specifications: cmyk(), css(), hcl(), hex(), hsi(), hsl(), hsv(), lab(), parse_color(), ryb(), temperature(), wavelength()

Examples

rgb()
rgb(1, 0, 0)
rgb(255, 0, 0, maxColorValue=255)
rgb(data.frame(c(0.2, 0.5), c(0.5,0.5), c(0.6, 0.4)))
rgb(matrix(c(0.2, 0.5, 0.5, 0.5, 0.6, 0.4), ncol=3))
rgb(c(0.2, 0.5), 0, 0)

red <- rgb(1, 0, 0)
transparent_red <- rgb(1, 0, 0, alpha=0.7)
show_col(c(red, transparent_red))

# Color ramps
ramp <- seq(0, 1, length.out=10)
rgb(red=ramp, green=0, blue=0, names=paste("red", 1:10, sep="."))
show_col(
  rgb(r=ramp),
  rgb(g=ramp),
  rgb(b=ramp)
)

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