Colour matching feature in R

[This article was first published on The Praise of Insects, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: The Praise of Insects.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)