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.ro.c < prev    next >
C/C++ Source or Header  |  1989-12-08  |  3KB  |  105 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 Screen *screen_ptr;
  13. extern unsigned long foreground_pixel, background_pixel, border_pixel;
  14. extern char *progname;
  15.  
  16. #define MAX_COLORS 3
  17.  
  18. static char *visual_class[] = {
  19. "StaticGray",
  20. "GrayScale",
  21. "StaticColor",
  22. "PseudoColor",
  23. "TrueColor",
  24. "DirectColor"
  25. };
  26.  
  27. get_colors()
  28. {
  29.         int default_depth;
  30.         Visual *default_visual;
  31.     static char *name[] = {"Red", "Yellow", "Green"};
  32.     XColor exact_def;
  33.     Colormap default_cmap;
  34.     int ncolors = 0;
  35.     int colors[MAX_COLORS];
  36.     int i = 5;
  37.     XVisualInfo visual_info;
  38.     
  39.     /* Try to allocate colors for PseudoColor, TrueColor, 
  40.      * DirectColor, and StaticColor.  Use black and white
  41.      * for StaticGray and GrayScale */
  42.  
  43.     default_depth = DefaultDepth(display, screen_num);
  44.         default_visual = DefaultVisual(display, screen_num);
  45.     default_cmap   = DefaultColormap(display, screen_num);
  46.     if (default_depth == 1) {
  47.         /* must be StaticGray, use black and white */
  48.         border_pixel = BlackPixel(display, screen_num);
  49.         background_pixel = WhitePixel(display, screen_num);
  50.         foreground_pixel = BlackPixel(display, screen_num);
  51.         return(0);
  52.     }
  53.  
  54.     while (!XMatchVisualInfo(display, screen_num, default_depth, /* visual class */i--, &visual_info))
  55.         ;
  56.     printf("%s: found a %s class visual at default_depth.\n", progname, visual_class[++i]);
  57.     
  58.     if (i < 2) {
  59.         /* No color visual available at default_depth.
  60.          * Some applications might call XMatchVisualInfo
  61.          * here to try for a GrayScale visual 
  62.          * if they can use gray to advantage, before 
  63.          * giving up and using black and white.
  64.          */
  65.         border_pixel = BlackPixel(display, screen_num);
  66.         background_pixel = WhitePixel(display, screen_num);
  67.         foreground_pixel = BlackPixel(display, screen_num);
  68.         return(0);
  69.     }
  70.  
  71.     /* otherwise, got a color visual at default_depth */
  72.  
  73.     /* The visual we found is not necessarily the 
  74.      * default visual, and therefore it is not necessarily
  75.      * the one we used to create our window.  However,
  76.      * we now know for sure that color is supported, so the
  77.      * following code will work (or fail in a controlled way).
  78.      * Let's check just out of curiosity: */
  79.     if (visual_info.visual != default_visual)
  80.         printf("%s: PseudoColor visual at default depth is not default visual!\nContinuing anyway...\n", progname);
  81.  
  82.     for (i = 0; i < MAX_COLORS; i++) {
  83.         printf("allocating %s\n", name[i]);
  84.         if (!XParseColor (display, default_cmap, name[i], &exact_def)) {
  85.             fprintf(stderr, "%s: color name %s not in database", progname, name[i]);
  86.             exit(0);
  87.         }
  88.         printf("The RGB values from the database are %d, %d, %d\n", exact_def.red, exact_def.green, exact_def.blue);
  89.            if (!XAllocColor(display, default_cmap, &exact_def)) {
  90.             fprintf(stderr, "%s: can't allocate color: all colorcells allocated and no matching cell found.\n", progname);
  91.         exit(0);
  92.         }
  93.         printf("The RGB values actually allocated are %d, %d, %d\n", exact_def.red, exact_def.green, exact_def.blue);
  94.         colors[i] = exact_def.pixel;
  95.         ncolors++;
  96.     }
  97.  
  98.     printf("%s: allocated %d read-only color cells\n", progname, ncolors);
  99.  
  100.     border_pixel = colors[0];
  101.     background_pixel = colors[1];
  102.     foreground_pixel = colors[2];
  103.     return(1);
  104. }
  105.