Skip to content

Instantly share code, notes, and snippets.

@ehedaya
Created April 2, 2014 15:49
Show Gist options
  • Save ehedaya/9936893 to your computer and use it in GitHub Desktop.
Save ehedaya/9936893 to your computer and use it in GitHub Desktop.
hash string to color
function djb2(str){
var hash = 5381;
for (var i = 0; i < str.length; i++) {
hash = ((hash << 5) + hash) + str.charCodeAt(i); /* hash * 33 + c */
}
return hash;
}
function hashStringToColor(str) {
var hash = djb2(str);
var r = (hash & 0xFF0000) >> 16;
var g = (hash & 0x00FF00) >> 8;
var b = hash & 0x0000FF;
return "#" + ("0" + r.toString(16)).substr(-2) + ("0" + g.toString(16)).substr(-2) + ("0" + b.toString(16)).substr(-2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment