UNPKG

@e-group/utils

Version:

eGroup team utils that share across projects.

28 lines (21 loc) 559 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = stringToColor; 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; }