Skip to content
Prev 349941 / 398513 Next

Convert color hex code to color names

Actually all 6 colors in rainbow(6) do have names. I missed the fact that rainbow() adds an alpha value that we need to strip off before comparing to the values in clrs$RGB:
[1] 12

So there are two color names for each color in rainbow(6):
1 red red1 
2 yellow yellow1 
3 green green1 
4 cyan cyan1 
5 blue blue1 
6 magenta magenta1

David C

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David L Carlson
Sent: Monday, April 13, 2015 12:07 PM
To: Boris Steipe; Alejo C.S.
Cc: r-help at r-project.org
Subject: Re: [R] Convert color hex code to color names

And expanding at a more elementary level. The reason you need to find the smallest difference is 
that all of the possible colors do not have names. There are 256^3 = 16,777,216 possible rgb color designations, but only 657 named colors. You can create a data frame of the named colors and their rgb designations using
maxColorValue=255), stringsAsFactors=FALSE)
'data.frame':   657 obs. of  2 variables:
 $ Color: chr  "white" "aliceblue" "antiquewhite" "antiquewhite1" ...
 $ RGB  : chr  "#FFFFFF" "#F0F8FF" "#FAEBD7" "#FFEFDB" ...
Color     RGB
1         white #FFFFFF
2     aliceblue #F0F8FF
3  antiquewhite #FAEBD7
4 antiquewhite1 #FFEFDB
5 antiquewhite2 #EEDFCC
6 antiquewhite3 #CDC0B0

So most colors do not have names. In your example, none of the colors in rainbow(6) have names:
[1] 0

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Boris Steipe
Sent: Monday, April 13, 2015 11:44 AM
To: Alejo C.S.
Cc: r-help at r-project.org
Subject: Re: [R] Convert color hex code to color names

To add slightly to that:

What you want to do is write a function that returns the named color that has the smallest difference to your input hex-triplet. But note that color difference is a large topic. Assuming you want to minimize *perceptual* differences, you want to calculate your differences in Lab color space. The function convertColor() has the option to convert hex to Lab. Example:
convertColor(t(col2rgb("thistle")), from="sRGB", to="Lab", scale.in=255)

Within Lab space, you can take the Euclidian distance.

That all said, I can't imagine why one would want to do this in the first place - color triplets are much more convenient than label strings :-)


B.
On Apr 13, 2015, at 11:45 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be> wrote:

            
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.