Skip to content

Instantly share code, notes, and snippets.

@csaybar
Last active December 13, 2019 15:14
Show Gist options
  • Save csaybar/a85d5755eeee1ac27c8e0caead8f3897 to your computer and use it in GitHub Desktop.
Save csaybar/a85d5755eeee1ac27c8e0caead8f3897 to your computer and use it in GitHub Desktop.
hex to Lab color space & Lab color space to hex.
hex_to_Labspace <- function(hex) {
hextolab <- function(x)
convertColor(x,from="sRGB",to="Lab")
rgb_col <- col2rgb(hex)/255
labspace <- apply(rgb_col, 2, hextolab) %>% t
colnames(labspace) <- c('L','a','b')
return(labspace)
}
Labspace_to_hex <- function(lab) {
labtohex <- function(x) {
rgb_space <- convertColor(x,from="Lab",
to="sRGB")
r <- rgb_space[1,1]
g <- rgb_space[1,2]
b <- rgb_space[1,3]
rgb(r, g, b, maxColorValue = 1)
}
return(apply(lab, 1, labtohex))
}
# example
colors <- c("#FFBEB2", "#AE123A")
labcol <- hex_to_Labspace(colors)
identical(Labspace_to_hex(labcol),colors)
@csaybar
Copy link
Author

csaybar commented Dec 13, 2019

ed_01k

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment