Skip to content

Instantly share code, notes, and snippets.

@axetroy
Last active March 20, 2021 14:02
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save axetroy/876137bc0ee60e589f977e8399cb3c81 to your computer and use it in GitHub Desktop.
Save axetroy/876137bc0ee60e589f977e8399cb3c81 to your computer and use it in GitHub Desktop.
根据一个字符串,生成一个动态的hex颜色
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