Skip to content

Commit 90df2af

Browse files
committed
change colorscale makeScaleFunction API
- options object now includes cmin / cmax - now respect all noNumericCheck + returnArray combination - small perf boost in noNumericCheck version
1 parent 8b1f52d commit 90df2af

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

src/components/colorscale/make_scale_function.js

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,28 @@ var Color = require('../color');
1818
/**
1919
* General colorscale function generator.
2020
*
21-
* By default, the routine compute that domain and range,
22-
* but optional can be called with pre-computed values.
21+
* Can be called in two forms:
22+
*
23+
* (1) makeScaleFunction(scl, { cmin: 0, cmax: 20 })
24+
* where cmin and cmax are used to compute the scale domain and range.
25+
*
26+
* (2) makeScaleFunction(scl, { domain: [0, 1, 3], range: [ 'red', 'green', 'blue'] })
27+
* where domain and range are the precomputed values.
2328
*
2429
* @param {array} scl
2530
* plotly.js colorscale array of arrays as found in fullData
26-
* @param {number} cmin
27-
* minimum color value (used to clamp scale)
28-
* @param {number} cmax
29-
* maximum color value (used to clamp scale)
30-
* @param {object} opts [optional]
31-
* - domain {array} precomputed domain
32-
* - range {array} precomputed range
33-
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks
34-
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings
3531
*
36-
* @return {function}
32+
* @param {object} opts
33+
* - cmin {number} minimum color value (used to clamp scale)
34+
* - cmax {number} maximum color value (used to clamp scale)
35+
* - domain {array} precomputed domain
36+
* - range {array} precomputed range
37+
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks
38+
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings
3739
*
40+
* @return {function}
3841
*/
39-
module.exports = function makeScaleFunction(scl, cmin, cmax, opts) {
42+
module.exports = function makeScaleFunction(scl, opts) {
4043
opts = opts || {};
4144

4245
var N = scl.length;
@@ -48,6 +51,9 @@ module.exports = function makeScaleFunction(scl, cmin, cmax, opts) {
4851
rangeOrig = opts.range;
4952
}
5053
else {
54+
var cmin = opts.cmin,
55+
cmax = opts.cmax;
56+
5157
domain = new Array(N);
5258
rangeOrig = new Array(N);
5359

@@ -64,32 +70,40 @@ module.exports = function makeScaleFunction(scl, cmin, cmax, opts) {
6470
for(i = 0; i < N; i++) {
6571
var rgba = tinycolor(rangeOrig[i]).toRgb();
6672

67-
range[i] = [ rgba.r, rgba.g, rgba.b, rgba.a ];
73+
range[i] = [rgba.r, rgba.g, rgba.b, rgba.a];
6874
}
6975

7076
var _sclFunc = d3.scale.linear()
7177
.domain(domain)
7278
.range(range)
7379
.clamp(true);
7480

75-
var sclFunc = function(v) {
76-
if(opts.noNumericCheck || isNumeric(v)) {
77-
var colorArray = _sclFunc(v);
81+
var noNumericCheck = opts.noNumericCheck,
82+
returnArray = opts.returnArray,
83+
sclFunc;
7884

79-
if(opts.returnArray) return colorArray;
80-
81-
var colorObj = {
82-
r: colorArray[0],
83-
g: colorArray[1],
84-
b: colorArray[2],
85-
a: colorArray[3]
86-
};
87-
88-
return tinycolor(colorObj).toRgbString();
89-
}
90-
else if(tinycolor(v).isValid()) return v;
91-
else return Color.defaultLine;
92-
};
85+
if(noNumericCheck && returnArray) {
86+
sclFunc = _sclFunc;
87+
}
88+
else if(noNumericCheck) {
89+
sclFunc = function(v) {
90+
return colorArray2rbga(_sclFunc(v));
91+
};
92+
}
93+
else if(returnArray) {
94+
sclFunc = function(v) {
95+
if(isNumeric(v)) return _sclFunc(v);
96+
else if(tinycolor(v).isValid()) return v;
97+
else return Color.defaultLine;
98+
};
99+
}
100+
else {
101+
sclFunc = function(v) {
102+
if(isNumeric(v)) return colorArray2rbga(_sclFunc(v));
103+
else if(tinycolor(v).isValid()) return v;
104+
else return Color.defaultLine;
105+
};
106+
}
93107

94108
// colorbar draw looks into the d3 scale closure for domain and range
95109

@@ -99,3 +113,14 @@ module.exports = function makeScaleFunction(scl, cmin, cmax, opts) {
99113

100114
return sclFunc;
101115
};
116+
117+
function colorArray2rbga(colorArray) {
118+
var colorObj = {
119+
r: colorArray[0],
120+
g: colorArray[1],
121+
b: colorArray[2],
122+
a: colorArray[3]
123+
};
124+
125+
return tinycolor(colorObj).toRgbString();
126+
}

0 commit comments

Comments
 (0)