Last active
September 26, 2018 11:39
-
-
Save lucasmrdt/79c82e6a715c153d86b13d7ced9a6be4 to your computer and use it in GitHub Desktop.
Get interpolated color between two other.
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
const parseHexColor = hexColor => hexColor | |
.match(/(?=#)?\w{2}/g) | |
.map(c => parseInt(c, 16)); | |
/** | |
* @param {string} beginColor eg. #FFFFFF must have 6 chars. | |
* @param {*} endColor eg. #000000 must have 6 chars. | |
* @param {*} percent must ∈ [0, 1]. | |
*/ | |
const interpolateColor = (beginColor, endColor, percent) => { | |
const bColor = parseHexColor(beginColor); | |
const eColor = parseHexColor(endColor); | |
const output = [...new Array(3)].map((_, ii) => | |
Math.round(bColor[ii] + (eColor[ii] - bColor[ii]) * percent) | |
.toString(16) | |
) | |
return `#${output.join('')}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment