Colour matching feature in R

December 8, 2009
By

(This article was first published on The Praise of Insects, and kindly contributed to R-bloggers)

I love R. It is an open-source statistical programming language that I found reasonably easy to learn, and find it incredibly versatile and useful. Because it's open-source anyone can contribute to it, and there are a huge number of packages that extend its capabilities to do pretty much anything.

I've been using it to do Principal Components Analysis on some colour data that I measured from photos of some Carpophilus specimens, and was trying to figure out how to relate the RGB values I got from the photos to actual names. R has in its core packages a colors() function with a lot of names assigned to particular RGB values. Thankfully, Barry Rowlingson has come up with a very nice little function that figures out which colour names the RGB value is closest to:

nearColour <- function(r,g,b){
ctable = col2rgb(colors())
cdiff = ctable - c(r,g,b)
cdist = cdiff[1,]*cdiff[1,]+cdiff[2,]*cdiff[2,]+cdiff[3,]*cdiff[3,]
return(colors()[cdist == min(cdist)])
}
Pretty useful for standardising colour names!

To leave a comment for the author, please follow the link and comment on his blog: The Praise of Insects.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.