Skip to content

Commit

Permalink
feat(colors): improve colors config
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Benitte committed Apr 18, 2016
1 parent bf8e189 commit 330356a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/ColorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ export const getColorGenerator = instruction => {
}

if (instruction === 'inherit') {
return d => d.data.color;
return d => (d.color || d.data.color);
}

const inheritMatches = instruction.match(/inherit:(darker|brighter)\(([0-9.]+)\)/);
if (inheritMatches) {
const method = inheritMatches[1];
const amount = inheritMatches[2];

return d => d3.rgb(d.data.color)[method](parseFloat(amount));
return d => d3.rgb(d.color || d.data.color)[method](parseFloat(amount));
}

throw new Error('Unable to determine color generator');
Expand Down
54 changes: 54 additions & 0 deletions test/ColorUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import test from 'ava';
import d3 from 'd3';
import { getColorGenerator } from '../src/ColorUtils';


test(`getColorGenerator() should return 'none' if 'none' provided`, t => {
t.is(getColorGenerator('none'), 'none');
});

test(`getColorGenerator() should return a function to use 'data.color' if 'inherit' provided`, t => {
const colorGenerator = getColorGenerator('inherit');
const color = '#FF0000';

t.is(typeof colorGenerator, 'function');
t.is(colorGenerator({ data: { color }}), color);
});

test(`getColorGenerator() should return a function to use darker 'data.color' if 'inherit:darker(*)' provided`, t => {
const colorGenerator = getColorGenerator('inherit:darker(1)');
const color = '#FF0000';

t.is(typeof colorGenerator, 'function');
t.deepEqual(colorGenerator({ data: { color }}), d3.rgb(color).darker(1));
});

test(`getColorGenerator() 'inherit:darker(*)' should support floats`, t => {
const colorGenerator = getColorGenerator('inherit:darker(.3)');
const color = '#FF0000';

t.is(typeof colorGenerator, 'function');
t.deepEqual(colorGenerator({ data: { color }}), d3.rgb(color).darker(.3));
});

test(`getColorGenerator() should return a function to use brighter 'data.color' if 'inherit:brighter(*)' provided`, t => {
const colorGenerator = getColorGenerator('inherit:brighter(1)');
const color = '#FF0000';

t.is(typeof colorGenerator, 'function');
t.deepEqual(colorGenerator({ data: { color }}), d3.rgb(color).brighter(1));
});

test(`getColorGenerator() 'inherit:brighter(*)' should support floats`, t => {
const colorGenerator = getColorGenerator('inherit:brighter(.3)');
const color = '#FF0000';

t.is(typeof colorGenerator, 'function');
t.deepEqual(colorGenerator({ data: { color }}), d3.rgb(color).brighter(.3));
});

test(`getColorGenerator() should throw on invalid directive`, t => {
t.throws(() => {
getColorGenerator('invalid');
}, 'Unable to determine color generator');
});

0 comments on commit 330356a

Please sign in to comment.