From: W.T. Bridgman wtbridgman@Radix.Net To: astro@sfasu.edu Subject: Re: Temperature to RGB Date: Monday, February 21, 2000 9:40 AM Dan, I've rewritten your stellar temperature->color routine in C. I modified the integration routine to use the trapezoid rule. Feel free to post it on your website: http://www.physics.sfasu.edu/astro/color.html. I think I've handled all the tabs so it *should* stay formatted, but let me know if it gets corrupted. Tom --------------- cut here ----------- /* * RGB VALUES FOR HOT OBJECTS by Dan Bruton (astro@sfasu.edu) * * This program can be found at * http://www.physics.sfasu.edu/astro/color.html * and was last updated on March 19, 1996. * * Reference Book * Color Science : Concepts and Methods, Quantitative Data and Formula * by Gunter Wyszecki, W. S. Stiles * John Wiley & Sons; ISBN: 0471021067 * * This program will calculate the RGB values for a given * energy distribution as a function of wavelength of light. * A black body is used and an example. * * NetPBM's ppmtogif can be used to convert the ppm image generated * to a gif. The red, green and blue values (RGB) are * assumed to vary linearly with wavelength (for GAM=1). * NetPBM Software: ftp://ftp.cs.ubc.ca/ftp/archive/netpbm/ *** * Converted to C by William T. Bridgman (bridgman@wyeth.gsfc.nasa.gov) * in February, 2000. * - Original integration method replaced by trapezoid rule. * */ #include #include #include /* Function prototypes */ void BlackBody(double temperature, double *X, double *Y,double *Z); void TickMark(double temperature, short iType,double *R, double *G, double *B); void XYZ2RGB(double xr, double yr, double zr, double xg, double yg, double zg, double xb, double yb, double zb, double xc, double yc, double zc, double *red, double *green,double *blue); double DMAX1(double x, double y, double z); int main(int argc,char** argv) { static short colorValues[600][100][3]; /* image array */ /* * Chromaticity Coordinates for Red, Green, Blue and White */ double XRed=0.64, YRed=0.33; double XGreen=0.29, YGreen=0.60; double XBlue=0.15, YBlue=0.06; double XWhite=0.3127, YWhite=0.3291; double ZRed, ZGreen, ZBlue, ZWhite; double X, Y, Z, red, green, blue; /* * IMAGE INFO - WIDTH, HEIGHT, DEPTH, GAMMA */ long imageWidth=600, imageHeight=50, colorDepth=255; short iType; long i, j; double gamma=0.8, colorMax, temperature, rangeMax; FILE *PPMFile; ZRed =1.0-(XRed+YRed); ZGreen =1.0-(XGreen+YGreen); ZBlue =1.0-(XBlue+YBlue); ZWhite =1.0-(XWhite+YWhite); /* * FIND COLOR VALUE, colorValues, OF EACH PIXEL */ colorMax=0.0; /* loop over temperature range */ for(i=0;i40) TickMark(temperature,iType,&red,&green,&blue); rangeMax=1.0e-10; if(red>rangeMax) rangeMax=red; if(green>rangeMax) rangeMax=green; if(blue>rangeMax) rangeMax=blue; colorValues[i][j][0]=(short)(float)(colorDepth)*pow(red/rangeMax,gamma); colorValues[i][j][1]=(short)(float)(colorDepth)*pow(green/rangeMax,gamma); colorValues[i][j][2]=(short)(float)(colorDepth)*pow(blue/rangeMax,gamma); } /* end of 'j' loop */ if(DMAX1(red,green,blue)>colorMax) colorMax=DMAX1(red,green,blue); } /* end of 'i' loop */ /* ***************************************************************************** * * WRITE OUTPUT TO PPM FILE * * Output color values are normalized to color depth. */ if((PPMFile=fopen("temp.ppm","w"))==NULL) { printf("Cannot open file.\n"); exit(1); } fprintf(PPMFile,"P3 \n"); fprintf(PPMFile,"# temp.ppm\n"); fprintf(PPMFile,"%d %d\n",imageWidth,imageHeight); fprintf(PPMFile,"%d\n",colorDepth); for(j=0;j1.0) *red=1.0; if(*green<0.0) *green=0.0; if(*green>1.0) *green=1.0; if(*blue<0.0) *blue=0.0; if(*blue>1.0) *blue=1.0; } /* ************************************************************************/ double DMAX1(double x, double y, double z) { double max; max=x; if(y>max) max=y; if(z>max) max=z; return max; }