[comp.windows.x] Question about colormap!!

yt07+@ANDREW.CMU.EDU (Yin-Cheng Tsai) (02/01/89)

Hi, folks,

     Could someone help me to find the bug in my code?

     I tried to make some exercises on Colormap, nevertheless, I got an
unsolvable problem.  If I use the default colormap, everything is going
very fine.  However, if I create the colormap with the default visual,
the XStoreNamedColor seems not working.

     Thanks for any comment and suggestion.
                                    -- yin-cheng


##-----------------  tst.c start from here  -------------------##
#include <stdio.h>
#include <X11/Xlib.h>

#define NPLANES               1
#define NCOLORS               4

main ()
{
    unsigned long plane_mask[NPLANES], pixels[NCOLORS], pixel;
    Display *display;
    XColor color;
    Window simple;
    Colormap cmap;
    int flag = 0;
    GC gc;

    if (!(display = XOpenDisplay (NULL))) {
        fprintf (stderr, "cannot open display\n");
        exit (-1);
    }

    simple = XCreateSimpleWindow (display, DefaultRootWindow (display),
             100, 100, 300, 300, 2, 0);

==>    cmap = DefaultColormap (display, DefaultScreen (display));

==>/*    cmap = XCreateColormap (display, simple,
            DefaultVisual (display, DefaultScreen (display)), AllocNone);
         XSetWindowColormap (display, simple, cmap);*/

    color.red = 255 << 8;
    color.green = 0;
    color.blue = 0;
    if (!XAllocColor (display, cmap, &color))
        fprintf (stderr, "allocation error\n");

    color.red = 0;
    color.green = 255 << 8;
    color.blue = 0;
    if (!XAllocColor (display, cmap, &color))
        fprintf (stderr, "allocation error\n");

    color.red = 0;
    color.green = 0;
    color.blue = 255 << 8;
    if (!XAllocColor (display, cmap, &color))
        fprintf (stderr, "allocation error\n");

    if (!XAllocColorCells (display, cmap, False, plane_mask, NPLANES,
            pixels, NCOLORS)) {
        fprintf (stderr, "cannot allocate any color cell\n");
        exit (-1);
    }

    pixel = pixels[0];
    gc = XCreateGC (display, simple, 0, NULL);
    XSetForeground (display, gc, pixel);

    XMapWindow (display, simple);
    XFlush (display);
    sleep (5);         /*  window must come up before next drawing command
                          is executed */

    XFillRectangle (display, simple, gc, 50, 50, 200, 200);
    XFlush (display);

    fprintf (stderr, "press '^C' to exit! ");
    fflush (stderr);
    while (1) {
        if (flag) {
            flag = 0;
            XStoreNamedColor (display, cmap, "gold", pixel, ~0);
            XFlush (display);
            sleep (1);
        }
        else {
            flag = 1;
            XStoreNamedColor (display, cmap, "navy", pixel, ~0);
            XFlush (display);
            sleep (1);
        }
    }
}

kam@hpcvlx.HP.COM (Keith Marchington) (02/02/89)

The program as written works fine on my HP9000 Model 360.  My guess is that
you are not running a window manager that handles the installation of the
colormap that you created.  You might try running it with wm or twm as 
your window manager.  Both of those handle installation of colormaps.

Keith Marchington
HP Corvallis