1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| export default function hexToRgba(hex, alpha = 1) {
| // 去除 # 符号
| hex = hex.replace('#', '');
|
| // 处理 3 位和 6 位十六进制颜色码
| if (hex.length === 3) {
| hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
| }
|
| // 解析红、绿、蓝通道的值
| const r = parseInt(hex.slice(0, 2), 16);
| const g = parseInt(hex.slice(2, 4), 16);
| const b = parseInt(hex.slice(4, 6), 16);
|
| // 生成 rgba 字符串
| return `rgba(${r}, ${g}, ${b}, ${alpha})`;
| }
|
|