[R] Convert color hex code to color names

Jim Lemon drjimlemon at gmail.com
Mon Apr 13 23:45:13 CEST 2015


Hi Alejo,
The color.id function in plotrix will do this, one color at a time:

sapply(rainbow(6),color.id)
     #FF0000FF #FFFF00FF #00FF00FF #00FFFFFF #0000FFFF #FF00FFFF
[1,] "red"     "yellow"  "green"   "cyan"    "blue"    "magenta"
[2,] "red1"    "yellow1" "green1"  "cyan1"   "blue1"   "magenta1"

Jim


On Tue, Apr 14, 2015 at 3:42 AM, David L Carlson <dcarlson at tamu.edu> wrote:

> 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:
>
> > rain <- substr(rain, 1, 7)
> > sum(clrs$RGB %in% rain)
> [1] 12
>
> So there are two color names for each color in rainbow(6):
>
> > for (i in 1:6) cat(i, colors()[clrs$RGB==rain[i]], "\n")
> 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
>
> > clrs <- data.frame(Color=colors(), RGB=rgb(t(col2rgb(colors())),
>     maxColorValue=255), stringsAsFactors=FALSE)
> > str(clrs)
> 'data.frame':   657 obs. of  2 variables:
>  $ Color: chr  "white" "aliceblue" "antiquewhite" "antiquewhite1" ...
>  $ RGB  : chr  "#FFFFFF" "#F0F8FF" "#FAEBD7" "#FFEFDB" ...
> > head(clrs)
>           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:
>
> > rain <- rainbow(6)
> > sum(clrs$RGB %in% rain)
> [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:
>
> > A combination of rgb(), col2rgb() and colors() can gives hex values for
> the
> > named colors.
> >
> > ir. Thierry Onkelinx
> > Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> and
> > Forest
> > team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> > Kliniekstraat 25
> > 1070 Anderlecht
> > Belgium
> >
> > To call in the statistician after the experiment is done may be no more
> > than asking him to perform a post-mortem examination: he may be able to
> say
> > what the experiment died of. ~ Sir Ronald Aylmer Fisher
> > The plural of anecdote is not data. ~ Roger Brinner
> > The combination of some data and an aching desire for an answer does not
> > ensure that a reasonable answer can be extracted from a given body of
> data.
> > ~ John Tukey
> >
> > 2015-04-13 17:28 GMT+02:00 Alejo C.S. <alej.c.s at gmail.com>:
> >
> >> Hi all, I want to convert the output of:
> >>
> >>> rainbow(6)
> >>
> >>> [1] "#FF0000FF" "#FFFF00FF" "#00FF00FF" "#00FFFFFF" "#0000FFFF"
> >> "#FF00FFFF"
> >>
> >> To a vector of color names. Any tip?
> >>
> >>
> >> Thanks in advance
> >>
> >> C.
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> 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.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > 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.
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list