Last active
December 13, 2019 15:14
-
-
Save csaybar/a85d5755eeee1ac27c8e0caead8f3897 to your computer and use it in GitHub Desktop.
hex to Lab color space & Lab color space to hex.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Author
csaybar
commented
Dec 13, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment