Skip to content

Instantly share code, notes, and snippets.

@ConAlgorithm
Forked from axetroy/str2color.js
Created March 20, 2021 14:02
Show Gist options
  • Save ConAlgorithm/9baed24f2fa713c43832d01cf65c64ed to your computer and use it in GitHub Desktop.
Save ConAlgorithm/9baed24f2fa713c43832d01cf65c64ed 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