export function genTextPortrait(name) {
  if (!name) {
    return ''
  }
  name = name.substring(0, 1)
  let cvs = document.createElement('canvas')
  cvs.setAttribute('width', 100)
  cvs.setAttribute('height', 100)
  let ctx = cvs.getContext('2d')
  ctx.fillStyle = transColor(name)
  ctx.fillRect(0, 0, 100, 100)
  ctx.fillStyle = 'rgb(255,255,255)'
  ctx.font = 50 + 'px Arial'
  ctx.textBaseline = 'middle'
  ctx.textAlign = 'center'
  ctx.fillText(name, 50, 50)
  return cvs.toDataURL('image/png', 1)
}

function transColor(name) {
  var str = ''
  for (var i = 0; i < name.length; i++) {
    str += parseInt(name[i].charCodeAt(0), 10).toString(16)
  }
  return '#' + str.slice(1, 4)
}
