1 |
greg |
1.1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Context handlers |
9 |
|
|
*/ |
10 |
|
|
|
11 |
|
|
#include <stdio.h> |
12 |
greg |
1.2 |
#include <math.h> |
13 |
greg |
1.1 |
#include <string.h> |
14 |
|
|
#include "parser.h" |
15 |
|
|
#include "lookup.h" |
16 |
|
|
|
17 |
|
|
/* default context values */ |
18 |
|
|
static C_COLOR c_dfcolor = C_DEFCOLOR; |
19 |
|
|
static C_MATERIAL c_dfmaterial = C_DEFMATERIAL; |
20 |
|
|
static C_VERTEX c_dfvertex = C_DEFVERTEX; |
21 |
|
|
|
22 |
|
|
/* the unnamed contexts */ |
23 |
|
|
static C_COLOR c_uncolor = C_DEFCOLOR; |
24 |
|
|
static C_MATERIAL c_unmaterial = C_DEFMATERIAL; |
25 |
|
|
static C_VERTEX c_unvertex = C_DEFVERTEX; |
26 |
|
|
|
27 |
|
|
/* the current contexts */ |
28 |
|
|
C_COLOR *c_ccolor = &c_uncolor; |
29 |
greg |
1.12 |
char *c_ccname = NULL; |
30 |
greg |
1.1 |
C_MATERIAL *c_cmaterial = &c_unmaterial; |
31 |
greg |
1.12 |
char *c_cmname = NULL; |
32 |
greg |
1.1 |
C_VERTEX *c_cvertex = &c_unvertex; |
33 |
greg |
1.12 |
char *c_cvname = NULL; |
34 |
greg |
1.1 |
|
35 |
greg |
1.3 |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
36 |
|
|
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
37 |
|
|
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
38 |
greg |
1.1 |
|
39 |
greg |
1.5 |
/* CIE 1931 Standard Observer */ |
40 |
|
|
static C_COLOR cie_xf = C_CIEX; |
41 |
|
|
static C_COLOR cie_yf = C_CIEY; |
42 |
|
|
static C_COLOR cie_zf = C_CIEZ; |
43 |
greg |
1.1 |
|
44 |
greg |
1.5 |
static int setspectrum(); |
45 |
|
|
static void mixcolors(); |
46 |
|
|
|
47 |
|
|
|
48 |
greg |
1.1 |
int |
49 |
|
|
c_hcolor(ac, av) /* handle color entity */ |
50 |
|
|
int ac; |
51 |
|
|
register char **av; |
52 |
|
|
{ |
53 |
greg |
1.5 |
double w, wsum; |
54 |
|
|
register int i; |
55 |
greg |
1.1 |
register LUENT *lp; |
56 |
|
|
|
57 |
|
|
switch (mg_entity(av[0])) { |
58 |
|
|
case MG_E_COLOR: /* get/set color context */ |
59 |
greg |
1.4 |
if (ac > 4) |
60 |
|
|
return(MG_EARGC); |
61 |
greg |
1.1 |
if (ac == 1) { /* set unnamed color context */ |
62 |
|
|
c_uncolor = c_dfcolor; |
63 |
|
|
c_ccolor = &c_uncolor; |
64 |
greg |
1.12 |
c_ccname = NULL; |
65 |
greg |
1.1 |
return(MG_OK); |
66 |
|
|
} |
67 |
|
|
lp = lu_find(&clr_tab, av[1]); /* lookup context */ |
68 |
|
|
if (lp == NULL) |
69 |
|
|
return(MG_EMEM); |
70 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
71 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
72 |
greg |
1.9 |
if (c_ccolor == NULL) |
73 |
greg |
1.1 |
return(MG_EUNDEF); |
74 |
|
|
return(MG_OK); |
75 |
|
|
} |
76 |
|
|
if (av[2][0] != '=' || av[2][1]) |
77 |
|
|
return(MG_ETYPE); |
78 |
greg |
1.9 |
if (c_ccolor == NULL) { /* create new color context */ |
79 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
80 |
|
|
if (lp->key == NULL) |
81 |
|
|
return(MG_EMEM); |
82 |
|
|
strcpy(lp->key, av[1]); |
83 |
|
|
lp->data = (char *)malloc(sizeof(C_COLOR)); |
84 |
|
|
if (lp->data == NULL) |
85 |
|
|
return(MG_EMEM); |
86 |
greg |
1.9 |
c_ccolor = (C_COLOR *)lp->data; |
87 |
|
|
c_ccolor->clock = 0; |
88 |
greg |
1.1 |
} |
89 |
greg |
1.12 |
c_ccname = lp->key; |
90 |
greg |
1.10 |
i = c_ccolor->clock; |
91 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
92 |
|
|
*c_ccolor = c_dfcolor; |
93 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
94 |
greg |
1.1 |
return(MG_OK); |
95 |
|
|
} |
96 |
|
|
lp = lu_find(&clr_tab, av[3]); /* lookup template */ |
97 |
|
|
if (lp == NULL) |
98 |
|
|
return(MG_EMEM); |
99 |
|
|
if (lp->data == NULL) |
100 |
|
|
return(MG_EUNDEF); |
101 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
102 |
greg |
1.9 |
c_ccolor->clock = i + 1; |
103 |
greg |
1.1 |
return(MG_OK); |
104 |
|
|
case MG_E_CXY: /* assign CIE XY value */ |
105 |
|
|
if (ac != 3) |
106 |
|
|
return(MG_EARGC); |
107 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
108 |
|
|
return(MG_ETYPE); |
109 |
|
|
c_ccolor->cx = atof(av[1]); |
110 |
|
|
c_ccolor->cy = atof(av[2]); |
111 |
greg |
1.5 |
c_ccolor->flags = C_CDXY|C_CSXY; |
112 |
greg |
1.1 |
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
113 |
|
|
c_ccolor->cx + c_ccolor->cy > 1.) |
114 |
|
|
return(MG_EILL); |
115 |
greg |
1.8 |
c_ccolor->clock++; |
116 |
greg |
1.1 |
return(MG_OK); |
117 |
greg |
1.5 |
case MG_E_CSPEC: /* assign spectral values */ |
118 |
|
|
if (ac < 5) |
119 |
|
|
return(MG_EARGC); |
120 |
|
|
if (!isint(av[1]) || !isint(av[2])) |
121 |
|
|
return(MG_ETYPE); |
122 |
|
|
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
123 |
|
|
ac-3, av+3)); |
124 |
|
|
case MG_E_CMIX: /* mix colors */ |
125 |
|
|
if (ac < 5 || (ac-1)%2) |
126 |
|
|
return(MG_EARGC); |
127 |
|
|
if (!isflt(av[1])) |
128 |
|
|
return(MG_ETYPE); |
129 |
|
|
wsum = atof(av[1]); |
130 |
|
|
if (wsum < 0.) |
131 |
|
|
return(MG_EILL); |
132 |
|
|
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
133 |
|
|
return(MG_EMEM); |
134 |
|
|
if (lp->data == NULL) |
135 |
|
|
return(MG_EUNDEF); |
136 |
|
|
*c_ccolor = *(C_COLOR *)lp->data; |
137 |
|
|
for (i = 3; i < ac; i += 2) { |
138 |
|
|
if (!isflt(av[i])) |
139 |
|
|
return(MG_ETYPE); |
140 |
|
|
w = atof(av[i]); |
141 |
|
|
if (w < 0.) |
142 |
|
|
return(MG_EILL); |
143 |
|
|
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
144 |
|
|
return(MG_EMEM); |
145 |
|
|
if (lp->data == NULL) |
146 |
|
|
return(MG_EUNDEF); |
147 |
|
|
mixcolors(c_ccolor, wsum, c_ccolor, |
148 |
|
|
w, (C_COLOR *)lp->data); |
149 |
|
|
wsum += w; |
150 |
|
|
} |
151 |
greg |
1.10 |
c_ccolor->clock++; |
152 |
greg |
1.5 |
return(MG_OK); |
153 |
greg |
1.1 |
} |
154 |
|
|
return(MG_EUNK); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
|
|
int |
159 |
|
|
c_hmaterial(ac, av) /* handle material entity */ |
160 |
|
|
int ac; |
161 |
|
|
register char **av; |
162 |
|
|
{ |
163 |
greg |
1.9 |
int i; |
164 |
greg |
1.1 |
register LUENT *lp; |
165 |
|
|
|
166 |
|
|
switch (mg_entity(av[0])) { |
167 |
|
|
case MG_E_MATERIAL: /* get/set material context */ |
168 |
greg |
1.4 |
if (ac > 4) |
169 |
|
|
return(MG_EARGC); |
170 |
greg |
1.1 |
if (ac == 1) { /* set unnamed material context */ |
171 |
|
|
c_unmaterial = c_dfmaterial; |
172 |
|
|
c_cmaterial = &c_unmaterial; |
173 |
greg |
1.12 |
c_cmname = NULL; |
174 |
greg |
1.1 |
return(MG_OK); |
175 |
|
|
} |
176 |
|
|
lp = lu_find(&mat_tab, av[1]); /* lookup context */ |
177 |
|
|
if (lp == NULL) |
178 |
|
|
return(MG_EMEM); |
179 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
180 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
181 |
greg |
1.9 |
if (c_cmaterial == NULL) |
182 |
greg |
1.1 |
return(MG_EUNDEF); |
183 |
|
|
return(MG_OK); |
184 |
|
|
} |
185 |
|
|
if (av[2][0] != '=' || av[2][1]) |
186 |
|
|
return(MG_ETYPE); |
187 |
greg |
1.9 |
if (c_cmaterial == NULL) { /* create new material */ |
188 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
189 |
|
|
if (lp->key == NULL) |
190 |
|
|
return(MG_EMEM); |
191 |
|
|
strcpy(lp->key, av[1]); |
192 |
|
|
lp->data = (char *)malloc(sizeof(C_MATERIAL)); |
193 |
|
|
if (lp->data == NULL) |
194 |
|
|
return(MG_EMEM); |
195 |
greg |
1.9 |
c_cmaterial = (C_MATERIAL *)lp->data; |
196 |
|
|
c_cmaterial->clock = 0; |
197 |
greg |
1.1 |
} |
198 |
greg |
1.12 |
c_cmname = lp->key; |
199 |
greg |
1.10 |
i = c_cmaterial->clock; |
200 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
201 |
|
|
*c_cmaterial = c_dfmaterial; |
202 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
203 |
greg |
1.1 |
return(MG_OK); |
204 |
|
|
} |
205 |
|
|
lp = lu_find(&mat_tab, av[3]); /* lookup template */ |
206 |
|
|
if (lp == NULL) |
207 |
|
|
return(MG_EMEM); |
208 |
|
|
if (lp->data == NULL) |
209 |
|
|
return(MG_EUNDEF); |
210 |
|
|
*c_cmaterial = *(C_MATERIAL *)lp->data; |
211 |
greg |
1.9 |
c_cmaterial->clock = i + 1; |
212 |
greg |
1.1 |
return(MG_OK); |
213 |
|
|
case MG_E_RD: /* set diffuse reflectance */ |
214 |
|
|
if (ac != 2) |
215 |
|
|
return(MG_EARGC); |
216 |
|
|
if (!isflt(av[1])) |
217 |
|
|
return(MG_ETYPE); |
218 |
|
|
c_cmaterial->rd = atof(av[1]); |
219 |
|
|
if (c_cmaterial->rd < 0. | c_cmaterial->rd > 1.) |
220 |
|
|
return(MG_EILL); |
221 |
|
|
c_cmaterial->rd_c = *c_ccolor; |
222 |
greg |
1.2 |
c_cmaterial->clock++; |
223 |
greg |
1.1 |
return(MG_OK); |
224 |
|
|
case MG_E_ED: /* set diffuse emittance */ |
225 |
|
|
if (ac != 2) |
226 |
|
|
return(MG_EARGC); |
227 |
|
|
if (!isflt(av[1])) |
228 |
|
|
return(MG_ETYPE); |
229 |
|
|
c_cmaterial->ed = atof(av[1]); |
230 |
|
|
if (c_cmaterial->ed < 0.) |
231 |
|
|
return(MG_EILL); |
232 |
|
|
c_cmaterial->ed_c = *c_ccolor; |
233 |
greg |
1.2 |
c_cmaterial->clock++; |
234 |
greg |
1.1 |
return(MG_OK); |
235 |
|
|
case MG_E_TD: /* set diffuse transmittance */ |
236 |
|
|
if (ac != 2) |
237 |
|
|
return(MG_EARGC); |
238 |
|
|
if (!isflt(av[1])) |
239 |
|
|
return(MG_ETYPE); |
240 |
|
|
c_cmaterial->td = atof(av[1]); |
241 |
|
|
if (c_cmaterial->td < 0. | c_cmaterial->td > 1.) |
242 |
|
|
return(MG_EILL); |
243 |
|
|
c_cmaterial->td_c = *c_ccolor; |
244 |
greg |
1.8 |
c_cmaterial->clock++; |
245 |
greg |
1.1 |
return(MG_OK); |
246 |
|
|
case MG_E_RS: /* set specular reflectance */ |
247 |
|
|
if (ac != 3) |
248 |
|
|
return(MG_EARGC); |
249 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
250 |
|
|
return(MG_ETYPE); |
251 |
|
|
c_cmaterial->rs = atof(av[1]); |
252 |
|
|
c_cmaterial->rs_a = atof(av[2]); |
253 |
|
|
if (c_cmaterial->rs < 0. | c_cmaterial->rs > 1. | |
254 |
|
|
c_cmaterial->rs_a < 0.) |
255 |
|
|
return(MG_EILL); |
256 |
|
|
c_cmaterial->rs_c = *c_ccolor; |
257 |
greg |
1.2 |
c_cmaterial->clock++; |
258 |
greg |
1.1 |
return(MG_OK); |
259 |
|
|
case MG_E_TS: /* set specular transmittance */ |
260 |
|
|
if (ac != 3) |
261 |
|
|
return(MG_EARGC); |
262 |
|
|
if (!isflt(av[1]) || !isflt(av[2])) |
263 |
|
|
return(MG_ETYPE); |
264 |
|
|
c_cmaterial->ts = atof(av[1]); |
265 |
|
|
c_cmaterial->ts_a = atof(av[2]); |
266 |
|
|
if (c_cmaterial->ts < 0. | c_cmaterial->ts > 1. | |
267 |
|
|
c_cmaterial->ts_a < 0.) |
268 |
|
|
return(MG_EILL); |
269 |
|
|
c_cmaterial->ts_c = *c_ccolor; |
270 |
greg |
1.2 |
c_cmaterial->clock++; |
271 |
greg |
1.1 |
return(MG_OK); |
272 |
greg |
1.13 |
case MG_E_SIDES: /* set number of sides */ |
273 |
|
|
if (ac != 2) |
274 |
|
|
return(MG_EARGC); |
275 |
|
|
if (!isint(av[1])) |
276 |
|
|
return(MG_ETYPE); |
277 |
|
|
i = atoi(av[1]); |
278 |
|
|
if (i == 1) |
279 |
|
|
c_cmaterial->sided = 1; |
280 |
|
|
else if (i == 2) |
281 |
|
|
c_cmaterial->sided = 0; |
282 |
|
|
else |
283 |
|
|
return(MG_EILL); |
284 |
|
|
c_cmaterial->clock++; |
285 |
|
|
return(MG_OK); |
286 |
greg |
1.1 |
} |
287 |
|
|
return(MG_EUNK); |
288 |
|
|
} |
289 |
|
|
|
290 |
|
|
|
291 |
|
|
int |
292 |
|
|
c_hvertex(ac, av) /* handle a vertex entity */ |
293 |
|
|
int ac; |
294 |
|
|
register char **av; |
295 |
|
|
{ |
296 |
greg |
1.9 |
int i; |
297 |
greg |
1.1 |
register LUENT *lp; |
298 |
|
|
|
299 |
|
|
switch (mg_entity(av[0])) { |
300 |
|
|
case MG_E_VERTEX: /* get/set vertex context */ |
301 |
greg |
1.4 |
if (ac > 4) |
302 |
|
|
return(MG_EARGC); |
303 |
greg |
1.1 |
if (ac == 1) { /* set unnamed vertex context */ |
304 |
|
|
c_unvertex = c_dfvertex; |
305 |
|
|
c_cvertex = &c_unvertex; |
306 |
greg |
1.12 |
c_cvname = NULL; |
307 |
greg |
1.1 |
return(MG_OK); |
308 |
|
|
} |
309 |
|
|
lp = lu_find(&vtx_tab, av[1]); /* lookup context */ |
310 |
|
|
if (lp == NULL) |
311 |
|
|
return(MG_EMEM); |
312 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
313 |
greg |
1.1 |
if (ac == 2) { /* reestablish previous context */ |
314 |
greg |
1.9 |
if (c_cvertex == NULL) |
315 |
greg |
1.1 |
return(MG_EUNDEF); |
316 |
|
|
return(MG_OK); |
317 |
|
|
} |
318 |
|
|
if (av[2][0] != '=' || av[2][1]) |
319 |
|
|
return(MG_ETYPE); |
320 |
greg |
1.9 |
if (c_cvertex == NULL) { /* create new vertex context */ |
321 |
greg |
1.1 |
lp->key = (char *)malloc(strlen(av[1])+1); |
322 |
|
|
if (lp->key == NULL) |
323 |
|
|
return(MG_EMEM); |
324 |
|
|
strcpy(lp->key, av[1]); |
325 |
|
|
lp->data = (char *)malloc(sizeof(C_VERTEX)); |
326 |
|
|
if (lp->data == NULL) |
327 |
|
|
return(MG_EMEM); |
328 |
greg |
1.9 |
c_cvertex = (C_VERTEX *)lp->data; |
329 |
greg |
1.1 |
} |
330 |
greg |
1.12 |
c_cvname = lp->key; |
331 |
greg |
1.10 |
i = c_cvertex->clock; |
332 |
greg |
1.1 |
if (ac == 3) { /* use default template */ |
333 |
|
|
*c_cvertex = c_dfvertex; |
334 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
335 |
greg |
1.1 |
return(MG_OK); |
336 |
|
|
} |
337 |
|
|
lp = lu_find(&vtx_tab, av[3]); /* lookup template */ |
338 |
|
|
if (lp == NULL) |
339 |
|
|
return(MG_EMEM); |
340 |
|
|
if (lp->data == NULL) |
341 |
|
|
return(MG_EUNDEF); |
342 |
|
|
*c_cvertex = *(C_VERTEX *)lp->data; |
343 |
greg |
1.9 |
c_cvertex->clock = i + 1; |
344 |
greg |
1.1 |
return(MG_OK); |
345 |
|
|
case MG_E_POINT: /* set point */ |
346 |
|
|
if (ac != 4) |
347 |
|
|
return(MG_EARGC); |
348 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
349 |
|
|
return(MG_ETYPE); |
350 |
|
|
c_cvertex->p[0] = atof(av[1]); |
351 |
|
|
c_cvertex->p[1] = atof(av[2]); |
352 |
|
|
c_cvertex->p[2] = atof(av[3]); |
353 |
greg |
1.8 |
c_cvertex->clock++; |
354 |
greg |
1.1 |
return(MG_OK); |
355 |
|
|
case MG_E_NORMAL: /* set normal */ |
356 |
|
|
if (ac != 4) |
357 |
|
|
return(MG_EARGC); |
358 |
|
|
if (!isflt(av[1]) || !isflt(av[2]) || !isflt(av[3])) |
359 |
|
|
return(MG_ETYPE); |
360 |
|
|
c_cvertex->n[0] = atof(av[1]); |
361 |
|
|
c_cvertex->n[1] = atof(av[2]); |
362 |
|
|
c_cvertex->n[2] = atof(av[3]); |
363 |
|
|
(void)normalize(c_cvertex->n); |
364 |
greg |
1.8 |
c_cvertex->clock++; |
365 |
greg |
1.1 |
return(MG_OK); |
366 |
|
|
} |
367 |
|
|
return(MG_EUNK); |
368 |
|
|
} |
369 |
|
|
|
370 |
|
|
|
371 |
|
|
void |
372 |
|
|
c_clearall() /* empty context tables */ |
373 |
|
|
{ |
374 |
|
|
c_uncolor = c_dfcolor; |
375 |
|
|
c_ccolor = &c_uncolor; |
376 |
greg |
1.3 |
lu_done(&clr_tab); |
377 |
greg |
1.1 |
c_unmaterial = c_dfmaterial; |
378 |
|
|
c_cmaterial = &c_unmaterial; |
379 |
greg |
1.3 |
lu_done(&mat_tab); |
380 |
greg |
1.1 |
c_unvertex = c_dfvertex; |
381 |
|
|
c_cvertex = &c_unvertex; |
382 |
greg |
1.3 |
lu_done(&vtx_tab); |
383 |
greg |
1.1 |
} |
384 |
|
|
|
385 |
|
|
|
386 |
greg |
1.11 |
C_MATERIAL * |
387 |
|
|
c_getmaterial(name) /* get a named material */ |
388 |
|
|
char *name; |
389 |
|
|
{ |
390 |
|
|
register LUENT *lp; |
391 |
|
|
|
392 |
|
|
if ((lp = lu_find(&mat_tab, name)) == NULL) |
393 |
|
|
return(NULL); |
394 |
|
|
return((C_MATERIAL *)lp->data); |
395 |
|
|
} |
396 |
|
|
|
397 |
|
|
|
398 |
greg |
1.1 |
C_VERTEX * |
399 |
|
|
c_getvert(name) /* get a named vertex */ |
400 |
|
|
char *name; |
401 |
|
|
{ |
402 |
|
|
register LUENT *lp; |
403 |
|
|
|
404 |
|
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
405 |
|
|
return(NULL); |
406 |
|
|
return((C_VERTEX *)lp->data); |
407 |
greg |
1.5 |
} |
408 |
|
|
|
409 |
|
|
|
410 |
greg |
1.7 |
C_COLOR * |
411 |
|
|
c_getcolor(name) /* get a named color */ |
412 |
|
|
char *name; |
413 |
|
|
{ |
414 |
|
|
register LUENT *lp; |
415 |
|
|
|
416 |
|
|
if ((lp = lu_find(&clr_tab, name)) == NULL) |
417 |
|
|
return(NULL); |
418 |
|
|
return((C_COLOR *)lp->data); |
419 |
|
|
} |
420 |
|
|
|
421 |
|
|
|
422 |
greg |
1.5 |
int |
423 |
|
|
c_isgrey(clr) /* check if color is grey */ |
424 |
|
|
register C_COLOR *clr; |
425 |
|
|
{ |
426 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) |
427 |
greg |
1.5 |
return(1); /* no settings == grey */ |
428 |
|
|
c_ccvt(clr, C_CSXY); |
429 |
|
|
return(clr->cx >= .323 && clr->cx <= .343 && |
430 |
|
|
clr->cy >= .323 && clr->cy <= .343); |
431 |
|
|
} |
432 |
|
|
|
433 |
|
|
|
434 |
|
|
void |
435 |
|
|
c_ccvt(clr, fl) /* convert color representations */ |
436 |
|
|
register C_COLOR *clr; |
437 |
|
|
int fl; |
438 |
|
|
{ |
439 |
|
|
double x, y, z; |
440 |
|
|
register int i; |
441 |
|
|
|
442 |
|
|
if (clr->flags & fl) /* already done */ |
443 |
|
|
return; |
444 |
greg |
1.8 |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */ |
445 |
greg |
1.5 |
*clr = c_dfcolor; |
446 |
|
|
else if (fl & C_CSXY) { /* cspec -> cxy */ |
447 |
|
|
x = y = z = 0.; |
448 |
|
|
for (i = 0; i < C_CNSS; i++) { |
449 |
|
|
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
450 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
451 |
|
|
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
452 |
|
|
} |
453 |
|
|
z += x + y; |
454 |
|
|
clr->cx = x / z; |
455 |
|
|
clr->cy = y / z; |
456 |
|
|
clr->flags |= C_CSXY; |
457 |
|
|
} else { /* cxy -> cspec */ |
458 |
|
|
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
459 |
|
|
x = clr->cx * z / cie_xf.ssum; |
460 |
|
|
y = clr->cy * z / cie_yf.ssum; |
461 |
|
|
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
462 |
|
|
clr->ssum = 0; |
463 |
|
|
for (i = 0; i < C_CNSS; i++) |
464 |
|
|
clr->ssum += clr->ssamp[i] = |
465 |
|
|
x * cie_xf.ssamp[i] + |
466 |
|
|
y * cie_yf.ssamp[i] + |
467 |
|
|
z * cie_zf.ssamp[i] ; |
468 |
|
|
clr->flags |= C_CSSPEC; |
469 |
|
|
} |
470 |
|
|
} |
471 |
|
|
|
472 |
|
|
|
473 |
|
|
static int |
474 |
|
|
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
475 |
|
|
register C_COLOR *clr; |
476 |
|
|
int wlmin, wlmax; |
477 |
|
|
int ac; |
478 |
|
|
char **av; |
479 |
|
|
{ |
480 |
|
|
double scale; |
481 |
|
|
float *va; |
482 |
|
|
register int i; |
483 |
|
|
int wl, pos; |
484 |
|
|
double wl0, wlstep; |
485 |
|
|
|
486 |
|
|
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
487 |
|
|
return(MG_EILL); |
488 |
|
|
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
489 |
|
|
return(MG_EMEM); |
490 |
|
|
scale = 0.; /* get values and maximum */ |
491 |
|
|
for (i = 0; i < ac; i++) { |
492 |
|
|
if (!isflt(av[i])) |
493 |
|
|
return(MG_ETYPE); |
494 |
|
|
va[i] = atof(av[i]); |
495 |
|
|
if (va[i] < 0.) |
496 |
|
|
return(MG_EILL); |
497 |
|
|
if (va[i] > scale) |
498 |
|
|
scale = va[i]; |
499 |
|
|
} |
500 |
|
|
if (scale == 0.) |
501 |
|
|
return(MG_EILL); |
502 |
|
|
scale = C_CMAXV / scale; |
503 |
|
|
clr->ssum = 0; /* convert to our spacing */ |
504 |
|
|
wl0 = wlmin; |
505 |
|
|
wlstep = (double)(wlmax - wlmin)/(ac-1); |
506 |
|
|
pos = 0; |
507 |
|
|
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
508 |
|
|
if (wl < wlmin || wl > wlmax) |
509 |
|
|
clr->ssamp[i] = 0; |
510 |
|
|
else { |
511 |
|
|
while (wl0 + wlstep < wl+FTINY) { |
512 |
|
|
wl0 += wlstep; |
513 |
|
|
pos++; |
514 |
|
|
} |
515 |
|
|
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
516 |
|
|
clr->ssamp[i] = scale*va[pos]; |
517 |
|
|
else /* interpolate if necessary */ |
518 |
|
|
clr->ssamp[i] = scale / wlstep * |
519 |
|
|
( va[pos]*(wl0+wlstep - wl) + |
520 |
|
|
va[pos+1]*(wl - wl0) ); |
521 |
|
|
clr->ssum += clr->ssamp[i]; |
522 |
|
|
} |
523 |
|
|
clr->flags = C_CDSPEC|C_CSSPEC; |
524 |
greg |
1.8 |
clr->clock++; |
525 |
greg |
1.5 |
free((MEM_PTR)va); |
526 |
|
|
return(MG_OK); |
527 |
|
|
} |
528 |
|
|
|
529 |
|
|
|
530 |
|
|
static void |
531 |
|
|
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
532 |
|
|
register C_COLOR *cres, *c1, *c2; |
533 |
|
|
double w1, w2; |
534 |
|
|
{ |
535 |
|
|
double scale; |
536 |
|
|
float cmix[C_CNSS]; |
537 |
|
|
register int i; |
538 |
|
|
|
539 |
|
|
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
540 |
|
|
c_ccvt(c1, C_CSSPEC); |
541 |
|
|
c_ccvt(c2, C_CSSPEC); |
542 |
|
|
w1 /= (double)c1->ssum; |
543 |
|
|
w2 /= (double)c2->ssum; |
544 |
|
|
scale = 0.; |
545 |
|
|
for (i = 0; i < C_CNSS; i++) { |
546 |
|
|
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
547 |
|
|
if (cmix[i] > scale) |
548 |
|
|
scale = cmix[i]; |
549 |
|
|
} |
550 |
|
|
scale = C_CMAXV / scale; |
551 |
|
|
cres->ssum = 0; |
552 |
|
|
for (i = 0; i < C_CNSS; i++) |
553 |
|
|
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
554 |
|
|
cres->flags = C_CDSPEC|C_CSSPEC; |
555 |
|
|
} else { /* CIE xy mixing */ |
556 |
|
|
c_ccvt(c1, C_CSXY); |
557 |
|
|
c_ccvt(c2, C_CSXY); |
558 |
|
|
scale = 1. / (w1/c1->cy + w2/c2->cy); |
559 |
|
|
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
560 |
|
|
cres->cy = (w1 + w2) * scale; |
561 |
|
|
cres->flags = C_CDXY|C_CSXY; |
562 |
|
|
} |
563 |
greg |
1.1 |
} |