字符串转 RGB

 1 function strToRGB (str = '', opacity = 1) {
 2     const resultOpacity = isNaN(Number(opacity)) ? 1 : (opacity > 1 ? 1 : Number(opacity))
 3     const arr = Array.prototype.map.call(String(str), (item) => item.charCodeAt())
 4     let [r, g, b] = [0, 0, 0]
 5     arr.forEach((item, index) => {
 6         if (index % 3 === 0) {
 7             r += item
 8         } else if (index % 3 === 1) {
 9             g += item
10         } else if (index % 3 === 2) {
11             b += item    
12         }
13     })
14     r %= 256
15     g %= 256
16     b %= 256
17     return `rgba(${r}, ${g}, ${b}, ${resultOpacity})`
18 }
原文地址:https://www.cnblogs.com/W-it-H-ou-T/p/14023886.html