[comp.windows.x] Xmas present for PseudoColor folks

etaylor@wilkins.iaims.bcm.tmc.edu (Eric Taylor) (12/15/90)

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

#define NHUE 3
#define RANGE (256 / NHUE)
#define NCOLORS (NHUE * RANGE)

static
get_hsv(i,col)
	XColor *col ;
{
	int hue = i / RANGE ;
	int resid = i % RANGE ;
	int r =
		(hue == 0) ?
			resid :
		(hue == 1) ?
			RANGE - 1 - resid :
			0 ;
	int b =
		(hue == 0) ?
			RANGE - 1 - resid :
		(hue == 2) ?
			resid :
			0 ;
	int g =
		(hue == 1) ?
			resid :
		(hue == 2) ?
			RANGE - 1 - resid :
			0 ;
	col->red = r * 65536 / RANGE ;
	col->green = g * 65536 / RANGE ;
	col->blue = b * 65536 / RANGE ;
	col->flags = DoRed | DoBlue | DoGreen ;
	col->pixel = i ;
}

main(argc,argv)
	char *argv[] ;
{
	Display *dpy = XOpenDisplay(NULL) ;
	int screen ;
	Window root ;
	Visual *visual ;
	Window w ;
	Colormap cmap ;
	XColor colors[NCOLORS] ;
	GC gc ;
	if (dpy == NULL)
	{
		fprintf(stderr,"Could not open display\n") ;
		exit(-1) ;
	}
	screen = DefaultScreen(dpy) ;
	root = DefaultRootWindow(dpy) ;
	visual = DefaultVisual(dpy,screen) ;
	if (visual->class != PseudoColor && visual->class != DirectColor || visual->map_entries < NCOLORS)
	{
		fprintf(stderr,"Can only work with 256-PseudoColor\n") ;
		exit(-1) ;
	}
	{
		int j ;
		for(j = 0; j < NCOLORS; j++)
		{
			get_hsv(j,&(colors[j])) ;
		}
	}
	cmap = XCreateColormap(dpy,root,visual,AllocAll) ;
	XStoreColors(dpy,cmap,colors,NCOLORS) ;
	{
		XSetWindowAttributes attr ;
		attr.colormap = cmap ;
		w = XCreateWindow(dpy,root,0,0,600,600,
			1,DefaultDepth(dpy,screen),
			InputOutput,visual,
			CWColormap,&attr) ;
		XSelectInput(dpy,w,ExposureMask | ButtonPressMask) ;
		XMapWindow(dpy,w) ;
	}
	gc = XCreateGC(dpy,w,0L,NULL) ;
	{
		int i = 0 ;
		for(;;)
		{
			int j ;
			int first = colors[0].pixel ;
			for(j = 0; j < NCOLORS - 1; j++)
			{
				colors[j].pixel = colors[j + 1].pixel ;
			}
			colors[j].pixel = first ;
			XStoreColors(dpy,cmap,colors,NCOLORS) ;
			XFlush(dpy) ;
			if (XPending(dpy))
			{
				XEvent event ;
				XNextEvent(dpy,&event) ;
				if (event.type == ButtonPress)
				{
					exit(0) ;
				}
				else if (event.type == Expose &&
				         event.xexpose.count == 0)
				{
					int i ;
					for(i = 0; i < NCOLORS; i++)
					{
						int width = (i + 1) * 2 ;
						int radius = ((NCOLORS - i - 1) + 1) * 2 ;
						XSetForeground(dpy,gc,i) ;
						XDrawRectangle(dpy,w,gc,300 - i,300 - i,width,width) ;
					}
				}
			}
			i = (i + 1) % NCOLORS ;
		}
	}
}
--
					Eric Taylor
					Baylor College of Medicine
					etaylor@wilkins.bcm.tmc.edu
					(713) 798-3776