Created
April 2, 2014 15:49
-
-
Save ehedaya/9936893 to your computer and use it in GitHub Desktop.
hash string to color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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