UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

21 lines (16 loc) 460 B
export default function stringToColor(string) { if (!string || !string.length) return; let hash = 0; let i; /* eslint-disable no-bitwise */ for (i = 0; i < string.length; i += 1) { hash = string.charCodeAt(i) + ((hash << 5) - hash); } let colour = '#'; for (i = 0; i < 3; i += 1) { const value = hash >> i * 8 & 0xff; colour += "00".concat(value.toString(16)).substr(-2); } /* eslint-enable no-bitwise */ return colour; }