Skip to contents

Converts Hex codes values to RGB vectors

Usage

hex_to_rgb(x)

Arguments

x

A hex colour code or recognised colour name, or a vector of them

Value

For a single colour, a named numeric vector, c(r = , g = , b = ). For several colours, a matrix with one row per colour and columns r, g and b. The rows are named after the names of x, if it has any. Any alpha channel in the hex codes is dropped.

Examples

hex_to_rgb("purple")
#>   r   g   b 
#> 160  32 240 
hex_to_rgb("#fafafa")
#>   r   g   b 
#> 250 250 250 
hex_to_rgb(c("unripe" = "#89973d", "ripe" = "#e8b92f"))
#>          r   g  b
#> unripe 137 151 61
#> ripe   232 185 47

# The output can be passed straight to other functions, e.g. to get a
# lighter version of a colour, as if it were 70% opaque on white
rgba_to_hex(c(hex_to_rgb("#9E0142"), 0.7))
#> [1] "#BB4D7A"