Find color names for colors close to colorBrewer p

2020-06-10 07:02发布

问题:

I want to use the R package SNA to do social network analysis. SNA colors elements only using R color names (text names).

I'd like to find near matches from a ColorBrewer palette (set3) to the color names in R.

There aren't many exact matches in the RGB space.

require(RColorBrewer) 
brew10 <- brewer.pal(10, "Set3")
rcol <- colors()
brew10rgb <- col2rgb(brew10)
allrgb <- col2rgb(rcol)
apply(t(brew10rgb), 1, paste, collapse="$$") %in% apply(t(allrgb), 1, paste,collapse="$$")
brew10rgb[,1]
fltr <- allrgb[1,]==141
allrgb[,fltr]
fltr <- allrgb[2,]==211
allrgb[,fltr]

Is there a way to pick good color names for a qualitative palette in R, or to map these RColorBrewer colors to existing colors?

回答1:

See if this is useful. (It's an LI distance on rgb space):

col.dist <- function(inp, comp) sum( abs(inp - col2rgb(comp) ) )

colors()[ apply(col2rgb(brew10), 2, 
             function(z) which.min( sapply(colors(), 
                           function(x) col.dist(inp=z, comp=x) ) ) ) ]
#-----------
 [1] "paleturquoise3"  "moccasin"        "lightsteelblue"  "salmon" 
 [5] "lightskyblue3"   "sandybrown"      "darkolivegreen2" "thistle2"
 [9] "gray85"          "orchid3"   

Looks like it might have succeeded looking at:

display.brewer.pal(10,"Set3")

(Although I have never see a thistle that color, and I would have thought number 7 to be more of a "lightolive" than a "darkolive".) You would proably get faster response, although this seemed acceptable, if you made the call to colors once and stored that matrix.