转换函数
//hex -> rgb
function hexToRgb(hex) {
return 'rgb(' + parseInt('0x' + hex.slice(1, 3)) + ',' + parseInt('0x' + hex.slice(3, 5)) + ',' + parseInt('0x' + hex.slice(5, 7)) + ')'
}
//hex -> rgba
function hexToRgba(hex, opacity) {
return 'rgba(' + parseInt('0x' + hex.slice(1, 3)) + ',' + parseInt('0x' + hex.slice(3, 5)) + ',' + parseInt('0x' + hex.slice(5, 7)) + ',' + opacity + ')'
}
使用样例
var sHex = '#ff008b';
console.log('十六进制格式:', sHex);
console.log('RGB格式:', hexToRgb(sHex));
console.log('RGBA格式:', hexToRgba(sHex, 0.5));