home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.3 Development Libraries / SGI IRIX 6.3 Development Libraries.iso / dist6.3 / ViewKit_dev.idb / usr / share / src / ViewKit / Applications / GLX / copycmap.c.z / copycmap.c
Encoding:
C/C++ Source or Header  |  1996-09-20  |  3.3 KB  |  116 lines

  1. /*
  2.  * This file includes the function CopyGlColormap that creates a new
  3.  * colormap for a GlxMDraw widget.  The colors specified
  4.  * by the colorInfo structure (describe in copycmap.h) are allocated
  5.  * in the colormap
  6.  * When possible, they will match the pixel value in parent,
  7.  * to avoid having incorrect colors installed in the X-based portion of
  8.  * the application.
  9.  * This function cannot be called until the widget is realized.
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <X11/Intrinsic.h>
  14. #include <X11/StringDefs.h>
  15. #include <X11/Xirisw/GlxMDraw.h>
  16. #include <X11/Xm/Xm.h>
  17. #include "copycmap.h"
  18.  
  19. void
  20. CopyGlColormap(glw, colorInfo, colorInfoSize)
  21. Widget glw;
  22. struct glxcColorInfo *colorInfo;
  23. int colorInfoSize;
  24. {
  25.     XVisualInfo *visualInfo;
  26.     int depth;
  27.     int maxcolor;
  28.     Colormap colormap;
  29.     Colormap pmap;
  30.     Arg args[10];
  31.     int n;
  32.     register i;
  33.     XColor rgb_db_def;
  34.     Display *display = XtDisplay(glw);
  35.  
  36.     /* first get the visual and depth*/
  37.     n = 0;
  38.     XtSetArg(args[n], XtNvisual, &visualInfo); n++;
  39.     XtSetArg(args[n], XtNdepth, &depth); n++;
  40.     XtGetValues(glw, args, n);
  41.     maxcolor = 1<<depth;
  42.     /* Create a new colormap */
  43.     colormap = XCreateColormap(display, XtWindow(glw),
  44.                    visualInfo->visual, AllocAll);
  45.     /* Add the colormap to the window */
  46.     n = 0;
  47.     XtSetArg(args[n], XtNcolormap, colormap); n++;
  48.     XtSetValues(glw, args, n);
  49.  
  50.     /* get the parent's colormap */
  51.     n = 0;
  52.     XtSetArg(args[n], XtNcolormap, &pmap); n++;
  53.     XtGetValues(XtParent(glw), args, n);
  54.  
  55.     /* for each color in the colorInfo, determine the correct pixel value */
  56.     for (i=0; i<colorInfoSize; i++)
  57.     {
  58.     colorInfo[i].color.flags = DoRed|DoGreen|DoBlue;
  59.     switch (colorInfo[i].type)
  60.     {
  61.     case GLXC_ABSOLUTE:
  62.         colorInfo[i].color.pixel = (Pixel)colorInfo[i].value;
  63.         XQueryColor (display, pmap, &colorInfo[i].color);
  64.         break;
  65.     case GLXC_NAMED:
  66.         if (!XAllocNamedColor(display, pmap, (char *)colorInfo[i].value,
  67.                   &colorInfo[i].color, &rgb_db_def))
  68.         {
  69.         fprintf (stderr, "could not allocate %s\n", (char *)colorInfo[i].value);
  70.         exit (1);
  71.         }
  72.         break;
  73.     case GLXC_RESOURCE:
  74.         n = 0;
  75.         XtSetArg(args[n], colorInfo[i].value, &colorInfo[i].color.pixel); n++;
  76.         XtGetValues(glw, args, n);
  77.         XQueryColor (display, pmap, &colorInfo[i].color);
  78.         break;
  79.     default:
  80.         fprintf (stderr, "unknown type in colorInfo\n");
  81.         exit (1);
  82.     }
  83.     }
  84.     /* We have determined all the colors.  Loop through the colors and
  85.      * make sure that they fit into the GL colormap.  If they do not,
  86.      * choose another color that fits into the colormap.
  87.      * After this check, store the colors into the colormap,
  88.      * and save in the variables */
  89.     for (i=0; i<colorInfoSize; i++)
  90.     {
  91.     if (colorInfo[i].color.pixel >= maxcolor)
  92.     {
  93.         /* it was too high.  find another pixel to use.
  94.          * start at the highest color and work down
  95.          */
  96.         register try, check;
  97.  
  98.         for (try = maxcolor-1; try >= 0 ; try--)
  99.         {
  100.         for (check = 0; check < colorInfoSize; check++)
  101.         {
  102.             if (colorInfo[check].color.pixel == try)
  103.             break;
  104.         }
  105.         if (check == colorInfoSize) /* didn't find a match */
  106.         {
  107.             colorInfo[i].color.pixel = try;
  108.             break;
  109.         }
  110.         }
  111.     }
  112.     XStoreColor(display, colormap, &colorInfo[i].color);
  113.     *colorInfo[i].result = (Colorindex)colorInfo[i].color.pixel;
  114.     }
  115. }    
  116.