export function genTextPortrait(name) {
  name = name.substring(0, 1)
  let cvs = document.createElement('canvas')
  cvs.setAttribute('width', 30)
  cvs.setAttribute('height', 40)
  let ctx = cvs.getContext('2d')
  ctx.fillStyle = transColor(name)
  ctx.fillRect(0, 0, 30, 40)
  ctx.fillStyle = 'rgb(255,255,255)'
  ctx.font = 30 * 0.6 + 'px Arial'
  ctx.textBaseline = 'middle'
  ctx.textAlign = 'center'
  ctx.fillText(name, 15, 20)
  return cvs.toDataURL('image/png', 1).replace('data:image/png;base64,', '')
}

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)
}
