ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` export const colorToRGB = (val, opa) => { var pattern = /^(#?)[a-fA-F0-9]{6}$/; //16进制颜色值校验规则 var isOpa = typeof opa == 'number'; //判断是否有设置不透明度 if (!pattern.test(val)) { //如果值不符合规则返回空字符 return ''; } var v = val.replace(/#/, ''); //如果有#号先去除#号 var rgbArr = []; var rgbStr = ''; for (var i = 0; i < 3; i++) { var item = v.substring(i * 2, i * 2 + 2); var num = parseInt(item, 16); rgbArr.push(num); } rgbStr = rgbArr.join(); rgbStr = 'rgb' + (isOpa ? 'a' : '') + '(' + rgbStr + (isOpa ? ',' + opa : '') + ')'; return rgbStr; } ```