Last active
October 7, 2022 03:29
-
-
Save dbwodlf3/e4a032df489df95c143403e354d49ad6 to your computer and use it in GitHub Desktop.
string to color
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
export function generate_hash_color(text, weight={red: 0, green:1, blue: 2}){ | |
let color1 = 0; | |
let color2 = 0; | |
let color3 = 0; | |
if(text.length==0){ | |
"rgb(0, 0, 0)"; | |
} | |
else if(text.length==1){ | |
color1 = text.charCodeAt(0) * 2 % 256 | |
color2 = text.charCodeAt(0) * 3 % 256 | |
color3 = text.charCodeAt(0) * 5 % 256 | |
} | |
else if(text.length==2){ | |
color1 = text.charCodeAt(0) * 2 % 256 | |
color2 = text.charCodeAt(1) * 3 % 256 | |
color3 = text.charCodeAt(0) * 5 % 256 | |
} | |
else { | |
color1 = text.charCodeAt(text.length/3) * 2 % 256 | |
color2 = text.charCodeAt(text.length/2) * 3 % 256 | |
color3 = text.charCodeAt(text.length-1) * 5 % 256 | |
} | |
const colors = [color1, color2, color3]; | |
colors.sort(); | |
return `rgb(${colors[weight.red]}, ${colors[weight.green]}}, ${colors[weight.blue]}})`; | |
} | |
export function string_to_number(text, key=1){ | |
let hash1 = 0; | |
let hash2 = 0; | |
let hash3 = 0; | |
if(text.length==0){ | |
"0"; | |
} | |
else if(text.length==1){ | |
hash1 = text.charCodeAt(0) * 28 % key | |
hash2 = text.charCodeAt(0) * 496 % key | |
hash3 = text.charCodeAt(0) * 8128 % key | |
} | |
else if(text.length==2){ | |
hash1 = text.charCodeAt(0) * 28 % key | |
hash2 = text.charCodeAt(1) * 496 % key | |
hash3 = text.charCodeAt(1) * 8128 % key | |
} | |
else { | |
hash1 = text.charCodeAt(text.length/3) * 28 % 256 | |
hash2 = text.charCodeAt(text.length/2) * 496 % 256 | |
hash3 = text.charCodeAt(text.length-1) * 8128 % 256 | |
} | |
return hash1+hash2+hash3 | |
} | |
export function get_color_set(order, setName="default"){ | |
const colorsets = { | |
default: [ | |
"#3d5a80", | |
"#98c1d9", | |
"#e0fbfc", | |
"#292341", | |
"#2b2d42", | |
"#8d99ae", | |
"#8ecae6", | |
"#023047", | |
"#355070", | |
"#118ab2", | |
"#073b4c", | |
"#003049", | |
"#14213d", | |
"#00171f", | |
"#007ea7", | |
"#00a8e8", | |
"#00296b", | |
"#003f88", | |
"#00509d", | |
"#03045e", | |
"#0077b7", | |
"#177e89", | |
"#084cc61", | |
"#90e0ef", | |
"#caf0f8", | |
], | |
blue :[ | |
] | |
} | |
return colorsets[setName][order%colorsets[setName].length]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment