Color representation
The RGB value is an integer so it is represented in memory by 4 bytes (or equivalently 32 bits).
Example:
00000001 00000010 00000011 00000100
Each byte represents one component of the color :
- 1st byte: alpha value (00000001 in the example) which corresponds to the opacity
- 2nd byte: red value (00000010 in the example)
- 3rd byte: green value (00000011 in the example)
- 4th byte: blue value (00000100 in the example)
0xff and 0xffffff symbols
0xff represents the hexadecimal value FF which is equal to the integer 255. Its binary representation is:
00000000 00000000 00000000 11111111
Similarly 0xffffff is represented by:
00000000 11111111 11111111 11111111
It corresponds to the color white (red, green and blue equal to 255).
& operator
The binary operator and "&" is applied on two integers i1 and i2 (i1 & i2). It returns an integer with all its bits equal to 0 except the ones which are equal to 1 in both i1 and i2.
For example, if we apply & on my first example and on 0xff, we obtain:
00000000 00000000 00000000 00000100
As a consequence, (& 0xff) enables to only keep the values of the last byte (i.e., the value of the blue component of the color).
// If the blue component of image.getRGB(i, j) is greater than b
if ((image.getRGB(i, j) & 0xff) > b) {
// Set the image to white
image.setRGB(i, j, 0xffffff) ;
} else {
// Set the image to black
image.setRGB(i, j, 0x000000);
}