1 |
greg |
1.1 |
#ifndef lint |
2 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
3 |
|
|
#endif |
4 |
|
|
|
5 |
|
|
/* |
6 |
|
|
* Convert colors and spectral ranges. |
7 |
|
|
*/ |
8 |
|
|
|
9 |
|
|
#include "color.h" |
10 |
|
|
|
11 |
|
|
/* |
12 |
|
|
* The following table contains the CIE tristimulus integrals |
13 |
|
|
* for X, Y, and Z. The table is cumulative, so that |
14 |
|
|
* each color coordinate integrates to 1. |
15 |
|
|
*/ |
16 |
|
|
|
17 |
|
|
#define STARTWL 380 /* starting wavelength (nanometers) */ |
18 |
|
|
#define INCWL 10 /* wavelength increment */ |
19 |
|
|
#define NINC 40 /* # of values */ |
20 |
|
|
|
21 |
|
|
static BYTE chroma[3][NINC] = { |
22 |
|
|
{ /* X */ |
23 |
|
|
0, 0, 0, 2, 6, 13, 22, 30, 36, 41, |
24 |
|
|
42, 43, 43, 44, 46, 52, 60, 71, 87, 106, |
25 |
|
|
128, 153, 178, 200, 219, 233, 243, 249, 252, 254, |
26 |
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
27 |
|
|
}, { /* Y */ |
28 |
|
|
0, 0, 0, 0, 0, 1, 2, 4, 7, 11, |
29 |
|
|
17, 24, 34, 48, 64, 84, 105, 127, 148, 169, |
30 |
|
|
188, 205, 220, 232, 240, 246, 250, 253, 254, 255, |
31 |
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
32 |
|
|
}, { /* Z */ |
33 |
|
|
0, 0, 2, 10, 32, 66, 118, 153, 191, 220, |
34 |
|
|
237, 246, 251, 253, 254, 255, 255, 255, 255, 255, |
35 |
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
36 |
|
|
255, 255, 255, 255, 255, 255, 255, 255, 255, 255 |
37 |
|
|
} |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
spec_rgb(col, s, e) /* compute RGB color from spectral range */ |
42 |
|
|
COLOR col; |
43 |
|
|
int s, e; |
44 |
|
|
{ |
45 |
|
|
COLOR ciecolor; |
46 |
|
|
|
47 |
|
|
spec_cie(ciecolor, s, e); |
48 |
|
|
cie_rgb(col, ciecolor); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
|
52 |
|
|
spec_cie(col, s, e) /* compute a color from a spectral range */ |
53 |
|
|
COLOR col; /* returned color */ |
54 |
|
|
int s, e; /* starting and ending wavelengths */ |
55 |
|
|
{ |
56 |
|
|
register int i, d, r; |
57 |
|
|
|
58 |
|
|
s -= STARTWL; |
59 |
|
|
if (s < 0) |
60 |
|
|
s = 0; |
61 |
|
|
|
62 |
|
|
e -= STARTWL; |
63 |
|
|
if (e >= INCWL*(NINC - 1)) |
64 |
|
|
e = INCWL*(NINC - 1) - 1; |
65 |
|
|
|
66 |
|
|
d = e / INCWL; /* interpolate values */ |
67 |
|
|
r = e % INCWL; |
68 |
|
|
for (i = 0; i < 3; i++) |
69 |
|
|
col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r; |
70 |
|
|
|
71 |
|
|
d = s / INCWL; |
72 |
|
|
r = s % INCWL; |
73 |
|
|
for (i = 0; i < 3; i++) |
74 |
|
|
col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r; |
75 |
|
|
|
76 |
|
|
col[RED] = (col[RED] + 0.5) / (256*INCWL); |
77 |
|
|
col[GRN] = (col[GRN] + 0.5) / (256*INCWL); |
78 |
|
|
col[BLU] = (col[BLU] + 0.5) / (256*INCWL); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
|
82 |
|
|
cie_rgb(rgbcolor, ciecolor) /* convert CIE to RGB (NTSC) */ |
83 |
|
|
register COLOR rgbcolor, ciecolor; |
84 |
|
|
{ |
85 |
|
|
static float cmat[3][3] = { |
86 |
|
|
1.73, -.48, -.26, |
87 |
|
|
-.81, 1.65, -.02, |
88 |
|
|
.08, -.17, 1.28, |
89 |
|
|
}; |
90 |
|
|
register int i; |
91 |
|
|
|
92 |
|
|
for (i = 0; i < 3; i++) { |
93 |
|
|
rgbcolor[i] = cmat[i][0]*ciecolor[0] + |
94 |
|
|
cmat[i][1]*ciecolor[1] + |
95 |
|
|
cmat[i][2]*ciecolor[2] ; |
96 |
|
|
if (rgbcolor[i] < 0.0) |
97 |
|
|
rgbcolor[i] = 0.0; |
98 |
|
|
} |
99 |
|
|
} |