[R] extracting RGB values from a colorspace class object

Jeffrey Horner jeff.horner at vanderbilt.edu
Fri Mar 3 16:11:22 CET 2006


Dylan Beaudette wrote:
> Greetings,
> 
> After pouring over the documentation for the 'colorspace' package, I have not 
> been able to figure out how the plot() method converts colorspace coordinates 
> to RGB values for display on the screen. I am convert between colorspaces 
> with the various as() methods... but cannot seem to find a way to extract RGB 
> (i.e. for displaying on a computer screen) triplets from color space 
> coordinates.

I don't know how it's done (I get really confused when trying to 
understand color spaces), but you'll have to download the code for the 
colorspace package and study the c code since that's where the 
conversion is done.

To get the RGB and hex triplets, this small example should help:

## Generate white in XYZ space
 > white = XYZ(95.047, 100.000, 108.883)
 > as(white,"RGB")
             R         G        B
[1,] 1.000008 0.9999907 1.000081
 > hex(white)
[1] "#FFFFFF"

> 
> Here is a sample of the data that I am working with:
> 	H		V	C		x	y	Y
>      2.5Y        0.2         2      1.43      0.97   0.237
>      2.5Y        0.4         2     0.729     0.588   0.467
>      2.5Y        0.6         2     0.563     0.491   0.699
>      2.5Y        0.6         4     0.995     0.734   0.699
>      2.5Y        0.8         2     0.479     0.439   0.943
>      2.5Y        0.8         4      0.82      0.64   0.943
>      2.5Y          1         2    0.4362    0.4177   1.21
> 
> I have converted xyY coordinates to XYZ coordinates, based on the equations 
> presented here:
> http://www.brucelindbloom.com/index.html?ColorCalcHelp.html
> 
> #read in the soil colors: munsell + xyY
> soil <- read.table("soil_colors", header=F, 
> col.names=c("H","V","C","x","y","Y"))
> 
> #convert to XYZ
> X <- (soil$x * soil$Y ) / soil$y
> Y <- soil$Y
> Z <- ( (1- soil$x - soil$y) * soil$Y ) / soil$y
> 
> #make an XYZ colorspace object:
> require(colorspace)
> soil_XYZ <- XYZ(X,Y,Z)
> 
> #visualize the new XYZ colorspace object in the L*U*V colorspace:
> plot(as(soil_XYZ, "LUV"), cex=2)
> 
> here is an example of the output:
> http://169.237.35.250/~dylan/temp/soil_colors-LUV_space.png
> 
> Somehow the plot() method in the colorspace package is converting color space 
> coordinates to their RGB values... Does anyone have an idea as to how to 
> access these RGB triplets?
> 
> Thanks in advance!
> 
> 
> 
> 


-- 
Jeffrey Horner       Computer Systems Analyst         School of Medicine
615-322-8606         Department of Biostatistics   Vanderbilt University




More information about the R-help mailing list