Last active
April 4, 2023 16:47
-
-
Save jrmessias/3ae2828d0ceca53f21bf9caed506b636 to your computer and use it in GitHub Desktop.
Function to convert string to color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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