[R] finding the colour at a point
Henrik Bengtsson
hb at maths.lth.se
Mon Jul 22 10:36:30 CEST 2002
I believe there is no straightforward way to do this. However, you can
always plot to a bitmap device, which you then convert to a PPM image file,
which the is read in to a data structure by the 'pixmap' package. The
problem is that you need non-[R] software to convert you temporary bitmap
file to a PPM file. However, I recommend that you go get the free
ImageMagick software (http://www.imagemagick.org), which is great. This can
be called from withing [R] if the system PATH is set up correctly.
Here's an outline:
# -------------------------------
# 1. Plot to a (PNG) bitmap file
# -------------------------------
pngfile <- paste(tempfile(), ".png", sep="")
png(png, file=pngfile, width=500, height=500, bg="white")
# No margins to make life easier
opar <- par(mar=c(0,0,0,0))
plot(1:10, pch=1:10, col=1:10, cex=3, lwd=2)
usr <- par("usr") # Maybe you'll need this and some other par's
par(opar)
dev.off()
# ----------------------
# 2. Convert PNG to PPM
# ----------------------
ppmfile <- paste(tempfile(), ".ppm", sep="")
system(paste("convert", pngfile, ppmfile)) # ImageMagick's 'convert'
unlink(pngfile)
# -------------------------------------------
# 3. Read the PPM using the 'pixmap' package
# -------------------------------------------
library(pixmap) # Available on http://cran.r-project.org
img <- read.pnm(ppmfile)
unlink(ppmfile)
# -------------------------------------------
# 4. Get the color values and the colormap
# -------------------------------------------
img <- as.pixmapIndexed(img)
colorMap <- attr(img, "col")
# --------------------------------------------------------------------
# 5. Example: Find the "first" red pixel and get its 5x5 neighborhood
# --------------------------------------------------------------------
idx <- which(img == which(colorMap == "#FF0000"), arr.ind=TRUE)
first <- idx[1,]
n55 <- t(which(matrix(T,ncol=5,nrow=5), arr.ind=TRUE)-3) # better way?
region <- first + n55
region <- region[1,] + (region[2,]-1)*nrow(img)
n55 <- matrix(img[region], ncol=5)
print(n55)
print(matrix(colorMap[n55], ncol=5))
where n55 become
[,1] [,2] [,3] [,4] [,5]
[1,] 2 2 2 3 3
[2,] 2 2 2 3 3
[3,] 2 2 3 3 3
[4,] 2 2 3 3 3
[5,] 2 2 2 2 2
This should be the lower left corner of the red triangle. Then you have to
play with par("usr") and friends to get the correct scale.
Cheers
Henrik Bengtsson
Dept. of Mathematical Statistics @ Centre for Mathematical Sciences
Lund Institute of Technology/Lund University, Sweden (+2h UTC)
+46 46 2229611 (off), +46 708 909208 (cell), +46 46 2224623 (fax)
h b @ m a t h s . l t h . s e, http://www.maths.lth.se/bioinformatics/
> -----Original Message-----
> From: owner-r-help at stat.math.ethz.ch
> [mailto:owner-r-help at stat.math.ethz.ch]On Behalf Of Richard Rowe
> Sent: Monday, July 22, 2002 3:44 AM
> To: r-help at stat.math.ethz.ch
> Subject: [R] finding the colour at a point
>
>
> I think this was asked recently but I can't find the item.
>
> Is it possible to find the colour at a point on a graphics page?
>
> I want to estimate the area of a complex shape by colouring it
> then random
> sampling from an enclosing box ...
>
> Richard Rowe
>
> Richard Rowe
> Senior Lecturer
> Department of Zoology and Tropical Ecology, James Cook University
> Townsville, Queensland 4811, Australia
> fax (61)7 47 25 1570
> phone (61)7 47 81 4851
> e-mail: Richard.Rowe at jcu.edu.au
> http://www.jcu.edu.au/school/tbiol/zoology/homepage.html
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list