封装
// 十六进制转化为rgb export const hexToRgb = hex => { var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex) return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null } /** * @param {Object} obj * @description 深拷贝 */ export const deepCopy = (obj) => { var result = Array.isArray(obj) ? [] : {} for (var key in obj) { if (obj.hasOwnProperty(key)) { if (typeof obj[key] === 'object') { result[key] = deepCopy(obj[key]) // 递归复制 } else { result[key] = obj[key] } } } return result }
import { deepCopy, hexToRgb } from '@/libs/tools'
// 颜色转rgb 判断是否是十六进制 if (/^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/.test(this.editFormData.template_style_info.bg_color)) { let result = hexToRgb(this.editFormData.template_style_info.bg_color) this.editFormData.template_style_info.bg_color = 'rgb' + '(' + result.r + ',' + result.g + ',' + result.b + ')' }