Created
December 12, 2019 12:25
-
-
Save el-gringo/8e05bcc785327e1c8a926858a00a791d to your computer and use it in GitHub Desktop.
stringToColor
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
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