This is a version of grDevices::col2rgb() that returns the colour values in the standard form expected by farver (matrix with a row per colour). As with encode_colour() it can do colour conversion on the fly, meaning that you can decode a hex string directly into any of the supported colour spaces.

decode_colour(colour, alpha = FALSE, to = "rgb", white = "D65", na_value = NA)



A character vector of hex-encoded values or a valid colour name as given in grDevices::colours().


If TRUE the alpha channel will be returned as well (scaled between 0 and 1). If no alpha channel exists in the colour it will be assumed 1. If FALSE any alpha channel is ignored.


The output colour space. Allowed values are: "cmy", "cmyk", "hsl", "hsb", "hsv", "lab" (CIE L*ab), "hunterlab" (Hunter Lab), "oklab", "lch" (CIE Lch(ab) / polarLAB), "luv", "rgb" (sRGB), "xyz", "yxy" (CIE xyY), "hcl" (CIE Lch(uv) / polarLuv), or "oklch" (Polar form of oklab)


The white reference of the output colour space. Will only have an effect for relative colour spaces such as Lab and luv. Any value accepted by as_white_ref() allowed.


A valid colour string or NA to use when colour contains NA elements. The general approach in farver is to carry NA values over, but if you want to mimick col2rgb() you should set na_value = 'transparent', i.e. treat NA as transparent white.


A numeric matrix with a row for each element in colour and either 3, 4, or 5 columns depending on the value of alpha and to.

Handling of non-finite and out of bounds values

NA, NaN, -Inf, and Inf are treated as invalid input and will result in NA values for the colour. If a given colourspace has finite bounds in some of their channels, the input will be capped before conversion, and the output will be capped before returning, so that both input and output colours are valid colours in their respective space. This means that converting back and forth between two colourspaces may result in a change in the colour if the gamut of one of the spaces is less than the other.

See also

Other encoding and decoding functions: encode_colour(), manip_channel


# basic use
decode_colour(c('#43e1f6', 'steelblue', '#67ce9fe4'))
#>        r   g   b
#> [1,]  67 225 246
#> [2,]  70 130 180
#> [3,] 103 206 159

# Return alpha as well (no alpha value is interpreted as 1)
decode_colour(c('#43e1f6', 'steelblue', '#67ce9fe4'), alpha = TRUE)
#>        r   g   b     alpha
#> [1,]  67 225 246 1.0000000
#> [2,]  70 130 180 1.0000000
#> [3,] 103 206 159 0.8941176

# Decode directly into specific colour space
decode_colour(c('#43e1f6', 'steelblue', '#67ce9fe4'), to = 'lch')
#>             l        c        h
#> [1,] 82.74938 40.01458 213.2785
#> [2,] 52.46552 32.44532 262.7880
#> [3,] 75.77774 43.36657 160.7072