如何解决使用js将十六进制颜色转换为int32值
现在,我需要知道如何更改十六进制等任何值
'#01ff00','#7fff82','#c1ffc0','#16F6F5','#81FFFC','#BFFFFF'
到int32(必要)
function toColor(num) {
num >>>= 0;
var b = num & 0xFF,g = (num & 0xFF00) >>> 8,r = (num & 0xFF0000) >>> 16,a = ( (num & 0xFF000000) >>> 24 ) / 255 ;
return "rgba(" + [r,g,b,a].join(",") + ")";
}
我使用了这个公式,但是我不知道如何应用逆向工程。
编辑:
我在数据库中具有这个值:-16744448,使用公式,我具有以下颜色值:'#008000',我需要在-16744448中再次转换该颜色。
当我使用注释中提到的公式时,我得到:32768
此值来自VB.net,此函数:Color.FromArgb
解决方法
我们要删除“#”,将其从十六进制解析为十进制,并补充16 ^ 6。
返回:
不补码,以十六进制的字符串填充前导零,并修剪到最后6个,在开头添加“#”。
var values=["#008000",'#01ff00','#7fff82','#c1ffc0','#16F6F5','#81FFFC','#BFFFFF'];
values.forEach(v=>{
var r=parseInt(v.slice(-6),16)-Math.pow(16,6);
var rr="#"+("0".repeat(6)+(Math.pow(16,6)+r).toString(16)).slice(-6);
console.log(v,r,rr);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
,
您可以使用parseInt
解析十六进制代码值。只需将基数设置为16
。这甚至可以处理三位数的十六进制代码,例如#F00
。
不久前我开始写CSS color value parser,并在下面使用了十六进制逻辑。
const colors = [ '#01ff00','#BFFFFF' ]
console.log(colors.map(color => toColor(hexToInt(color))));
function hexToInt(input) {
return parseInt(input.replace(/^#([\da-f])([\da-f])([\da-f])$/i,'#$1$1$2$2$3$3').substring(1),16);
};
function toColor(num) {
num >>>= 0;
const b = num & 0xFF,g = (num & 0xFF00) >>> 8,r = (num & 0xFF0000) >>> 16,a = ( (num & 0xFF000000) >>> 24 ) / 255 ;
return "rgba(" + [r,g,b,a].join(",") + ")";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。