Skip to content

Instantly share code, notes, and snippets.

@lucasmrdt
Last active September 26, 2018 11:39
Show Gist options
  • Save lucasmrdt/79c82e6a715c153d86b13d7ced9a6be4 to your computer and use it in GitHub Desktop.
Save lucasmrdt/79c82e6a715c153d86b13d7ced9a6be4 to your computer and use it in GitHub Desktop.
Get interpolated color between two other.
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