-
-
Save nikolas/b0cce2261f1382159b507dd492e1ceef to your computer and use it in GitHub Desktop.
Linear interpolation for hexadecimal colors.
This file contains hidden or 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 hex colors. | |
* | |
* Based on: | |
* https://gist.github.com/rosszurowski/67f04465c424a9bc0dae | |
* | |
* @param {Number} a (hex color start val) | |
* @param {Number} b (hex color end val) | |
* @param {Number} amount (the amount to fade from a to b) | |
* | |
* @example | |
* // returns 0x7f7f7f | |
* lerpColor(0x000000, 0xffffff, 0.5) | |
* | |
* @returns {Number} | |
*/ | |
const lerpColor = function(a, b, amount) { | |
const ar = a >> 16, | |
ag = a >> 8 & 0xff, | |
ab = a & 0xff, | |
br = b >> 16, | |
bg = b >> 8 & 0xff, | |
bb = b & 0xff, | |
rr = ar + amount * (br - ar), | |
rg = ag + amount * (bg - ag), | |
rb = ab + amount * (bb - ab); | |
return (rr << 16) + (rg << 8) + (rb | 0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! This was helpful. I used the following to return a CSS-readable string: