Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a negative number when using getRGB()?

I am new to color stuff, rendering etc. and watching a couple tutorial videos about rendering etc. My question is, when I call a getRGB method on a pixel, it returns a negative int. what is the meaning of this negative number? For example, when i call getRGB on a color with r: 186, g: 186, b: 186, it returns -4539718. How is this number related to its rgb value? I've made a couple of google search but was not successful.

like image 863
Burak Özmen Avatar asked Jul 11 '13 23:07

Burak Özmen


2 Answers

The getRGB method returns an int whose 4 bytes are the alpha, red, green, and blue components in that order. Assuming that the pixel is not transparent, the alpha is 255 (0xFF). It's the most significant byte in the int, and the first bit is set in that value. Because in Java int values are signed according to Two's Complement, the value is actually negative because that first bit is on.

like image 117
rgettman Avatar answered Oct 17 '22 20:10

rgettman


To get the color of a pixel:

Color c = new Color(image.getRGB(10,10));
int red = c.getRed();
int green = c.getGreen();
int blue = c.getBlue();
like image 29
Tudor Avatar answered Oct 17 '22 20:10

Tudor