Created
April 8, 2018 18:27
-
-
Save cogell/620c258db371f6b0c49dc9b25da4de54 to your computer and use it in GitHub Desktop.
Quick Color Utils for Styled-Components
This file contains hidden or 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
import { convert } from 'css-color-function'; | |
/** | |
* Some helpful notes: | |
* | |
* Documentation of convert interface: | |
* https://github.com/postcss/postcss-color-function#interface-according-to-css-specs | |
* | |
* shade: mixes color with pure black https://drafts.csswg.org/css-color/#tint-shade-adjusters | |
* tint: mixes color with pure white https://drafts.csswg.org/css-color/#tint-shade-adjusters | |
* | |
* hue: adjusts the hue of the base color, when the base color is interpreted as an HSL color | |
*/ | |
const d2p = decimal => `${decimal * 100}%`; // decimal to percentage | |
const alpha = (color, decimal) => convert(`color(${color} a(${decimal}))`); | |
const lighten = (color, decimal) => | |
convert(`color(${color} tint(${d2p(decimal)}))`); | |
const darken = (color, decimal) => | |
convert(`color(${color} shade(${d2p(decimal)}))`); | |
const hover = color => darken(color, 0.1); | |
const click = color => darken(color, 0.2); | |
const disabled = color => lighten(color, 0.5); | |
export default { | |
active: click, | |
alpha, | |
click, | |
darken, | |
disabled, | |
focus: hover, | |
hover, | |
lighten, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment