functionrgbaTo16color(color){let val = color
.replace(/rgba?\(/,'').replace(/\)/,'').replace(/[\s+]/g,'').split(',');let a =parseFloat(val[3]||1),
r = Math.floor(a *parseInt(val[0])+(1- a)*255),
g = Math.floor(a *parseInt(val[1])+(1- a)*255),
b = Math.floor(a *parseInt(val[2])+(1- a)*255);return"#"+("0"+ r.toString(16)).slice(-2)+("0"+ g.toString(16)).slice(-2)+("0"+ b.toString(16)).slice(-2);}
二、十六进制颜色转成rgba颜色(或rgb颜色)
functionhexToRGBA(hex, alpha){let r =parseInt(hex.slice(1,3),16);let g =parseInt(hex.slice(3,5),16);let b =parseInt(hex.slice(5,7),16);if(alpha){return"rgba("+ r +", "+ g +", "+ b +", "+ alpha +")";}else{return"rgb("+ r +", "+ g +", "+ b +")";}}
三、十进制颜色转换16进制颜色
例:2201331 转换成 #2196f3
functiondeciToHex(color, defaultColor='#DC143C'){// 指定个非法的值不生效if(color && color[0]==='#'){return color
}var c = defaultColor ||'none'
color =parseInt(color)if(color ===undefined|| color >16777215|| color <0|| color ===null){return c
}
c = color.toString(16)
c =leftZeroPad(c,6)
c ='#'+ c
return c
functionleftZeroPad(val, minLength){varMANY_ZEROS='000000000000000000'if(typeof(val)!=='string'){
val =String(val)}return(MANY_ZEROS.substring(0, minLength - val.length))+ val
}}