Skip to content

Instantly share code, notes, and snippets.

@el-gringo
Created December 12, 2019 12:25
Show Gist options
  • Save el-gringo/8e05bcc785327e1c8a926858a00a791d to your computer and use it in GitHub Desktop.
Save el-gringo/8e05bcc785327e1c8a926858a00a791d to your computer and use it in GitHub Desktop.
stringToColor
const SEED = 16777215;
const FACTOR = 49979693;
function stringToColor(str) {
let b = 64;
let d = 192;
let f = 0;
if (str.length > 0) {
for (var i = 0; i < str.length; i++)
str[i].charCodeAt(0) > d && (d = str[i].charCodeAt(0)),
(f = parseInt(SEED / d)),
(b = (b + str[i].charCodeAt(0) * f * FACTOR) % SEED);
}
const hex = ((b * str.length) % SEED).toString(16);
return hex.padEnd(6, hex);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment