File: c_color.c

package info (click to toggle)
plotutils 2.4.1-11
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 11,676 kB
  • ctags: 6,967
  • sloc: ansic: 76,305; sh: 15,172; cpp: 12,403; yacc: 2,604; makefile: 888; lex: 144
file content (310 lines) | stat: -rw-r--r-- 11,069 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
/* This file contains device-specific color database access routines.
   These routines are called by various CGMPlotter methods, before drawing
   objects.

   The CGM output file will use either 24-bit RGB or 48-bit RGB, depending
   on the value of CGM_BINARY_BYTES_PER_COLOR_COMPONENT (set in extern.h; 
   1 or 2, respectively).  This code assumes that the value is 1 or 2, even
   though CGM files allow 3 or 4 as well.  To handle the `4' case, we
   should rewrite this to use unsigned ints rather than signed ints.

   The reason we don't bother with 3 or 4 is that internally, libplot uses
   48-bit color.  So 48-bit RGB in the CGM output file is all we need. */

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

void
#ifdef _HAVE_PROTOS
_c_set_pen_color(R___(Plotter *_plotter) int cgm_object_type)
#else
_c_set_pen_color(R___(_plotter) cgm_object_type)
     S___(Plotter *_plotter;) 
     int cgm_object_type;
#endif
{
  int red_long, green_long, blue_long;
  int red, green, blue;
  int byte_count, data_byte_count, data_len;
  int fullstrength;

  if (_plotter->drawstate->pen_type == 0
      && cgm_object_type != CGM_OBJECT_TEXT)
    /* don't do anything, pen color will be ignored when writing objects */
    return;

  /* 48-bit RGB */
  red_long = _plotter->drawstate->fgcolor.red;
  green_long = _plotter->drawstate->fgcolor.green;
  blue_long = _plotter->drawstate->fgcolor.blue;

  /* 24-bit or 48-bit RGB (as used in CGMs) */
  switch (CGM_BINARY_BYTES_PER_COLOR_COMPONENT)
    {
    case 1:
      /* 24-bit */
      red = (((unsigned int)red_long) >> 8) & 0xff;
      green = (((unsigned int)green_long) >> 8) & 0xff;
      blue = (((unsigned int)blue_long) >> 8) & 0xff;
      break;
    case 2:
    default:
      /* 48-bit */
      red = red_long;
      green = green_long;
      blue = blue_long;
      break;
    }

  fullstrength = (1 << (8 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT)) - 1;
  if ((red != 0 || green != 0 || blue != 0)
      && (red != fullstrength || green != fullstrength || blue != fullstrength))
    _plotter->cgm_page_need_color = true;

  switch (cgm_object_type)
    {
    case CGM_OBJECT_OPEN:
      if (_plotter->cgm_line_color.red != red 
	  || _plotter->cgm_line_color.green != green
	  || _plotter->cgm_line_color.blue != blue)
	/* emit "LINE_COLOR" command */
	{
	  data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 4,
				    data_len, &byte_count,
				    "LINECOLR");
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)red,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)green,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)blue,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update our knowledge of CGM's pen color */
	  _plotter->cgm_line_color.red = red;
	  _plotter->cgm_line_color.green = green;
	  _plotter->cgm_line_color.blue = blue;
	}
      break;
    case CGM_OBJECT_CLOSED:
      if (_plotter->cgm_edge_color.red != red 
	  || _plotter->cgm_edge_color.green != green
	  || _plotter->cgm_edge_color.blue != blue)
	/* emit "EDGE_COLOR" command */
	{
	  data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 29,
				    data_len, &byte_count,
				    "EDGECOLR");
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)red,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)green,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)blue,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update our knowledge of CGM's edge color */
	  _plotter->cgm_edge_color.red = red;
	  _plotter->cgm_edge_color.green = green;
	  _plotter->cgm_edge_color.blue = blue;
	}
      break;
    case CGM_OBJECT_MARKER:
      if (_plotter->cgm_marker_color.red != red 
	  || _plotter->cgm_marker_color.green != green
	  || _plotter->cgm_marker_color.blue != blue)
	/* emit "MARKER COLOR" command */
	{
	  data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 8,
				    data_len, &byte_count,
				    "MARKERCOLR");
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)red,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)green,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)blue,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update our knowledge of CGM's marker color */
	  _plotter->cgm_marker_color.red = red;
	  _plotter->cgm_marker_color.green = green;
	  _plotter->cgm_marker_color.blue = blue;
	}
      break;
    case CGM_OBJECT_TEXT:
      if (_plotter->cgm_text_color.red != red 
	  || _plotter->cgm_text_color.green != green
	  || _plotter->cgm_text_color.blue != blue)
	/* emit "TEXT COLOR" command */
	{
	  data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
	  byte_count = data_byte_count = 0;
	  _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				    CGM_ATTRIBUTE_ELEMENT, 14,
				    data_len, &byte_count,
				    "TEXTCOLR");
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)red,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)green,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				     (unsigned int)blue,
				     data_len, &data_byte_count, &byte_count);
	  _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
					&byte_count);
	  /* update our knowledge of CGM's text color */
	  _plotter->cgm_text_color.red = red;
	  _plotter->cgm_text_color.green = green;
	  _plotter->cgm_text_color.blue = blue;
	}
      break;
    default:
      break;
    }
}

