Actual source code: xcolor.c

  1: /*$Id: xcolor.c,v 1.72 2001/03/23 23:20:15 balay Exp $*/

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

  6:     Currently we default to using cmapping[0 to PETSC_DRAW_BASIC_COLORS-1] for the basic colors and 
  7:   cmapping[DRAW_BASIC_COLORS to 255] for a uniform hue of all the colors. But in the contour
  8:   plot we only use from PETSC_DRAW_BASIC_COLORS to 240 since the ones beyond that are too dark.

 10: */
 11: #include "src/sys/src/draw/impls/x/ximpl.h"
 12: #include <X11/Xatom.h>

 14: static char *(colornames[PETSC_DRAW_BASIC_COLORS]) = { "white",
 15:                                                  "black",
 16:                                                  "red",
 17:                                                  "green",
 18:                                                  "cyan",
 19:                                                  "blue",
 20:                                                  "magenta",
 21:                                                  "aquamarine",
 22:                                                  "forestgreen",
 23:                                                  "orange",
 24:                                                  "violet",
 25:                                                  "brown",
 26:                                                  "pink",
 27:                                                  "coral",
 28:                                                  "gray",
 29:                                                  "yellow",
 30:                                                  "gold",
 31:                                                  "lightpink",
 32:                                                  "mediumturquoise",
 33:                                                  "khaki",
 34:                                                  "dimgray",
 35:                                                  "yellowgreen",
 36:                                                  "skyblue",
 37:                                                  "darkgreen",
 38:                                                  "navyblue",
 39:                                                  "sandybrown",
 40:                                                  "cadetblue",
 41:                                                  "powderblue",
 42:                                                  "deeppink",
 43:                                                  "thistle",
 44:                                                  "limegreen",
 45:                                                  "lavenderblush",
 46:                                                  "plum"};

 48: EXTERN int XiInitCmap(PetscDraw_X*);
 49: EXTERN int XiGetVisualClass(PetscDraw_X *);

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

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

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

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

 68: */
 69: static Colormap  gColormap  = 0;
 70: static PixVal    gCmapping[256];
 71:        int       gNumcolors = 0;

 73: int PetscDrawSetUpColormap_Shared(Display *display,int screen,Visual *visual,Colormap colormap)
 74: {
 75:   XColor        colordef,ecolordef;
 76:   unsigned char *red,*green,*blue;
 77:   int           i,ierr,ncolors;
 78:   PetscTruth    fast;

 81:   if (colormap) {
 82:     gColormap = colormap;
 83:   } else {
 84:     gColormap   = DefaultColormap(display,screen);
 85:   }

 87:   /* set the basic colors into the color map */
 88:   for (i=0; i<PETSC_DRAW_BASIC_COLORS; i++) {
 89:     XAllocNamedColor(display,gColormap,colornames[i],&colordef,&ecolordef);
 90:     gCmapping[i] = colordef.pixel;
 91:   }

 93:   /* set the uniform hue colors into the color map */
 94:   ncolors = 256-PETSC_DRAW_BASIC_COLORS;
 95:   ierr    = PetscMalloc(3*ncolors*sizeof(unsigned char),&red);
 96:   green   = red   + ncolors;
 97:   blue    = green + ncolors;
 98:   ierr    = PetscDrawUtilitySetCmapHue(red,green,blue,ncolors);
 99:   ierr    = PetscOptionsHasName(PETSC_NULL,"-draw_fast",&fast);
100:   if (!fast) {
101:     for (i=PETSC_DRAW_BASIC_COLORS; i<ncolors+PETSC_DRAW_BASIC_COLORS; i++) {
102:       colordef.red    = ((int)red[i-PETSC_DRAW_BASIC_COLORS]   * 65535) / 255;
103:       colordef.green  = ((int)green[i-PETSC_DRAW_BASIC_COLORS] * 65535) / 255;
104:       colordef.blue   = ((int)blue[i-PETSC_DRAW_BASIC_COLORS]  * 65535) / 255;
105:       colordef.flags  = DoRed | DoGreen | DoBlue;
106:       XAllocColor(display,gColormap,&colordef);
107:       gCmapping[i]   = colordef.pixel;
108:     }
109:   }
110:   PetscFree(red);
111:   PetscLogInfo(0,"PetscDrawSetUpColormap_Shared:Successfully allocated colorsn");

113:   return(0);
114: }

