We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to write a program where the color of the pixel the mouse is over is returned. I'm getting a number back, but what does it mean? black seems to be -16777216 and white returns -1
color c = get(mouseX, mouseY);
println(c);
Answers
Yes, color is just an integer (32 bits in the RAM). Processing repurposed it as a "color" but when you see "color" in Processing it is just an int. The preprocessor even replaces color in your code with int to make it valid java, they are absolutely equal. The first eight bits are alpha, the next eight bits are red, the following eight bits are green and the last eight bits are blue. Try doing println(binary(c)) for various colours to get more familiar with the structure.
The reason white is -1 is because red, green and blue are 255, and 255 is 1111 1111 in binary so you get 1111 1111 1111 1111 1111 1111 1111 1111 which would be -1 I believe.
If you truly had black it would be something like 1111 1111 0000 0000 0000 0000 0000 0000 which indeed turns out to be -16777216.
If you want to retrieve the colour components, the easiest way is to do red(c), green(c) and blue(c).
Thank you very much!
BTW, if you add println(binary(c)); right under the previous println, it will print out the binary representation as well. In case your curious. Cheers
This is for the same project and I didn't want to create a new post, so ill ask here. I now know which pixels are a certain color, and want to now send the coordinates of those pixels to an arduino. How do I get the coordinate value of a pixel if pixels[] is one dimensional? Is there something that is like the opposite of get() ? Thanks!
Well, you used two coordinates in the get() method. In the example above that would be mouseX and mouseY. If you managed to find a colour with get() you also have the coordinates of that colour, because you needed those coordinates to find the colour in the first place!
pixels[]
is more or less similar to get() except that pixels[] is one dimensional so if you wanted to find a colour at a certain position (x, y) you would need to do pixels[image.width*y+x].Sorry, I should have explained better. I now have it so that the program automatically goes through all the pixels in the window, and highlights the ones of a color I'm interested in (black pixels, for example, may be highlighted in red, and all other colored pixels are set to white). I now only have the pixels I'm interested in, and want their coordinates to be sent to an arduino. Basically, how can I convert their pixels[] number into x,y coordinates? can this be done?
Also a "Python Mode" version: :D