Skip to content

Instantly share code, notes, and snippets.

@RealOrangeOne
Created December 25, 2015 20:07
Show Gist options
  • Save RealOrangeOne/6820b67e3b75200c82d5 to your computer and use it in GitHub Desktop.
Save RealOrangeOne/6820b67e3b75200c82d5 to your computer and use it in GitHub Desktop.
Hex colour code transition algorithm. Convert to .ino later!
var element = $('div');
const original = "ff7f00";
const final = "333333";
var R = original.substring(0, 2);
var G = original.substring(2, 4);
var B = original.substring(4, 6);
console.log(R, G, B);
var finalR = final.substring(0, 2);
var finalG = final.substring(2, 4);
var finalB = final.substring(4, 6);
while ((R + G + B) != final) {
if (parseInt(R, 16) < parseInt(finalR, 16)) {
R = (parseInt(R, 16) + 1).toString(16);
} else if (parseInt(R, 16) > parseInt(finalR, 16)) {
R = (parseInt(R, 16) - 1).toString(16);
}
if (parseInt(G, 16) < parseInt(finalG, 16)) {
G = (parseInt(G, 16) + 1).toString(16);
} else if (parseInt(G, 16) > parseInt(finalG, 16)) {
G = (parseInt(G, 16) - 1).toString(16);
}
if (parseInt(B, 16) < parseInt(finalB, 16)) {
B = (parseInt(B, 16) + 1).toString(16);
} else if (parseInt(B, 16) > parseInt(finalB, 16)) {
B = (parseInt(B, 16) - 1).toString(16);
}
var current = '#' + R + G + B;
// console.log("Setting color to", current);
element[0].style.backgroundColor = current;
sleep(10)
while (element[0].style.backgroundColor != current);
console.log("Colour changed");
// console.log("Final:", final);
}
alert("Done");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment