Grim Reaper
Grim Reaper

Reputation: 561

3 Digit Color Code Java

I started learning java game programming and I am doing it by watching Notch's code being explained by a guy on YouTube, so I need help with this video:

https://www.youtube.com/watch?v=7eotyB7oNHE&list=PL8CAB66181A502179&index=5

He implemented colors in his game, and so did I, but I don't understand how they work. He made a function get in the colours class and it is called like this:

Colours.get(colour1, colour2, colour3, colour4);

He is using a spritesheet to do this, he will replace the black color with the "colour1", the dark gray colour with "colour2", light gray with "colour3" and white with "colour4". The problem is that I don't understand the following: how can I get 3 digit colors without using letters?

Thanks!

Upvotes: 1

Views: 439

Answers (1)

ethanfar
ethanfar

Reputation: 3778

He's using the int representation of colors.
Each color is represented by 4 values of 8 bits each:

  • Red value
  • Green value
  • Blue value
  • Alpha value

The int representation packs those 8-bit values into a single 32-bit int number so that the alpha value gets the highest bits, then the red value, then the green and finally, the blue value gets the lowest bits. Therefore, using bit-wise operations, you can create the int value out of the color component values as follows:

public static int getColorIntRepresentationOutOfColorComponentByteValues(byte alpha, byte red, byte green, byte blue) {
    return ((int)alpha << 24) | ((int)red << 16) | ((int)green << 8) | (int)blue;
}

Upvotes: 1

Related Questions