File: g_colors.c

package info (click to toggle)
plotutils 2.4.1-15
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,072 kB
  • ctags: 6,952
  • sloc: ansic: 76,305; cpp: 12,402; sh: 8,475; yacc: 2,604; makefile: 894; lex: 144
file content (646 lines) | stat: -rw-r--r-- 17,668 bytes parent folder | download | duplicates (3)
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* This file contains color methods that are GNU extensions to libplot:
   pencolor, fillcolor, bgcolor, and the convenience function, color.

   It also contains _grayscale_approx(), which computes grayscale
   approximations to RGB colors. */

/* It also includes the pencolorname, fillcolorname, and bgcolorname
   methods, which are GNU extensions to libplot; also the convenience
   function colorname.  They search a database of known names (stored in
   g_colorname.h) for a specified color name.  If the name is found, its
   interpretation as a 48-bit RGB color is determined, and pencolor,
   fillcolor, or bgcolor is called to set the color.  If the name is not
   found, a default color (black for pen and fill, white for bg) is
   substituted. */

#include "sys-defines.h"
#include "extern.h"
#include "g_colorname.h"

/* forward references */
static bool _string_to_precise_color ____P((const char *name, plColor *color_p));

/* The pencolor method, which is a GNU extension to libplot.  It sets a
   drawing attribute: the pen color (``foreground color'') of objects
   created in the drawing operations that follow.  The fill color may be
   set separately, by invoking fillcolor() and filltype().

   We use the RGB color model.  In principle we support 48-bit color (16
   bits, i.e. 0x0000 through 0xffff, for each of red, green, and blue). */

int
#ifdef _HAVE_PROTOS
_API_pencolor (R___(Plotter *_plotter) int red, int green, int blue)
#else
_API_pencolor (R___(_plotter) red, green, blue)
     S___(Plotter *_plotter;) 
     int red, green, blue;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "pencolor: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  if ((red > 0xffff) || (green > 0xffff) || (blue > 0xffff))
    /* OOB switches to default */
    {
      red = _default_drawstate.fgcolor.red;
      green = _default_drawstate.fgcolor.green;
      blue = _default_drawstate.fgcolor.blue;
    }

  if (_plotter->data->emulate_color)
    /* replace by grayscale approximation */
    red = green = blue = _grayscale_approx (red, green, blue);

  /* save our notion of foreground color */
  _plotter->drawstate->fgcolor.red = red;
  _plotter->drawstate->fgcolor.green = green;  
  _plotter->drawstate->fgcolor.blue = blue;

  return 0;
}

/* The fillcolor method, which is a GNU extension to libplot.  It sets a
   drawing attribute: the fill color of objects created in the following
   drawing operations.  Actually the true fill color (if filling is not
   disabled) will be a desaturated version of the user-specified fill
   color.  The desaturation level is set by invoking filltype().

   In principle we support 48-bit color (16 bits, i.e. 0x0000 through
   0xffff, for each of red, green, and blue). */

int
#ifdef _HAVE_PROTOS
_API_fillcolor (R___(Plotter *_plotter) int red, int green, int blue)
#else
_API_fillcolor (R___(_plotter) red, green, blue)
     S___(Plotter *_plotter;) 
     int red, green, blue;
#endif
{
  double red_d, green_d, blue_d;
  double desaturate;
  plColor new_rgb;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "fillcolor: invalid operation");
      return -1;
    }

  _API_endpath (S___(_plotter)); /* flush path if any */

  if ((red > 0xffff) || (green > 0xffff) || (blue > 0xffff))
    /* OOB switches to default */
    {
      red = _default_drawstate.fillcolor.red;
      green = _default_drawstate.fillcolor.green;
      blue = _default_drawstate.fillcolor.blue;
    }

  if (_plotter->data->emulate_color)
    /* replace by grayscale approximation */
    red = green = blue = _grayscale_approx (red, green, blue);

  /* save the basic fillcolor (i.e. the base fillcolor, unaffected by the
     fill type) */
  _plotter->drawstate->fillcolor_base.red = red;
  _plotter->drawstate->fillcolor_base.green = green;  
  _plotter->drawstate->fillcolor_base.blue = blue;

  if (_plotter->drawstate->fill_type == 0)
    /* won't be doing filling, so stop right here */
    return 0;

  /* update fillcolor, taking fill type into account */

  /* scale each RGB from a 16-bit quantity to range [0.0,1.0] */
  red_d = ((double)red)/0xFFFF;
  green_d = ((double)green)/0xFFFF;
  blue_d = ((double)blue)/0xFFFF;

  /* fill_type, if nonzero, specifies the extent to which the nominal fill
     color should be desaturated.  1 means no desaturation, 0xffff means
     complete desaturation (white). */
  desaturate = ((double)_plotter->drawstate->fill_type - 1.)/0xFFFE;
  red_d = red_d + desaturate * (1.0 - red_d);
  green_d = green_d + desaturate * (1.0 - green_d);
  blue_d = blue_d + desaturate * (1.0 - blue_d);

  /* restore each RGB to a 16-bit quantity (48 bits in all) */
  new_rgb.red = IROUND(0xFFFF * red_d);
  new_rgb.green = IROUND(0xFFFF * green_d);
  new_rgb.blue = IROUND(0xFFFF * blue_d);

  /* store actual fill color in drawing state */
  _plotter->drawstate->fillcolor = new_rgb;

  return 0;
}

