Skip to content

Commit c45a2a0

Browse files
authored
Merge pull request #796 from plotly/rangeselector-active-color
Configurable range selector active color
2 parents e8446b1 + 63e622f commit c45a2a0

File tree

8 files changed

+71
-6
lines changed

8 files changed

+71
-6
lines changed

src/components/color/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ color.combine = function(front, back) {
5757
return tinycolor(fcflat).toRgbString();
5858
};
5959

60+
color.contrast = function(cstr, lightAmount, darkAmount) {
61+
var tc = tinycolor(cstr);
62+
63+
var newColor = tc.isLight() ?
64+
tc.darken(darkAmount) :
65+
tc.lighten(lightAmount);
66+
67+
return newColor.toString();
68+
};
69+
6070
color.stroke = function(s, c) {
6171
var tc = tinycolor(c);
6272
s.style({'stroke': color.tinyRGB(tc), 'stroke-opacity': tc.getAlpha()});

src/components/rangeselector/attributes.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ module.exports = {
8181
role: 'style',
8282
description: 'Sets the background color of the range selector buttons.'
8383
},
84+
activecolor: {
85+
valType: 'color',
86+
role: 'style',
87+
description: 'Sets the background color of the active range selector button.'
88+
},
8489
bordercolor: {
8590
valType: 'color',
8691
dflt: colorAttrs.defaultLine,

src/components/rangeselector/constants.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
rx: 3,
2222
ry: 3,
2323

24-
// color given to active and hovered buttons
25-
activeColor: '#d3d3d3'
24+
// light fraction used to compute the 'activecolor' default
25+
lightAmount: 25,
26+
darkAmount: 10
2627
};

src/components/rangeselector/defaults.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
'use strict';
1010

1111
var Lib = require('../../lib');
12+
var Color = require('../color');
1213

1314
var attributes = require('./attributes');
1415
var buttonAttrs = require('./button_attributes');
@@ -38,7 +39,8 @@ module.exports = function rangeSelectorDefaults(containerIn, containerOut, layou
3839

3940
Lib.coerceFont(coerce, 'font', layout.font);
4041

41-
coerce('bgcolor');
42+
var bgColor = coerce('bgcolor');
43+
coerce('activecolor', Color.contrast(bgColor, constants.lightAmount, constants.darkAmount));
4244
coerce('bordercolor');
4345
coerce('borderwidth');
4446
};

src/components/rangeselector/draw.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function drawButtonRect(button, selectorLayout, d) {
142142

143143
function getFillColor(selectorLayout, d) {
144144
return (d.isActive || d.isHovered) ?
145-
constants.activeColor :
145+
selectorLayout.activecolor :
146146
selectorLayout.bgcolor;
147147
}
148148

test/image/mocks/range_selector_style.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,7 @@
10251025
"y": 0.2,
10261026
"yanchor": "bottom",
10271027
"bgcolor": "#d3d3d3",
1028+
"activecolor": "#d3d3d3",
10281029
"borderwidth": 2,
10291030
"bordercolor": "#ff7f0e"
10301031
},

test/jasmine/tests/color_test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,25 @@ describe('Test color:', function() {
175175
});
176176

177177
});
178+
179+
describe('contrast', function() {
180+
181+
it('should darken light colors', function() {
182+
var out = Color.contrast('#eee', 10, 20);
183+
184+
expect(out).toEqual('#bbbbbb');
185+
});
186+
187+
it('should darken light colors (2)', function() {
188+
var out = Color.contrast('#fdae61', 10, 20);
189+
190+
expect(out).toEqual('#f57a03');
191+
});
192+
193+
it('should lighten dark colors', function() {
194+
var out = Color.contrast('#2b83ba', 10, 20);
195+
196+
expect(out).toEqual('#449dd4');
197+
});
198+
});
178199
});

test/jasmine/tests/range_selector_test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var RangeSelector = require('@src/components/rangeselector');
22
var getUpdateObject = require('@src/components/rangeselector/get_update_object');
3-
var constants = require('@src/components/rangeselector/constants');
43

54
var d3 = require('d3');
65
var Plotly = require('@lib');
@@ -427,6 +426,16 @@ describe('range selector interactions:', function() {
427426
});
428427
}
429428

429+
function checkButtonColor(bgColor, activeColor) {
430+
d3.selectAll('.button').each(function(d) {
431+
var rect = d3.select(this).select('rect');
432+
433+
expect(rect.style('fill')).toEqual(
434+
d.isActive ? activeColor : bgColor
435+
);
436+
});
437+
}
438+
430439
it('should display the correct nodes', function() {
431440
assertNodeCount('.rangeselector', 1);
432441
assertNodeCount('.button', mockCopy.layout.xaxis.rangeselector.buttons.length);
@@ -457,6 +466,22 @@ describe('range selector interactions:', function() {
457466
});
458467
});
459468

469+
it('should be able to change its style on `relayout`', function(done) {
470+
var prefix = 'xaxis.rangeselector.';
471+
472+
checkButtonColor('rgb(238, 238, 238)', 'rgb(212, 212, 212)');
473+
474+
Plotly.relayout(gd, prefix + 'bgcolor', 'red').then(function() {
475+
checkButtonColor('rgb(255, 0, 0)', 'rgb(255, 128, 128)');
476+
477+
return Plotly.relayout(gd, prefix + 'activecolor', 'blue');
478+
}).then(function() {
479+
checkButtonColor('rgb(255, 0, 0)', 'rgb(0, 0, 255)');
480+
481+
done();
482+
});
483+
});
484+
460485
it('should update range and active button when clicked', function() {
461486
var range0 = gd.layout.xaxis.range[0];
462487
var buttons = d3.selectAll('.button').select('rect');
@@ -482,7 +507,7 @@ describe('range selector interactions:', function() {
482507
var pos = getRectCenter(button.node());
483508

484509
var fillColor = Color.rgb(gd._fullLayout.xaxis.rangeselector.bgcolor);
485-
var activeColor = Color.rgb(constants.activeColor);
510+
var activeColor = 'rgb(212, 212, 212)';
486511

487512
expect(button.style('fill')).toEqual(fillColor);
488513

0 commit comments

Comments
 (0)