File: xcolor.c

package info (click to toggle)
mpich 1.1.0-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 22,116 kB
  • ctags: 27,349
  • sloc: ansic: 193,435; sh: 11,172; fortran: 6,545; makefile: 5,801; cpp: 5,020; tcl: 3,548; asm: 3,536; csh: 1,079; java: 614; perl: 183; awk: 168; sed: 70; f90: 62
file content (568 lines) | stat: -rw-r--r-- 15,914 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include "mpetools.h"
#include "basex11.h"
#include "baseclr.h"

#if defined(NEEDS_STDLIB_PROTOTYPES)
#include "protofix.h"
#endif

/*
    This file contains routines to provide color support where available.
    This is made difficult by the wide variety of color implementations
    that X11 supports, and the failure of the X consortium to recognize
    until very recently that window managers must have a minimum,
    standardized interface that includes color (and window mapping,
    mouse control, ...  )
 */
/* for some reason, this isn't included by the X11 includes */
#include <X11/Xatom.h>
int XBHlsHelper ANSI_ARGS(( int, int, int ));

/* cmap is ignored for now? */
void XBInitColors( XBWin, cmap, nc )
XBWindow *XBWin;
Colormap cmap;
int      nc;
{
    PixVal   white_pixel, black_pixel;

/* reset the number of colors from info on the display */
/* This is wrong; it needs to take the value from the visual */
/* Also, I'd like to be able to set this so as to force B&W behaviour
   on color displays */
    if (nc > 0) 
	XBWin->numcolors = nc;
    else
	XBWin->numcolors = 1 << DefaultDepth( XBWin->disp, XBWin->screen );

/* we will use the default colormap of the visual */
    if (!XBWin->cmap)
	XBWin->cmap = XBCreateColormap( XBWin->disp, XBWin->screen, XBWin->vis );

/* get the initial colormap */
    if (XBWin->numcolors > 2)
	XBInitCmap( XBWin );
    else {
	/* note that the 1-bit colormap is the DEFAULT map */
	white_pixel     = WhitePixel(XBWin->disp,XBWin->screen);
	black_pixel     = BlackPixel(XBWin->disp,XBWin->screen);
	/* the default "colormap"; the mapping from color indices to 
	   X pixel values */
	XBWin->cmapping[BLACK]   = black_pixel;
	XBWin->cmapping[WHITE]   = white_pixel;
	XBWin->foreground        = black_pixel;
	XBWin->background        = white_pixel;
    }
}


/*
    Set the initial color map
 */
int XBInitCmap( XBWin )
XBWindow *XBWin;
{
    XColor  colordef;
    int     i;
/* Also, allocate black and white first, in the same order that
   there "pixel" values are, incase the pixel values assigned
   start from 0 */
    XBAllocBW( XBWin, &XBWin->cmapping[WHITE], &XBWin->cmapping[BLACK] );
    XBWin->background = XBWin->cmapping[WHITE];
    XBWin->foreground = XBWin->cmapping[BLACK];
/* Look up the colors so that they can be use server standards
   (and be corrected for the monitor) */
    for (i=2; i<16; i++) {
	XParseColor( XBWin->disp, XBWin->cmap, colornames[i], &colordef );
	XAllocColor( XBWin->disp, XBWin->cmap, &colordef );
	XBWin->cmapping[i]   = colordef.pixel;
    }
    XBWin->maxcolors = 15;

    return 0;
}

/*
 * The input to this routine is RGB, not HLS.
 * X colors are 16 bits, not 8, so we have to shift the input by 8.
 */
