Skip to content

Commit cb86fac

Browse files
committed
colorscale: split make_color_scale_function
- into extractScale and makeColorScaleFn
1 parent 654f5cd commit cb86fac

File tree

13 files changed

+127
-101
lines changed

13 files changed

+127
-101
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
/**
13+
* Extract colorscale into numeric domain and color range.
14+
*
15+
* @param {array} scl colorscale array of arrays
16+
* @param {number} cmin minimum color value (used to clamp scale)
17+
* @param {number} cmax maximum color value (used to clamp scale)
18+
*/
19+
module.exports = function extractScale(scl, cmin, cmax) {
20+
var N = scl.length,
21+
domain = new Array(N),
22+
range = new Array(N);
23+
24+
for(var i = 0; i < N; i++) {
25+
var si = scl[i];
26+
27+
domain[i] = cmin + si[0] * (cmax - cmin);
28+
range[i] = si[1];
29+
}
30+
31+
return {
32+
domain: domain,
33+
range: range
34+
};
35+
};

src/components/colorscale/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ exports.getScale = require('./get_scale');
2727

2828
exports.flipScale = require('./flip_scale');
2929

30-
exports.makeScaleFunction = require('./make_scale_function');
30+
exports.extractScale = require('./extract_scale');
31+
32+
exports.makeColorScaleFunc = require('./make_color_scale_func');

src/components/colorscale/make_scale_function.js renamed to src/components/colorscale/make_color_scale_func.js

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,64 +18,32 @@ var Color = require('../color');
1818
/**
1919
* General colorscale function generator.
2020
*
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.
28-
*
29-
* @param {array} scl
30-
* plotly.js colorscale array of arrays as found in fullData
21+
* @param {object} specs output of Colorscale.extractScale or precomputed domain, range.
22+
* - domain {array}
23+
* - range {array}
3124
*
3225
* @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
3726
* - noNumericCheck {boolean} if true, scale func bypasses numeric checks
3827
* - returnArray {boolean} if true, scale func return 4-item array instead of color strings
3928
*
4029
* @return {function}
4130
*/
42-
module.exports = function makeScaleFunction(scl, opts) {
31+
module.exports = function makeColorScaleFunc(specs, opts) {
4332
opts = opts || {};
4433

45-
var N = scl.length;
46-
47-
var domain, rangeOrig, i;
48-
49-
if(opts.domain && opts.range) {
50-
domain = opts.domain;
51-
rangeOrig = opts.range;
52-
}
53-
else {
54-
var cmin = opts.cmin,
55-
cmax = opts.cmax;
56-
57-
domain = new Array(N);
58-
rangeOrig = new Array(N);
59-
60-
for(i = 0; i < N; i++) {
61-
var si = scl[i];
62-
63-
domain[i] = cmin + si[0] * (cmax - cmin);
64-
rangeOrig[i] = si[1];
65-
}
66-
}
67-
68-
var range = new Array(N);
69-
70-
for(i = 0; i < N; i++) {
71-
var rgba = tinycolor(rangeOrig[i]).toRgb();
34+
var domain = specs.domain,
35+
range = specs.range,
36+
N = range.length,
37+
_range = new Array(N);
7238

73-
range[i] = [rgba.r, rgba.g, rgba.b, rgba.a];
39+
for(var i = 0; i < N; i++) {
40+
var rgba = tinycolor(range[i]).toRgb();
41+
_range[i] = [rgba.r, rgba.g, rgba.b, rgba.a];
7442
}
7543

7644
var _sclFunc = d3.scale.linear()
7745
.domain(domain)
78-
.range(range)
46+
.range(_range)
7947
.clamp(true);
8048

8149
var noNumericCheck = opts.noNumericCheck,
@@ -109,7 +77,7 @@ module.exports = function makeScaleFunction(scl, opts) {
10977

11078
sclFunc.domain = _sclFunc.domain;
11179

112-
sclFunc.range = function() { return rangeOrig; };
80+
sclFunc.range = function() { return range; };
11381

11482
return sclFunc;
11583
};

src/components/drawing/index.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ var isNumeric = require('fast-isnumeric');
1414

1515
var Registry = require('../../registry');
1616
var Color = require('../color');
17+
var Colorscale = require('../colorscale');
1718
var Lib = require('../../lib');
1819
var svgTextUtils = require('../../lib/svg_text_utils');
1920

2021
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
2122
var subTypes = require('../../traces/scatter/subtypes');
2223
var makeBubbleSizeFn = require('../../traces/scatter/make_bubble_size_func');
23-
var makeScaleFunction = require('../colorscale/make_scale_function');
2424

2525
var drawing = module.exports = {};
2626

@@ -332,10 +332,9 @@ drawing.tryColorscale = function(cont, contIn, prefix) {
332332
Lib.nestedProperty(contIn, prefix + 'cmax').set(max);
333333
}
334334

335-
return makeScaleFunction(scl, {
336-
cmin: min,
337-
cmax: max
338-
});
335+
return Colorscale.makeColorScaleFunc(
336+
Colorscale.extractScale(scl, min, max)
337+
);
339338
}
340339
else return Lib.identity;
341340
};

src/lib/gl_format_color.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
var tinycolor = require('tinycolor2');
1313
var isNumeric = require('fast-isnumeric');
1414

15-
var makeScaleFunction = require('../components/colorscale/make_scale_function');
15+
var Colorscale = require('../components/colorscale');
1616
var colorDflt = require('../components/color/attributes').defaultLine;
1717

1818
var str2RgbaArray = require('./str2rgbarray');
@@ -42,10 +42,13 @@ function formatColor(containerIn, opacityIn, len) {
4242
var sclFunc, getColor, getOpacity, colori, opacityi;
4343

4444
if(containerIn.colorscale !== undefined) {
45-
sclFunc = makeScaleFunction(containerIn.colorscale, {
46-
cmin: containerIn.cmin,
47-
cmax: containerIn.cmax
48-
});
45+
sclFunc = Colorscale.makeColorScaleFunc(
46+
Colorscale.extractScale(
47+
containerIn.colorscale,
48+
containerIn.cmin,
49+
containerIn.cmax
50+
)
51+
);
4952
}
5053
else sclFunc = validateColor;
5154

src/traces/choropleth/plot.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ var Axes = require('../../plots/cartesian/axes');
1515
var Fx = require('../../plots/cartesian/graph_interact');
1616
var Color = require('../../components/color');
1717
var Drawing = require('../../components/drawing');
18+
var Colorscale = require('../../components/colorscale');
1819

19-
var makeScaleFunction = require('../../components/colorscale/make_scale_function');
2020
var getTopojsonFeatures = require('../../lib/topojson_utils').getTopojsonFeatures;
2121
var locationToFeature = require('../../lib/geo_location_utils').locationToFeature;
2222
var arrayToCalcItem = require('../../lib/array_to_calc_item');
@@ -152,10 +152,13 @@ plotChoropleth.style = function(geo) {
152152
marker = trace.marker || {},
153153
markerLine = marker.line || {};
154154

155-
var sclFunc = makeScaleFunction(trace.colorscale, {
156-
cmin: trace.zmin,
157-
cmax: trace.zmax
158-
});
155+
var sclFunc = Colorscale.makeColorScaleFunc(
156+
Colorscale.extractScale(
157+
trace.colorscale,
158+
trace.zmin,
159+
trace.zmax
160+
)
161+
);
159162

160163
s.selectAll('path.choroplethlocation')
161164
.each(function(pt) {

src/traces/contour/make_color_map.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'use strict';
1111

1212
var d3 = require('d3');
13-
var makeScaleFunction = require('../../components/colorscale/make_scale_function');
13+
var Colorscale = require('../../components/colorscale');
1414

1515
module.exports = function makeColorMap(trace) {
1616
var contours = trace.contours,
@@ -67,9 +67,10 @@ module.exports = function makeColorMap(trace) {
6767
}
6868
}
6969

70-
return makeScaleFunction(scl, {
70+
return Colorscale.makeColorScaleFunc({
7171
domain: domain,
7272
range: range,
73+
}, {
7374
noNumericCheck: true
7475
});
7576
};

src/traces/heatmap/colorbar.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var isNumeric = require('fast-isnumeric');
1313

1414
var Lib = require('../../lib');
1515
var Plots = require('../../plots/plots');
16-
var makeScaleFunction = require('../../components/colorscale/make_scale_function');
16+
var Colorscale = require('../../components/colorscale');
1717
var drawColorbar = require('../../components/colorbar/draw');
1818

1919

@@ -34,11 +34,14 @@ module.exports = function colorbar(gd, cd) {
3434
}
3535

3636
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
37-
var sclFunc = makeScaleFunction(trace.colorscale, {
38-
cmin: zmin,
39-
cmax: zmax,
40-
noNumericCheck: true
41-
});
37+
var sclFunc = Colorscale.makeColorScaleFunc(
38+
Colorscale.extractScale(
39+
trace.colorscale,
40+
zmin,
41+
zmax
42+
),
43+
{ noNumericCheck: true }
44+
);
4245

4346
cb.fillcolor(sclFunc)
4447
.filllevels({start: zmin, end: zmax, size: (zmax - zmin) / 254})

src/traces/heatmap/plot.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ var tinycolor = require('tinycolor2');
1313

1414
var Registry = require('../../registry');
1515
var Lib = require('../../lib');
16+
var Colorscale = require('../../components/colorscale');
1617
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
17-
var makeScaleFunction = require('../../components/colorscale/make_scale_function');
1818

1919
var maxRowLength = require('./max_row_length');
2020

@@ -166,12 +166,14 @@ function plotOne(gd, plotinfo, cd) {
166166
canvas.height = canvasH;
167167
var context = canvas.getContext('2d');
168168

169-
var sclFunc = makeScaleFunction(trace.colorscale, {
170-
cmin: trace.zmin,
171-
cmax: trace.zmax,
172-
noNumericCheck: true,
173-
returnArray: true
174-
});
169+
var sclFunc = Colorscale.makeColorScaleFunc(
170+
Colorscale.extractScale(
171+
trace.colorscale,
172+
trace.zmin,
173+
trace.zmax
174+
),
175+
{ noNumericCheck: true, returnArray: true }
176+
);
175177

176178
// map brick boundaries to image pixels
177179
var xpx,

src/traces/scatter/colorbar.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var isNumeric = require('fast-isnumeric');
1313

1414
var Lib = require('../../lib');
1515
var Plots = require('../../plots/plots');
16-
var makeScaleFunction = require('../../components/colorscale/make_scale_function');
16+
var Colorscale = require('../../components/colorscale');
1717
var drawColorbar = require('../../components/colorbar/draw');
1818

1919

@@ -40,11 +40,14 @@ module.exports = function colorbar(gd, cd) {
4040
if(!isNumeric(cmax)) cmax = Lib.aggNums(Math.max, null, vals);
4141

4242
var cb = cd[0].t.cb = drawColorbar(gd, cbId);
43-
var sclFunc = makeScaleFunction(marker.colorscale, {
44-
cmin: cmin,
45-
cmax: cmax,
46-
noNumericCheck: true
47-
});
43+
var sclFunc = Colorscale.makeColorScaleFunc(
44+
Colorscale.extractScale(
45+
marker.colorscale,
46+
cmin,
47+
cmax
48+
),
49+
{ noNumericCheck: true }
50+
);
4851

4952
cb.fillcolor(sclFunc)
5053
.filllevels({start: cmin, end: cmax, size: (cmax - cmin) / 254})

0 commit comments

Comments
 (0)