/* convenience function */

int
#ifdef _HAVE_PROTOS
_API_color (R___(Plotter *_plotter) int red, int green, int blue)
#else
_API_color (R___(_plotter) red, green, blue)
     S___(Plotter *_plotter;) 
     int red, green, blue;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "color: invalid operation");
      return -1;
    }

  _API_pencolor (R___(_plotter) red, green, blue);
  _API_fillcolor (R___(_plotter) red, green, blue);  

  return 0;
}

/* The bgcolor method, which is a GNU extension to libplot.  It sets a
   drawing attribute: the background color.

   We use the RGB color model.  In principle we support 48-bit color (16
   bits, i.e. 0x0000 through 0xffff, for each of red, green, and blue). */

int
#ifdef _HAVE_PROTOS
_API_bgcolor (R___(Plotter *_plotter) int red, int green, int blue)
#else
_API_bgcolor (R___(_plotter) red, green, blue)
     S___(Plotter *_plotter;) 
     int red, green, blue;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "bgcolor: invalid operation");
      return -1;
    }

  if ((red > 0xffff) || (green > 0xffff) || (blue > 0xffff))
    /* OOB switches to default */
    {
      red = _default_drawstate.bgcolor.red;
      green = _default_drawstate.bgcolor.green;
      blue = _default_drawstate.bgcolor.blue;
    }

  if (_plotter->data->emulate_color)
    /* replace by grayscale approximation */
    red = green = blue = _grayscale_approx (red, green, blue);

  /* save our notion of background color */
  _plotter->drawstate->bgcolor.red = red;
  _plotter->drawstate->bgcolor.green = green;  
  _plotter->drawstate->bgcolor.blue = blue;

  return 0;
}

/* compute a 16-bit grayscale approximation to a 48-bit RGB; optionally
   used by pencolorname, fillcolorname, bgcolorname methods */
int
#ifdef _HAVE_PROTOS
_grayscale_approx (int red, int green, int blue)
#else
_grayscale_approx (red, green, blue)
     int red, green, blue;
#endif
{
  double gray;
  
  /* compute CIE luminance according to Rec. 709 */
  gray = 0.212671 * red + 0.715160 * green + 0.072169 * blue;
  return IROUND(gray);
}

/* Below are the pencolorname, fillcolorname, and bgcolorname methods,
   which are GNU extensions to libplot.  They search a database of known
   names (stored in g_colorname.h) for a specified color name.  If the name
   is found, its interpretation as a 48-bit RGB color is determined, and
   pencolor, fillcolor, or bgcolor is called to set the color.  If the name
   is not found, a default color (black for pen and fill, white for bg) is
   substituted.

   The lowest-level routine is _string_to_color(). */

int 
#ifdef _HAVE_PROTOS
_API_pencolorname (R___(Plotter *_plotter) const char *name)
#else
_API_pencolorname (R___(_plotter) name)
     S___(Plotter *_plotter;)
     const char *name;
#endif
{
  plColor color;
  int intred, intgreen, intblue;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "pencolorname: invalid operation");
      return -1;
    }

  /* null pointer ignored */
  if (!name)
    return 0;

  /* RGB values for default pen color */
  intred = _default_drawstate.fgcolor.red;
  intgreen = _default_drawstate.fgcolor.green;
  intblue = _default_drawstate.fgcolor.blue;

  if (_string_to_color (name, &color, _plotter->data->color_name_cache))
    {
      unsigned int red, green, blue;
      
      red = color.red;
      green = color.green;
      blue = color.blue;
      /* to convert from 24-bit to 48-bit color, double bytes */
      intred = (red << 8) | red;
      intgreen = (green << 8) | green;
      intblue = (blue << 8) | blue;
    }
  else if (_plotter->data->pen_color_warning_issued == false)
    {
      char *buf;
		
      buf = (char *)_plot_xmalloc (strlen (name) + 100);
      sprintf (buf, "substituting \"black\" for undefined pen color \"%s\"", 
	       name);
      _plotter->warning (R___(_plotter) buf);
      free (buf);
      _plotter->data->pen_color_warning_issued = true;
    }

  _API_pencolor (R___(_plotter) intred, intgreen, intblue);

  return 0;
}

int 
#ifdef _HAVE_PROTOS
_API_fillcolorname (R___(Plotter *_plotter) const char *name)
#else
_API_fillcolorname (R___(_plotter) name)
     S___(Plotter *_plotter;) 
     const char *name;
#endif
{
  plColor color;
  int intred, intgreen, intblue;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "fillcolorname: invalid operation");
      return -1;
    }

  /* null pointer ignored */
  if (!name)
    return 0;

  /* RGB values for default fill color */
  intred = _default_drawstate.fillcolor.red;
  intgreen = _default_drawstate.fillcolor.green;
  intblue = _default_drawstate.fillcolor.blue;

  if (_string_to_color (name, &color, _plotter->data->color_name_cache))
    {
      unsigned int red, green, blue;

      red = color.red;
      green = color.green;
      blue = color.blue;
      /* to convert from 24-bit to 48-bit color, double bytes */
      intred = (red << 8) | red;
      intgreen = (green << 8) | green;
      intblue = (blue << 8) | blue;
    }
  else if (_plotter->data->fill_color_warning_issued == false)
    {
      char *buf;
		
      buf = (char *)_plot_xmalloc (strlen (name) + 100);
      sprintf (buf, "substituting \"black\" for undefined fill color \"%s\"", 
	       name);
      _plotter->warning (R___(_plotter) buf);
      free (buf);
      _plotter->data->fill_color_warning_issued = true;
    }

  _API_fillcolor (R___(_plotter) intred, intgreen, intblue);

  return 0;
}

/* convenience function */

int
#ifdef _HAVE_PROTOS
_API_colorname (R___(Plotter *_plotter) const char *name)
#else
_API_colorname (R___(_plotter) name)
     S___(Plotter *_plotter;)
     const char *name;
#endif
{
  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "colorname: invalid operation");
      return -1;
    }

  _API_pencolorname (R___(_plotter) name);
  _API_fillcolorname (R___(_plotter) name);
  
  return 0;
}

int 
#ifdef _HAVE_PROTOS
_API_bgcolorname (R___(Plotter *_plotter) const char *name)
#else
_API_bgcolorname (R___(_plotter) name)
     S___(Plotter *_plotter;) 
     const char *name;
#endif
{
  plColor color;
  int intred, intgreen, intblue;

  if (!_plotter->data->open)
    {
      _plotter->error (R___(_plotter) 
		       "bgcolorname: invalid operation");
      return -1;
    }

  /* null pointer ignored */
  if (!name)
    return 0;

  if (strcmp (name, "none") == 0)
    /* turn off background (some Plotters can implement this) */
    {
      _plotter->drawstate->bgcolor_suppressed = true;
      /* treat as default, for benefit of Plotters that can't */
      name = "white";		
    }
  else
    _plotter->drawstate->bgcolor_suppressed = false;

  /* RGB values for default color [white, presumably] */
  intred = _default_drawstate.bgcolor.red;
  intgreen = _default_drawstate.bgcolor.green;
  intblue = _default_drawstate.bgcolor.blue;

  if (_string_to_color (name, &color, _plotter->data->color_name_cache))
    {
      unsigned int red, green, blue;

      red = color.red;
      green = color.green;
      blue = color.blue;
      /* to convert from 24-bit to 48-bit color, double bytes */
      intred = (red << 8) | red;
      intgreen = (green << 8) | green;
      intblue = (blue << 8) | blue;
    }
  else if (_plotter->data->bg_color_warning_issued == false)
    {
      char *buf;
		
      buf = (char *)_plot_xmalloc (strlen (name) + 100);
      sprintf (buf, "substituting \"white\" for undefined background color \"%s\"", 
	       name);
      _plotter->warning (R___(_plotter) buf);
      free (buf);
      _plotter->data->bg_color_warning_issued = true;
    }

  _API_bgcolor (R___(_plotter) intred, intgreen, intblue);

  return 0;
}

/* _string_to_color() searches a database of known color names, in
   g_colorname.h, for a specified string.  Matches are case-insensitive and
   ignore spaces.  The retrieved RGB components are returned via a pointer.

   We don't wish to search through the entire (long) color database.  (It
   contains 600+ color name strings.)  So any Plotter maintains a cache
   of previously found colors. */

bool
#ifdef _HAVE_PROTOS
_string_to_color (const char *name, plColor *color_p, plColorNameCache *color_name_cache)
#else
_string_to_color (name, color_p, color_name_cache)
     const char *name;
     plColor *color_p;
     plColorNameCache *color_name_cache;
#endif
{
  plColor color;
  plCachedColorNameInfo **cached_colors_p;
  bool found = false;
  char *squeezed_name, *nptr;
  const plColorNameInfo *info, *found_info = NULL;
  const char *optr;
  plCachedColorNameInfo *cached_info;

  if (name == NULL)		/* avoid core dumps */
    return false;
  
  if (color_name_cache == NULL)	/* avoid core dumps */
    return false;

  /* first check whether string is of the form "#ffffff" */
  if (_string_to_precise_color (name, &color))
    {
      *color_p = color;
      return true;
    }

  /* copy string, removing spaces */
  squeezed_name = (char *)_plot_xmalloc (strlen (name) + 1);
  optr = name, nptr = squeezed_name;
  while (*optr)
    {
      if (*optr == '\0')
	break;
      if (*optr != ' ')
	*nptr++ = *optr;
      optr++;
    }
  *nptr = '\0';

  /* Search our list of cached, previously used color names, doing string
     comparison.  If this were only for use by the X11 driver, we'd use
     XrmPermStringToQuark to get a faster-compared representation. */

  cached_colors_p = &color_name_cache->cached_colors;
  cached_info = *cached_colors_p;
  while (cached_info)
    {
      if (strcasecmp (cached_info->info->name, squeezed_name) == 0)
	{
	  found = true;
	  found_info = cached_info->info;
	  break;
	}
      cached_info = cached_info->next;
    }

  if (!found)
   /* not previously used, so search master colorname table (this is slower) */
    {
      info = _colornames;	/* start at head of list in g_colorname.h */
      while (info->name)
	{
	  if (strcasecmp (info->name, squeezed_name) == 0)
	    {
	      found = true;
	      found_info = info;
	      break;
	    }
	  info++;
	}

      if (found)
	/* copy to head of cached color list */
	{
	  plCachedColorNameInfo *old_cached_colors, *cached_colors;

	  old_cached_colors = *cached_colors_p;
	  cached_colors = 
	    (plCachedColorNameInfo *)_plot_xmalloc (sizeof (plCachedColorNameInfo));
	  cached_colors->next = old_cached_colors;
	  cached_colors->info = found_info;
	  *cached_colors_p = cached_colors;
	}
    }
  
  free (squeezed_name);
  if (found)
    {
      color_p->red = found_info->red;
      color_p->green = found_info->green;
      color_p->blue = found_info->blue;
    }

  return found;
}

/* Attempt to map a string to a 24-bit RGB; this will work if the string is
   of the form "#ffffff". */

static bool
#ifdef _HAVE_PROTOS
_string_to_precise_color (const char *name, plColor *color_p)
#else
_string_to_precise_color (name, color_p)
     const char *name;
     plColor *color_p;
#endif
{
  const char *good_hex_digits = "0123456789abcdefABCDEF";
  int i, num_assigned;
  
  if (name == (const char *)NULL || *name != '#')
    return false;
  
  for (i = 1; i <= 8 ; i++)
    {
      bool found;
      const char *cp;
      
      if (name[i] == '\0')
	break;
      cp = good_hex_digits;
      found = false;
      while (*cp)
	{
	  if (name[i] == *cp)
	    {
	      found = true;
	      break;
	    }
	  cp++;
	}
      if (found == false)
	return false;
    }
  if (i != 7)
    return false;
  
  /* okay, have something like "#ffffff"; can now safely use scanf() */

  num_assigned = sscanf (name, "#%2x%2x%2x", 
			 &(color_p->red), &(color_p->green), &(color_p->blue));

  return (num_assigned == 3 ? true : false);
}


/* The cache of color names is currently implemented as a linked list. */

plColorNameCache *
#ifdef _HAVE_PROTOS
_create_color_name_cache (void)
#else
_create_color_name_cache ()
#endif
{
  plColorNameCache *new_cache;
  
  new_cache = (plColorNameCache *)_plot_xmalloc(sizeof(plColorNameCache));
  new_cache->cached_colors = NULL;
  return new_cache;
}

void 
#ifdef _HAVE_PROTOS
_delete_color_name_cache (plColorNameCache *color_name_cache)
#else
_delete_color_name_cache (color_name_cache)
     plColorNameCache *color_name_cache;
#endif
{
  plCachedColorNameInfo *colorptr;

  if (color_name_cache == (plColorNameCache *)NULL)
    return;

  colorptr = color_name_cache->cached_colors;
  while (colorptr != NULL)	/* free linked list */
    {
      plCachedColorNameInfo *next_colorptr;
      
      next_colorptr = colorptr->next;
      free (colorptr);
      colorptr = next_colorptr;
    }

  free (color_name_cache);	/* free structure itself */
}