This may not be the most elegant solution, but it should do its job:
color.names <- function(d) {
The color.names function uses the same input as col2rgb , i.e.
any of the three types of R-colors, i.e. either a color name (as indicated in color ()), a hexadecimal string of the form "#rrggbb" or "#rrggbbaa" (see rgb), or a positive integer i palette () [i].
so that you can use it to get the names of standard colors by doing color.names(1:8)
For efficiency of calculations, a data set with standard colors can be pre-calculated, as in this example:
init.color.names <- function() { z <- as.data.frame(t(sapply(colors(distinct=T),col2rgb))) colnames(z) <- colnames(z2) z$name <- rownames(z) library(dplyr) function(d) { z2 <- as.data.frame(t(col2rgb(d))) z2 %>% left_join(z) %>% select(name) } } cl <- init.color.names() cl(1:3) cl(rainbow(4))
source share