Yes, with one Unix-specific hitch: the colorname-to-code database is
hard-coded in src/main/graphics.c and not (as far as I know) accessible
from R code. However, on Unix you can just look at /usr/lib/X11/rgb.txt
(or some such file) to get the same information.
Except for the unwanted line break in the first line (there should be a
space after the ")" and before the "\\" at the beginning of
the next line)
these are my hacks for converting a name, internal color specification
(hex string beginning with "#"), or palette number to an RGB. Have
fun.
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.list[pmatch(name,color.list[,"name"]),1:3]
}
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
}
rgb
}
}
mma.color <- function(col) {
if (is.character(col))
if (substr(col,1,1)=="#")
rgbvec <- color.to.rgb(col)
else
rgbvec <- colorname.to.rgb(col)
else
if (is.numeric(col)) {
pal <- palette()
if (col>length(pal))
col <- col %% length(pal)
rgbvec <- colorname.to.rgb(pal[col])
}
paste("RGBColor[",paste(rgbvec/255,collapse=","),"]",sep="")
}
On Fri, 15 Dec 2000, Duncan Murdoch wrote:
> There are a lot of ways to specify colours in R plot functions (number
> from the palette, by name, etc.). I'd like to pass a colour from an R
> function to an external function, and I'd like it to have the same
> flexibility. To avoid having to interpret all possible colour
> specifications myself, I need a function to convert a general colour
> specification into a standard form (e.g. r,g,b values).
>
> Is there such a thing somewhere?
>
> Duncan Murdoch
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help 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-help-request at
stat.math.ethz.ch
>
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
--
318 Carr Hall bolker at zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help 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-help-request at
stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._