next up previous contents
Next: Programming Tips Up: Using Colour Previous: Allocating Color cells

Read Only Colorcells: An easy Example


#include <stdio.h> 
#include <X11/X.h>
#include <X11/Xlib.h> 
#define NAME "red"  
 main (argc, argv) 
 char   *argv[]; 
 int     argc; 
     XColor   color_cell, truecolor;
     XSetWindowAttributes attributes; 
     Visual *visual; 
     int depth; 
     int which_color,which_cell,which_plane; 
     Display *display; 
     Window  win; 
     GC gc; 
     Colormap cmap; 
     XEvent event; 
     int screen;
     setbuf (stdout, NULL); 
     setbuf (stderr, NULL); 

     display = XOpenDisplay(NULL); 
     screen = DefaultScreen(display);
     visual = DefaultVisual(display,screen); 
     depth  = DefaultDepth(display,screen); 
     cmap= XDefaultColormap(display,screen); 

     attributes.background_pixel      = XWhitePixel(display,screen); 
     attributes.border_pixel          = XBlackPixel(display,screen); 
     attributes.override_redirect     = 1; 
     win = XCreateWindow(display,XRootWindow(display,screen),0,0,507,426,5,
           depth, InputOutput,visual,
           CWBackPixel | CWBorderPixel | CWOverrideRedirect,&attributes);
     XSelectInput(display,win,ButtonPressMask+ KeyPressMask) ; 
       /* first by RGB values */
       color_cell.flags= DoRed | DoGreen | DoBlue; = 10000; = 40000; = 60000;        
       if (XAllocColor(display,cmap,&color_cell)==0)
           fprintf("XAllocColor failed\n");
       XDrawImageString(display,win,gc,250,50,"RGB color",9); 
       /* now by name */
       if (XAllocNamedColor(display,cmap,
                             NAME, &color_cell, &truecolor) == 0) {
            fprintf(stderr, "Color '%s' unknown\n", NAME);
       if ( != ||
       != ||
       != {
            fprintf(stderr, "Warning: %s color may be wrong\n", NAME);
       XDrawImageString(display,win,gc,250,80, "named color",11); 
       printf("now press button: \n"); 
       }while(event.type != ButtonPress); 
       printf("closing display\n"); 

The following routine consults the database /usr/lib/X11/rgb.txt which contains RGB info on the named colors

Status XLookupColor(display,cmap,spec,screen_def,exact_def);
   char *spec;         /* case ignored */
   XColor *screen_def; /* RETURNS the values actually used in the colormap */
   XColor *exact_def;  /* RETURNS the closest color provided by the hardware*/
useful little routines are
Status XParseColor(display,colormap,spec, screen_def)
    char *spec;        /* name of colour or spec string        */
         /* a spec string must begin with a hash sign and then */
         /* contain 3,6,9 or 12 hex digits with no spaces      */
         /* these are taken in groups of 1,2,3 or 4 respectively*/
         /* to give the desired RGB value for the colour       */
    Color *screen_def;    /* RETURN nearest RGB supported by hardware */

   XColor *def; /*supplies pixel value, RETURNS RGB */
XQueryColors(display,cmap,defs, ncolors);
   XColor defs[ncolors]; /*supplies pixel values, RETURNS RGB */
X11R5 introduced a more sophisticated color system called Xcms which is beyond the scope of this handout.

Tim Love
Mon Mar 11 17:03:18 GMT 1996