Using pixels with processing?


#1

I want to make a very simple neural network and I thought I should make it about recognizing patterns with 2x2 pixels. I got a strange output when looking at the colours when I was just setting it up. I’m wondering if it’s a bug or if I made a mistake anywhere.

void setup(){
  size(600, 400);
}

void draw(){
  background(255);
  
  color w = color(255);
  color b = color(0);
  set(1, 1, b);
  set(2, 1, w);
  set(1, 2, w);
  set(2, 2, w);
  
  int px1 = get(1, 1);
  int px2 = get(2, 1);
  int px3 = get(1, 2);
  int px4 = get(2, 2);
  
  print(px1, px2, px3, px4, "\n");
}

What I believe I should have gotten: 0 255 255 255
What I got: -16777216 -1 -1 -1

I know there is another way of doing this but I want to know why it’s doing this.

Thanks for reading and in advance for helping me and anyone else who reads this.


#2

It is not a bug or mistake it is just the way color are stored in the computer.

When you say color(255) you are actually creating a color with red, blue and green channel at a value of 255 so it gives you a white color. The same goes with color(0) except that the 3 channels are at a value of 0.

Now when you ask for the pixel color with get(), you get an integer representing the color as stored in the memory of your computer and thus not in the RGB style as you first set you color with.

This is why you get those strange number and not the 0 or 255. You can get to know more about it on that page: https://processing.org/reference/color_datatype.html

Now you can get that 255 or 0 value by asking to get one of the 3 channels that you set using the red(), blue(), or green() functions. Here is a link on how they work: https://processing.org/reference/red_.html


#3

Processing.org/reference/hex_.html


#4

Oh, so it’s represented in Hex Code but outputted as an int. Thanks!