So I need to make a code that turns a string into hex colour code. For example turn the string “purple” into the hex code that matches purple, but if its a random thing such as “fjskf”, it should still turn it into a random colour.
I have tried this so far:
function convert(str: string){
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let colour = '#';
for (let i = 0; i < 3; i++) {
const value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
};
But it doesnt work for every color like pink and green and blue. Thank you!