Last active
March 20, 2021 14:02
-
-
Save axetroy/876137bc0ee60e589f977e8399cb3c81 to your computer and use it in GitHub Desktop.
根据一个字符串,生成一个动态的hex颜色
This file contains 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment