RGBA透明色转换为16进制颜色

924次浏览

前言

最近接到需求,是rgba转换为16进制,一开始我还和产品争论,16进制不都是不透明颜色吗?透明的怎么转换为16进制呢?没错,原来真的可以转换,看来我落伍了。我很久之前就写过一篇文章 JS十六进制颜色(#fff)与RGB, HSL颜色的相互转换,感兴趣点击查看,我之前的rgba都是转换为2个颜色,一个是rgb,一个是opacity透明度,没有想到,rgba竟然可以转换为一个16进制的颜色,只不过由原来6位变为8位。

颜色透明度对照表

100% — FF
99% — FC
98% — FA
97% — F7
96% — F5
95% — F2
94% — F0
93% — ED
92% — EB
91% — E8
90% — E6
89% — E3
88% — E0
87% — DE
86% — DB
85% — D9
84% — D6
83% — D4
82% — D1
81% — CF
80% — CC
79% — C9
78% — C7
77% — C4
76% — C2
75% — BF
74% — BD
73% — BA
72% — B8
71% — B5
70% — B3
69% — B0
68% — AD
67% — AB
66% — A8
65% — A6
64% — A3
63% — A1
62% — 9E
61% — 9C
60% — 99
59% — 96
58% — 94
57% — 91
56% — 8F
55% — 8C
54% — 8A
53% — 87
52% — 85
51% — 82
50% — 80
49% — 7D
48% — 7A
47% — 78
46% — 75
45% — 73
44% — 70
43% — 6E
42% — 6B
41% — 69
40% — 66
39% — 63
38% — 61
37% — 5E
36% — 5C
35% — 59
34% — 57
33% — 54
32% — 52
31% — 4F
30% — 4D
29% — 4A
28% — 47
27% — 45
26% — 42
25% — 40
24% — 3D
23% — 3B
22% — 38
21% — 36
20% — 33
19% — 30
18% — 2E
17% — 2B
16% — 29
15% — 26
14% — 24
13% — 21
12% — 1F
11% — 1C
10% — 1A
9% — 17
8% — 14
7% — 12
6% — 0F
5% — 0D
4% — 0A
3% — 08
2% — 05
1% — 03
0% — 00

改造RGB转为十六进制代码

改造代码如下,这样就可以通过一个函数,将RGBA直接转换为十六进制了。

   let colorOpacity={
        100:'ff',
        99: 'fc',
        98: 'fa',
        97: 'f7',
        96: 'f5',
        95: 'f2',
        94: 'f0',
        93: 'ed',
        92: 'eb',
        91: 'e8',
        90: 'e6',
        89: 'e3',
        88: 'e0',
        87: 'de',
        86: 'db',
        85: 'd9',
        84: 'd6',
        83: 'd4',
        82: 'd1',
        81: 'cf',
        80: 'cc',
        79: 'c9',
        78: 'c7',
        77: 'c4',
        76: 'c2',
        75: 'bf',
        74: 'bd',
        73: 'ba',
        72: 'b8',
        71: 'b5',
        70: 'b3',
        69: 'b0',
        68: 'ad',
        67: 'ab',
        66: 'a8',
        65: 'a6',
        64: 'a3',
        63: 'a1',
        62: '9e',
        61: '9c',
        60: '99',
        59: '96',
        58: '94',
        57: '91',
        56: '8f',
        55: '8c',
        54: '8a',
        53: '87',
        52: '85',
        51: '82',
        50: '80',
        49: '7d',
        48: '7a',
        47: '78',
        46: '75',
        45: '73',
        44: '70',
        43: '6e',
        42: '6b',
        41: '69',
        40: '66',
        39: '63',
        38: '61',
        37: '5e',
        36: '5c',
        35: '59',
        34: '57',
        33: '54',
        32: '52',
        31: '4f',
        30: '4d',
        29: '4a',
        28: '47',
        27: '45',
        26: '42',
        25: '40',
        24: '3d',
        23: '3b',
        22: '38',
        21: '36',
        20: '33',
        19: '30',
        18: '2e',
        17: '2b',
        16: '29',
        15: '26',
        14: '24',
        13: '21',
        12: '1f',
        11: '1c',
        10: '1a',
        9: '17',
        8: '14',
        7: '12',
        6: '0f',
        5: '0d',
        4: '0a',
        3: '08',
        2: '05',
        1: '03',
        0: '00',
    }
    let colorRgbaToHex = function(color){
    let that = color;
    // 如果是rgba颜色表示
    if (/^(rgba|RGBA)/.test(that)) {
      let aColor = that.replace(/(?:\(|\)|rgba|RGBA)*/g, "").split(",");
      let opacity = aColor.pop()
      let opacityHax = !isNaN(opacity)?colorOpacity[Number(opacity) *100]:''

      let strHex = "#";
        for (let i=0; i<aColor.length; i++) {
          let hex = Number(aColor[i]).toString(16);
            if (hex.length < 2) {
                hex = '0' + hex;    
            }
            strHex += hex;
        }
        strHex = strHex+opacityHax
        if (strHex.length !== 9) {
            strHex = that  
        }
        return strHex
    }
    return that
}

console.log(colorRgbaToHex('rgba(45,34,66,.5)'))

小结

本文是haorooms博客整理,欢迎访问haorooms前端博客。RGBA透明色转换为16进制颜色,就到这里。

相关文章: