Skip to content

Instantly share code, notes, and snippets.

@nilz3ro
Last active November 8, 2017 16:52
Show Gist options
  • Save nilz3ro/20d5a6ed31402729c934304795da0cb6 to your computer and use it in GitHub Desktop.
Save nilz3ro/20d5a6ed31402729c934304795da0cb6 to your computer and use it in GitHub Desktop.
Darken Hex Colors by percentage as a JS one-liner 😎
import { chunk } from 'lodash';
const darkenByPercentage = (hexString, percent) =>
chunk(hexString.substr(1).split(''), 2)
.reduce((memo, pair) => [...memo, `0x${pair.join('')}`], '')
.map(Number)
.map(digit => digit - Math.ceil(digit * (percent / 100)))
.reduce((memo, digit) => `${memo}${digit.toString(16)}`, '#');
// darkenByPercentage('#ffffff', 20);
// => '#cccccc'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment