[comp.windows.x] Creating & Installing a Colormap

amathur@alpha.ces.cwru.edu (Alok Mathur) (04/20/91)

Hi !

I am using Xlib and  need to install a colormap with very specific RGB
values for each pixel. I basically want to specify shades of 1 color
with increasing intensities. 
eg.
R	G	B
250	20	20
245	20	20
240	20	20

I am new to X programming. I have tried XCreateColormap & XStoreColor
but that doesn't seem to work.

I have created a window using XCreateSimpleWindow.
I then do the following :

  created = XCreateColormap(display,window,visual,AllocAll);
  for(i=1;i<=40;i++)
  {
	clr.red = 20;
	clr.green = 255 - i*5;
	clr.blue = 20;
	clr.pixel = (unsigned long) i;
	clr.flags = DoRed | DoGreen | DoBlue;
	XStoreColor(display,created,&clr);
  }

Later I raise the window and use

  for(i=1;i<=40;i++)
  {
     XSetForeground(display,gc,(unsigned long) i);
     XFillRectangle(display,window,gc,10,10,100,100);
     XFlush(display);
  }

I get a black, red, blue, yellow, grey, green, black, black, ..... black, ....
rectangles and not shades of green. Even if I vary values of red,
green, blue, I still get the same colored rectangles.
I am unable to install the colormap at all as even if I omit the
XStoreColor loop, I get the same colors.

I have also tried include XInstallColormap & XSetWindowColormap
commands before raising the window, but that doesn't help either and  I
still get the same colored rectangles.

Could someone help me out with my problem ?

Thanks a lot,

Alok.

I am using a SUN 3/60 and SunOS 4.1
The following is relevant information from xdpyinfo

screen #0:
  depths (2):    1, 8
  root window id:    0x8006e
  depth of root window:    8 planes
  number of colormaps:    minimum 1, maximum 1
  default colormap:    0x8006b
  default number of colormap cells:    256
  preallocated pixels:    black 1, white 0
  options:    backing-store YES, save-unders YES
  number of visuals:    6
  default visual id:  0x80064
  visual:
    visual id:    0x80064
    class:    PseudoColor
    depth:    8 planes
    size of colormap:    256 entries
    red, green, blue masks:    0x0, 0x0, 0x0
    significant bits in color specification:    8 bits

ekberg@asl.dl.nec.COM (Tom Ekberg) (04/22/91)

 > I am using Xlib and  need to install a colormap with very specific RGB
 > values for each pixel. I basically want to specify shades of 1 color
 > with increasing intensities. 
 > eg.
 > R	G	B
 > 250	20	20
 > 245	20	20
 > 240	20	20

The problem is that color values are scaled to 65535 (0..65525) and not 255
(0..255).  If you multiply your values by 256 you will be much closer to the
colors that you want.

  -- tom, ekberg@asl.dl.nec.com (x3503)

mouse@lightning.mcrcim.mcgill.EDU (der Mouse) (05/02/91)

> I am using Xlib and  need to install a colormap with very specific
> RGB values for each pixel. I basically want to specify shades of 1
> color with increasing intensities.

> I am new to X programming. I have tried XCreateColormap & XStoreColor
> but that doesn't seem to work.

Ah, colormaps, a fertile source of confusion.

> I have created a window using XCreateSimpleWindow.  I then do the
> following :

>   created = XCreateColormap(display,window,visual,AllocAll);

I assume visual is an 8-bit PseudoColor visual.

>   for(i=1;i<=40;i++)
>   {
> 	clr.red = 20;
> 	clr.green = 255 - i*5;
> 	clr.blue = 20;

Here's one problem: at the Xlib level, all colors are normalized to a
0-65535 range; the server deals with converting them into whatever form
the hardware wants.  You should probably multiply all three numbers by
256 before doing the XStoreColor; otherwise, everything will come out
looking black.

> 	clr.pixel = (unsigned long) i;
> 	clr.flags = DoRed | DoGreen | DoBlue;
> 	XStoreColor(display,created,&clr);
>   }

Except as I noted above, this looks fine on a once-over.

> Later I raise the window and use

>   for(i=1;i<=40;i++)
>   {
>      XSetForeground(display,gc,(unsigned long) i);
>      XFillRectangle(display,window,gc,10,10,100,100);
>      XFlush(display);
>   }

You could move the XFlush outside of the loop, but that's not a problem
(except that it will probably cause the rectangles to flash by too fast
to see).  At any rate, this too looks fine.

> I get a black, red, blue, yellow, grey, green, black, black, .....
> black, .... rectangles and not shades of green.  Even if I vary
> values of red, green, blue, I still get the same colored rectangles.
> I am unable to install the colormap at all as even if I omit the
> XStoreColor loop, I get the same colors.

Your colormap is most likely not being installed.  (If it were,
everything would be black (as I mentioned above), not what you report.)

> I have also tried include XInstallColormap & XSetWindowColormap
> commands before raising the window, but that doesn't help either and
> I still get the same colored rectangles.

Ah, this becomes interesting.  XSetWindowColormap should be what you
want.  When you called XInstallColormap, the whole screen should have
flashed black very briefly, right?  This is your install happening,
then the window manager noticing and reinstalling the colormap that its
colormap focus policy says should be installed.  Colormap installation
is the window manager's responsibility; you should just
XSetWindowColormap and let the window manager deal with installing
things according to its policy.  (If you don't like its policy, find
out how to configure it, yell at wherever you got it from, and/or
switch to one that behaves the way you want.)

You will probably need to do something to get the window to have
colormap focus; typically, moving the mouse pointer into the window.
You don't say what window manager you're using, so I can't say much
more on the subject.

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu