SOURCE

// echarts绘图有些情况下rgba带有透明度不识别 换算成十六进制
function hexify(color) {
  if (/rgba/.test(color)) {
    const values = color
      .replace(/rgba?\(/, '')
      .replace(/\)/, '')
      .replace(/[\s+]/g, '')
      .split(',');
    const a = parseFloat(values[3] || 1);
    const r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255);
    const g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255);
    const b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
    return (
      '#' +
      ('0' + r.toString(16)).slice(-2) +
      ('0' + g.toString(16)).slice(-2) +
      ('0' + b.toString(16)).slice(-2)
    );
  } else {
    return color;
  }
}

console.log(hexify('rgba(0,0,0,.3)'))
console 命令行工具 X clear

                    
>
console