提交 2e630f44 编写于 作者: L liyongning

fix: 修复 hexToRgba 转换出错的 bug

上级 8319b947
...@@ -8,14 +8,14 @@ ...@@ -8,14 +8,14 @@
a: number; a: number;
} }
*/ */
export function hexToRgba (hex) { export function hexToRgba(hex) {
// 异常情况 // 异常情况
if (!hex) { if (!hex) {
return { return {
r: 0, r: 0,
g: 0, g: 0,
b: 0, b: 0,
a: 0 a: 0,
} }
} }
// 去掉 # // 去掉 #
...@@ -27,24 +27,24 @@ export function hexToRgba (hex) { ...@@ -27,24 +27,24 @@ export function hexToRgba (hex) {
r: 0, r: 0,
g: 0, g: 0,
b: 0, b: 0,
a: 0 a: 0,
} }
} }
// 格式化 tmpHex,使其变成 rrggbb 或 rrggbbaa // 格式化 tmpHex,使其变成 rrggbb 或 rrggbbaa
if (tmpHexLen === 3 || tmpHexLen === 4) { if (tmpHexLen === 3 || tmpHexLen === 4) {
// rgb => rrggbb || rgba => rrggbbaa // rgb => rrggbb || rgba => rrggbbaa
tmpHex = tmpHex.replace(/(\w{1})/g, 'r1r1') tmpHex = tmpHex.replace(/(\w{1})/g, '$1$1')
} }
// r1 ~ a2 // rgba
const [r1, r2, g1, g2, b1, b2, a1, a2] = tmpHex.match(/(\w{1})/g) let [sr, sg, sb, sa] = tmpHex.match(/(\w{2})/g)
// rgb // rgb
const r = parseInt(r1) * 16 + parseInt(r2); const g = parseInt(g1) * 16 + parseInt(g2); const b = parseInt(b1) * 16 + parseInt(b2) const r = parseInt(sr, 16), g = parseInt(sg, 16), b = parseInt(sb, 16)
if (!a1) { if (!sa) {
return { r, g, b, a: 1 } return { r, g, b, a: 1 }
} }
return { return {
r, g, b, a: (`0x100${a1}${a2}` - 0x10000) / 255 r, g, b, a: (`0x100${sa}` - 0x10000) / 255
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册