Skip to content

Commit 688639f

Browse files
committed
Merge pull request #422 from plotly/colorscale-opacity
Colorscale opacity maintenance
2 parents 52be69a + 5518bef commit 688639f

File tree

5 files changed

+77
-9
lines changed

5 files changed

+77
-9
lines changed

src/components/colorbar/draw.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
var d3 = require('d3');
13+
var tinycolor = require('tinycolor2');
1314

1415
var Plotly = require('../../plotly');
1516
var Plots = require('../../plots/plots');
@@ -356,13 +357,22 @@ module.exports = function draw(gd, id) {
356357
if(i!==filllevels.length-1) {
357358
z[1] += (z[1]>z[0]) ? 1 : -1;
358359
}
360+
361+
362+
// Tinycolor can't handle exponents and
363+
// at this scale, removing it makes no difference.
364+
var colorString = fillcolormap(d).replace('e-', ''),
365+
opaqueColor = tinycolor(colorString).toHexString();
366+
367+
// Colorbar cannot currently support opacities so we
368+
// use an opaque fill even when alpha channels present
359369
d3.select(this).attr({
360370
x: xLeft,
361371
width: Math.max(thickPx,2),
362372
y: d3.min(z),
363-
height: Math.max(d3.max(z)-d3.min(z),2)
364-
})
365-
.style('fill',fillcolormap(d));
373+
height: Math.max(d3.max(z)-d3.min(z),2),
374+
fill: opaqueColor
375+
});
366376
});
367377

368378
var lines = container.select('.cblines')

src/components/colorscale/make_scale_function.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@ module.exports = function makeScaleFunction(scl, cmin, cmax) {
2626
for(var i = 0; i < N; i++) {
2727
si = scl[i];
2828
domain[i] = cmin + si[0] * (cmax - cmin);
29-
range[i] = si[1];
29+
range[i] = tinycolor(si[1]).toRgb();
3030
}
3131

3232
var sclFunc = d3.scale.linear()
3333
.domain(domain)
34-
.interpolate(d3.interpolateRgb)
34+
.interpolate(d3.interpolateObject)
3535
.range(range);
3636

3737
return function(v) {
3838
if(isNumeric(v)) {
39-
var sclVal = Lib.constrain(v, cmin, cmax);
40-
return sclFunc(sclVal);
39+
var sclVal = Lib.constrain(v, cmin, cmax),
40+
colorObj = sclFunc(sclVal);
41+
42+
return tinycolor(colorObj).toRgbString();
4143
}
4244
else if(tinycolor(v).isValid()) return v;
4345
else return Color.defaultLine;
22.9 KB
Loading
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"data": [
3+
{
4+
"y": [
5+
5,
6+
5,
7+
5,
8+
5
9+
],
10+
"mode": "markers",
11+
"marker": {
12+
"size": 40,
13+
"colorscale": [
14+
[
15+
0,
16+
"rgb(255,0.0,0.0)"
17+
],
18+
[
19+
1,
20+
"rgba(0.0,0.0,255,0.5)"
21+
]
22+
],
23+
"color": [
24+
0,
25+
1,
26+
2,
27+
3
28+
],
29+
"cmin": 0,
30+
"cmax": 3
31+
},
32+
"uid": "07bab4"
33+
}
34+
],
35+
"layout": {
36+
"title": "Scatter Plot with a Color Dimension",
37+
"xaxis": {
38+
"range": [
39+
-0.271356783919598,
40+
3.271356783919598
41+
],
42+
"autorange": true
43+
},
44+
"yaxis": {
45+
"type": "linear",
46+
"range": [
47+
4,
48+
6
49+
],
50+
"autorange": true
51+
},
52+
"height": 450,
53+
"width": 1100,
54+
"autosize": true
55+
}
56+
}

test/jasmine/tests/colorscale_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ describe('Test colorscale:', function() {
389389
color4 = scaleFunction(4);
390390

391391
expect(color1).toEqual(color2);
392-
expect(color1).toEqual('#050aac');
392+
expect(color1).toEqual('rgb(5, 10, 172)');
393393
expect(color3).toEqual(color4);
394-
expect(color4).toEqual('#b20a1c');
394+
expect(color4).toEqual('rgb(178, 10, 28)');
395395
});
396396
});
397397
});

0 commit comments

Comments
 (0)