本文实例讲述了JS实现颜色的10进制转化成rgba格式的方法。分享给大家供大家参考,具体如下:
当我们在已知一个整数如(0~256x256x256x256 之间的颜色值),想获得关于它的3原色加一个透明度时,即alpha, red, green, blue,可采用如下方法:
方法一:
function getColor(number) { let color = number; const blue = parseInt(color % 0x100, 10); color = color >>> 8; const green = parseInt(color % 0x100, 10); color = color >>> 8; const red = parseInt(color % 0x100, 10); const alpha = (parseInt(color >>> 8, 10) / 0xFF).toFixed(1); return `rgba(${red}, ${green}, ${blue}, ${alpha})`;}
方法二:
function getColor(number) { const alpha = number >> 24 & 0xff; const red = number >> 16 & 0xff; const green = number >> 8 & 0xff; const blue = number & 0xff; return `rgba(${red}, ${green}, ${blue}, ${alpha})`;}
希望本文所述对大家JavaScript程序设计有所帮助。
新闻热点
疑难解答