[Rd] Re: [R] mapping of colornames into hsv?

Ben Bolker bolker@zoo.ufl.edu
Mon, 28 Feb 2000 15:57:34 -0500 (EST)

  [ Sent to R-devel instead of R-help ]

  I wrote the following hacks recently, which might be useful to those on
a Unix system (with tr, sed, uniq, and the color list living in
/usr/lib/X11/rgb.txt): I think that along with palette(), rgb() this
provides most of the functionality for translating among

color numbers
color names
rgb vectors
colors as specified by topo.color() etc. (hex strings)

(I didn't write an rgb vector -> color name translator, would be easy but
seems less useful than the other translations.) Of course, it would be
nice to see these built in instead of hacked.  They could probably even be
hacked better.

system("tr A-Z a-z </usr/lib/X11/rgb.txt | sed -e 's/\\([a-zA-Z]\\) \\([a-zA-Z]\\)/\\1\\2/g' | uniq >colors.tmp")
color.list <- read.table("colors.tmp",skip=1,as.is=TRUE)
names(color.list) <- c("red","green","blue","name")

colorname.to.rgb <- function(name) {
color.to.rgb <- function(color) {
#  given color as hexadecimal, return RGB values
  hexvec <- c(0:9,"A","B","C","D","E","F")
  rgb <- numeric(3)
  names(rgb) <- c("red","blue","green")
  for (i in (0:2)) {
    h1 <- which(hexvec==substr(color,i*2+2,i*2+2))-1
    h2 <- which(hexvec==substr(color,i*2+3,i*2+3))-1
    rgb[i+1] <- h1*16+h2

On 28 Feb 2000, Peter Dalgaard BSA wrote:

> "Jens Oehlschlägel-Akiyoshi" <jens.oehlschlaegel-akiyoshi@mdfactory.de> writes:
> > I couldn't find this in online help or the archives:
> > Is there any R function or object giving the mapping of the colornames as
> > given by colors() into the hsv() model?
> It isn't there. You might be able to cook something up which calls the
> internal name2col() C function which returns the RGB color code for a
> string name. 
> [And as I was experimenting with this I tried palette(colors()) and
> got a segfault...]

Ben Bolker                                  bolker@zoo.ufl.edu
Zoology Department, University of Florida   http://www.zoo.ufl.edu/bolker
318 Carr Hall/Box 118525                    tel: (352) 392-5697
Gainesville, FL 32611-8525                  fax: (352) 392-3704

r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch