Skip to content

Instantly share code, notes, and snippets.

@wiledal
Created July 29, 2014 17:03
Show Gist options
  • Save wiledal/24e7b7e3697bf86bc52a to your computer and use it in GitHub Desktop.
Save wiledal/24e7b7e3697bf86bc52a to your computer and use it in GitHub Desktop.
Blend two hexes
function blendHex(hex1, hex2, steps) {
var rgb1 = {
r: parseInt(hex1.slice(1,3), 16),
b: parseInt(hex1.slice(3,5), 16),
g: parseInt(hex1.slice(5,7), 16)
}
var rgb2 = {
r: parseInt(hex2.slice(1,3), 16),
b: parseInt(hex2.slice(3,5), 16),
g: parseInt(hex2.slice(5,7), 16)
}
var add = {
r: rgb2.r - rgb1.r,
g: rgb2.g - rgb1.g,
b: rgb2.b - rgb1.b
}
var result = [];
for (var i = 0; i < steps; i++) {
var newColor = {
r: rgb1.r + Math.round(add.r / steps) * i,
g: rgb1.g + Math.round(add.g / steps) * i,
b: rgb1.b + Math.round(add.b / steps) * i
}
result.push("#" + ((1 << 24) + (newColor.r << 16) + (newColor.b << 8) + newColor.g).toString(16).slice(1));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment