File: t_color.c

package info (click to toggle)
plotutils 2.6-15
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,040 kB
  • sloc: ansic: 68,670; sh: 20,086; cpp: 12,382; yacc: 2,588; makefile: 838; lex: 137
file content (128 lines) | stat: -rw-r--r-- 4,058 bytes parent folder | download | duplicates (7)
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
/* This file is part of the GNU plotutils package.  Copyright (C) 1995,
   1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.

   The GNU plotutils package is free software.  You may redistribute it
   and/or modify it under the terms of the GNU General Public License as
   published by the Free Software foundation; either version 2, or (at your
   option) any later version.

   The GNU plotutils package is distributed in the hope that it will be
   useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with the GNU plotutils package; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
   Boston, MA 02110-1301, USA. */

/* For TekPlotter objects, setting the pen color or background color has no
   effect unless the plotter is using the Tektronix emulation of MS-DOS
   kermit.  If so, we compute a quantized color and output the appropriate
   ANSI escape sequence, if the color is different from the ANSI.SYS color
   the emulation is currently using.

   When the TekPlotter is created, the current ANSI.SYS pen and bg colors
   are set to -1 (see t_defplot.c).  That's a nonsensical value, equivalent
   to `unknown'. */

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

#define ONEBYTE (0xff)

/* forward references */
static int kermit_pseudocolor (int red, int green, int blue);

void
_pl_t_set_pen_color(S___(Plotter *_plotter))
{
  if (_plotter->tek_display_type == TEK_DPY_KERMIT)
    {
      int new_kermit_fgcolor;

      new_kermit_fgcolor = 
	kermit_pseudocolor (_plotter->drawstate->fgcolor.red, 
			    _plotter->drawstate->fgcolor.green, 
			    _plotter->drawstate->fgcolor.blue);
      if (new_kermit_fgcolor != _plotter->tek_kermit_fgcolor)
	{
	  _write_string (_plotter->data, 
				  _pl_t_kermit_fgcolor_escapes[new_kermit_fgcolor]);
	  _plotter->tek_kermit_fgcolor = new_kermit_fgcolor;
	}
    }
}  

void
_pl_t_set_bg_color(S___(Plotter *_plotter))
{
  if (_plotter->tek_display_type == TEK_DPY_KERMIT)
    {
      int new_kermit_bgcolor;

      new_kermit_bgcolor = 
	kermit_pseudocolor (_plotter->drawstate->bgcolor.red, 
			    _plotter->drawstate->bgcolor.green, 
			    _plotter->drawstate->bgcolor.blue);
      if (new_kermit_bgcolor != _plotter->tek_kermit_bgcolor)
	{
	  _write_string (_plotter->data,
				  _pl_t_kermit_bgcolor_escapes[new_kermit_bgcolor]);
	  _plotter->tek_kermit_bgcolor = new_kermit_bgcolor;
	}
    }
}  

/* kermit_pseudocolor quantizes to one of kermit's native 16 colors.  (They
   provide a [rather strange] partition of the color cube; see
   t_color2.c.) */

/* find closest known point within the RGB color cube, using Euclidean
   distance as our metric */
static int
kermit_pseudocolor (int red, int green, int blue)
{
  unsigned long int difference = INT_MAX;
  int i;
  int best = 0;
  
  /* reduce to 24 bits */
  red = (red >> 8) & ONEBYTE;
  green = (green >> 8) & ONEBYTE;
  blue = (blue >> 8) & ONEBYTE;

  for (i = 0; i < TEK_NUM_ANSI_SYS_COLORS; i++)
    {
      unsigned long int newdifference;
      
      if (_pl_t_kermit_stdcolors[i].red == 0xff
	  && _pl_t_kermit_stdcolors[i].green == 0xff
	  && _pl_t_kermit_stdcolors[i].blue == 0xff)
	/* white is a possible quantization only for white itself (our
           convention) */
	{
	  if (red == 0xff && green == 0xff && blue == 0xff)
	    {
	      difference = 0;
	      best = i;
	    }
	  continue;
	}

      newdifference = (((_pl_t_kermit_stdcolors[i].red - red) 
			* (_pl_t_kermit_stdcolors[i].red - red))
		       + ((_pl_t_kermit_stdcolors[i].green - green) 
			  * (_pl_t_kermit_stdcolors[i].green - green))
		       + ((_pl_t_kermit_stdcolors[i].blue - blue) 
			  * (_pl_t_kermit_stdcolors[i].blue - blue)));
      
      if (newdifference < difference)
	{
	  difference = newdifference;
	  best = i;
	}
    }
  return best;
}