1

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? Converting them straight to hex makes them all dark blue or black...

(Edit) The first answer helped, and I get colors other than black now, but they're still not the right hue. (I know the Google Maps API takes RGB, not HSV, so it's not that...)

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: Correct colors

This is what I get in JS with my current code This is what I get in JS with my current code

Andrew Torr
  • 1,057
  • 3
  • 13
  • 26

2 Answers2

2

Yes, you can convert the int into hex as follows :

var num = 2147473665;

var hexString = num.toString(16);

Hex string is now 7FFFD901

var alphalessHexString = hexString.substr(2, 6)

alphalessHexString is now FFD901

Put it all together in one line:

var num = 2147473665;
var alphalessHexString = num.toString(16).substr(2, 6)

alphalessHexString is now FFD901

Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
  • This doesn't seem to work correctly... will I have to make a substring to get rid of the alpha? – Andrew Torr Jun 12 '16 at 01:01
  • I just edited the answer to be fully complete. – Jonathan Eustace Jun 12 '16 at 01:32
  • That doesn't seem to be it either... I'm getting values like -7f0000, -ff4c01, -ff5c2e, etc. - they're all dark gray or blue. They don't contain the right values as substrings either... Maybe I can't convert it directly to a hex value? The original int comes from a Java color object in ARGB format – Andrew Torr Jun 12 '16 at 01:34
  • Post your full code above. The example I posted works fine for me. – Jonathan Eustace Jun 12 '16 at 01:39
  • 1
    Andrew, you're starting with a negative number [because of the Java int type](http://stackoverflow.com/questions/17605197/what-is-the-meaning-of-a-negative-number-when-using-getrgb). So in your JS perhaps convert it to a positive number first: `(-num).toString(16),substr(2)` and then -2147473665 becomes ffd901. – nnnnnn Jun 12 '16 at 01:40
  • Nope :/ I'm getting the same numbers, but positive. Colors look exactly the same – Andrew Torr Jun 12 '16 at 01:45
  • But ffd901 isn't dark gray or blue, it's yucky yellow. – nnnnnn Jun 12 '16 at 01:49
  • Oh, duh, I forgot to add the hashtag to the string. I'm getting actual colors now, but they look like the opposite color... Is that what you were talking about in the part of the comment you deleted? lol – Andrew Torr Jun 12 '16 at 02:01
0

If you have a color ARGB then what you have to do is

  int hx=-16767233 & 0x00ffffff;
  g2.setColor(new Color(hx));
  g2.fillRect(0, 0, w, h);

now hx is RGB without the alpha.

gpasch
  • 2,672
  • 3
  • 10
  • 12