int XBCmap( red, green, blue, mapsize, XBWin )
int           mapsize;
unsigned char red[], green[], blue[];
XBWindow      *XBWin;
{
    int         i, err;
    XColor      colordef;
    PixVal      white_pixel, black_pixel, pix, white_pix, black_pix;

    white_pixel     = WhitePixel(XBWin->disp,XBWin->screen);
    black_pixel     = BlackPixel(XBWin->disp,XBWin->screen);

/*
    Free the old colors if we have the colormap.
 */
    if (XBWin->cmap != DefaultColormap( XBWin->disp, XBWin->screen ) ) {
	if (XBGetVisualClass( XBWin ) == PseudoColor ||
	    XBGetVisualClass( XBWin ) == DirectColor )
	    XFreeColors( XBWin->disp, XBWin->cmap, XBWin->cmapping,
			 XBWin->maxcolors + 1, (unsigned long)0 );
    }

/*
   The sun convention is that 0 is the background and 2**depth-1 is
   foreground.  We make these the Xtools conventions (ignoring foreground)
  */

    if (mapsize > XBWin->numcolors) mapsize = XBWin->numcolors;

    XBWin->maxcolors = mapsize - 1;
/*  Now, set the color values

    Since it is hard (impossible?) to insure that black and white are
    allocated to the SAME pixel values in the default/window manager
    colormap, we ALWAYS allocate black and white FIRST

    Note that we may have allocated more than mapsize colors if the
    map did not include black or white.  We need to handle this later.
 */
    XBAllocBW( XBWin, &white_pix, &black_pix );
    err = 0;
    for (i=0; i<mapsize; i++) {
	if (red[i] == 0 && green[i] == 0 && blue[i] == 0)
	    XBWin->cmapping[i]   = black_pix;
	else if (red[i] == 255 && green[i] == 255 && blue[i] == 255)
	    XBWin->cmapping[i]   = white_pix;
	else {
	    colordef.red    = ((int)red[i]   * 65535) / 255;
	    colordef.green  = ((int)green[i] * 65535) / 255;
	    colordef.blue   = ((int)blue[i]  * 65535) / 255;
	    colordef.flags  = DoRed | DoGreen | DoBlue;
	    if (!XAllocColor( XBWin->disp, XBWin->cmap, &colordef ))
		err = 1;
	    XBWin->cmapping[i]   = colordef.pixel;
	}
	/* printf( "pixel value for %d is %d\n\r", i, XBWin->cmap[i] ); */
    }

/* make sure that there are 2 different colors */
    pix             = XBWin->cmapping[0];
    for (i=1; i<mapsize; i++)
	if (pix != XBWin->cmapping[i]) break;
    if (i >= mapsize) {
	/* no different colors */
	if (XBWin->cmapping[0] != black_pixel)
	    XBWin->cmapping[0]   = black_pixel;
	else
	    XBWin->cmapping[0]   = white_pixel;
    }

/*
    The window needs to be told the new background pixel so that things
    like XClearArea will work

    Note that this should not be called until the window is actually
    created.
 */
    if (XBWin->win)
	XSetWindowBackground( XBWin->disp, XBWin->win, XBWin->cmapping[0] );

/*
   Note that since we haven't allocated a range of pixel-values to this
   window, the changes will only take effect with future writes.
   Further, several colors may have been mapped to the same display color.
   We could detect this only by seeing if there are any duplications
   among the XBWin->cmap values.
 */

/* 
   Remaining bug: foreground and background not set.
 */
    return err;
}

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

    The next layer is the colormap.  The installation of colormaps is
    the buisness of the window manager (in some distant later release).
    Rather than fight with that, we will use the default colormap.
    This usually does not have many (any?) sharable color entries,
    so we just try to match with the existing entries.
 */

/*
    This routine gets the visual class (PseudoColor, etc) and returns
    it.  It finds the default visual.  Possible returns are
	PseudoColor
	StaticColor
	DirectColor
	TrueColor
	GrayScale
	StaticGray
 */
int XBSetVisualClass( XBWin )
XBWindow *XBWin;
{
    XVisualInfo vinfo;
    if (XMatchVisualInfo( XBWin->disp, XBWin->screen, 
			  24, DirectColor, &vinfo)) {
	XBWin->vis    = vinfo.visual;
	return 0;
    }
    if (XMatchVisualInfo( XBWin->disp, XBWin->screen, 
			  8, PseudoColor, &vinfo)) {
	XBWin->vis    = vinfo.visual;
	return 0;
    }
    if (XMatchVisualInfo( XBWin->disp, XBWin->screen,
			  DefaultDepth(XBWin->disp,XBWin->screen), 
			  PseudoColor, &vinfo)) {
	XBWin->vis    = vinfo.visual;
	return 0;
    }
    XBWin->vis    = DefaultVisual( XBWin->disp, XBWin->screen );

    return 0;
}

int XBGetVisualClass( XBWin )
XBWindow *XBWin;
{
    return XBWin->vis->class;
}

/* Should pass this an XBWin */
Colormap XBCreateColormap( display, screen, visual )
Display *display;
int      screen;
Visual  *visual;
{
    Colormap Cmap;

    if (DefaultDepth( display, screen ) <= 1)
	Cmap    = DefaultColormap( display, screen );
    else
	Cmap    = XCreateColormap( display, RootWindow(display,screen),
				   visual, AllocNone );
    return Cmap;
}


int XBSetColormap( XBWin )
XBWindow *XBWin;
{
    XSetWindowColormap( XBWin->disp, XBWin->win, XBWin->cmap );
    return 0;
}


int XBAllocBW( XBWin, white, black )
XBWindow *XBWin;
PixVal   *white, *black;
{
    XColor  bcolor, wcolor;
    XParseColor( XBWin->disp, XBWin->cmap, "black", &bcolor );
    XParseColor( XBWin->disp, XBWin->cmap, "white", &wcolor );
    if (BlackPixel(XBWin->disp,XBWin->screen) == 0) {
	XAllocColor( XBWin->disp, XBWin->cmap, &bcolor );
	XAllocColor( XBWin->disp, XBWin->cmap, &wcolor );
    }
    else {
	XAllocColor( XBWin->disp, XBWin->cmap, &wcolor );
	XAllocColor( XBWin->disp, XBWin->cmap, &bcolor );
    }
    *black = bcolor.pixel;
    *white = wcolor.pixel;

    return 0;
}


int XBGetBaseColor( XBWin, white_pix, black_pix )
XBWindow *XBWin;
PixVal   *white_pix, *black_pix;
{
    *white_pix  = XBWin->cmapping[WHITE];
    *black_pix  = XBWin->cmapping[BLACK];

    return 0;
}

/*
    Set up a color map, using uniform separation in hue space.
    Map entries are Red, Green, Blue.
    Values are "gamma" corrected.
 */

/*  
   Gamma is a monitor dependent value.  The value here is an 
   approximate that gives somewhat better results than Gamma = 1.
 */
static double Gamma = 2.0;
#include <math.h>

int XBSetGamma( g )
double g;
{
    Gamma = g;

    return 0;
}

int XBSetCmapHue( red, green, blue, mapsize )
int             mapsize;
unsigned char   *red, *green, *blue;
{
    int     i, hue, lightness, saturation;
    double  igamma = 1.0 / Gamma;

    red[0]      = 0;
    green[0]    = 0;
    blue[0]     = 0;
    hue         = 0;        /* in 0:359 */
    lightness   = 50;       /* in 0:100 */
    saturation  = 100;      /* in 0:100 */
    for (i = 1; i < mapsize-1; i++) {
	XBHlsToRgb( hue, lightness, saturation, red + i, green + i, blue + i );
	red[i]   = floor( 255.999 * pow( ((double)  red[i])/255.0, igamma ) );
	blue[i]  = floor( 255.999 * pow( ((double) blue[i])/255.0, igamma ) );
	green[i] = floor( 255.999 * pow( ((double)green[i])/255.0, igamma ) );
	hue += (359/(mapsize-2));
    }
    red  [mapsize-1]    = 255;
    green[mapsize-1]    = 255;
    blue [mapsize-1]    = 255;

    return 0;
}

/*
 * This algorithm is from Foley and van Dam, page 616
 * given
 *   (0:359, 0:100, 0:100).
 *      h       l      s
 * set
 *   (0:255, 0:255, 0:255)
 *      r       g      b
 */
int XBHlsHelper( h, n1, n2 )
int     h, n1, n2;
{
while (h > 360) h = h - 360;
while (h < 0)   h = h + 360;
if (h < 60) return n1 + (n2-n1)*h/60;
if (h < 180) return n2;
if (h < 240) return n1 + (n2-n1)*(240-h)/60;
return n1;
}

int XBHlsToRgb( h, l, s, r, g, b )
int             h, l, s;
unsigned char   *r, *g, *b;
{
int m1, m2;         /* in 0 to 100 */
if (l <= 50) m2 = l * ( 100 + s ) / 100 ;           /* not sure of "/100" */
else         m2 = l + s - l*s/100;

m1  = 2*l - m2;
if (s == 0) {
    /* ignore h */
    *r  = 255 * l / 100;
    *g  = 255 * l / 100;
    *b  = 255 * l / 100;
    }
else {
    *r  = (255 * XBHlsHelper( h+120, m1, m2 ) ) / 100;
    *g  = (255 * XBHlsHelper( h, m1, m2 ) )     / 100;
    *b  = (255 * XBHlsHelper( h-120, m1, m2 ) ) / 100;
    }
return 0;
}


/*
    This routine returns the pixel value for the specified color
    Returns 0 on failure, <>0 otherwise.
 */
int XBFindColor( XBWin, name, pixval )
XBWindow *XBWin;
char     *name;
PixVal   *pixval;
{
    XColor   colordef;
    int      st;

    st = XParseColor( XBWin->disp, XBWin->cmap, name, &colordef );
    if (st) {
	st  = XAllocColor( XBWin->disp, XBWin->cmap, &colordef );
	if (st)
	    *pixval = colordef.pixel;
    }
    else
	printf( "did not find color %s\n", name );
    return st;
}

/*
    When there are several windows being displayed, it may help to
    merge their colormaps together so that all of the windows
    may be displayed simultaneously with true colors.
    These routines attempt to accomplish this
 */

/*
 * The input to this routine is RGB, not HLS.
 * X colors are 16 bits, not 8, so we have to shift the input by 8.
 * This is like XBCmap, except that it APPENDS to the existing
 * colormap.
 */
int XBAddCmap( red, green, blue, mapsize, XBWin )
int           mapsize;
unsigned char red[], green[], blue[];
XBWindow      *XBWin;
{
    int      i, err;
    XColor   colordef;
    int      cmap_start;

    if (mapsize + XBWin->maxcolors > XBWin->numcolors)
	mapsize = XBWin->numcolors - XBWin->maxcolors;

    cmap_start  = XBWin->maxcolors;
    XBWin->maxcolors += mapsize;

    err = 0;
    for (i=0; i<mapsize; i++) {
	colordef.red    = ((int)red[i]   * 65535) / 255;
	colordef.green  = ((int)green[i] * 65535) / 255;
	colordef.blue   = ((int)blue[i]  * 65535) / 255;
	colordef.flags  = DoRed | DoGreen | DoBlue;
	if (!XAllocColor( XBWin->disp, XBWin->cmap, &colordef ))
	    err = 1;
	XBWin->cmapping[cmap_start+i]    = colordef.pixel;
	/* printf( "pixel value for %d is %d\n\r", i, XBWin->cmapping[i] ); */
    }

    return err;
}


/*
    Another real need is to assign "colors" that make sense for
    a monochrome display, without unduely penalizing color displays.
    This routine takes a color name, a window, and a flag that
    indicates whether this is "background" or "foreground".
    In the monchrome case (or if the color is otherwise unavailable),
    the "background" or "foreground" colors will be chosen
 */
