HexToRgb() converts a hexstring color the its red/green/blue representation.

HexToRgb(hex)

Arguments

hex

a color or a vector of colors specified as hexadecimal string of the form "#RRGGBB" or "#RRGGBBAA"

Details

A hex color is written as a hash character, "#", followed by 3 or 4 hexadecimal numbers, say 6, resp. 8, digits (0-9A-F). The first 3 pairs of digits specify the red, green and blue components. When there are 8 digits, then the last pair is interpreted as alpha channel defining transparency, where 00 represents a fully transparent color and FF represent a fully opaque color.
The result will be returned as a matrix having 3 or 4 rows, depending on if the input contained a RRGGBBAA definition or not. No distinction is made between upper and lower case. A missing leading # is tolerated.

Value

a matrix with 3 or 4 rows.

Author

Andri Signorell <andri@signorell.net>

See also

Examples

HexToRgb(c("#ADD8E6", "#FA1572"))
#>       ADD8E6 FA1572
#> red      173    250
#> green    216     21
#> blue     230    114

# 4-digit representation returns a 4 row matrix
HexToRgb(hex=c("#A52A2ABB","#A52A3B","C52A3B"))
#>       A52A2ABB A52A3BFF C52A3BFF
#> red        165      165      197
#> green       42       42       42
#> blue        42       59       59
#> alpha      187      255      255