0

I know a formula to convert a colour to its RGB. Eg.

Color c=new Color(100,100,100);
Int rgb1= c.getRed()*65536+c.getGreen()*256+c.getBlue();

This is how we get single RGB value.

Using java programming

int rgb2=c.getRGB();

when I compare this two rgb1 and rgb2, they show a different value. What's wrong in my first equation. Please kindly solve my problem. Thanks

NEKIBUR RAHMAN
  • 204
  • 3
  • 15
  • What does getRead() return? – Scott Hunter Apr 15 '17 at 15:41
  • Sorry its getRed(). I have corrected it. – NEKIBUR RAHMAN Apr 15 '17 at 15:42
  • As a side note, a left-shift `a << b` is mathematically equivalent to a multiplication `a * 2^b`. It would be more idiomatic to do those multiplications as left-shifts since you are working with packed bytes rather than an actual number. `(red << 16) | (green << 8) | blue` – Radiodef Apr 15 '17 at 15:58

1 Answers1

1

That because the first way ignores the Alpha value of the color.
You can see that c.getAlpha() returns 255 and you don't use this value, but c.getRGB(); does use it, so it returns FF646464 (in hex base), which is different from 646464.

TDG
  • 5,909
  • 3
  • 30
  • 51