Skip to content

Instantly share code, notes, and snippets.

@chiplay
Created May 29, 2012 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chiplay/2829468 to your computer and use it in GitHub Desktop.
Save chiplay/2829468 to your computer and use it in GitHub Desktop.
Get Color by Percent
function get_color(start = "000000",end = "ffffff",percent = 50)
{
// start should always be darker
var sr = start.slice(0,2), sg = start.slice(2,2), sb = start.slice(4,2), er = end.slice(0,2), eg = end.slice(2,2), eb = end.slice(4,2);
var r = Math.ceil(((parseInt(er,16) - parseInt(sr,16))*percent)+parseInt(sr,16)), g = Math.ceil(((parseInt(eg,16) - parseInt(sg,16))*percent)+parseInt(sg,16)), b = Math.ceil(((parseInt(eb,16) - parseInt(sb,16))*percent)+parseInt(sb,16));
var hex = (r << 16) | (g << 8) | b;
return "#"+hex.toString(16);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment