color_conv: Convert colors

View source: R/color_conv.R

color_convR Documentation

Convert colors

Description

Convert from common color inputs to specified output type, adding alpha transparency for output formats that support it (hexa, rgba).

Usage

color_conv(
  color,
  alpha = 1,
  from = c("guess", "col", "hex", "hexa", "hex3", "rgb", "rgba", "lab"),
  to = c("hexa", "hex", "rgba", "rgb", "lab", "hsv")
)

Arguments

color

A color in one of the input formats (see Details)

alpha

Alpha transparency (values <=1 converted to 0-255); ignored if color has alpha already

from, to

Input and output color spaces, see Details below.

Details

  • color: one of the R colours listed in grDevices::colors(), e.g., "red"

  • hex: hexadecimal string, e.g., "#FF0000"

  • hexa: hexadecimal string with alpha, e.g., "#FF0000FF"

  • hex3: abbreviated hexadecimal string, e.g., "#F00"

  • rgb: vector of red, green and blue values 0-255, e.g., c(255, 0, 0)

  • rgba: vector of red, green, blue and alpha values 0-255, e.g., c(255, 0, 0, 255)

  • lab: CIE-Lab color

  • hsv: vector of hue, saturation and value values (0-1), e.g., c(h=0, s = 1, v = 1)

Value

color in to format

See Also

Other color: col2lab(), lab2rgb()

Examples

# different ways to input red
color_conv("red")
color_conv("#FF0000")
color_conv("#FF0000FF")
color_conv(c(255,0,0))
color_conv("rgb(255,0,0)") # you can use CSS-style text
color_conv(c(255,0,0,255))

# Lab must have names or use text format to be guessed
color_conv(c(l = 53.2, a = 80.1, b = 67.2)) 
color_conv("lab(53.2,80.1,67.2)")

# else, it will be guessed as rgb; fix by setting from explicitly
color_conv(c(53.2, 80.1, 67.2))
color_conv(c(53.2, 80.1, 67.2), from = "lab")

# add 50% alpha transparency to dodgerblue
color_conv("dodgerblue", alpha = 0.5, to = "rgba")


webmorphR documentation built on June 2, 2022, 5:07 p.m.