Skip to content

Instantly share code, notes, and snippets.

@chasemc
Created January 8, 2018 15:05
Show Gist options
  • Save chasemc/4d5ba92e22f132708f4ee3a987caaf6c to your computer and use it in GitHub Desktop.
Save chasemc/4d5ba92e22f132708f4ee3a987caaf6c to your computer and use it in GitHub Desktop.
Convert a vector of named colors to HEX values in R
# cols is a vector of named colors
# First we convert the named colors to rgb, and store the values in a list
rgbVals<-lapply(cols,col2rgb)
# Then convert the list to a data frame and flip it
rgbVals<-t(as.data.frame(rgbVals,col.names = seq(1,length(rgbVals),by=1)))
# Then we convert the rgb values to HEX
hexVal<-sapply(1:length(rgbVal[,1]),function(x){ rgb(rgbVal[x,"red"],rgbVal[x,"green"],rgbVal[x,"blue"],maxColorValue = 255) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment