Skip to content

Commit 1a55816

Browse files
committed
move _scaler function from plot to calc step
1 parent 720db11 commit 1a55816

File tree

3 files changed

+58
-56
lines changed

3 files changed

+58
-56
lines changed

src/traces/image/attributes.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ module.exports = extendFlat({
4141
zmin: {
4242
valType: 'info_array',
4343
items: [
44-
{valType: 'number', editType: 'plot'},
45-
{valType: 'number', editType: 'plot'},
46-
{valType: 'number', editType: 'plot'},
47-
{valType: 'number', editType: 'plot'}
44+
{valType: 'number', editType: 'calc'},
45+
{valType: 'number', editType: 'calc'},
46+
{valType: 'number', editType: 'calc'},
47+
{valType: 'number', editType: 'calc'}
4848
],
4949
role: 'info',
50-
editType: 'plot',
50+
editType: 'calc',
5151
description: [
5252
'Array defining the lower bound for each color component.',
5353
'Note that the default value will depend on the colormodel.',
@@ -57,13 +57,13 @@ module.exports = extendFlat({
5757
zmax: {
5858
valType: 'info_array',
5959
items: [
60-
{valType: 'number', editType: 'plot'},
61-
{valType: 'number', editType: 'plot'},
62-
{valType: 'number', editType: 'plot'},
63-
{valType: 'number', editType: 'plot'}
60+
{valType: 'number', editType: 'calc'},
61+
{valType: 'number', editType: 'calc'},
62+
{valType: 'number', editType: 'calc'},
63+
{valType: 'number', editType: 'calc'}
6464
],
6565
role: 'info',
66-
editType: 'plot',
66+
editType: 'calc',
6767
description: [
6868
'Array defining the higher bound for each color component.',
6969
'Note that the default value will depend on the colormodel.',

src/traces/image/calc.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
'use strict';
1010

11+
var Lib = require('../../lib');
12+
var constants = require('./constants');
13+
var isNumeric = require('fast-isnumeric');
1114
var Axes = require('../../plots/cartesian/axes');
1215
var maxRowLength = require('../../lib').maxRowLength;
1316

@@ -28,6 +31,7 @@ module.exports = function calc(gd, trace) {
2831
if(ya && ya.type === 'log') for(i = 0; i < h; i++) yrange.push(y0 + i * trace.dy);
2932
trace._extremes[xa._id] = Axes.findExtremes(xa, xrange);
3033
trace._extremes[ya._id] = Axes.findExtremes(ya, yrange);
34+
trace._scaler = makeScaler(trace);
3135

3236
var cd0 = {
3337
x0: x0,
@@ -38,3 +42,47 @@ module.exports = function calc(gd, trace) {
3842
};
3943
return [cd0];
4044
};
45+
46+
function scale(zero, factor, min, max) {
47+
return function(c) {
48+
c = (c - zero) * factor;
49+
c = Lib.constrain(c, min, max);
50+
return c;
51+
};
52+
}
53+
54+
function constrain(min, max) {
55+
return function(c) { return Lib.constrain(c, min, max);};
56+
}
57+
58+
// Generate a function to scale color components according to zmin/zmax and the colormodel
59+
function makeScaler(trace) {
60+
var colormodel = trace.colormodel;
61+
var n = colormodel.length;
62+
var cr = constants.colormodel[colormodel];
63+
64+
var s = [];
65+
// Loop over all color components
66+
for(var k = 0; k < n; k++) {
67+
if(cr.min[k] !== trace.zmin[k] || cr.max[k] !== trace.zmax[k]) {
68+
s.push(scale(
69+
trace.zmin[k],
70+
(cr.max[k] - cr.min[k]) / (trace.zmax[k] - trace.zmin[k]),
71+
cr.min[k],
72+
cr.max[k]
73+
));
74+
} else {
75+
s.push(constrain(cr.min[k], cr.max[k]));
76+
}
77+
}
78+
79+
return function(pixel) {
80+
var c = pixel.slice(0, n);
81+
for(var k = 0; k < n; k++) {
82+
var ck = c[k];
83+
if(!isNumeric(ck)) return false;
84+
c[k] = s[k](ck);
85+
}
86+
return c;
87+
};
88+
}

src/traces/image/plot.js

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,9 @@
1010

1111
var d3 = require('d3');
1212
var Lib = require('../../lib');
13-
var isNumeric = require('fast-isnumeric');
1413
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1514
var constants = require('./constants');
1615

17-
function scale(zero, factor, min, max) {
18-
return function(c) {
19-
c = (c - zero) * factor;
20-
c = Lib.constrain(c, min, max);
21-
return c;
22-
};
23-
}
24-
25-
function constrain(min, max) {
26-
return function(c) { return Lib.constrain(c, min, max);};
27-
}
28-
29-
// Generate a function to scale color components according to zmin/zmax and the colormodel
30-
var makeScaler = function(trace) {
31-
var colormodel = trace.colormodel;
32-
var n = colormodel.length;
33-
var cr = constants.colormodel[colormodel];
34-
35-
var s = [];
36-
// Loop over all color components
37-
for(var k = 0; k < n; k++) {
38-
if(cr.min[k] !== trace.zmin[k] || cr.max[k] !== trace.zmax[k]) {
39-
s.push(scale(
40-
trace.zmin[k],
41-
(cr.max[k] - cr.min[k]) / (trace.zmax[k] - trace.zmin[k]),
42-
cr.min[k],
43-
cr.max[k]
44-
));
45-
} else {
46-
s.push(constrain(cr.min[k], cr.max[k]));
47-
}
48-
}
49-
50-
return function(pixel) {
51-
var c = pixel.slice(0, n);
52-
for(var k = 0; k < n; k++) {
53-
var ck = c[k];
54-
if(!isNumeric(ck)) return false;
55-
c[k] = s[k](ck);
56-
}
57-
return c;
58-
};
59-
};
60-
6116
module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
6217
var xa = plotinfo.xaxis;
6318
var ya = plotinfo.yaxis;
@@ -136,7 +91,6 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
13691
var ipx = function(i) {return Lib.constrain(Math.round(xa.c2p(x0 + i * dx) - left), 0, imageWidth);};
13792
var jpx = function(j) {return Lib.constrain(Math.round(ya.c2p(y0 + j * dy) - top), 0, imageHeight);};
13893

139-
trace._scaler = makeScaler(trace);
14094
var fmt = constants.colormodel[trace.colormodel].fmt;
14195
var c;
14296
for(i = 0; i < cd0.w; i++) {

0 commit comments

Comments
 (0)