User Tools

Site Tools


Processing Tutorial

Part 8: Editing the pixels[] array

pixels[] holds a color value for every pixel currently drawn in the system display window. It can be read and modified to change the display. To use this array to modify the display:

  • First call loadPixels() to populate the array (otherwise it will be null).
  • Now you can access and change the array directly.
  • When finished editing, call updatePixels() to commit the changes. This will redraw the screen.

By default, the color values stored in pixels[] are made up of Red, Green and Blue (RGB) values. This is called RGB mode. Each of the Red, Green, and Blue values are 8-bit numbers from 0 to 255:

color c = color(125,125,125);

You can read these values back with the red(), green() and blue() functions. To modify a pixel, you would use something like:

pixels[x] = color(125, green(pixels[x]), blue(pixels[x]));

Note: a common error is to treat a color value as an object. Color values are just 32-bit integers (8 bits for each RGB value plus alpha channel information stored in the extra 8 bits - we'll talk about alpha channels later on), so any code which treats a color as an object will cause a syntax error. Treat color values as the type color in Processing code, and as type int in Java code.

Prev: Part 7- A Few Useful Global Variables | Next: Part 9- A Few Functions for Creating and Manipulating Basic Shapes

part_8-_editing_the_pixels_array.txt · Last modified: 2007/09/13 07:27 by scarl