summaryrefslogtreecommitdiff
path: root/support/dktools/plptcol.ctr
blob: c923979636065b2a4bf65e9c4248b0ff748288b2 (plain)
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
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2014-xxxx
license		=	bsd


%%	header

#ifdef __cplusplus
extern "C" {
#endif

/**	Dynamically allocate new color structure.
	@param	name	Color name.
	@param	r	Red.
	@param	g	Green.
	@param	b	Blue.
	@return	Pointer to new structure on success, NULL on error.
*/
plpt_named_color_t *
plptcolor_new(char const *name, double r, double g, double b, dk3_app_t *app);

/**	Compare two named colors.
	@param	l	Left color.
	@param	r	Right color.
	@param	cr	Comparison criteria (0=name).
	@return	Comparison result.
*/
int
plptcolor_compare(void const *l, void const *r, int cr);

/**	Destroy color structure, release memory.
	@param	ptr	Color structure to destroy.
*/
void
plptcolor_delete(plpt_named_color_t *ptr);

/**	Find color information.
	@param	job	Job structure.
	@param	txt	LaTeX color specification, i.e. "brown!80!blue",
	modified during analysis.
	@param	r	Pointer to red result variable.
	@param	g	Pointer to green result variable.
	@param	b	Pointer to blue result variable.
*/
int
plptcol_get_color(
  plpdftex_job_t	*job,
  char 			*txt,
  double		*r,
  double		*g,
  double		*b
);

#ifdef __cplusplus
}
#endif


%%	module

#include "plpdftex.h"



$!trace-include



void
plptcolor_delete(plpt_named_color_t *ptr)
{
  if (ptr) {
    dk3_release(ptr->name);
    dk3_delete(ptr);
  }
}



plpt_named_color_t *
plptcolor_new(char const *name, double r, double g, double b, dk3_app_t *app)
{
  plpt_named_color_t	*back	= NULL;
  if (name) {
    back = dk3_new_app(plpt_named_color_t,1,app);
    if (back) {
      back->r = r;
      back->g = g;
      back->b = b;
      back->name = dk3str_c8_dup_app(name, app);
      if (!(back->name)) {
        dk3_delete(back);
	back = NULL;
      }
    }
  }
  return back;
}



int
plptcolor_compare(void const *l, void const *r, int cr)
{
  int			 back	= 0;
  plpt_named_color_t	*pl;
  plpt_named_color_t	*pr;

  if (l) {
    if (r) {
      pl = (plpt_named_color_t *)l;
      pr = (plpt_named_color_t *)r;
      switch(cr) {
	case 1: {
	  if (pl->name) {
	    back = dk3str_c8_cmp(pl->name, (char *)pr);
	    if(-1 > back) back = -1;
	    if( 1 < back) back =  1;
	  } else back = -1;
	} break;
	default: {
          if (pl->name) {
            if (pr->name) {
	      back = dk3str_c8_cmp(pl->name, pr->name);
	      if(-1 > back) back = -1;
	      if( 1 < back) back =  1;
	    } else back = 1;
          } else {
            if (pr->name) back = -1;
          }
	} break;
      }
    } else back = 1;
  } else {
    if (r) back = -1;
  }
  return back;
}



/**	Predefined colors.
*/
static plpt_named_color_t const plptc_predefined_colors[] = {
  { "white",     1.0, 1.0, 1.0 },
  { "black",     0.0, 0.0, 0.0 },
  { "red",       1.0, 0.0, 0.0 },
  { "green",     0.0, 1.0, 0.0 },
  { "blue",      0.0, 0.0, 1.0 },
  { "cyan",      0.0, 1.0, 1.0 },
  { "magenta",   1.0, 0.0, 1.0 },
  { "yellow",    1.0, 1.0, 0.0 },
  { "gray",      0.5, 0.5, 0.5 },
  { "lightgray", 0.75, 0.75, 0.75 },
  { "darkgray",  0.25, 0.25, 0.25 },
  { "brown",     0.75, 0.5 , 0.25 },
  { "lime",      0.75, 1.0 , 0.0  },
  { "olive",     0.5 , 0.5 , 0.0  },
  { "orange",    1.0 , 0.5 , 0.0  },
  { "pink",      1.0 , 0.75, 0.75 },
  { "purple",    0.75, 0.0 , 0.25 },
  { "teal",      0.00, 0.5 , 0.5  },
  { "violet",    0.5 , 0.0 , 0.5  }
};

/**	Number of predefined colors.
*/
static size_t const sz_plptc_predefined_colors =
sizeof(plptc_predefined_colors)/sizeof(plpt_named_color_t);



/**	Find a named color.
	@param	job	Job structure.
	@param	txt	Color name.
	@return	Pointer to plpt_named_color_t on success, NULL on error.
*/
static
plpt_named_color_t const	*
plptcol_find_color(plpdftex_job_t *job, char const *txt)
{
  plpt_named_color_t const	*back	=	NULL;
  size_t		 	i;

  for (i = 0; i < sz_plptc_predefined_colors; i++) {
    if (0 == strcmp(txt, plptc_predefined_colors[i].name)) {
      back = &(plptc_predefined_colors[i]);
    }
  }
  if (NULL == back) {
    back = (plpt_named_color_t *)dk3sto_it_find_like(
      job->sico, txt, 1
    );
  }
  return back;
}



int
plptcol_get_color(
  plpdftex_job_t	*job,
  char 			*txt,
  double		*r,
  double		*g,
  double		*b
)
{
  plpt_named_color_t const	*cptr = NULL;
  char		*stt	=	NULL;	/* Start of text */
  char		*per	=	NULL;	/* Percentage value as text */
  char		*bgc	=	NULL;	/* Background color */
  char		*npt	=	NULL;	/* Next pointer */
  double	rres	=	0.0;	/* Result value for red */
  double	gres	=	0.0;	/* Result value for green */
  double	bres	=	0.0;	/* Result value for blue */
  double	bgr	=	0.0;	/* Background red */
  double	bgg	=	0.0;	/* Background green */
  double	bgb	=	0.0;	/* Background blue */
  double	f	=	1.0;	/* Factor */
  int		back	=	0;	/* Function result */
  $? "+ plptcol_get_color \"%!8s\"", TR_8STR(txt)
  /*	Check arguments.
  */
  if ((NULL == r) || (NULL == g) || (NULL == b)) {	$? "! r, g, b"
    goto finished;
  }
  if ((NULL == job) || (NULL == txt)) {			$? "! job, txt"
    goto cleanup;
  }

  /*	Scan first color component.
  */
  stt = txt;
  dk3str_c8_chomp(stt, NULL);
  per = strchr(stt, '!');
  if (NULL != per) { *(per++) = '\0'; }
  cptr = plptcol_find_color(job, txt);
  if (NULL == cptr) {					$? "! color not found"
    /* ##### ERROR: Color not found! */
    goto cleanup;
  }
  rres = cptr->r;		$? ". r = %lg", rres
  gres = cptr->g;		$? ". g = %lg", gres
  bres = cptr->b;		$? ". b = %lg", bres

  /*	Mix against further components.
  */
  while (NULL != per) {
    npt = NULL;
    bgc = strchr(per, '!');
    if (NULL != bgc) {
      *(bgc++) = '\0';
      npt = strchr(bgc, '!');
      if (NULL != npt) {
        *(npt++) = '\0';
      }
    }
    if (0 == dk3ma_d_from_c8_string(&f, per, NULL)) {
      /* ##### ERROR: Not a number */			$? "! not a number"
      goto cleanup;
    }
    if ((0.0 > f) || (100.0 < f)) {			$? "! out of range"
      /* ##### ERROR: Out of range */
      goto cleanup;
    }
    f = f / 100.0;
    bgr = bgg = bgb = 1.0;
    if (bgc) {
      cptr = plptcol_find_color(job, txt);
      if (NULL == cptr) {
        /* ##### ERROR: Color not found */
	goto cleanup;
      }
      bgr = cptr->r; bgg = cptr->g; bgb = cptr->b;
    }
    rres = f * rres + (1.0 - f) * bgr;	$? ". r = %lg", rres
    gres = f * gres + (1.0 - f) * bgg;	$? ". g = %lg", gres
    bres = f * bres + (1.0 - f) * bgb;	$? ". b = %lg", bres
    per = npt;
  }
  back = 1;				$? ". success"

  /*	Transfer results and return.
  */
  cleanup:
  *r = rres; *g = gres; *b = bres;
  finished:
  $? "- plptcol_get_color %d rgb=(%lg %lg %lg)", back, rres, gres, bres
  return back;
}