PixVal XBGetColor( XBWin, name, is_fore )
XBWindow *XBWin;
char     *name;
int      is_fore;
{
    PixVal pixval;
    if (XBWin->numcolors == 2 || !XBFindColor( XBWin, name, &pixval ))
	pixval  = is_fore ? XBWin->cmapping[WHITE] : XBWin->cmapping[BLACK];
    return pixval;
}

/*
   This routine takes a named color and returns a color that is either
   lighter or darker
 */
PixVal XBSimColor( XBWin, pixel, intensity, is_fore )
XBWindow *XBWin;
PixVal   pixel;
int      intensity, is_fore;
{
    XColor   colordef, colorsdef;
    char     RGBcolor[20];
    PixVal   red, green, blue;
    int      st;

    colordef.pixel = pixel;
    XQueryColor( XBWin->disp, XBWin->cmap, &colordef );
/* Adjust the color value up or down.  Get the RGB values for the color */
    red   = colordef.red;
    green = colordef.green;
    blue  = colordef.blue;
#define min(a,b) ((a)<(b) ? a : b)
#define max(a,b) ((a)>(b) ? a : b)
#define WHITE_AMOUNT 5000
    if (intensity > 0) {
	/* Add white to the color */
	red   = min(65535,red + WHITE_AMOUNT);
	green = min(65535,green + WHITE_AMOUNT);
	blue  = min(65535,blue + WHITE_AMOUNT);
    }
    else {
	/* Subtract white from the color */
	red   = (red   < WHITE_AMOUNT) ? 0 : red - WHITE_AMOUNT;
	green = (green < WHITE_AMOUNT) ? 0 : green - WHITE_AMOUNT;
	blue  = (blue  < WHITE_AMOUNT) ? 0 : blue - WHITE_AMOUNT;
    }
    sprintf( RGBcolor, "rgb:%4.4x/%4.4x/%4.4x", (unsigned int)red, 
	     (unsigned int) green, (unsigned int) blue );
    st = XLookupColor( XBWin->disp, XBWin->cmap, RGBcolor, &colordef, 
		       &colorsdef );
    return  colorsdef.pixel;
}


/* 
  XBUniformHues - Set the colormap to a uniform distribution

  Input parameters:
. XBwin - window to set colors for
. ncolors - number of colors

  Note:
  This routine sets the colors in the current colormap, if the default
  colormap is used.  The Pixel values chosen are in the cmapping 
  structure; this is used by routines such as the XB contour plotter.
  */  
void XBUniformHues( XBwin, ncolors )
XBWindow *XBwin;
int      ncolors;
{
    unsigned char *red, *green, *blue;

    red   = (unsigned char *)MALLOC( 3 * ncolors * sizeof(unsigned char) );   
    CHKPTR(red);
    green = red + ncolors;
    blue  = green + ncolors;
    XBSetCmapHue( red, green, blue, ncolors );
    XBCmap( red, green, blue, ncolors, XBwin );
    FREE( red );
}

/* 
  XBSetCmapLight - Create rgb values from a single color by adding white
  
  Input Parameters:
. mapsize - number of values

  Output Parameters:
. red - red values
. green - green values
. blue - blue values

  Note:
  The initial color is (red[0],green[0],blue[0]).
  */
void XBSetCmapLight( red, green, blue, mapsize )
     int             mapsize;
     unsigned char   *red, *green, *blue;
{
  int     i ;

  for (i = 1; i < mapsize-1; i++) 
    {
      blue[i]  = i*(255-(int)blue[0])/(mapsize-2)+(int)blue[0] ;
      green[i] = i*(255-(int)green[0])/(mapsize-2)+(int)green[0] ;
      red[i]   = i*(255-(int)red[0])/(mapsize-2)+(int)red[0] ;
    }
  red[mapsize-1] = green[mapsize-1] = blue[mapsize-1] = 255;
}