Search code examples
rcolors

Convert hex colour to HCL


Given a character vector of some hexadecimal colour codes:

hcl(h = c(0, 120, 240), c = 35, l = 85)
#> [1] "#FFC5D0" "#BBDEB1" "#B8D8F8"

How can I find their HCL representation?

Expected result:

cbind(h = c(0, 120, 240), c = 35, l = 85)
#>        h  c  l
#> [1,]   0 35 85
#> [2,] 120 35 85
#> [3,] 240 35 85

Solution

  • decode_colour() from the farver package can be used to do this directly:

    library(farver)
    
    hex <- hcl(h = c(0, 120, 240), c = 35, l = 85)
    
    decode_colour(hex, to = "hcl")
    #>             h        c        l
    #> [1,] 359.3883 35.09095 84.86854
    #> [2,] 120.2448 34.65261 84.98657
    #> [3,] 239.3602 34.63943 85.03581
    

    The result is not exact, but close enough.