Skip to content

Instantly share code, notes, and snippets.

@morganrallen
Created November 7, 2012 14:37
Show Gist options
  • Save morganrallen/4031967 to your computer and use it in GitHub Desktop.
Save morganrallen/4031967 to your computer and use it in GitHub Desktop.
Relative RGB
function RGB2HEX(s) {
return [ parseInt(s.substr(0,2), 16), parseInt(s.substr(2,2), 16), parseInt(s.substr(4,2), 16) ];
}
function ItoS(i) {
return i === 0 ? "00" : i.toString(16);
}
function HEX2RGB(s) {
return ItoS(s[0]) + ItoS(s[1]) + ItoS(s[2]);
}
function HexDiff(f, s) {
return [ f[0] - s[0], f[1] - s[1], f[2] - s[2] ];
}
var metal = "000000";
var notSoMetal = "000010";
var howNotMetal = HexDiff(RGB2HEX(notSoMetal), RGB2HEX(metal));
var bob = RGB2HEX("bb00bb");
var newBob = [bob[0] + howNotMetal[0], bob[1] + howNotMetal[1], bob[2] + howNotMetal[2]];
console.log(howNotMetal, bob);
console.log(HEX2RGB(newBob));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment