Convert a string's hash value into a corresponding RGB color using TypeScript
About 120 wordsLess than 1 minute...
Abstract
Convert a string's hash value into a corresponding RGB color using TypeScript.
Coding
function hashStringToColor(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
const color = (hash & 0x00FFFFFF)
.toString(16)
.toUpperCase()
.padStart(6, `0`);
return `#${color}`;
}
Powered by Waline v3.3.0