一个小工具: 字符串rgba转数组。
/**
* 字符串rgba转数组
* @param {string} color "rgba(1,2,3,4)"
* @param {boolean} normalized 是否被255归一化
* @return {array} [1,2,3,4]
*/
export function rgba2arr(color, normalized = true) {
let ret = [];
let colorStr = new RegExp(/(?<=\()\S+(?=\))/).exec(color);
if (colorStr) {
ret = colorStr[0].split(",");
ret = ret.map(item => {
return Number.parseFloat(item, 2);
});
}
if (normalized) {
ret = ret.map(item => {
return (item = +item / 255);
});
}
return ret;
}