且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java在JavaScript中将颜色整数转换为RGB字符串

更新时间:2022-05-17 05:20:26

你需要处理有符号整数。请参见如何在JavaScript中将十进制转换为十六进制

You need to handle signed integers. See How to convert decimal to hex in JavaScript?

console.log(getHexColor(-16731137)); // light blue
console.log(getHexColor(-1218518)); // orange

function getHexColor(number){
    return "#"+((number)>>>0).toString(16).slice(-6);
}