Skip to content

Instantly share code, notes, and snippets.

@bhelx
Created February 20, 2013 18:20
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 bhelx/4997731 to your computer and use it in GitHub Desktop.
Save bhelx/4997731 to your computer and use it in GitHub Desktop.
var NickList = function () {
this.nicks = [];
this.colorMap = {};
this.colors = [
'#ff00ff',
'#00007f',
'#009300',
'#ff0000',
'#7f0000',
'#9c009c',
'#fc7f00',
'#7f7f7f',
'#ffffff'
];
};
NickList.prototype.colorFor = function (nick) {
var color = this.colorMap[nick]; // keep a cache
if (color) return color;
var hash = 0;
for (var i = 0; i < nick.length; i++) {
var ch = nick.charCodeAt(i);
hash = ((hash << 5) - hash) + ch;
hash = hash & hash; // Convert to 32bit integer
}
return (this.colorMap[nick] = this.colors[Math.abs(hash) % this.colors.length]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment