-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(colors): add smart declarative color management
- Loading branch information
Raphael Benitte
committed
Apr 17, 2016
1 parent
7122dca
commit c4fb533
Showing
6 changed files
with
208 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import d3 from 'd3'; | ||
|
||
|
||
export const getColorGenerator = instruction => { | ||
if (instruction === 'none') { | ||
return 'none'; | ||
} | ||
|
||
if (instruction === 'inherit') { | ||
return d => 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)); | ||
} | ||
|
||
throw new Error('Unable to determine color generator'); | ||
}; | ||
|
||
|
||
export const getColorStyleObject = (instruction, property) => { | ||
const style = {}; | ||
|
||
const color = getColorGenerator(instruction); | ||
if (color !== 'none') { | ||
style[property] = color; | ||
} | ||
|
||
return style; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.