UNPKG

jknife

Version:
33 lines (31 loc) 812 B
/** * @description 颜色值转换为RGB数组 * @param {String} sColor #707B7C * @return {Array} * @interactive * * @example * console.log(colorHexToRgb('#707B7C')) // [112, 123, 124] */ function colorHexToRgb (sColor) { const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/ sColor = sColor.toLowerCase() if (sColor && reg.test(sColor)) { if (sColor.length === 4) { let sColorNew = '#' for (let i = 1; i < 4; i += 1) { sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1)) } sColor = sColorNew } // 处理六位的颜色值 const sColorChange = [] for (let i = 1; i < 7; i += 2) { sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2))) } return sColorChange } else { return [] } } export default colorHexToRgb