forked from bpostlethwaite/colormap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (115 loc) · 3.4 KB
/
index.js
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
/*
* Ben Postlethwaite
* January 2013
* License MIT
*/
'use strict';
var colorScale = require('./colorScale');
var lerp = require('lerp')
module.exports = createColormap;
function createColormap (spec) {
/*
* Default Options
*/
var indicies, rgba, fromrgba, torgba,
nsteps, cmap, colormap, format,
nshades, colors, alpha, index, i,
r = [],
g = [],
b = [],
a = [];
if ( !spec ) spec = {};
nshades = (spec.nshades || 72) - 1;
format = spec.format || 'hex';
colormap = spec.colormap;
if (!colormap) colormap = 'jet';
if (typeof colormap === 'string') {
colormap = colormap.toLowerCase();
if (!colorScale[colormap]) {
throw Error(colormap + ' not a supported colorscale');
}
cmap = colorScale[colormap];
} else if (Array.isArray(colormap)) {
cmap = colormap.slice();
} else {
throw Error('unsupported colormap option', colormap);
}
if (cmap.length > nshades) {
throw new Error(
colormap+' map requires nshades to be at least size '+cmap.length
);
}
if (!Array.isArray(spec.alpha)) {
if (typeof spec.alpha === 'number') {
alpha = [spec.alpha, spec.alpha];
} else {
alpha = [1, 1];
}
} else if (spec.alpha.length !== 2) {
alpha = [1, 1];
} else {
alpha = spec.alpha.slice();
}
// map index points from 0..1 to 0..n-1
indicies = cmap.map(function(c) {
return Math.round(c.index * nshades);
});
// Add alpha channel to the map
alpha[0] = Math.min(Math.max(alpha[0], 0), 1);
alpha[1] = Math.min(Math.max(alpha[1], 0), 1);
var steps = cmap.map(function(c, i) {
var index = cmap[i].index
var rgba = cmap[i].rgb.slice();
// if user supplies their own map use it
if (rgba.length === 4 && rgba[3] >= 0 && rgba[3] <= 1) {
return rgba
}
rgba[3] = alpha[0] + (alpha[1] - alpha[0])*index;
return rgba
})
/*
* map increasing linear values between indicies to
* linear steps in colorvalues
*/
var colors = []
for (i = 0; i < indicies.length-1; ++i) {
nsteps = indicies[i+1] - indicies[i];
fromrgba = steps[i];
torgba = steps[i+1];
for (var j = 0; j < nsteps; j++) {
var amt = j / nsteps
colors.push([
Math.round(lerp(fromrgba[0], torgba[0], amt)),
Math.round(lerp(fromrgba[1], torgba[1], amt)),
Math.round(lerp(fromrgba[2], torgba[2], amt)),
lerp(fromrgba[3], torgba[3], amt)
])
}
}
//add 1 step as last value
colors.push(cmap[cmap.length - 1].rgb.concat(alpha[1]))
if (format === 'hex') colors = colors.map( rgb2hex );
else if (format === 'rgbaString') colors = colors.map( rgbaStr );
else if (format === 'float') colors = colors.map( rgb2float );
return colors;
};
function rgb2float (rgba) {
return [
rgba[0] / 255,
rgba[1] / 255,
rgba[2] / 255,
rgba[3]
]
}
function rgb2hex (rgba) {
var dig, hex = '#';
for (var i = 0; i < 3; ++i) {
dig = rgba[i];
dig = dig.toString(16);
hex += ('00' + dig).substr( dig.length );
}
return hex;
}
function rgbaStr (rgba) {
return 'rgba(' + rgba.join(',') + ')';
}