| 29 |
|
C_MATERIAL *c_cmaterial = &c_unmaterial; |
| 30 |
|
C_VERTEX *c_cvertex = &c_unvertex; |
| 31 |
|
|
| 32 |
< |
static LUTAB clr_tab; /* color lookup table */ |
| 33 |
< |
static LUTAB mat_tab; /* material lookup table */ |
| 34 |
< |
static LUTAB vtx_tab; /* vertex lookup table */ |
| 32 |
> |
static LUTAB clr_tab = LU_SINIT(free,free); /* color lookup table */ |
| 33 |
> |
static LUTAB mat_tab = LU_SINIT(free,free); /* material lookup table */ |
| 34 |
> |
static LUTAB vtx_tab = LU_SINIT(free,free); /* vertex lookup table */ |
| 35 |
|
|
| 36 |
+ |
/* CIE 1931 Standard Observer */ |
| 37 |
+ |
static C_COLOR cie_xf = C_CIEX; |
| 38 |
+ |
static C_COLOR cie_yf = C_CIEY; |
| 39 |
+ |
static C_COLOR cie_zf = C_CIEZ; |
| 40 |
|
|
| 41 |
+ |
static int setspectrum(); |
| 42 |
+ |
static void mixcolors(); |
| 43 |
+ |
|
| 44 |
+ |
|
| 45 |
|
int |
| 46 |
|
c_hcolor(ac, av) /* handle color entity */ |
| 47 |
|
int ac; |
| 48 |
|
register char **av; |
| 49 |
|
{ |
| 50 |
+ |
double w, wsum; |
| 51 |
+ |
register int i; |
| 52 |
|
register LUENT *lp; |
| 53 |
|
|
| 54 |
|
switch (mg_entity(av[0])) { |
| 55 |
|
case MG_E_COLOR: /* get/set color context */ |
| 56 |
+ |
if (ac > 4) |
| 57 |
+ |
return(MG_EARGC); |
| 58 |
|
if (ac == 1) { /* set unnamed color context */ |
| 59 |
|
c_uncolor = c_dfcolor; |
| 60 |
|
c_ccolor = &c_uncolor; |
| 101 |
|
return(MG_ETYPE); |
| 102 |
|
c_ccolor->cx = atof(av[1]); |
| 103 |
|
c_ccolor->cy = atof(av[2]); |
| 104 |
+ |
c_ccolor->flags = C_CDXY|C_CSXY; |
| 105 |
|
if (c_ccolor->cx < 0. | c_ccolor->cy < 0. | |
| 106 |
|
c_ccolor->cx + c_ccolor->cy > 1.) |
| 107 |
|
return(MG_EILL); |
| 108 |
|
return(MG_OK); |
| 109 |
+ |
case MG_E_CSPEC: /* assign spectral values */ |
| 110 |
+ |
if (ac < 5) |
| 111 |
+ |
return(MG_EARGC); |
| 112 |
+ |
if (!isint(av[1]) || !isint(av[2])) |
| 113 |
+ |
return(MG_ETYPE); |
| 114 |
+ |
return(setspectrum(c_ccolor, atoi(av[1]), atoi(av[2]), |
| 115 |
+ |
ac-3, av+3)); |
| 116 |
+ |
case MG_E_CMIX: /* mix colors */ |
| 117 |
+ |
if (ac < 5 || (ac-1)%2) |
| 118 |
+ |
return(MG_EARGC); |
| 119 |
+ |
if (!isflt(av[1])) |
| 120 |
+ |
return(MG_ETYPE); |
| 121 |
+ |
wsum = atof(av[1]); |
| 122 |
+ |
if (wsum < 0.) |
| 123 |
+ |
return(MG_EILL); |
| 124 |
+ |
if ((lp = lu_find(&clr_tab, av[2])) == NULL) |
| 125 |
+ |
return(MG_EMEM); |
| 126 |
+ |
if (lp->data == NULL) |
| 127 |
+ |
return(MG_EUNDEF); |
| 128 |
+ |
*c_ccolor = *(C_COLOR *)lp->data; |
| 129 |
+ |
for (i = 3; i < ac; i += 2) { |
| 130 |
+ |
if (!isflt(av[i])) |
| 131 |
+ |
return(MG_ETYPE); |
| 132 |
+ |
w = atof(av[i]); |
| 133 |
+ |
if (w < 0.) |
| 134 |
+ |
return(MG_EILL); |
| 135 |
+ |
if ((lp = lu_find(&clr_tab, av[i+1])) == NULL) |
| 136 |
+ |
return(MG_EMEM); |
| 137 |
+ |
if (lp->data == NULL) |
| 138 |
+ |
return(MG_EUNDEF); |
| 139 |
+ |
mixcolors(c_ccolor, wsum, c_ccolor, |
| 140 |
+ |
w, (C_COLOR *)lp->data); |
| 141 |
+ |
wsum += w; |
| 142 |
+ |
} |
| 143 |
+ |
return(MG_OK); |
| 144 |
|
} |
| 145 |
|
return(MG_EUNK); |
| 146 |
|
} |
| 155 |
|
|
| 156 |
|
switch (mg_entity(av[0])) { |
| 157 |
|
case MG_E_MATERIAL: /* get/set material context */ |
| 158 |
+ |
if (ac > 4) |
| 159 |
+ |
return(MG_EARGC); |
| 160 |
|
if (ac == 1) { /* set unnamed material context */ |
| 161 |
|
c_unmaterial = c_dfmaterial; |
| 162 |
|
c_cmaterial = &c_unmaterial; |
| 271 |
|
|
| 272 |
|
switch (mg_entity(av[0])) { |
| 273 |
|
case MG_E_VERTEX: /* get/set vertex context */ |
| 274 |
+ |
if (ac > 4) |
| 275 |
+ |
return(MG_EARGC); |
| 276 |
|
if (ac == 1) { /* set unnamed vertex context */ |
| 277 |
|
c_unvertex = c_dfvertex; |
| 278 |
|
c_cvertex = &c_unvertex; |
| 336 |
|
} |
| 337 |
|
|
| 338 |
|
|
| 287 |
– |
static void |
| 288 |
– |
freectx(lp) /* free a context table entry */ |
| 289 |
– |
register LUENT *lp; |
| 290 |
– |
{ |
| 291 |
– |
free((MEM_PTR)lp->key); |
| 292 |
– |
free((MEM_PTR)lp->data); |
| 293 |
– |
} |
| 294 |
– |
|
| 295 |
– |
|
| 339 |
|
void |
| 340 |
|
c_clearall() /* empty context tables */ |
| 341 |
|
{ |
| 342 |
|
c_uncolor = c_dfcolor; |
| 343 |
|
c_ccolor = &c_uncolor; |
| 344 |
< |
lu_done(&clr_tab, freectx); |
| 344 |
> |
lu_done(&clr_tab); |
| 345 |
|
c_unmaterial = c_dfmaterial; |
| 346 |
|
c_cmaterial = &c_unmaterial; |
| 347 |
< |
lu_done(&mat_tab, freectx); |
| 347 |
> |
lu_done(&mat_tab); |
| 348 |
|
c_unvertex = c_dfvertex; |
| 349 |
|
c_cvertex = &c_unvertex; |
| 350 |
< |
lu_done(&vtx_tab, freectx); |
| 350 |
> |
lu_done(&vtx_tab); |
| 351 |
|
} |
| 352 |
|
|
| 353 |
|
|
| 360 |
|
if ((lp = lu_find(&vtx_tab, name)) == NULL) |
| 361 |
|
return(NULL); |
| 362 |
|
return((C_VERTEX *)lp->data); |
| 363 |
+ |
} |
| 364 |
+ |
|
| 365 |
+ |
|
| 366 |
+ |
int |
| 367 |
+ |
c_isgrey(clr) /* check if color is grey */ |
| 368 |
+ |
register C_COLOR *clr; |
| 369 |
+ |
{ |
| 370 |
+ |
if (!clr->flags) |
| 371 |
+ |
return(1); /* no settings == grey */ |
| 372 |
+ |
c_ccvt(clr, C_CSXY); |
| 373 |
+ |
return(clr->cx >= .323 && clr->cx <= .343 && |
| 374 |
+ |
clr->cy >= .323 && clr->cy <= .343); |
| 375 |
+ |
} |
| 376 |
+ |
|
| 377 |
+ |
|
| 378 |
+ |
void |
| 379 |
+ |
c_ccvt(clr, fl) /* convert color representations */ |
| 380 |
+ |
register C_COLOR *clr; |
| 381 |
+ |
int fl; |
| 382 |
+ |
{ |
| 383 |
+ |
double x, y, z; |
| 384 |
+ |
register int i; |
| 385 |
+ |
|
| 386 |
+ |
if (clr->flags & fl) /* already done */ |
| 387 |
+ |
return; |
| 388 |
+ |
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set */ |
| 389 |
+ |
*clr = c_dfcolor; |
| 390 |
+ |
else if (fl & C_CSXY) { /* cspec -> cxy */ |
| 391 |
+ |
x = y = z = 0.; |
| 392 |
+ |
for (i = 0; i < C_CNSS; i++) { |
| 393 |
+ |
x += cie_xf.ssamp[i] * clr->ssamp[i]; |
| 394 |
+ |
y += cie_yf.ssamp[i] * clr->ssamp[i]; |
| 395 |
+ |
z += cie_zf.ssamp[i] * clr->ssamp[i]; |
| 396 |
+ |
} |
| 397 |
+ |
z += x + y; |
| 398 |
+ |
clr->cx = x / z; |
| 399 |
+ |
clr->cy = y / z; |
| 400 |
+ |
clr->flags |= C_CSXY; |
| 401 |
+ |
} else { /* cxy -> cspec */ |
| 402 |
+ |
z = (cie_xf.ssum + cie_yf.ssum + cie_zf.ssum) / 3.; |
| 403 |
+ |
x = clr->cx * z / cie_xf.ssum; |
| 404 |
+ |
y = clr->cy * z / cie_yf.ssum; |
| 405 |
+ |
z = (1. - clr->cx - clr->cy) * z / cie_zf.ssum; |
| 406 |
+ |
clr->ssum = 0; |
| 407 |
+ |
for (i = 0; i < C_CNSS; i++) |
| 408 |
+ |
clr->ssum += clr->ssamp[i] = |
| 409 |
+ |
x * cie_xf.ssamp[i] + |
| 410 |
+ |
y * cie_yf.ssamp[i] + |
| 411 |
+ |
z * cie_zf.ssamp[i] ; |
| 412 |
+ |
clr->flags |= C_CSSPEC; |
| 413 |
+ |
} |
| 414 |
+ |
} |
| 415 |
+ |
|
| 416 |
+ |
|
| 417 |
+ |
static int |
| 418 |
+ |
setspectrum(clr, wlmin, wlmax, ac, av) /* convert a spectrum */ |
| 419 |
+ |
register C_COLOR *clr; |
| 420 |
+ |
int wlmin, wlmax; |
| 421 |
+ |
int ac; |
| 422 |
+ |
char **av; |
| 423 |
+ |
{ |
| 424 |
+ |
double scale; |
| 425 |
+ |
float *va; |
| 426 |
+ |
register int i; |
| 427 |
+ |
int wl, pos; |
| 428 |
+ |
double wl0, wlstep; |
| 429 |
+ |
|
| 430 |
+ |
if (wlmin < C_CMINWL || wlmin >= wlmax || wlmax > C_CMAXWL) |
| 431 |
+ |
return(MG_EILL); |
| 432 |
+ |
if ((va = (float *)malloc(ac*sizeof(float))) == NULL) |
| 433 |
+ |
return(MG_EMEM); |
| 434 |
+ |
scale = 0.; /* get values and maximum */ |
| 435 |
+ |
for (i = 0; i < ac; i++) { |
| 436 |
+ |
if (!isflt(av[i])) |
| 437 |
+ |
return(MG_ETYPE); |
| 438 |
+ |
va[i] = atof(av[i]); |
| 439 |
+ |
if (va[i] < 0.) |
| 440 |
+ |
return(MG_EILL); |
| 441 |
+ |
if (va[i] > scale) |
| 442 |
+ |
scale = va[i]; |
| 443 |
+ |
} |
| 444 |
+ |
if (scale == 0.) |
| 445 |
+ |
return(MG_EILL); |
| 446 |
+ |
scale = C_CMAXV / scale; |
| 447 |
+ |
clr->ssum = 0; /* convert to our spacing */ |
| 448 |
+ |
wl0 = wlmin; |
| 449 |
+ |
wlstep = (double)(wlmax - wlmin)/(ac-1); |
| 450 |
+ |
pos = 0; |
| 451 |
+ |
for (i = 0, wl = C_CMINWL; i < C_CNSS; i++, wl += C_CWLI) |
| 452 |
+ |
if (wl < wlmin || wl > wlmax) |
| 453 |
+ |
clr->ssamp[i] = 0; |
| 454 |
+ |
else { |
| 455 |
+ |
while (wl0 + wlstep < wl+FTINY) { |
| 456 |
+ |
wl0 += wlstep; |
| 457 |
+ |
pos++; |
| 458 |
+ |
} |
| 459 |
+ |
if (wl+FTINY >= wl0 && wl-FTINY <= wl0) |
| 460 |
+ |
clr->ssamp[i] = scale*va[pos]; |
| 461 |
+ |
else /* interpolate if necessary */ |
| 462 |
+ |
clr->ssamp[i] = scale / wlstep * |
| 463 |
+ |
( va[pos]*(wl0+wlstep - wl) + |
| 464 |
+ |
va[pos+1]*(wl - wl0) ); |
| 465 |
+ |
clr->ssum += clr->ssamp[i]; |
| 466 |
+ |
} |
| 467 |
+ |
clr->flags = C_CDSPEC|C_CSSPEC; |
| 468 |
+ |
free((MEM_PTR)va); |
| 469 |
+ |
return(MG_OK); |
| 470 |
+ |
} |
| 471 |
+ |
|
| 472 |
+ |
|
| 473 |
+ |
static void |
| 474 |
+ |
mixcolors(cres, w1, c1, w2, c2) /* mix two colors according to weights given */ |
| 475 |
+ |
register C_COLOR *cres, *c1, *c2; |
| 476 |
+ |
double w1, w2; |
| 477 |
+ |
{ |
| 478 |
+ |
double scale; |
| 479 |
+ |
float cmix[C_CNSS]; |
| 480 |
+ |
register int i; |
| 481 |
+ |
|
| 482 |
+ |
if ((c1->flags|c2->flags) & C_CDSPEC) { /* spectral mixing */ |
| 483 |
+ |
c_ccvt(c1, C_CSSPEC); |
| 484 |
+ |
c_ccvt(c2, C_CSSPEC); |
| 485 |
+ |
w1 /= (double)c1->ssum; |
| 486 |
+ |
w2 /= (double)c2->ssum; |
| 487 |
+ |
scale = 0.; |
| 488 |
+ |
for (i = 0; i < C_CNSS; i++) { |
| 489 |
+ |
cmix[i] = w1*c1->ssamp[i] + w2*c2->ssamp[i]; |
| 490 |
+ |
if (cmix[i] > scale) |
| 491 |
+ |
scale = cmix[i]; |
| 492 |
+ |
} |
| 493 |
+ |
scale = C_CMAXV / scale; |
| 494 |
+ |
cres->ssum = 0; |
| 495 |
+ |
for (i = 0; i < C_CNSS; i++) |
| 496 |
+ |
cres->ssum += cres->ssamp[i] = scale*cmix[i] + .5; |
| 497 |
+ |
cres->flags = C_CDSPEC|C_CSSPEC; |
| 498 |
+ |
} else { /* CIE xy mixing */ |
| 499 |
+ |
c_ccvt(c1, C_CSXY); |
| 500 |
+ |
c_ccvt(c2, C_CSXY); |
| 501 |
+ |
scale = 1. / (w1/c1->cy + w2/c2->cy); |
| 502 |
+ |
cres->cx = (c1->cx*w1/c1->cy + c2->cx*w2/c2->cy) * scale; |
| 503 |
+ |
cres->cy = (w1 + w2) * scale; |
| 504 |
+ |
cres->flags = C_CDXY|C_CSXY; |
| 505 |
+ |
} |
| 506 |
|
} |