116: /*
117:     Keep a record of which pixel numbers in the cmap have been 
118:   used so far; this is to allow us to try to reuse as much of the current
119:   colormap as possible.
120: */
121: static PetscTruth cmap_pixvalues_used[256];
122: static int        cmap_base = 0;

124: int PetscDrawSetUpColormap_Private(Display *display,int screen,Visual *visual,Colormap colormap)
125: {
126:   Colormap      defaultmap = DefaultColormap(display,screen);
127:   int           ierr,found,i,ncolors;
128:   XColor        colordef;
129:   unsigned char *red,*green,*blue;
130:   PetscTruth    fast;


134:   if (colormap) {
135:     gColormap = colormap;
136:   } else {
137:     gColormap = XCreateColormap(display,RootWindow(display,screen),visual,AllocAll);
138:   }

140:   cmap_base = 0;
141:   PetscMemzero(cmap_pixvalues_used,256*sizeof(PetscTruth));

143:   /* set the basic colors into the color map */
144:   for (i=0; i<PETSC_DRAW_BASIC_COLORS; i++) {
145:     XParseColor(display,gColormap,colornames[i],&colordef);
146:       /* try to allocate the color in the default-map */
147:     found = XAllocColor(display,defaultmap,&colordef);
148:     /* use it, if it it exists and is not already used in the new colormap */
149:     if (found && colordef.pixel < 256  && !cmap_pixvalues_used[colordef.pixel]) {
150:       cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
151:     /* otherwise search for the next available slot */
152:     } else {
153:       while (cmap_pixvalues_used[cmap_base]) cmap_base++;
154:       colordef.pixel                   = cmap_base;
155:       cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
156:     }
157:     XStoreColor(display,gColormap,&colordef);
158:     gCmapping[i] = colordef.pixel;
159:   }

161:   /* set the uniform hue colors into the color map */
162:   ncolors = 256-PETSC_DRAW_BASIC_COLORS;
163:   ierr    = PetscMalloc(3*ncolors*sizeof(unsigned char),&red);
164:   green   = red   + ncolors;
165:   blue    = green + ncolors;
166:   ierr    = PetscDrawUtilitySetCmapHue(red,green,blue,ncolors);
167:   ierr    = PetscOptionsHasName(PETSC_NULL,"-draw_fast",&fast);
168:   if (!fast) {
169:     for (i=PETSC_DRAW_BASIC_COLORS; i<ncolors+PETSC_DRAW_BASIC_COLORS; i++) {
170:       colordef.red    = ((int)red[i-PETSC_DRAW_BASIC_COLORS]   * 65535) / 255;
171:       colordef.green  = ((int)green[i-PETSC_DRAW_BASIC_COLORS] * 65535) / 255;
172:       colordef.blue   = ((int)blue[i-PETSC_DRAW_BASIC_COLORS]  * 65535) / 255;
173:       colordef.flags  = DoRed | DoGreen | DoBlue;
174:       /* try to allocate the color in the default-map */
175:       found = XAllocColor(display,defaultmap,&colordef);
176:       /* use it, if it it exists and is not already used in the new colormap */
177:       if (found && colordef.pixel < 256  && !cmap_pixvalues_used[colordef.pixel]) {
178:         cmap_pixvalues_used[colordef.pixel] = PETSC_TRUE;
179:         /* otherwise search for the next available slot */
180:       } else {
181:         while (cmap_pixvalues_used[cmap_base]) cmap_base++;
182:         colordef.pixel                   = cmap_base;
183:         cmap_pixvalues_used[cmap_base++] = PETSC_TRUE;
184:       }
185:       XStoreColor(display,gColormap,&colordef);
186:       gCmapping[i]   = colordef.pixel;
187:     }
188:   }
189:   PetscFree(red);
190:   PetscLogInfo(0,"PetscDrawSetUpColormap_Private:Successfully allocated colorsn");
191:   return(0);
192: }

194: int PetscDrawSetUpColormap_X(Display *display,int screen,Visual *visual,Colormap colormap)
195: {
196:   int         ierr;
197:   PetscTruth  sharedcolormap;
198:   XVisualInfo vinfo;


202:   /* 
203:      This is wrong; it needs to take the value from the visual 
204:   */
205:   gNumcolors = 1 << DefaultDepth(display,screen);

207:   PetscOptionsHasName(PETSC_NULL,"-draw_x_shared_colormap",&sharedcolormap);
208:   /*
209:         Need to determine if window supports allocating a private colormap,
210:     if not, set flag to 1
211:   */
212:   if (XMatchVisualInfo(display,screen,24,StaticColor,&vinfo) ||
213:       XMatchVisualInfo(display,screen,24,TrueColor,&vinfo) ||
214:       XMatchVisualInfo(display,screen,16,StaticColor,&vinfo) ||
215:       XMatchVisualInfo(display,screen,16,TrueColor,&vinfo)) {
216:     sharedcolormap = PETSC_TRUE;
217:   }

219:   /* generate the X color map object */
220:   if (sharedcolormap) {
221:     PetscDrawSetUpColormap_Shared(display,screen,visual,colormap);
222:   } else {
223:     PetscDrawSetUpColormap_Private(display,screen,visual,colormap);
224:   }
225: 
226:   return(0);
227: }

229: int PetscDrawSetColormap_X(PetscDraw_X* XiWin,char *host,Colormap colormap)
230: {

234:   if (XiWin->depth < 8) {
235:     SETERRQ(1,"PETSc Graphics require monitors with at least 8 bit color (256 colors)");
236:   }
237:   if (!gColormap){
238:     Display  *display;  /* Private display will exist forever contains colormap shared by all windows */
239:     int      screen;
240:     Visual*  vis;

242:     display = XOpenDisplay(host);
243:     screen  = DefaultScreen(display);
244:     vis     = DefaultVisual(display,screen);

246:     PetscDrawSetUpColormap_X(display,screen,vis,colormap);
247:   }
248:   XiWin->cmap = gColormap;
249:   PetscMemcpy(XiWin->cmapping,gCmapping,256*sizeof(PixVal));
250:   XiWin->background = XiWin->cmapping[PETSC_DRAW_WHITE];
251:   XiWin->foreground = XiWin->cmapping[PETSC_DRAW_BLACK];
252:   return(0);
253: }

255: /*
256:     Color in X is many-layered.  The first layer is the "visual",a
257:     immutable attribute of a window set when the window is
258:     created.

260:     The next layer is the colormap.  The installation of colormaps is
261:     the buisness of the window manager (in some distant later release).
262: */

264: /*
265:     This routine gets the visual class (PseudoColor, etc) and returns
266:     it.  It finds the default visual.  Possible returns are
267:         PseudoColor
268:         StaticColor
269:         DirectColor
270:         TrueColor
271:         GrayScale
272:         StaticGray
273:  */
274: int XiSetVisualClass(PetscDraw_X* XiWin)
275: {
276:   XVisualInfo vinfo;

279:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,24,DirectColor,&vinfo)) {
280:     XiWin->vis    = vinfo.visual;
281:     return(0);
282:   }
283:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,8,PseudoColor,&vinfo)) {
284:     XiWin->vis    = vinfo.visual;
285:     return(0);
286:   }
287:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,
288:     DefaultDepth(XiWin->disp,XiWin->screen),PseudoColor,&vinfo)) {
289:     XiWin->vis    = vinfo.visual;
290:     return(0);
291:   }
292:   XiWin->vis    = DefaultVisual(XiWin->disp,XiWin->screen);
293:   return(0);
294: }

296: int XiGetVisualClass(PetscDraw_X* XiWin)
297: {
299: #if defined(__cplusplus)
300:   PetscFunctionReturn(XiWin->vis->c_class);
301: #else
302:   PetscFunctionReturn(XiWin->vis->class);
303: #endif
304: }


307: int XiSetColormap(PetscDraw_X* XiWin)
308: {
310:   XSetWindowColormap(XiWin->disp,XiWin->win,XiWin->cmap);
311:   return(0);
312: }

314: int XiGetBaseColor(PetscDraw_X* XiWin,PixVal* white_pix,PixVal* black_pix)
315: {
317:   *white_pix  = XiWin->cmapping[PETSC_DRAW_WHITE];
318:   *black_pix  = XiWin->cmapping[PETSC_DRAW_BLACK];
319:   return(0);
320: }



324: /*
325:     This routine returns the pixel value for the specified color
326:     Returns 0 on failure,<>0 otherwise.
327:  */
328: int XiFindColor(PetscDraw_X *XiWin,char *name,PixVal *pixval)
329: {
330:   XColor   colordef;
331:   int      st;

334:   st = XParseColor(XiWin->disp,XiWin->cmap,name,&colordef);
335:   if (st) {
336:     st  = XAllocColor(XiWin->disp,XiWin->cmap,&colordef);
337:     if (st)  *pixval = colordef.pixel;
338:   }
339:   PetscFunctionReturn(st);
340: }

342: /*
343:     Another real need is to assign "colors" that make sense for
344:     a monochrome display,without unduely penalizing color displays.
345:     This routine takes a color name,a window, and a flag that
346:     indicates whether this is "background" or "foreground".
347:     In the monchrome case (or if the color is otherwise unavailable),
348:     the "background" or "foreground" colors will be chosen
349:  */
350: PixVal XiGetColor(PetscDraw_X* XiWin,char *name,int is_fore)
351: {
352:   PixVal pixval;

355:   if (XiWin->numcolors == 2 || !XiFindColor(XiWin,name,&pixval)) {
356:     pixval  = is_fore ? XiWin->cmapping[PETSC_DRAW_WHITE] : XiWin->cmapping[PETSC_DRAW_BLACK];
357:   }
358:   PetscFunctionReturn(pixval);
359: }

361: /*
362:    This routine takes a named color and returns a color that is either
363:    lighter or darker
364:  */
365: PixVal XiSimColor(PetscDraw_X *XiWin,PixVal pixel,int intensity,int is_fore)
366: {
367:   XColor   colordef,colorsdef;
368:   char     RGBcolor[20];
369:   PixVal   red,green,blue;

372:   colordef.pixel = pixel;
373:   XQueryColor(XiWin->disp,XiWin->cmap,&colordef);
374:   /* Adjust the color value up or down.  Get the RGB values for the color */
375:   red   = colordef.red;
376:   green = colordef.green;
377:   blue  = colordef.blue;
378: #define WHITE_AMOUNT 5000
379:   if (intensity > 0) {
380:     /* Add white to the color */
381:     red   = PetscMin(65535,red + WHITE_AMOUNT);
382:     green = PetscMin(65535,green + WHITE_AMOUNT);
383:     blue  = PetscMin(65535,blue + WHITE_AMOUNT);
384:   } else {
385:     /* Subtract white from the color */
386:     red   = (red   < WHITE_AMOUNT) ? 0 : red - WHITE_AMOUNT;
387:     green = (green < WHITE_AMOUNT) ? 0 : green - WHITE_AMOUNT;
388:     blue  = (blue  < WHITE_AMOUNT) ? 0 : blue - WHITE_AMOUNT;
389:   }
390:   sprintf(RGBcolor,"rgb:%4.4x/%4.4x/%4.4x",(unsigned int)red,
391:                      (unsigned int)green,(unsigned int)blue);
392:   XLookupColor(XiWin->disp,XiWin->cmap,RGBcolor,&colordef,&colorsdef);
393:   PetscFunctionReturn(colorsdef.pixel);
394: }

396: /*
397:   XiSetCmapLight - Create rgb values from a single color by adding white
398:   
399:   The initial color is (red[0],green[0],blue[0]).
400: */
401: int XiSetCmapLight(unsigned char *red,unsigned char *green,unsigned char *blue,int mapsize)
402: {
403:   int     i ;

406:   for (i=1; i<mapsize-1; i++) {
407:       blue[i]  = i*(255-(int)blue[0])/(mapsize-2)+blue[0] ;
408:       green[i] = i*(255-(int)green[0])/(mapsize-2)+green[0] ;
409:       red[i]   = i*(255-(int)red[0])/(mapsize-2)+red[0] ;
410:   }
411:   red[mapsize-1] = green[mapsize-1] = blue[mapsize-1] = 255;
412:   return(0);
413: }