index.js 477 B

123456789101112131415161718192021222324
  1. 'use strict';
  2. /***
  3. * Convert string to hex color.
  4. *
  5. * @param {String} str Text to hash and convert to hex.
  6. * @returns {String}
  7. * @api public
  8. */
  9. module.exports = function hex(str) {
  10. for (
  11. var i = 0, hash = 0;
  12. i < str.length;
  13. hash = str.charCodeAt(i++) + ((hash << 5) - hash)
  14. );
  15. var color = Math.floor(
  16. Math.abs(
  17. (Math.sin(hash) * 10000) % 1 * 16777216
  18. )
  19. ).toString(16);
  20. return '#' + Array(6 - color.length + 1).join('0') + color;
  21. };