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.
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.
> 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.About 1-2 years ago, I have improved the demo("colors", package = "grDevices") demo in R.... with inspiration from Marius Hofert. The demo now features a nearRcolor() function that was written for somewhat like that purpose. ##' Find close R colors() to a given color {original by Marius Hofert) ##' using Euclidean norm in (HSV / RGB / ...) color space nearRcolor <- function(rgb, cSpace = c("hsv", "rgb255", "Luv", "Lab"), dist = switch(cSpace, "hsv" = 0.10, "rgb255" = 30, "Luv" = 15, "Lab" = 12)) ............. ............. It allows to use different color spaces and a default set of cutoffs, for defining what "near" means. I had thought at the time to make a regular function out of it, but then did not follow up on myself :-) Martin Maechler, ETH Zurich and R core team> 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.
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.