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.ovl. < prev    next >
Text File  |  1989-12-08  |  4KB  |  133 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, background_pixel, overlay_pixel_1, overlay_pixel_2;
  13. extern unsigned long overlay_plane_mask;
  14.  
  15. #define MAX_COLORS 2
  16. #define MAX_PLANES 1
  17. #define MAX_CELLS 4       /* MAX_COLORS * 2 ^ MAX_PLANES */
  18. #define CANNOT_OVERLAY 0
  19. #define CAN_OVERLAY 1
  20.  
  21. static char *visual_class[] = {
  22. "StaticGray",
  23. "GrayScale",
  24. "StaticColor",
  25. "PseudoColor",
  26. "TrueColor",
  27. "DirectColor"
  28. };
  29.  
  30. int
  31. get_colors()
  32. {
  33.         int default_depth;
  34.     static char *name[] = {"Red", "Yellow", "Green", "Green"};
  35.     XColor exact_defs[MAX_CELLS];
  36.     Colormap default_cmap;
  37.     int ncolors = 4;
  38.     int plane_masks[MAX_PLANES];
  39.     int colors[MAX_COLORS];
  40.     int i;
  41.     XVisualInfo visual_info;
  42.     int class;
  43.  
  44.     default_depth = DefaultDepth(display, screen_num);
  45.     default_cmap   = DefaultColormap(display, screen_num);
  46.     if (default_depth == 1) {
  47.         /* must be StaticGray, use black and white */
  48.         background_pixel = WhitePixel(display, screen_num);
  49.         foreground = BlackPixel(display, screen_num);
  50.         printf("using black and white\n");
  51.         return(CANNOT_OVERLAY);
  52.     }
  53.  
  54.     if (!XMatchVisualInfo(display, screen_num, default_depth, PseudoColor, &visual_info)) {
  55.         if (!XMatchVisualInfo(display, screen_num, default_depth, DirectColor, &visual_info)) {
  56.         /* No PseudoColor or TrueColor visual available at default_depth.
  57.          * Some applications might try for a GrayScale visual 
  58.          * here if they can use gray to advantage, before 
  59.          * giving up and using black and white.
  60.          */
  61.                 background_pixel = WhitePixel(display, screen_num);
  62.                    foreground = BlackPixel(display, screen_num);
  63.         printf("using black and white\n");
  64.         return(CANNOT_OVERLAY);
  65.         }
  66.     }
  67.  
  68.     /* got PseudoColor or TrueColor visual at default depth */
  69.  
  70.     /* The visual we found is not necessarily the 
  71.      * default visual, and therefore it is not necessarily
  72.      * the one we used to create our window.  However,
  73.      * we now know for sure that color is supported, so the
  74.      * following code will work (or fail in a controlled way).
  75.      */
  76.  
  77.        if (XAllocColorCells (display, default_cmap, False, plane_masks, 1, colors, 2) == 0) {
  78.         /* Can't get enough read/write cells to overlay.
  79.          * Try at least to get three colors. */
  80.            if (XAllocColorCells (display, default_cmap, False, plane_masks, 0, colors, 3) == 0) {
  81.             /* Can't even get that.  Give up and
  82.              * use black and white */
  83.                        background_pixel = WhitePixel(display, screen_num);
  84.                        foreground = BlackPixel(display, screen_num);
  85.             printf("using black and white\n");
  86.             return(CANNOT_OVERLAY);
  87.         }
  88.         else
  89.             ncolors = 3;
  90.     }
  91.           
  92.     /* allocated three or four colorcells succesfully,
  93.      * now set their colors - three and four
  94.      * are set to the same RGB values */
  95.     for (i = 0; i < ncolors; i++)
  96.     {
  97.         if (!XParseColor (display, default_cmap, name[i], &exact_defs[i])) {
  98.             fprintf(stderr, "basic: color name %s not in database", name[i]);
  99.             exit(0);
  100.         }
  101.         /* this needed before calling XStoreColors */
  102.             exact_defs[i].flags = DoRed | DoGreen | DoBlue;
  103.     }
  104.     printf("got RGB values\n");
  105.  
  106.     /* set pixel value in struct to the allocated ones */
  107.     exact_defs[0].pixel = colors[0];
  108.     exact_defs[1].pixel = colors[1];
  109.     exact_defs[2].pixel = colors[0] | plane_masks[0];
  110.     exact_defs[3].pixel = colors[1] | plane_masks[0];
  111.  
  112.     /* this sets the color of the read/write cells */
  113.     XStoreColors (display, default_cmap, exact_defs, ncolors);
  114.     printf("stored colors\n");
  115.  
  116.     background_pixel = exact_defs[0].pixel;
  117.     foreground = exact_defs[1].pixel;
  118.     printf("set f and g\n");
  119.     if (ncolors == 4) {
  120.                    overlay_pixel_1 = exact_defs[2].pixel;
  121.                    overlay_pixel_2 = exact_defs[3].pixel;
  122.                    overlay_plane_mask = plane_masks[0];
  123.         printf("set can\n");
  124.         return(CAN_OVERLAY);
  125.     }
  126.     else {
  127.         /* this must be used as a normal color, not overlay */
  128.         overlay_pixel_1 = exact_defs[2].pixel;
  129.         printf("set can't\n");
  130.         return(CANNOT_OVERLAY);
  131.     }
  132. }
  133.