Posted in Javascript onSeptember 04, 2017
本文实例讲述了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})`; }
JS实现颜色的10进制转化成rgba格式的方法
- Author -
蓝精灵依米声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@