Java - Decimal color to RGB color

12,911

Solution 1

You could do

Color color = new Color(16777215);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();

Solution 2

You where telling right: RGB values are encoded as bytes in a int. R is byte 2, G is byte 1 and B is byte 0, summing up to a 24bit color depth. Depending on the endianess, this could be a possible representation.

00000000 00000000 00000000 00000000  <-- 32bit int
         ^             ^      ^
         |             |      |
         +--red here   |      +--green here
              8bit     |            8bit
                       |
                       +--blue here
                             8bit

You can extract RGB values with some bit shift and masking:

int red = (color >> 16) & 0xff;
int green = (color >> 8) & 0xff;
int blue = color & 0xff;

Solution 3

Decimal, hexadecimal: does not matter. Your computer uses binary representations internally.

You may have noticed that 0xFF00 == 65280.

Decimal and Hexadecimal are user representations.

Solution 4

You can get the channels out by simple bitwise operations.

int r = (color >> 16) & 0xff;
int g = (color >> 8)  & 0xff;
int b = color & 0xff;

From there, it should be easy to do whatever you want with the color information.

Share:
12,911
Alex
Author by

Alex

Eh work. Why can't I go play games now.

Updated on June 14, 2022

Comments

  • Alex
    Alex almost 2 years

    I have a decimal (not hexadecimal) color code and, using Java, I need to convert it to the three RGB colors.

    So for example, 16777215 (pure white) needs to get converted to Red: 255 Green: 255 Blue: 255.
    65280 (pure green) needs to get converted to Red: 0 Green 255: Blue: 0

    Here is a converter for more examples.


    Just doing some small calculations and playing with the calculator on the page linked above, I have determined:
    • Red equals 65536 (256^2)
      • (255x65536 = 16711680, aka pure red)
    • Green equals 256 (256^1)
      • (255x256 = 65280, aka pure green)
    • Blue equals 1 (256^0)
      • (255x1 = 255, aka pure blue)

    I can tell it obviously has something to do with bytes, but I am missing that last little bit. I am not the best with the whole concept of bits/bytes/etc and how it interacts with Java, so it is likely fairly simple.

    So, anyone know the best way of going about this? What would be the best way to convert a single numerical decimal color into the three separate RGB values using java?

  • Jems
    Jems over 10 years
    Java has a straight-forward Color constructor taking the single int that works too.
  • Alex
    Alex over 10 years
    This is exactly what I needed. Did not know the whole Color thing existed. Thank you very much!
  • Alex
    Alex over 10 years
    This seems like it would work for what I needed, and much better information, but the Color thing Reimeus posted does exactly what I need and looks much more simple. Thanks very much, wish I could select both as best answer
  • Stefano Sanfilippo
    Stefano Sanfilippo over 10 years
    Yeah I agree. I did not delete this because the ASCII art might help you understand how RGB values have been packed in the number you have. You should definitely go @Reimeus way.
  • Jørgen
    Jørgen almost 7 years
    Great answer! It can be used in multiple programming languages with the same solution. Even when there is no Color object available to do this.