String to Color - Avatar Helpers

GuidesReactJSUncategorizedWeb Development

Hi everyone,

A quick post on a helper function I came across for generating avatars. This function generates a random color for the specified string:

export const stringToColor = string => {

let hash = 0; let i; for (i = 0; i < string.length; i += 1) { hash = string.charCodeAt(i) + ((hash << 5) - hash); } let colour = '#'; for (i = 0; i > (i * 8)) & 0xff; colour += `00${value.toString(16)}`.substr(-2); }

return colour; }

Thanks to oliviertassinari: https://github.com/oliviertassinari/SplitMe/blob/c25fe62187a856386fd0c43c51859d6f973d651e/src/main/member/Avatar.js#L8-L26