首页 > 技术文章 > 颜色color转为rgb格式

cnlg123 2019-07-15 11:35 原文

function convertHexToRGB(color) {
      if (color.length === 4) {
            let extendedColor = "#"
            for (let i = 1; i < color.length; i++) {
                 extendedColor += color.charAt(i) + color.charAt(i)
             }
            color = extendedColor
       }
       const values = {
             r: parseInt(color.substr(1, 2), 16),
            g: parseInt(color.substr(3, 2), 16),
            b: parseInt(color.substr(5, 2), 16),
           }
         return `${values.r}, ${values.g}, ${values.b}`
}
 
backgroundColor: `rgba(${convertHexToRGB(color)},0.2)`

推荐阅读