-
Notifications
You must be signed in to change notification settings - Fork 0
/
colormap.c
149 lines (125 loc) · 4.55 KB
/
colormap.c
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
#include "colormap.h"
/* How the dicts are organized (colors.py in matploblib)
*
* Example: suppose you want red to increase from 0 to 1 over
the bottom half, green to do the same over the middle half,
and blue over the top half. Then you would use::
cdict = {'red': [(0.0, 0.0, 0.0),
(0.5, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0),
(0.25, 0.0, 0.0),
(0.75, 1.0, 1.0),
(1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0),
(0.5, 0.0, 0.0),
(1.0, 1.0, 1.0)]}
Each row in the table for a given color is a sequence of
*x*, *y0*, *y1* tuples. In each sequence, *x* must increase
monotonically from 0 to 1. For any input value *z* falling
between *x[i]* and *x[i+1]*, the output value of a given color
will be linearly interpolated between *y1[i]* and *y0[i+1]*::
row i: x y0 y1
/
/
row i+1: x y0 y1
Hence y0 in the first row and y1 in the last row are never used.
*/
void fill_cm_lookuptable_bone(unsigned int table[][3], const int N)
{
/* original bone specs. From _cm.py (matplotlib)
*
* {'red': ((0., 0., 0.),(0.746032, 0.652778, 0.652778),(1.0, 1.0, 1.0)),
* 'green': ((0., 0., 0.),(0.365079, 0.319444, 0.319444),
* (0.746032, 0.777778, 0.777778),(1.0, 1.0, 1.0)),
* 'blue': ((0., 0., 0.),(0.365079, 0.444444, 0.444444),(1.0, 1.0, 1.0))}
*
*/
/* bone clippled at 15% and 90% */
struct intval_node red_desc[] = {
{0.0, 0.15}, /* {0.0, 0.0} */
{0.746032, 0.652778}, /* {0.746032, 0.652778} */
{1.0, 0.9}
};
/* Green */
struct intval_node green_desc[] = {
{0.0, 0.15},
{0.365079, 0.319444},
{0.746032, 0.777778},
{1.0, 0.9}
};
/* Blue */
struct intval_node blue_desc[] = {
{0.0, 0.15},
{0.365079, 0.444444},
{1.0, 0.9},
};
struct color_desc red = { 3, red_desc };
struct color_desc green = { 4, green_desc };
struct color_desc blue = { 3, blue_desc };
interpolate_col(&red, table, N, 0);
interpolate_col(&green, table, N, 1);
interpolate_col(&blue, table, N, 2);
}
void fill_cm_lookuptable_jet(unsigned int table[][3], const int N)
{
/* Red */
struct intval_node red_desc[] = {
{0.00, 0.0},
{0.35, 0.0},
{0.66, 1.0},
{0.89, 1.0},
{1.00, 0.5}
};
/* Green */
struct intval_node green_desc[] = {
{0.00, 0.0},
{0.125, 0.0},
{0.375, 1.0},
{0.64, 1.0},
{0.91, 0.0},
{1.00, 0.0}
};
/* Blue */
struct intval_node blue_desc[] = {
{0.00, 0.5},
{0.11, 1.0},
{0.34, 1.0},
{0.65, 0.0},
{1.00, 0.0}
};
struct color_desc red = { 5, red_desc };
struct color_desc green = { 6, green_desc };
struct color_desc blue = { 5, blue_desc };
interpolate_col(&red, table, N, 0);
interpolate_col(&green, table, N, 1);
interpolate_col(&blue, table, N, 2);
}
void interpolate_col(const struct color_desc *color, unsigned int table[][3],
const int N, const int col_index)
{
double x_l, x_r; /* endpoints position interval */
int ind_lo, ind_hi; /* indices for subintervals */
double val_l, val_r; /* endpoints value interval */
for (int l = 0; l < color->n_subintervals; l++) {
x_l = color->nodes[l].position;
x_r = color->nodes[l + 1].position;
ind_lo = (int) rint(N * x_l);
ind_hi = (int) (rint(N * x_r) + 1.);
val_l = color->nodes[l].value;
val_r = color->nodes[l + 1].value;
if (val_l == val_r) {
for (int i = ind_lo; i < ind_hi; i++)
table[i][col_index] = (unsigned int) rint(N * val_l);
} else {
/* Interpolate linearly */
for (int i = ind_lo; i < ind_hi; i++) {
table[i][col_index] =
(unsigned int) rint(N * (val_l + (val_r - val_l)
* (i - (double) ind_lo) / (ind_hi -
(double)
ind_lo)));
}
}
}
}