rhexrgbcolor-spacecielab

Convert Lab colour values in RGB and HEX in R


Conversion of RGB values into HEX values is easy with R:

    x <- c("165 239 210", "111 45 93")
    sapply(strsplit(x, " "), function(x)
    rgb(x[1], x[2], x[3], maxColorValue=255))
    #[1] "#A5EFD2" "#6F2D5D"

How can I convert CIELab values into RGB and HEX?

x <- c("20 0 0", "50 0 0")
[...code...]
#[1] "#303030" "#777777"

Solution

  • Here is one way using library(colorspace)

    library(colorspace)
    
    z <- c("20 0 0", "50 0 0")
    b <- do.call(rbind, lapply(strsplit(z, split = " "), as.numeric))
    b <- LAB(b)
    as(b, "RGB")
    #output:
                  R          G          B
    [1,] 0.02989077 0.02989025 0.02989294
    [2,] 0.18418803 0.18418480 0.18420138
    

    It can not convert directly to HEX but it can convert to: RGB, XYZ, HSV, HLS, LAB, polarLAB, LUV, polarLUV.