Skip to content

Instantly share code, notes, and snippets.

@jrmessias
Last active April 4, 2023 16:47
Show Gist options
  • Save jrmessias/3ae2828d0ceca53f21bf9caed506b636 to your computer and use it in GitHub Desktop.
Save jrmessias/3ae2828d0ceca53f21bf9caed506b636 to your computer and use it in GitHub Desktop.
Function to convert string to color
var stringToColor = function(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var color = '#';
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
color += ('00' + value.toString(16)).substr(-2);
}
return color;
}
var color = stringToColor("string");
console.log(color);
console.log("https://www.colorhexmap.com/" + color.replace("#", ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment