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 const 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;

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

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

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

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

115:   return(0);
116: }

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

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


138:   if (colormap) {
139:     gColormap = colormap;
140:   } else {
141:     gColormap = XCreateColormap(display,RootWindow(display,screen),visual,AllocAll);
142:   }

144:   cmap_base = 0;
145:   PetscMemzero(cmap_pixvalues_used,256*sizeof(PetscTruth));

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

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

200: int PetscDrawSetUpColormap_X(Display *display,int screen,Visual *visual,Colormap colormap)
201: {
202:   int         ierr;
203:   PetscTruth  sharedcolormap;
204:   XVisualInfo vinfo;


208:   /* 
209:      This is wrong; it needs to take the value from the visual 
210:   */
211:   gNumcolors = 1 << DefaultDepth(display,screen);

213:   PetscOptionsHasName(PETSC_NULL,"-draw_x_shared_colormap",&sharedcolormap);
214:   /*
215:         Need to determine if window supports allocating a private colormap,
216:     if not, set flag to 1
217:   */
218:   if (XMatchVisualInfo(display,screen,24,StaticColor,&vinfo) ||
219:       XMatchVisualInfo(display,screen,24,TrueColor,&vinfo)   ||
220:       XMatchVisualInfo(display,screen,16,StaticColor,&vinfo) ||
221:       XMatchVisualInfo(display,screen,16,TrueColor,&vinfo)   ||
222:       XMatchVisualInfo(display,screen,15,StaticColor,&vinfo) ||
223:       XMatchVisualInfo(display,screen,15,TrueColor,&vinfo)) {
224:     sharedcolormap = PETSC_TRUE;
225:   }

227:   /* generate the X color map object */
228:   if (sharedcolormap) {
229:     PetscDrawSetUpColormap_Shared(display,screen,visual,colormap);
230:   } else {
231:     PetscDrawSetUpColormap_Private(display,screen,visual,colormap);
232:   }
233: 
234:   return(0);
235: }

239: int PetscDrawSetColormap_X(PetscDraw_X* XiWin,char *host,Colormap colormap)
240: {

244:   if (XiWin->depth < 8) {
245:     SETERRQ(1,"PETSc Graphics require monitors with at least 8 bit color (256 colors)");
246:   }
247:   if (!gColormap){
248:     Display  *display;  /* Private display will exist forever contains colormap shared by all windows */
249:     int      screen;
250:     Visual*  vis;

252:     display = XOpenDisplay(host);
253:     screen  = DefaultScreen(display);
254:     vis     = DefaultVisual(display,screen);

256:     PetscDrawSetUpColormap_X(display,screen,vis,colormap);
257:   }
258:   XiWin->cmap = gColormap;
259:   PetscMemcpy(XiWin->cmapping,gCmapping,256*sizeof(PixVal));
260:   XiWin->background = XiWin->cmapping[PETSC_DRAW_WHITE];
261:   XiWin->foreground = XiWin->cmapping[PETSC_DRAW_BLACK];
262:   return(0);
263: }

265: /*
266:     Color in X is many-layered.  The first layer is the "visual",a
267:     immutable attribute of a window set when the window is
268:     created.

270:     The next layer is the colormap.  The installation of colormaps is
271:     the buisness of the window manager (in some distant later release).
272: */

274: /*
275:     This routine gets the visual class (PseudoColor, etc) and returns
276:     it.  It finds the default visual.  Possible returns are
277:         PseudoColor
278:         StaticColor
279:         DirectColor
280:         TrueColor
281:         GrayScale
282:         StaticGray
283:  */
286: int XiSetVisualClass(PetscDraw_X* XiWin)
287: {
288:   XVisualInfo vinfo;

291:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,24,DirectColor,&vinfo)) {
292:     XiWin->vis    = vinfo.visual;
293:     return(0);
294:   }
295:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,8,PseudoColor,&vinfo)) {
296:     XiWin->vis    = vinfo.visual;
297:     return(0);
298:   }
299:   if (XMatchVisualInfo(XiWin->disp,XiWin->screen,
300:     DefaultDepth(XiWin->disp,XiWin->screen),PseudoColor,&vinfo)) {
301:     XiWin->vis    = vinfo.visual;
302:     return(0);
303:   }
304:   XiWin->vis    = DefaultVisual(XiWin->disp,XiWin->screen);
305:   return(0);
306: }

310: int XiGetVisualClass(PetscDraw_X* XiWin)
311: {
313: #if defined(__cplusplus)
314:   PetscFunctionReturn(XiWin->vis->c_class);
315: #else
316:   PetscFunctionReturn(XiWin->vis->class);
317: #endif
318: }


323: int XiSetColormap(PetscDraw_X* XiWin)
324: {
326:   XSetWindowColormap(XiWin->disp,XiWin->win,XiWin->cmap);
327:   return(0);
328: }

332: int XiGetBaseColor(PetscDraw_X* XiWin,PixVal* white_pix,PixVal* black_pix)
333: {
335:   *white_pix  = XiWin->cmapping[PETSC_DRAW_WHITE];
336:   *black_pix  = XiWin->cmapping[PETSC_DRAW_BLACK];
337:   return(0);
338: }



342: /*
343:     This routine returns the pixel value for the specified color
344:     Returns 0 on failure,<>0 otherwise.
345:  */
348: int XiFindColor(PetscDraw_X *XiWin,char *name,PixVal *pixval)
349: {
350:   XColor   colordef;
351:   int      st;

354:   st = XParseColor(XiWin->disp,XiWin->cmap,name,&colordef);
355:   if (st) {
356:     st  = XAllocColor(XiWin->disp,XiWin->cmap,&colordef);
357:     if (st)  *pixval = colordef.pixel;
358:   }
359:   PetscFunctionReturn(st);
360: }

362: /*
363:     Another real need is to assign "colors" that make sense for
364:     a monochrome display,without unduely penalizing color displays.
365:     This routine takes a color name,a window, and a flag that
366:     indicates whether this is "background" or "foreground".
367:     In the monchrome case (or if the color is otherwise unavailable),
368:     the "background" or "foreground" colors will be chosen
369:  */
372: PixVal XiGetColor(PetscDraw_X* XiWin,char *name,int is_fore)
373: {
374:   PixVal pixval;

377:   if (XiWin->numcolors == 2 || !XiFindColor(XiWin,name,&pixval)) {
378:     pixval  = is_fore ? XiWin->cmapping[PETSC_DRAW_WHITE] : XiWin->cmapping[PETSC_DRAW_BLACK];
379:   }
380:   PetscFunctionReturn(pixval);
381: }

383: /*
384:    This routine takes a named color and returns a color that is either
385:    lighter or darker
386:  */
389: PixVal XiSimColor(PetscDraw_X *XiWin,PixVal pixel,int intensity,int is_fore)
390: {
391:   XColor   colordef,colorsdef;
392:   char     RGBcolor[20];
393:   PixVal   red,green,blue;

396:   colordef.pixel = pixel;
397:   XQueryColor(XiWin->disp,XiWin->cmap,&colordef);
398:   /* Adjust the color value up or down.  Get the RGB values for the color */
399:   red   = colordef.red;
400:   green = colordef.green;
401:   blue  = colordef.blue;
402: #define WHITE_AMOUNT 5000
403:   if (intensity > 0) {
404:     /* Add white to the color */
405:     red   = PetscMin(65535,red + WHITE_AMOUNT);
406:     green = PetscMin(65535,green + WHITE_AMOUNT);
407:     blue  = PetscMin(65535,blue + WHITE_AMOUNT);
408:   } else {
409:     /* Subtract white from the color */
410:     red   = (red   < WHITE_AMOUNT) ? 0 : red - WHITE_AMOUNT;
411:     green = (green < WHITE_AMOUNT) ? 0 : green - WHITE_AMOUNT;
412:     blue  = (blue  < WHITE_AMOUNT) ? 0 : blue - WHITE_AMOUNT;
413:   }
414:   sprintf(RGBcolor,"rgb:%4.4x/%4.4x/%4.4x",(unsigned int)red,
415:                      (unsigned int)green,(unsigned int)blue);
416:   XLookupColor(XiWin->disp,XiWin->cmap,RGBcolor,&colordef,&colorsdef);
417:   PetscFunctionReturn(colorsdef.pixel);
418: }

420: /*
421:   XiSetCmapLight - Create rgb values from a single color by adding white
422:   
423:   The initial color is (red[0],green[0],blue[0]).
424: */
427: int XiSetCmapLight(unsigned char *red,unsigned char *green,unsigned char *blue,int mapsize)
428: {
429:   int     i ;

432:   for (i=1; i<mapsize-1; i++) {
433:       blue[i]  = i*(255-(int)blue[0])/(mapsize-2)+blue[0] ;
434:       green[i] = i*(255-(int)green[0])/(mapsize-2)+green[0] ;
435:       red[i]   = i*(255-(int)red[0])/(mapsize-2)+red[0] ;
436:   }
437:   red[mapsize-1] = green[mapsize-1] = blue[mapsize-1] = 255;
438:   return(0);
439: }