Actual source code: xcolor.c


  2: /*
  3:     Code for managing color the X implementation of the PetscDraw routines.

  5:     Currently we default to using cmapping[0 to PETSC_DRAW_BASIC_COLORS-1] for the basic colors and
  6:     cmapping[DRAW_BASIC_COLORS to 255] for countour plots.

  8: */
  9: #include <../src/sys/classes/draw/impls/x/ximpl.h>
 10: #include <X11/Xatom.h>

 12: static const char *colornames[PETSC_DRAW_BASIC_COLORS] = {"white",   "black",     "red",      "green",      "cyan",      "blue",       "magenta",   "aquamarine",      "forestgreen", "orange",        "violet",
 13:                                                           "brown",   "pink",      "coral",    "gray",       "yellow",    "gold",       "lightpink", "mediumturquoise", "khaki",       "dimgray",       "yellowgreen",
 14:                                                           "skyblue", "darkgreen", "navyblue", "sandybrown", "cadetblue", "powderblue", "deeppink",  "thistle",         "limegreen",   "lavenderblush", "plum"};

 16: /*
 17:    Sets up a color map for a display. This is shared by all the windows
 18:   opened on that display; this is to save time when windows are open so
 19:   each one does not have to create its own color map which can take 15 to 20 seconds

 21:      This is new code written 2/26/1999 Barry Smith,I hope it can replace
 22:   some older,rather confusing code.

 24:      The calls to XAllocNamedColor() and XAllocColor() are very slow
 25:      because we have to request from the X server for each
 26:      color. Could not figure out a way to request a large number at the
 27:      same time.

 29:    IMPORTANT: this code will fail if user opens windows on two different
 30:   displays: should add error checking to detect this. This is because all windows
 31:   share the same gColormap and gCmapping.

 33: */
 34: static Colormap          gColormap = 0;
 35: static PetscDrawXiPixVal gCmapping[PETSC_DRAW_MAXCOLOR];
 36: static unsigned char     gCpalette[PETSC_DRAW_MAXCOLOR][3];

 38: PetscErrorCode PetscDrawSetUpColormap_Shared(Display *display, int screen, Visual *visual, Colormap colormap)
 39: {
 40:   int           i, k, ncolors = PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS;
 41:   unsigned char R[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 42:   unsigned char G[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 43:   unsigned char B[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 44:   XColor        colordef, ecolordef;
 45:   PetscBool     fast = PETSC_FALSE;

 47:   if (colormap) gColormap = colormap;
 48:   else gColormap = DefaultColormap(display, screen);

 50:   /* set the basic colors into the color map */
 51:   for (i = 0; i < PETSC_DRAW_BASIC_COLORS; i++) {
 52:     XAllocNamedColor(display, gColormap, colornames[i], &colordef, &ecolordef);
 53:     gCmapping[i]    = colordef.pixel;
 54:     gCpalette[i][0] = (unsigned char)(colordef.red >> 8);
 55:     gCpalette[i][1] = (unsigned char)(colordef.green >> 8);
 56:     gCpalette[i][2] = (unsigned char)(colordef.blue >> 8);
 57:   }

 59:   /* set the contour colors into the colormap */
 60:   PetscOptionsGetBool(NULL, NULL, "-draw_fast", &fast, NULL);
 61:   PetscDrawUtilitySetCmap(NULL, ncolors, R, G, B);
 62:   for (i = 0, k = PETSC_DRAW_BASIC_COLORS; i < ncolors; i++, k++) {
 63:     colordef.red   = (unsigned short)(R[i] << 8);
 64:     colordef.green = (unsigned short)(G[i] << 8);
 65:     colordef.blue  = (unsigned short)(B[i] << 8);
 66:     colordef.flags = DoRed | DoGreen | DoBlue;
 67:     colordef.pixel = gCmapping[PETSC_DRAW_BLACK];
 68:     if (!fast) XAllocColor(display, gColormap, &colordef);
 69:     gCmapping[k]    = colordef.pixel;
 70:     gCpalette[k][0] = R[i];
 71:     gCpalette[k][1] = G[i];
 72:     gCpalette[k][2] = B[i];
 73:   }

 75:   PetscInfo(NULL, "Successfully allocated colors\n");
 76:   return 0;
 77: }

 79: /*
 80:     Keep a record of which pixel numbers in the cmap have been
 81:   used so far; this is to allow us to try to reuse as much of the current
 82:   colormap as possible.
 83: */
 84: static PetscBool cmap_pixvalues_used[PETSC_DRAW_MAXCOLOR];
 85: static int       cmap_base = 0;

 87: PetscErrorCode PetscDrawSetUpColormap_Private(Display *display, int screen, Visual *visual, Colormap colormap)
 88: {
 89:   int           found, i, k, ncolors = PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS;
 90:   unsigned char R[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 91:   unsigned char G[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 92:   unsigned char B[PETSC_DRAW_MAXCOLOR - PETSC_DRAW_BASIC_COLORS];
 93:   Colormap      defaultmap = DefaultColormap(display, screen);
 94:   XColor        colordef;
 95:   PetscBool     fast = PETSC_FALSE;

 97:   if (colormap) gColormap = colormap;
 98:   else gColormap = XCreateColormap(display, RootWindow(display, screen), visual, AllocAll);

100:   cmap_base = 0;

102:   PetscMemzero(cmap_pixvalues_used, sizeof(cmap_pixvalues_used));

104:   /* set the basic colors into the color map */
105:   for (i = 0; i < PETSC_DRAW_BASIC_COLORS; i++) {
106:     XParseColor(display, gColormap, colornames[i], &colordef);
107:     /* try to allocate the color in the default-map */
108:     found = XAllocColor(display, defaultmap, &colordef);
109:     /* use it, if it it exists and is not already used in the new colormap */
110:     if (found && colordef.pixel < PETSC_DRAW_MAXCOLOR && !cmap_pixvalues_used[colordef.pixel]) {
111:       cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
112:       /* otherwise search for the next available slot */
113:     } else {
114:       while (cmap_pixvalues_used[cmap_base]) cmap_base++;
115:       colordef.pixel                   = cmap_base;
116:       cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
117:     }
118:     XStoreColor(display, gColormap, &colordef);
119:     gCmapping[i]    = colordef.pixel;
120:     gCpalette[i][0] = (unsigned char)(colordef.red >> 8);
121:     gCpalette[i][1] = (unsigned char)(colordef.green >> 8);
122:     gCpalette[i][2] = (unsigned char)(colordef.blue >> 8);
123:   }

125:   /* set the contour colors into the colormap */
126:   PetscOptionsGetBool(NULL, NULL, "-draw_fast", &fast, NULL);
127:   PetscDrawUtilitySetCmap(NULL, ncolors, R, G, B);
128:   for (i = 0, k = PETSC_DRAW_BASIC_COLORS; i < ncolors; i++, k++) {
129:     colordef.red   = (unsigned short)(R[i] << 8);
130:     colordef.green = (unsigned short)(G[i] << 8);
131:     colordef.blue  = (unsigned short)(B[i] << 8);
132:     colordef.flags = DoRed | DoGreen | DoBlue;
133:     colordef.pixel = gCmapping[PETSC_DRAW_BLACK];
134:     if (!fast) {
135:       /* try to allocate the color in the default-map */
136:       found = XAllocColor(display, defaultmap, &colordef);
137:       /* use it, if it it exists and is not already used in the new colormap */
138:       if (found && colordef.pixel < PETSC_DRAW_MAXCOLOR && !cmap_pixvalues_used[colordef.pixel]) {
139:         cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
140:         /* otherwise search for the next available slot */
141:       } else {
142:         while (cmap_pixvalues_used[cmap_base]) cmap_base++;
143:         colordef.pixel                   = cmap_base;
144:         cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
145:       }
146:       XStoreColor(display, gColormap, &colordef);
147:     }
148:     gCmapping[k]    = colordef.pixel;
149:     gCpalette[k][0] = R[i];
150:     gCpalette[k][1] = G[i];
151:     gCpalette[k][2] = B[i];
152:   }

154:   PetscInfo(NULL, "Successfully allocated colors\n");
155:   return 0;
156: }

158: PetscErrorCode PetscDrawSetUpColormap_X(Display *display, int screen, Visual *visual, Colormap colormap)
159: {
160:   PetscBool   sharedcolormap = PETSC_FALSE;
161:   XVisualInfo vinfo;

163:   PetscOptionsGetBool(NULL, NULL, "-draw_x_shared_colormap", &sharedcolormap, NULL);
164:   /*
165:      Need to determine if window supports allocating a private colormap,
166:   */
167:   if (XMatchVisualInfo(display, screen, 24, StaticColor, &vinfo) || XMatchVisualInfo(display, screen, 24, TrueColor, &vinfo) || XMatchVisualInfo(display, screen, 16, StaticColor, &vinfo) || XMatchVisualInfo(display, screen, 16, TrueColor, &vinfo) || XMatchVisualInfo(display, screen, 15, StaticColor, &vinfo) || XMatchVisualInfo(display, screen, 15, TrueColor, &vinfo))
168:     sharedcolormap = PETSC_TRUE;
169:   /*
170:      Generate the X colormap object
171:   */
172:   if (sharedcolormap) {
173:     PetscDrawSetUpColormap_Shared(display, screen, visual, colormap);
174:   } else {
175:     PetscDrawSetUpColormap_Private(display, screen, visual, colormap);
176:   }
177:   return 0;
178: }

180: PETSC_INTERN PetscErrorCode PetscDrawSetColormap_X(PetscDraw_X *, Colormap);

182: PetscErrorCode PetscDrawSetColormap_X(PetscDraw_X *XiWin, Colormap colormap)
183: {
184:   PetscBool fast = PETSC_FALSE;

186:   PetscOptionsGetBool(NULL, NULL, "-draw_fast", &fast, NULL);
188:   if (!gColormap) PetscDrawSetUpColormap_X(XiWin->disp, XiWin->screen, XiWin->vis, colormap);
189:   XiWin->cmap     = gColormap;
190:   XiWin->cmapsize = fast ? PETSC_DRAW_BASIC_COLORS : PETSC_DRAW_MAXCOLOR;
191:   PetscMemcpy(XiWin->cmapping, gCmapping, sizeof(XiWin->cmapping));
192:   PetscMemcpy(XiWin->cpalette, gCpalette, sizeof(XiWin->cpalette));
193:   XiWin->background = XiWin->cmapping[PETSC_DRAW_WHITE];
194:   XiWin->foreground = XiWin->cmapping[PETSC_DRAW_BLACK];
195:   return 0;
196: }

198: PetscErrorCode PetscDrawXiColormap(PetscDraw_X *XiWin)
199: {
200:   return PetscDrawSetColormap_X(XiWin, (Colormap)0);
201: }

203: /*
204:     Color in X is many-layered.  The first layer is the "visual",a
205:     immutable attribute of a window set when the window is
206:     created.

208:     The next layer is the colormap.  The installation of colormaps is
209:     the business of the window manager (in some distant later release).
210: */

212: /*
213:     This routine gets the visual class (PseudoColor, etc) and returns
214:     it.  It finds the default visual.  Possible returns are
215:         PseudoColor
216:         StaticColor
217:         DirectColor
218:         TrueColor
219:         GrayScale
220:         StaticGray
221:  */
222: PetscErrorCode PetscDrawXiSetVisualClass(PetscDraw_X *XiWin)
223: {
224:   XVisualInfo vinfo;

226:   if (XMatchVisualInfo(XiWin->disp, XiWin->screen, 24, DirectColor, &vinfo)) {
227:     XiWin->vis = vinfo.visual;
228:   } else if (XMatchVisualInfo(XiWin->disp, XiWin->screen, 8, PseudoColor, &vinfo)) {
229:     XiWin->vis = vinfo.visual;
230:   } else if (XMatchVisualInfo(XiWin->disp, XiWin->screen, DefaultDepth(XiWin->disp, XiWin->screen), PseudoColor, &vinfo)) {
231:     XiWin->vis = vinfo.visual;
232:   } else {
233:     XiWin->vis = DefaultVisual(XiWin->disp, XiWin->screen);
234:   }
235:   return 0;
236: }

238: PetscErrorCode PetscDrawXiSetColormap(PetscDraw_X *XiWin)
239: {
240:   XSetWindowColormap(XiWin->disp, XiWin->win, XiWin->cmap);
241:   return 0;
242: }

244: /*
245:    Get RGB color entries out of the X colormap
246: */
247: PetscErrorCode PetscDrawXiGetPalette(PetscDraw_X *XiWin, unsigned char palette[PETSC_DRAW_MAXCOLOR][3])
248: {
249:   int    k;
250:   XColor colordef[PETSC_DRAW_MAXCOLOR];

252:   for (k = 0; k < PETSC_DRAW_MAXCOLOR; k++) {
253:     colordef[k].pixel = XiWin->cmapping[k];
254:     colordef[k].flags = DoRed | DoGreen | DoBlue;
255:   }
256:   XQueryColors(XiWin->disp, XiWin->cmap, colordef, PETSC_DRAW_MAXCOLOR);
257:   for (k = 0; k < PETSC_DRAW_MAXCOLOR; k++) {
258:     palette[k][0] = (unsigned char)(colordef[k].red >> 8);
259:     palette[k][1] = (unsigned char)(colordef[k].green >> 8);
260:     palette[k][2] = (unsigned char)(colordef[k].blue >> 8);
261:   }
262:   return 0;
263: }