1

I have a problem. I have a custom object with fields like

private String id;
private int key;
private String name;
private String desc;
private int sortorder;
private int color;

And constructor like

 public Label(String id, int key, String name, String desc, int sortorder, int color)

I'm putting color when creating object like this

Color.rgb(dialogColorRed, dialogColorGreen, dialogColorBlue)

Where dialogColorRed, dialogColorGreen, dialogColorBlue - are randomly assigned values from 0 to 255.

Then I'm packing an ArrayList of my Objects in JSON, the object looks like this (pay attention to color field)

"color": -6508994,
    "desc": "",
    "id": "81fed08a336b185e226a93f199f34803",
    "key": 87,
    "name": "w",
    "sortorder": 0

I'm getting negative color int value, but i need something like this

 "color":"4278255360"

What is the problem?

Oleg Misko
  • 31
  • 3
  • 2
    how is it that you need something like that? how does that translate into a color? – ItamarG3 Dec 12 '16 at 11:58
  • 5
    Java does not have unsigned ints, and the alpha channel of your color flips the sign bit of the 32-bit int. – laalto Dec 12 '16 at 12:00
  • @ItamarGreen this will be transformed by other program. I just need to pack a color like that – Oleg Misko Dec 12 '16 at 12:16
  • @laalto so what can you recommend? – Oleg Misko Dec 12 '16 at 12:17
  • Do you actually have a problem that needs to be solved? – laalto Dec 12 '16 at 12:17
  • @laalto Yes! :) I need to convert my negative RGB color into the color "color":"4278255360" this style. I've watched this answer http://stackoverflow.com/questions/5526690/convert-a-raw-negative-rgb-int-value-back-to-a-3-number-rgb-value?rq=1 I don't really get how to get the normal R, G and B and create an Integer from it – Oleg Misko Dec 12 '16 at 12:24
  • Do you mean `Color.getRGB(dialogColorRed, dialogColorGreen, dialogColorBlue)`? (`getRGB` instead of `rgb`) – Ole V.V. Dec 12 '16 at 12:33
  • @OleV.V. No. I mean that I'm storing color int with method Color.rgb and if i want to get that color then the program returns me a negative integer. I need to transform this integer into color value "color":"4278255360" - this style. Or another method to store the color – Oleg Misko Dec 12 '16 at 12:39
  • 2
    I still don't see a problem. Negative values will be represented with the same bit pattern representing the same color when parsed back to a 32-bit int. – laalto Dec 12 '16 at 12:42
  • Funny. I asked because my `java.awt.Color` class doesn’t have an `rgb` method. Sounds like there’s something I haven’t understood about your question. – Ole V.V. Dec 12 '16 at 12:42
  • What happens if you just leave the negative value in there and pretend everything is good? Does something go wrong? What? – Ole V.V. Dec 12 '16 at 12:44
  • Do you need the alpha channel of your colour? If not, you may do `mySignedColorInt & 0xFFFFFF` to obtain a non-negative int that represents your colour. – Ole V.V. Dec 12 '16 at 12:47
  • @OleV.V. Yes. The program can't parse the JSON with such color values. – Oleg Misko Dec 12 '16 at 12:47
  • @OleV.V. it must be an integer representing ARGB in decimal format (4294967040 → 0xFFFFFF00 → A:FF R:FF G:FF B:00 – Oleg Misko Dec 12 '16 at 12:48
  • Next idea: convert it to a `long`. A Java `int` cannot hold 4294967040, but a `long` can. – Ole V.V. Dec 12 '16 at 12:49
  • You may also look into `Integer.toUnsignedString(int)`. `Integer.toUnsignedString(-6508994)` yields `4288458302`. – Ole V.V. Dec 12 '16 at 13:21

1 Answers1

0

Hi quite late but have you tried, private long key; instead of private int key; Explanation : The key you use to store the color code may be bigger than an integer can hold.

The valid range of an Integer variable is -2147483648 through +2147483647.

                       2147483647 MAX

And you want this number : 4278255360. When an Integer hit the max value, 2147483647 + 1 = -2147483648, it's new value is now the min value.

Benoit
  • 1