• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to extract R, G and B values from an RGB integer?  RSS feed

 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I use Graphics' getColor() method, it returns an integer in the 0x00RRGGBB format.

Does anyone know how I can get the red, green and blue components from that integer?

Thanks,
James
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is a d*mn nuisance that the getRBG() method of Color is labelled inaccurately; it returns ARGB not RGB. That appears to be how it was written, and I can't see there is any way of changing it now.

try:-
myColor.getRed() * 0x10000 + myColor.getGreen() + 0x100 + myColor.getBlue();

I think I have spelt the methods correctly.
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different suggestion:-

If you already have the number, cast it to a long, and try
I have tried the % operator with ints and didn't have any success.

[edit]I forgot the casts to (int)[/edit]
[ April 20, 2007: Message edited by: Campbell Ritchie ]
 
James Hodgkiss
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The second suggestion seems to work fine - many thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!