Skip to content

Instantly share code, notes, and snippets.

@jeystaats
Created February 24, 2017 09:15
Show Gist options
  • Save jeystaats/7d3171b6a5310c33bbd5196a86b5e820 to your computer and use it in GitHub Desktop.
Save jeystaats/7d3171b6a5310c33bbd5196a86b5e820 to your computer and use it in GitHub Desktop.
Turn a string ( username e.g ) into a RGB ( 15e5de ) . Only put the hashtag in front of it.
// Turn a string ( like a username , into a RGB code (15e5de) , you can use this for styling purposes per user in chat examples
hashCode(str) { // java String#hashCode
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
},
intToRGB(i){
var c = (i & 0x00FFFFFF)
.toString(16)
.toUpperCase();
return "00000".substring(0, 6 - c.length) + c;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment