Thanks to those who took time to respond. Based on the functions
that were pointed out to me I have the following:
# colors.hex and colors.name convert color names to hex and visa versa
# note that each color has a unique hex code but each hex code may have
# more than one color
colors.hex <- function( x=colors() ) {
color.hex <- function(x) do.call( "rgb",
as.list(col2rgb(x)/255) )
sapply( x, color.hex )
}
colors.name <- function( x ) {
color.name <- function( x ) colors()[ colors.hex() == x ]
lapply( x, color.name )
}
# For example,
colors.hex( "red" )
colors.hex( colors()[1:5] )
colors.name( "#FF0000" )
colors.name( rainbow(3) )
colors.name( rainbow(7) ) # note: only first element has a name
# LL partitions all color names into equivalence classes w same hex code
# LL2 is similar but only has equivalence classes with more than one name
LL <- by( colors(), colors.hex(), as.vector )
LL2 <- LL[ lapply( LL, length ) > 1 ]