Skip to content

Instantly share code, notes, and snippets.

@sandgraham
Created September 10, 2021 17:19
Show Gist options
  • Save sandgraham/2974c86842883429d42a51f203c2e9d7 to your computer and use it in GitHub Desktop.
Save sandgraham/2974c86842883429d42a51f203c2e9d7 to your computer and use it in GitHub Desktop.
Convert string to hex code
// Hash any string into an integer value
// Then we'll use the int and convert to hex.
function hashCode(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
// Convert an int to hexadecimal with a max length
// of six characters.
function intToARGB(i) {
var hex = ((i>>24)&0xFF).toString(16) +
((i>>16)&0xFF).toString(16) +
((i>>8)&0xFF).toString(16) +
(i&0xFF).toString(16);
// Sometimes the string returned will be too short so we
// add zeros to pad it out, which later get removed if
// the length is greater than six.
hex += '000000';
return hex.substring(0, 6);
}
// Extend the string type to allow converting to hex for quick access.
function toHexColour(str) {
return intToARGB(hashCode(str));
}
// toHexColour("foo") => "018cc6"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment