Source: scripts/helper/_user.js

'use strict';

/**
 * User functions.
 * @namespace user
 * @memberof UCASDesignFramework
 * @param {object} global - UCASDesignFramework object.
 * @param {object} _u - UCASUtilities object.
 */
(function (global, _u) {
  /**
   * Creates a user avatar
   * @function getAvatar
   * @memberof! UCASDesignFramework.user
   * @public
   * @param {string} userName - The name of the user
   * @return {string} - The svg image for use as a background
   */
  function getAvatar (userName) {
    userName = userName || false

    if (userName) {
      var letter = userName.charAt(0).toUpperCase()
      var letterCode = letter.charCodeAt(0)

      // Limit to a palette of colours to ensure they fit with the theme and are readable
      var colors = ['#e00023', '#759500', '#117882', '#1077d0', '#223992', '#752482']
      var mappedNumber = ((letterCode >= 65) && (letterCode <= 90)) ? _u.maths.mapRange(letterCode, [65, 90], [0, 5], true) : 0
      var avatarColour = encodeURIComponent(colors[mappedNumber])
      letter = encodeURIComponent(_u.string.sanitize(letter))

      return 'data:image/svg+xml;charset=utf-8,%3Csvg%20height%3D%2238%22%20width%3D%2238%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%3E%3Ccircle%20cx%3D%2219%22%20cy%3D%2219%22%20fill%3D%22' + avatarColour + '%22%20r%3D%2219%22%3E%3C%2Fcircle%3E%3Ctext%20dy%3D%220.35em%22%20fill%3D%22white%22%20style%3D%22%20font-size%3A%2022px%3B%20font-family%3A%20%27Arial%27%2C%20sans-serif%3B%20text-anchor%3A%20middle%3B%20white-space%3A%20pre%3B%22%20x%3D%2219%22%20y%3D%2219%22%3E' + letter + '%3C%2Ftext%3E%3C%2Fsvg%3E'
    }

    return 'data:image/svg+xml;charset=utf-8,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2033%2033%22%20width%3D%2235px%22%20height%3D%2235px%22%3E%3Cg%20transform%3D%22translate%28-1667%20-7%29%22%3E%3Ccircle%20cx%3D%2216.5%22%20cy%3D%2216.5%22%20r%3D%2216.5%22%20fill%3D%22%23414141%22%20transform%3D%22translate%281667%207%29%22%2F%3E%3Cpath%20d%3D%22M1671.6%2035c1.6-6.5%208.2-10.6%2014.7-9%204.4%201.1%207.9%204.5%209%209-3.1%203.2-7.4%205-11.9%205-4.4%200-8.6-1.8-11.8-5z%22%20fill%3D%22%23e3e3e3%22%2F%3E%3Cellipse%20cx%3D%221683.5%22%20cy%3D%2220.6%22%20rx%3D%227.9%22%20ry%3D%227.9%22%20fill%3D%22%23efefef%22%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E'
  }

  // Expose public methods.
  global.user = {
    getAvatar: getAvatar
  }
})(UCASDesignFramework, UCASUtilities)