void
#ifdef _HAVE_PROTOS
_c_set_fill_color(R___(Plotter *_plotter) int cgm_object_type)
#else
_c_set_fill_color(R___(_plotter) cgm_object_type)
     S___(Plotter *_plotter;)
     int cgm_object_type;
#endif
{
  int red_long, green_long, blue_long;
  int red, green, blue;
  int fullstrength;
  int byte_count, data_byte_count, data_len;

  if (_plotter->drawstate->fill_type == 0)
    /* don't do anything, fill color will be ignored when writing objects */
    return;

  if (cgm_object_type != CGM_OBJECT_OPEN
      && cgm_object_type != CGM_OBJECT_CLOSED)
    /* don't do anything; won't be filling */
    return;

  /* obtain each RGB as a 16-bit quantity (48 bits in all) */
  red_long = _plotter->drawstate->fillcolor.red;
  green_long = _plotter->drawstate->fillcolor.green;
  blue_long = _plotter->drawstate->fillcolor.blue;

  /* 24-bit or 48-bit RGB (as used in CGMs) */
  switch (CGM_BINARY_BYTES_PER_COLOR_COMPONENT)
    {
    case 1:
      /* 24-bit */
      red = (((unsigned int)red_long) >> 8) & 0xff;
      green = (((unsigned int)green_long) >> 8) & 0xff;
      blue = (((unsigned int)blue_long) >> 8) & 0xff;
      break;
    case 2:
    default:
      /* 48-bit */
      red = red_long;
      green = green_long;
      blue = blue_long;
      break;
    }

  fullstrength = (1 << (8 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT)) - 1;
  if ((red != 0 || green != 0 || blue != 0)
      && (red != fullstrength || green != fullstrength || blue != fullstrength))
    _plotter->cgm_page_need_color = true;

  if (_plotter->cgm_fillcolor.red != red 
      || _plotter->cgm_fillcolor.green != green
      || _plotter->cgm_fillcolor.blue != blue)
    /* emit "FILL COLOR" command */
    {
      data_len = 3 * CGM_BINARY_BYTES_PER_COLOR_COMPONENT;
      byte_count = data_byte_count = 0;
      _cgm_emit_command_header (_plotter->data->page, _plotter->cgm_encoding,
				CGM_ATTRIBUTE_ELEMENT, 23,
				data_len, &byte_count,
				"FILLCOLR");
      _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				 (unsigned int)red,
				 data_len, &data_byte_count, &byte_count);
      _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				 (unsigned int)green,
				 data_len, &data_byte_count, &byte_count);
      _cgm_emit_color_component (_plotter->data->page, false, _plotter->cgm_encoding,
				 (unsigned int)blue,
				 data_len, &data_byte_count, &byte_count);
      _cgm_emit_command_terminator (_plotter->data->page, _plotter->cgm_encoding,
				    &byte_count);
      /* update our knowledge of CGM's fill color */
      _plotter->cgm_fillcolor.red = red;
      _plotter->cgm_fillcolor.green = green;
      _plotter->cgm_fillcolor.blue = blue;
    }
}

void
#ifdef _HAVE_PROTOS
_c_set_bg_color(S___(Plotter *_plotter))
#else
_c_set_bg_color(S___(_plotter))
     S___(Plotter *_plotter;)
#endif
{
  int red_long, green_long, blue_long;
  int red, green, blue;

  /* 48-bit RGB */
  red_long = _plotter->drawstate->bgcolor.red;
  green_long = _plotter->drawstate->bgcolor.green;
  blue_long = _plotter->drawstate->bgcolor.blue;

  /* 24-bit or 48-bit RGB (as used in CGMs) */
  switch (CGM_BINARY_BYTES_PER_COLOR_COMPONENT)
    {
    case 1:
      /* 24-bit */
      red = (((unsigned int)red_long) >> 8) & 0xff;
      green = (((unsigned int)green_long) >> 8) & 0xff;
      blue = (((unsigned int)blue_long) >> 8) & 0xff;
      break;
    case 2:
    default:
      /* 48-bit */
      red = red_long;
      green = green_long;
      blue = blue_long;
      break;
    }

  /* update our knowledge of what CGM's background color should be (we'll
     use it only when we write the picture header) */
  _plotter->cgm_bgcolor.red = red;
  _plotter->cgm_bgcolor.green = green;
  _plotter->cgm_bgcolor.blue = blue;

  /* should the just-computed color be ignored, i.e., did the user really
     specify "none" as the background color? */
  _plotter->cgm_bgcolor_suppressed = _plotter->drawstate->bgcolor_suppressed;
}