Skip to content

Configurable range selector active color #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/components/color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ color.combine = function(front, back) {
return tinycolor(fcflat).toRgbString();
};

color.contrast = function(cstr, lightAmount, darkAmount) {
var tc = tinycolor(cstr);

var newColor = tc.isLight() ?
tc.darken(darkAmount) :
tc.lighten(lightAmount);

return newColor.toString();
};

color.stroke = function(s, c) {
var tc = tinycolor(c);
s.style({'stroke': color.tinyRGB(tc), 'stroke-opacity': tc.getAlpha()});
Expand Down
5 changes: 5 additions & 0 deletions src/components/rangeselector/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ module.exports = {
role: 'style',
description: 'Sets the background color of the range selector buttons.'
},
activecolor: {
valType: 'color',
role: 'style',
description: 'Sets the background color of the active range selector button.'
},
bordercolor: {
valType: 'color',
dflt: colorAttrs.defaultLine,
Expand Down
5 changes: 3 additions & 2 deletions src/components/rangeselector/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
rx: 3,
ry: 3,

// color given to active and hovered buttons
activeColor: '#d3d3d3'
// light fraction used to compute the 'activecolor' default
lightAmount: 25,
darkAmount: 10
};
4 changes: 3 additions & 1 deletion src/components/rangeselector/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'use strict';

var Lib = require('../../lib');
var Color = require('../color');

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

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

coerce('bgcolor');
var bgColor = coerce('bgcolor');
coerce('activecolor', Color.contrast(bgColor, constants.lightAmount, constants.darkAmount));
coerce('bordercolor');
coerce('borderwidth');
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/rangeselector/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function drawButtonRect(button, selectorLayout, d) {

function getFillColor(selectorLayout, d) {
return (d.isActive || d.isHovered) ?
constants.activeColor :
selectorLayout.activecolor :
selectorLayout.bgcolor;
}

Expand Down
1 change: 1 addition & 0 deletions test/image/mocks/range_selector_style.json
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,7 @@
"y": 0.2,
"yanchor": "bottom",
"bgcolor": "#d3d3d3",
"activecolor": "#d3d3d3",
"borderwidth": 2,
"bordercolor": "#ff7f0e"
},
Expand Down
21 changes: 21 additions & 0 deletions test/jasmine/tests/color_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,25 @@ describe('Test color:', function() {
});

});

describe('contrast', function() {

it('should darken light colors', function() {
var out = Color.contrast('#eee', 10, 20);

expect(out).toEqual('#bbbbbb');
});

it('should darken light colors (2)', function() {
var out = Color.contrast('#fdae61', 10, 20);

expect(out).toEqual('#f57a03');
});

it('should lighten dark colors', function() {
var out = Color.contrast('#2b83ba', 10, 20);

expect(out).toEqual('#449dd4');
});
});
});
29 changes: 27 additions & 2 deletions test/jasmine/tests/range_selector_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var RangeSelector = require('@src/components/rangeselector');
var getUpdateObject = require('@src/components/rangeselector/get_update_object');
var constants = require('@src/components/rangeselector/constants');

var d3 = require('d3');
var Plotly = require('@lib');
Expand Down Expand Up @@ -427,6 +426,16 @@ describe('range selector interactions:', function() {
});
}

function checkButtonColor(bgColor, activeColor) {
d3.selectAll('.button').each(function(d) {
var rect = d3.select(this).select('rect');

expect(rect.style('fill')).toEqual(
d.isActive ? activeColor : bgColor
);
});
}

it('should display the correct nodes', function() {
assertNodeCount('.rangeselector', 1);
assertNodeCount('.button', mockCopy.layout.xaxis.rangeselector.buttons.length);
Expand Down Expand Up @@ -457,6 +466,22 @@ describe('range selector interactions:', function() {
});
});

it('should be able to change its style on `relayout`', function(done) {
var prefix = 'xaxis.rangeselector.';

checkButtonColor('rgb(238, 238, 238)', 'rgb(212, 212, 212)');

Plotly.relayout(gd, prefix + 'bgcolor', 'red').then(function() {
checkButtonColor('rgb(255, 0, 0)', 'rgb(255, 128, 128)');

return Plotly.relayout(gd, prefix + 'activecolor', 'blue');
}).then(function() {
checkButtonColor('rgb(255, 0, 0)', 'rgb(0, 0, 255)');

done();
});
});

it('should update range and active button when clicked', function() {
var range0 = gd.layout.xaxis.range[0];
var buttons = d3.selectAll('.button').select('rect');
Expand All @@ -482,7 +507,7 @@ describe('range selector interactions:', function() {
var pos = getRectCenter(button.node());

var fillColor = Color.rgb(gd._fullLayout.xaxis.rangeselector.bgcolor);
var activeColor = Color.rgb(constants.activeColor);
var activeColor = 'rgb(212, 212, 212)';

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

Expand Down