Last active
November 8, 2017 16:52
-
-
Save nilz3ro/20d5a6ed31402729c934304795da0cb6 to your computer and use it in GitHub Desktop.
Darken Hex Colors by percentage as a JS one-liner 😎
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
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