Skip to content

Instantly share code, notes, and snippets.

@cogell
Created April 8, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cogell/620c258db371f6b0c49dc9b25da4de54 to your computer and use it in GitHub Desktop.
Save cogell/620c258db371f6b0c49dc9b25da4de54 to your computer and use it in GitHub Desktop.
Quick Color Utils for Styled-Components
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