java 修改js颜色,Java在JavaScript中将颜色整数转换为RGB字符串

I'm trying to use some Java Color int values from a database in Javascript. What's the correct way to convert the Java color int (like -2147473665) to an RGB string (like '#ffff00') using Javascript?

Right now I'm taking the number, flipping it negative, converting it to hex, and adding a hashtag, but that gives me the wrong color. (The substring is to account for the alpha channel)

function getClients() {

var query = new Parse.Query(Client);

query.each(function(client) {

var clientName = client.get("clientName");

var borderColor = '#' + (-client.get("borderColor")).toString(16);

var fillColor = '#' + (-client.get("fillColor")).toString(16).substr(2);

var outline = client.get("outline");

console.log(client.get("borderColor"));

console.log(client.get("borderColor").toString(16));

console.log(hexToRGB(client.get("borderColor")));

var clientPoly = new google.maps.Polygon({

paths: outline,

strokeColor: borderColor,

strokeOpacity: 1,

strokeWeight: 2,

fillColor: fillColor,

fillOpacity: 0.5

});

clientPoly.setMap(mMap);

});

}

For example, the int -16767233 should be navy blue, but it's showing up as yellow.

-16731137 should be light blue, but it's red

-1218518 should be orange, but it's blue

Correct colors:

tVSF1.jpg

This is what I get in JS with my current code

hmmbp.png

What is going on here?? I know it's supposed to be RGB, and not HSV, so that's not it...

解决方案

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

console.log(getHexColor(-1218518)); // orange

function getHexColor(number){

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值