Skip to content

Instantly share code, notes, and snippets.

@charltoons
Created May 6, 2013 02:35
Show Gist options
  • Save charltoons/5523081 to your computer and use it in GitHub Desktop.
Save charltoons/5523081 to your computer and use it in GitHub Desktop.
Finds the color in between two colors.
function colorInBetween(color1, color2){
function toNumber(hexStr){ return parseInt('0x'+hexStr); }
function toColorObject(color){
var obj = {};
obj.red = toNumber(color.slice(1,3));
obj.green = toNumber(color.slice(3,5));
obj.blue = toNumber(color.slice(5,7));
return obj;
}
function getAverage(val1, val2){
return (val1 < val2)
? Math.floor(((val2 - val1)/2)+val1)
: Math.floor(((val1 - val2)/2)+val2);
}
c1 = toColorObject(color1);
c2 = toColorObject(color2);
c3 = {};
c3.red = getAverage(c1.red, c2.red);
c3.green = getAverage(c1.green, c2.green);
c3.blue = getAverage(c1.blue, c2.blue);
return ('#' + Number(c3.red).toString(16)
+ Number(c3.green).toString(16)
+ Number(c3.blue).toString(16));
}
console.log(colorInBetween('#e74c3c', '#f1c40f'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment