| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
|
|
static const char RCSid[] = "$Id$";
|
| 3 |
|
|
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* Spectral color handling routines
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#include <stdio.h>
|
| 9 |
|
|
#include "ccolor.h"
|
| 10 |
|
|
|
| 11 |
|
|
|
| 12 |
|
|
C_COLOR c_dfcolor = C_DEFCOLOR;
|
| 13 |
|
|
|
| 14 |
|
|
/* CIE 1931 Standard Observer curves */
|
| 15 |
|
|
static C_COLOR cie_xf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
|
| 16 |
|
|
{14,42,143,435,1344,2839,3483,3362,2908,1954,956,
|
| 17 |
|
|
320,49,93,633,1655,2904,4334,5945,7621,9163,10263,
|
| 18 |
|
|
10622,10026,8544,6424,4479,2835,1649,874,468,227,
|
| 19 |
|
|
114,58,29,14,7,3,2,1,0}, 106836L, .467, .368, 362.230
|
| 20 |
|
|
};
|
| 21 |
|
|
static C_COLOR cie_yf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
|
| 22 |
|
|
{0,1,4,12,40,116,230,380,600,910,1390,2080,3230,
|
| 23 |
|
|
5030,7100,8620,9540,9950,9950,9520,8700,7570,6310,
|
| 24 |
|
|
5030,3810,2650,1750,1070,610,320,170,82,41,21,10,
|
| 25 |
|
|
5,2,1,1,0,0}, 106856L, .398, .542, 493.525
|
| 26 |
|
|
};
|
| 27 |
|
|
static C_COLOR cie_zf = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY|C_CSEFF,
|
| 28 |
|
|
{65,201,679,2074,6456,13856,17471,17721,16692,
|
| 29 |
|
|
12876,8130,4652,2720,1582,782,422,203,87,39,21,17,
|
| 30 |
|
|
11,8,3,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
|
| 31 |
|
|
106770L, .147, .077, 54.363
|
| 32 |
|
|
};
|
| 33 |
|
|
/* Derived CIE 1931 Primaries (imaginary) */
|
| 34 |
|
|
static C_COLOR cie_xp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
|
| 35 |
|
|
{-174,-198,-195,-197,-202,-213,-235,-272,-333,
|
| 36 |
|
|
-444,-688,-1232,-2393,-4497,-6876,-6758,-5256,
|
| 37 |
|
|
-3100,-815,1320,3200,4782,5998,6861,7408,7754,
|
| 38 |
|
|
7980,8120,8199,8240,8271,8292,8309,8283,8469,
|
| 39 |
|
|
8336,8336,8336,8336,8336,8336},
|
| 40 |
|
|
127424L, 1., .0,
|
| 41 |
|
|
};
|
| 42 |
|
|
static C_COLOR cie_yp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
|
| 43 |
|
|
{-451,-431,-431,-430,-427,-417,-399,-366,-312,
|
| 44 |
|
|
-204,57,691,2142,4990,8810,9871,9122,7321,5145,
|
| 45 |
|
|
3023,1123,-473,-1704,-2572,-3127,-3474,-3704,
|
| 46 |
|
|
-3846,-3927,-3968,-3999,-4021,-4038,-4012,-4201,
|
| 47 |
|
|
-4066,-4066,-4066,-4066,-4066,-4066},
|
| 48 |
|
|
-23035L, .0, 1.,
|
| 49 |
|
|
};
|
| 50 |
|
|
static C_COLOR cie_zp = { 1, NULL, C_CDSPEC|C_CSSPEC|C_CSXY,
|
| 51 |
|
|
{4051,4054,4052,4053,4054,4056,4059,4064,4071,
|
| 52 |
|
|
4074,4056,3967,3677,2933,1492,313,-440,-795,
|
| 53 |
|
|
-904,-918,-898,-884,-869,-863,-855,-855,-851,
|
| 54 |
|
|
-848,-847,-846,-846,-846,-845,-846,-843,-845,
|
| 55 |
|
|
-845,-845,-845,-845,-845},
|
| 56 |
|
|
36057L, .0, .0,
|
| 57 |
|
|
};
|
| 58 |
|
|
|
| 59 |
|
|
|
| 60 |
|
|
int
|
| 61 |
|
|
c_isgrey(clr) /* check if color is grey */
|
| 62 |
|
|
register C_COLOR *clr;
|
| 63 |
|
|
{
|
| 64 |
|
|
if (!(clr->flags & (C_CSXY|C_CSSPEC)))
|
| 65 |
|
|
return(1); /* no settings == grey */
|
| 66 |
|
|
c_ccvt(clr, C_CSXY);
|
| 67 |
|
|
return(clr->cx >= .323 && clr->cx <= .343 &&
|
| 68 |
|
|
clr->cy >= .323 && clr->cy <= .343);
|
| 69 |
|
|
}
|
| 70 |
|
|
|
| 71 |
|
|
|
| 72 |
|
|
void
|
| 73 |
|
|
c_ccvt(clr, fl) /* convert color representations */
|
| 74 |
|
|
register C_COLOR *clr;
|
| 75 |
|
|
int fl;
|
| 76 |
|
|
{
|
| 77 |
|
|
double x, y, z;
|
| 78 |
|
|
register int i;
|
| 79 |
|
|
|
| 80 |
|
|
fl &= ~clr->flags; /* ignore what's done */
|
| 81 |
|
|
if (!fl) /* everything's done! */
|
| 82 |
|
|
return;
|
| 83 |
|
|
if (!(clr->flags & (C_CSXY|C_CSSPEC))) /* nothing set! */
|
| 84 |
|
|
*clr = c_dfcolor;
|
| 85 |
|
|
if (fl & C_CSXY) { /* cspec -> cxy */
|
| 86 |
|
|
x = y = z = 0.;
|
| 87 |
|
|
for (i = 0; i < C_CNSS; i++) {
|
| 88 |
|
|
x += cie_xf.ssamp[i] * clr->ssamp[i];
|
| 89 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i];
|
| 90 |
|
|
z += cie_zf.ssamp[i] * clr->ssamp[i];
|
| 91 |
|
|
}
|
| 92 |
|
|
x /= (double)cie_xf.ssum;
|
| 93 |
|
|
y /= (double)cie_yf.ssum;
|
| 94 |
|
|
z /= (double)cie_zf.ssum;
|
| 95 |
|
|
z += x + y;
|
| 96 |
|
|
clr->cx = x / z;
|
| 97 |
|
|
clr->cy = y / z;
|
| 98 |
|
|
clr->flags |= C_CSXY;
|
| 99 |
|
|
} else if (fl & C_CSSPEC) { /* cxy -> cspec */
|
| 100 |
|
|
x = clr->cx;
|
| 101 |
|
|
y = clr->cy;
|
| 102 |
|
|
z = 1. - x - y;
|
| 103 |
|
|
clr->ssum = 0;
|
| 104 |
|
|
for (i = 0; i < C_CNSS; i++) {
|
| 105 |
|
|
clr->ssamp[i] = x*cie_xp.ssamp[i] + y*cie_yp.ssamp[i]
|
| 106 |
|
|
+ z*cie_zp.ssamp[i] + .5;
|
| 107 |
|
|
if (clr->ssamp[i] < 0) /* out of gamut! */
|
| 108 |
|
|
clr->ssamp[i] = 0;
|
| 109 |
|
|
else
|
| 110 |
|
|
clr->ssum += clr->ssamp[i];
|
| 111 |
|
|
}
|
| 112 |
|
|
clr->flags |= C_CSSPEC;
|
| 113 |
|
|
}
|
| 114 |
|
|
if (fl & C_CSEFF) { /* compute efficacy */
|
| 115 |
|
|
if (clr->flags & C_CSSPEC) { /* from spectrum */
|
| 116 |
|
|
y = 0.;
|
| 117 |
|
|
for (i = 0; i < C_CNSS; i++)
|
| 118 |
|
|
y += cie_yf.ssamp[i] * clr->ssamp[i];
|
| 119 |
|
|
clr->eff = C_CLPWM * y / clr->ssum;
|
| 120 |
|
|
} else /* clr->flags & C_CSXY */ { /* from (x,y) */
|
| 121 |
|
|
clr->eff = clr->cx*cie_xf.eff + clr->cy*cie_yf.eff +
|
| 122 |
|
|
(1. - clr->cx - clr->cy)*cie_zf.eff;
|
| 123 |
|
|
}
|
| 124 |
|
|
clr->flags |= C_CSEFF;
|
| 125 |
|
|
}
|
| 126 |
|
|
}
|