/**
* Created by cld on 2018/3.
* 十六进制颜色值域RGB格式颜色值之间的相互转换
*/
//十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
/*RGB颜色转换为16进制
*定义的函数
*/
let RGBToHex = function(rgb) {
var regexp = /[0-9]{0,3}/g
var re = rgb.match(regexp) //利用正则表达式去掉多余的部分,将rgb中的数字提取
var hexColor = '#'
var hex = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
]
for (var i = 0; i < re.length; i++) {
var r = null,
c = re[i],
l = c
var hexAr = []
while (c > 16) {
r = c % 16
c = (c / 16) >> 0
hexAr.push(hex[r])
}
hexAr.push(hex[c])
if (l < 16 && l != '') {
hexAr.push(0)
}
hexColor += hexAr.reverse().join('')
}
//alert(hexColor)
return hexColor
}
/*16进制颜色转为RGB格式
*直接定义在String.prototype属性上
*/
String.prototype.colorRgb = function() {
var sColor = this.toLowerCase()
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = '#'
for (var i = 1; i < 4; i += 1) {
sColorNew += sColor
.slice(i, i + 1)
.concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
//处理六位的颜色值
var sColorChange = []
for (var i = 1; i < 7; i += 2) {
sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
}
return 'rgb(' + sColorChange.join(', ') + ')'
} else {
return sColor
}
}
export { RGBToHex }
* Created by cld on 2018/3.
* 十六进制颜色值域RGB格式颜色值之间的相互转换
*/
//十六进制颜色值的正则表达式
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/
/*RGB颜色转换为16进制
*定义的函数
*/
let RGBToHex = function(rgb) {
var regexp = /[0-9]{0,3}/g
var re = rgb.match(regexp) //利用正则表达式去掉多余的部分,将rgb中的数字提取
var hexColor = '#'
var hex = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'A',
'B',
'C',
'D',
'E',
'F'
]
for (var i = 0; i < re.length; i++) {
var r = null,
c = re[i],
l = c
var hexAr = []
while (c > 16) {
r = c % 16
c = (c / 16) >> 0
hexAr.push(hex[r])
}
hexAr.push(hex[c])
if (l < 16 && l != '') {
hexAr.push(0)
}
hexColor += hexAr.reverse().join('')
}
//alert(hexColor)
return hexColor
}
/*16进制颜色转为RGB格式
*直接定义在String.prototype属性上
*/
String.prototype.colorRgb = function() {
var sColor = this.toLowerCase()
if (sColor && reg.test(sColor)) {
if (sColor.length === 4) {
var sColorNew = '#'
for (var i = 1; i < 4; i += 1) {
sColorNew += sColor
.slice(i, i + 1)
.concat(sColor.slice(i, i + 1))
}
sColor = sColorNew
}
//处理六位的颜色值
var sColorChange = []
for (var i = 1; i < 7; i += 2) {
sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
}
return 'rgb(' + sColorChange.join(', ') + ')'
} else {
return sColor
}
}
export { RGBToHex }