[comp.windows.x] colormap needed !

yanling@ccwf.cc.utexas.edu (yanling) (06/26/91)

Hi X world,

    I got a question about colormap. I need to plot a color picture of 
temperature field. I would like to code the temperature field as red for 
highest temperature and blue for lowest temperature in a geometric
domain. The color should be changed linearly from red to blue with the 
temperature from highest to lowest. 


    The colormap I need should be a standard spectrum like we see from physics
textbook. When the pixal values go from 2 to 256, the colors vary from red
orange, yellow, green ... blue.


    Thanks in advance.


     --yanling  

miguel@txiki.radiology.arizona.EDU (06/27/91)

In article <51207@ut-emx.uucp> yanling@ccwf.cc.utexas.edu (yanling) writes:

>Hi X world,
>
>    I got a question about colormap. I need to plot a color picture of 
>temperature field. I would like to code the temperature field as red for 
>highest temperature and blue for lowest temperature in a geometric
>domain. The color should be changed linearly from red to blue with the 
>temperature from highest to lowest. 
>
>
>    The colormap I need should be a standard spectrum like we see from physics
>textbook. When the pixal values go from 2 to 256, the colors vary from red
>orange, yellow, green ... blue.
>
>
>    Thanks in advance.
>
>
>     --yanling  

I don't have the values for a standard optical spectrum colormap but I do have an 
algorithm to generate one (256 entries from blue (0) to red (255)) on
the pixel_array array:

-------------------------------------  cut here 
-----------------------------------------------
/*****************************************************************************
 *  file: gen_spect_colormap.c                                               *
 *  function: gen_spect_colormap                                             *
 *  by Miguel Parra (Computer programmer).                                   *
 *  miguel@txiki.radiology.arizona.edu                                       *
 *                                                                           *
 *  Generate a linear optical spectrum colormap into the extern array        *
 *  pixel_array (256 entries from blue (0) to red (255))                     *
 *  this routine should be called during initialization.                     *
 *                                                                           *
 *  Dependencies:                                                            *
 *     This funcion depends on the previous initialization of the variables: *
 *          mydisplay, myscreen and mycolormap                               *
 *                                                                           *
 ****************************************************************************/

#include   <X11/Xlib.h>
#include   <X11/Xatom.h>
#include   <X11/Xutil.h>
#include   <stdio.h>

extern	Display		*mydisplay;
extern	int		myscreen;
extern	Colormap	mycolormap;
extern	unsigned long	pixel_array[256];

gen_spect_colormap(void)
{
    XColor	hardware_color;
    int		status, i;
    unsigned	red,green,blue;

    for( i = 0; i < 255; i++ ) {
	/* no red for i from 0 - 127 */
	if (i<128) {
	   red=0;
	   if (i < 64) {
		/* all blue increasing green. (for i from 0 - 63) */
		green=i*4;
		blue=255;
	   } else {
		/* stabilize green at top & decrease blue (i from  64 - 127) */
		green=255;
		blue=(128-i)*4;
	   }
	} else {
	   /* no blue for i from 128 - 255 */
	   blue=0;
	   if (i < 192) {
		/* keep green at top & increase red (i from 128 - 191) */
		red=(i-128)*4;
		green=255;
	   } else {
		/* stabilize red at top & decrease green (i from 192 - 255) */
		red=255;
		green=(512-i)*4;
	    }
	}

	/* now allocate color */
	hardware_color.blue  = blue  << 8;
	hardware_color.green = green << 8;
	hardware_color.red   = red   << 8;
	status = XAllocColor(mydisplay, mycolormap, &hardware_color);

	/* check if there is enough space in this colormap */
	if ( status != 0 ) {
	   /* store pixel number in array (for continuity) */
	    pixel_array[i] = hardware_color.pixel;
	} else {
	    /* if there is not enough space in this colormap create another */
	    mycolormap = XCopyColormapAndFree(mydisplay,mycolormap);
	    status = XAllocColor(mydisplay, mycolormap, &hardware_color);
	    if ( status != 0 ) {
		/* store pixel number in array (for continuity) */
		pixel_array[i] = hardware_color.pixel;
	    } else {
		printf("couldn't allocate color %d\n", i);
		fflush(stdout);
		pixel_array[i] =  BlackPixel(mydisplay, myscreen);
	    }
	}
    }
}

-------------------------------------  cut here 
-----------------------------------------------

   You can use this algorithm to generate a table to be read later on,
or you could use it to 
allocate the colors dinamically the only thing you have to do is fillup
the gaps (declare 
variables and frame it into a function).

   This approach doesn't give you the exact colormap you are looking for
(since I opted for a linear increment) but it is a very good approximation.
Feel free to use it and to redistribute it, just include me on the
appropiate credits.
Hope this helps.

Miguel Parra
Computer Programmer
University or Arizona
Radiology Dept.
E-Mail: miguel@txiki.radiology.arizona.edu