Created
October 19, 2017 14:09
-
-
Save glafarge/20baca4de50ed7f95bc0614aca41e74b to your computer and use it in GitHub Desktop.
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
/** | |
* A linear interpolator for hexadecimal colors | |
* @param {String} a | |
* @param {String} b | |
* @param {Number} amount | |
* @example | |
* // returns #7F7F7F | |
* lerpColor('#000000', '#ffffff', 0.5) | |
* @returns {String} | |
*/ | |
function lerpColor(a, b, amount) { | |
var ah = parseInt(a.replace(/#/g, ''), 16), | |
ar = ah >> 16, ag = ah >> 8 & 0xff, ab = ah & 0xff, | |
bh = parseInt(b.replace(/#/g, ''), 16), | |
br = bh >> 16, bg = bh >> 8 & 0xff, bb = bh & 0xff, | |
rr = ar + amount * (br - ar), | |
rg = ag + amount * (bg - ag), | |
rb = ab + amount * (bb - ab); | |
return '#' + ((1 << 24) + (rr << 16) + (rg << 8) + rb | 0).toString(16).slice(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment