-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcolorscale_test.js
456 lines (395 loc) · 15.9 KB
/
colorscale_test.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
var Colorscale = require('@src/components/colorscale');
var Lib = require('@src/lib');
var Plots = require('@src/plots/plots');
var Heatmap = require('@src/traces/heatmap');
var Scatter = require('@src/traces/scatter');
describe('Test colorscale:', function() {
'use strict';
describe('isValidScale', function() {
var isValidScale = Colorscale.isValidScale,
scl;
it('should accept colorscale strings', function() {
expect(isValidScale('Earth')).toBe(true);
expect(isValidScale('Greens')).toBe(true);
expect(isValidScale('Nop')).toBe(false);
});
it('should accept only array of 2-item arrays', function() {
expect(isValidScale('a')).toBe(false);
expect(isValidScale([])).toBe(false);
expect(isValidScale([null, undefined])).toBe(false);
expect(isValidScale([{}, [1, 'rgb(0, 0, 200']])).toBe(false);
expect(isValidScale([[0, 'rgb(200, 0, 0)'], {}])).toBe(false);
expect(isValidScale([[0, 'rgb(0, 0, 200)'], undefined])).toBe(false);
expect(isValidScale([null, [1, 'rgb(0, 0, 200)']])).toBe(false);
expect(isValidScale(['a', 'b'])).toBe(false);
expect(isValidScale(['a'])).toBe(false);
expect(isValidScale([['a'], ['b']])).toBe(false);
scl = [[0, 'rgb(0, 0, 200)'], [1, 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(true);
});
it('should accept only arrays with 1st val = 0 and last val = 1', function() {
scl = [[0.2, 'rgb(0, 0, 200)'], [1, 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(false);
scl = [['0', 'rgb(0, 0, 200)'], [1, 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(true);
scl = [[0, 'rgb(0, 0, 200)'], [1.2, 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(false);
scl = [[0, 'rgb(0, 0, 200)'], ['1.0', 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(true);
});
it('should accept ascending order number-color items', function() {
scl = [['rgb(0, 0, 200)', 0], ['rgb(200, 0, 0)', 1]];
expect(isValidScale(scl)).toBe(false);
scl = [[0, 0], [1, 1]];
expect(isValidScale(scl)).toBe(false);
scl = [[0, 'a'], [1, 'b']];
expect(isValidScale()).toBe(false);
scl = [[0, 'rgb(0, 0, 200)'], [0.6, 'rgb(200, 200, 0)'],
[0.3, 'rgb(0, 200, 0)'], [1, 'rgb(200, 0, 0)']];
expect(isValidScale(scl)).toBe(false);
});
});
describe('flipScale', function() {
var flipScale = Colorscale.flipScale,
scl;
it('should flip a colorscale', function() {
scl = [[0, 'rgb(0, 0, 200)'], ['0.5', 'rgb(0, 0, 0)'], ['1.0', 'rgb(200, 0, 0)']];
expect(flipScale(scl)).toEqual(
[[0, 'rgb(200, 0, 0)'], [0.5, 'rgb(0, 0, 0)'], [1, 'rgb(0, 0, 200)']]
);
});
});
describe('hasColorscale', function() {
var hasColorscale = Colorscale.hasColorscale,
trace;
it('should return false when marker is not defined', function() {
var shouldBeFalse = [
{},
{marker: null}
];
shouldBeFalse.forEach(function(trace) {
expect(hasColorscale(trace, 'marker')).toBe(false);
});
});
it('should return false when marker is not defined (nested version)', function() {
var shouldBeFalse = [
{},
{marker: null},
{marker: {line: null}}
];
shouldBeFalse.forEach(function(trace) {
expect(hasColorscale(trace, 'marker.line')).toBe(false);
});
});
it('should return true when marker color is an Array with at least one number', function() {
trace = {
marker: {
color: [1, 2, 3],
line: {
color: [2, 3, 4]
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
trace = {
marker: {
color: ['1', 'red', '#d0d0d0'],
line: {
color: ['blue', '3', '#fff']
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
trace = {
marker: {
color: ['green', 'red', 'blue'],
line: {
color: ['rgb(100, 100, 100)', '#d0d0d0', '#fff']
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(false);
expect(hasColorscale(trace, 'marker.line')).toBe(false);
});
it('should return true when marker showscale is true', function() {
trace = {
marker: {
showscale: true,
line: {
showscale: true
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
});
it('should return true when marker colorscale is valid', function() {
trace = {
marker: {
colorscale: 'Greens',
line: {
colorscale: [[0, 'rgb(0,0,0)'], [1, 'rgb(0,0,0)']]
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
});
it('should return true when marker cmin & cmax are numbers', function() {
trace = {
marker: {
cmin: 10,
cmax: 20,
line: {
cmin: 10,
cmax: 20
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
});
it('should return true when marker colorbar is defined', function() {
trace = {
marker: {
colorbar: {},
line: {
colorbar: {}
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
});
it('should return true when marker color is a typed array with at least one non-NaN', function() {
trace = {
marker: {
color: new Float32Array([1, 2, 3]),
line: {
color: new Float32Array([2, 3, 4])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
trace = {
marker: {
color: new Float32Array([1, NaN, 3]),
line: {
color: new Float32Array([2, 3, NaN])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(true);
expect(hasColorscale(trace, 'marker.line')).toBe(true);
trace = {
marker: {
color: new Float32Array([NaN, undefined, 'not-a-number']),
line: {
color: new Float32Array(['not-a-number', NaN, undefined])
}
}
};
expect(hasColorscale(trace, 'marker')).toBe(false);
expect(hasColorscale(trace, 'marker.line')).toBe(false);
});
});
describe('handleDefaults (heatmap-like version)', function() {
var handleDefaults = Colorscale.handleDefaults,
layout = {
font: Plots.layoutAttributes.font,
_dfltTitle: {colorbar: 'cb'}
},
opts = {prefix: '', cLetter: 'z'};
var traceIn, traceOut;
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, Heatmap.attributes, attr, dflt);
}
beforeEach(function() {
traceOut = {};
});
it('should set auto to true when min/max are valid', function() {
traceIn = {
zmin: -10,
zmax: 10
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.zauto).toBe(false);
});
it('should fall back to auto true when min/max are invalid', function() {
traceIn = {
zmin: 'dsa',
zmax: null
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.zauto).toBe(true);
traceIn = {
zmin: 10,
zmax: -10
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.zauto).toBe(true);
});
it('should coerce autocolorscale to false unless set to true', function() {
traceIn = {};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.autocolorscale).toBe(false);
traceIn = {
colorscale: 'Greens'
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.autocolorscale).toBe(false);
traceIn = {
autocolorscale: true
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.autocolorscale).toBe(true);
});
it('should coerce showscale to true unless set to false', function() {
traceIn = {};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.showscale).toBe(true);
traceIn = { showscale: false };
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.showscale).toBe(false);
});
});
describe('handleDefaults (scatter-like version)', function() {
var handleDefaults = Colorscale.handleDefaults,
layout = {
font: Plots.layoutAttributes.font,
_dfltTitle: {colorbar: 'cb'}
},
opts = {prefix: 'marker.', cLetter: 'c'};
var traceIn, traceOut;
function coerce(attr, dflt) {
return Lib.coerce(traceIn, traceOut, Scatter.attributes, attr, dflt);
}
beforeEach(function() {
traceOut = { marker: {} };
});
it('should coerce autocolorscale to true by default', function() {
traceIn = { marker: { line: {} } };
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.marker.autocolorscale).toBe(true);
});
it('should coerce autocolorscale to false when valid colorscale is given', function() {
traceIn = {
marker: { colorscale: 'Greens' }
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.marker.autocolorscale).toBe(false);
traceIn = {
marker: { colorscale: 'nope' }
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.marker.autocolorscale).toBe(true);
});
it('should coerce showscale to true if colorbar is specified', function() {
traceIn = { marker: {} };
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.marker.showscale).toBe(false);
traceIn = {
marker: {
colorbar: {}
}
};
handleDefaults(traceIn, traceOut, layout, coerce, opts);
expect(traceOut.marker.showscale).toBe(true);
});
});
describe('calc', function() {
var calcColorscale = Colorscale.calc;
var trace, z;
beforeEach(function() {
trace = {};
z = {};
});
it('should be RdBuNeg when autocolorscale and z <= 0', function() {
trace = {
type: 'heatmap',
z: [[0, -1.5], [-2, -10]],
autocolorscale: true,
_input: {autocolorscale: true}
};
z = [[0, -1.5], [-2, -10]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(true);
expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']);
});
it('should set autocolorscale to false if it wasn\'t explicitly set true in input', function() {
trace = {
type: 'heatmap',
z: [[0, -1.5], [-2, -10]],
autocolorscale: true,
_input: {}
};
z = [[0, -1.5], [-2, -10]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(false);
expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']);
});
it('should be Blues when the only numerical z <= -0.5', function() {
trace = {
type: 'heatmap',
z: [['a', 'b'], [-0.5, 'd']],
autocolorscale: true,
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [-0.5, undefined]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(true);
expect(trace.colorscale[5]).toEqual([1, 'rgb(220,220,220)']);
});
it('should be Reds when the only numerical z >= 0.5', function() {
trace = {
type: 'heatmap',
z: [['a', 'b'], [0.5, 'd']],
autocolorscale: true,
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [0.5, undefined]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(true);
expect(trace.colorscale[0]).toEqual([0, 'rgb(220,220,220)']);
});
it('should be reverse the auto scale when reversescale is true', function() {
trace = {
type: 'heatmap',
z: [['a', 'b'], [0.5, 'd']],
autocolorscale: true,
reversescale: true,
_input: {autocolorscale: true}
};
z = [[undefined, undefined], [0.5, undefined]];
calcColorscale(trace, z, '', 'z');
expect(trace.autocolorscale).toBe(true);
expect(trace.colorscale[trace.colorscale.length - 1])
.toEqual([1, 'rgb(220,220,220)']);
});
});
describe('extractScale + makeColorScaleFunc', function() {
var scale = [
[0, 'rgb(5,10,172)'],
[0.35, 'rgb(106,137,247)'],
[0.5, 'rgb(190,190,190)'],
[0.6, 'rgb(220,170,132)'],
[0.7, 'rgb(230,145,90)'],
[1, 'rgb(178,10,28)']
];
var specs = Colorscale.extractScale(scale, 2, 3);
var sclFunc = Colorscale.makeColorScaleFunc(specs);
it('should constrain color array values between cmin and cmax', function() {
var color1 = sclFunc(1),
color2 = sclFunc(2),
color3 = sclFunc(3),
color4 = sclFunc(4);
expect(color1).toEqual(color2);
expect(color1).toEqual('rgb(5, 10, 172)');
expect(color3).toEqual(color4);
expect(color4).toEqual('rgb(178, 10, 28)');
});
});
});