Skip to content

Instantly share code, notes, and snippets.

@LynnAU
Created September 14, 2017 00:30
Show Gist options
  • Save LynnAU/225b601b356415c5eff6ae0eb46f50a7 to your computer and use it in GitHub Desktop.
Save LynnAU/225b601b356415c5eff6ae0eb46f50a7 to your computer and use it in GitHub Desktop.
Convert an ID string, snowflake or anything, to a decimal colour
function IDToColour(id) {
var hash = 0;
var numbers = id.split('');
for(var i = 0; i < id.length; i++) {
hash = id.charCodeAt(i) + ((hash << 5) - hash);
}
var c = (hash & 0x00FFFFFF).toString(16).toUpperCase();
return parseInt('00000'.substring(0, 6 - c.length) + c, 16);
}
IDToColour('123456789'); //11669557
IDToColour('987654321'); //1981237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment