home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xlibpr3.zip / basicwin / color / get_color.rw.c < prev    next >
C/C++ Source or Header  |  1989-12-08  |  3KB  |  94 lines

  1. /*
  2.  * Copyright 1989 O'Reilly and Associates, Inc.
  3.  * See ../Copyright for complete rights and liability information.
  4.  */
  5. #include <X11/Xlib.h>
  6. #include <X11/Xutil.h>
  7. #include <X11/Xos.h>
  8. #include <stdio.h>
  9.  
  10. extern Display *display;
  11. extern int screen_num;
  12. extern unsigned long foreground_pixel, background_pixel, border_pixel;
  13.  
  14. #define MAX_COLORS 3
  15.  
  16. get_colors()
  17. {
  18.         int default_depth;
  19.         Visual *default_visual;
  20.     static char *name[] = {"Red", "Yellow", "Green"};
  21.     XColor exact_defs[MAX_COLORS];
  22.     Colormap default_cmap;
  23.     int ncolors = MAX_COLORS;
  24.     int plane_masks[1];
  25.     int colors[MAX_COLORS];
  26.     int i;
  27.     XVisualInfo visual_info;
  28.     int class;
  29.  
  30.     class = PseudoColor;
  31.     default_depth = DefaultDepth(display, screen_num);
  32.         default_visual = DefaultVisual(display, screen_num);
  33.     default_cmap   = DefaultColormap(display, screen_num);
  34.     if (default_depth == 1) {
  35.         /* must be StaticGray, use black and white */
  36.         border_pixel = BlackPixel(display, screen_num);
  37.         background_pixel = WhitePixel(display, screen_num);
  38.         foreground_pixel = BlackPixel(display, screen_num);
  39.         return(0);
  40.     }
  41.  
  42.     if (!XMatchVisualInfo(display, screen_num, default_depth, PseudoColor, &visual_info)) {
  43.         if (!XMatchVisualInfo(display, screen_num, default_depth, DirectColor, &visual_info)) {
  44.         /* No PseudoColor visual available at default_depth.
  45.          * Some applications might try for a GrayScale visual 
  46.          * here if they can use gray to advantage, before 
  47.          * giving up and using black and white.
  48.          */
  49.         border_pixel = BlackPixel(display, screen_num);
  50.         background_pixel = WhitePixel(display, screen_num);
  51.         foreground_pixel = BlackPixel(display, screen_num);
  52.         return(0);
  53.         }
  54.     }
  55.  
  56.     /* got PseudoColor visual at default_depth */
  57.  
  58.     /* The visual we found is not necessarily the 
  59.      * default visual, and therefore it is not necessarily
  60.      * the one we used to create our window.  However,
  61.      * we now know for sure that color is supported, so the
  62.      * following code will work (or fail in a controlled way).
  63.      */
  64.  
  65.     /* allocate as many cells as we can */
  66.     ncolors = MAX_COLORS;
  67.     while (1) {
  68.            if (XAllocColorCells (display, default_cmap, False, plane_masks, 0, colors, ncolors))
  69.                   break;
  70.     ncolors--;
  71.     if (ncolors = 0)
  72.         fprintf(stderr, "basic: couldn't allocate read/write colors\n");
  73.         exit(0);
  74.     }
  75.  
  76.     printf("basic: allocated %d read/write color cells\n", ncolors);
  77.  
  78.     for (i = 0; i < ncolors; i++) {
  79.             if (!XParseColor (display, default_cmap, name[i], &exact_defs[i])) {
  80.             fprintf(stderr, "basic: color name %s not in database", name[i]);
  81.             exit(0);
  82.         }
  83.  
  84.         /* set pixel value in struct to the allocated one */
  85.         exact_defs[i].pixel = colors[i];
  86.     }
  87.  
  88.     /* this sets the color of read/write cell */
  89.     XStoreColors (display, default_cmap, exact_defs, ncolors);
  90.     border_pixel = colors[0];
  91.     background_pixel = colors[1];
  92.     foreground_pixel = colors[2];
  93. }
  94.