Technically it's unnecessary to transfer the colors from the pixel array to a color variable or an array of colors, since the pixel array itself is already an array of colors. But I can imagine that for some reason, conceptually or otherwise, you find it more convenient to do this. So I've written some code to answer your question. This example loads an image and then loads all it's colors into an array of colors. It also displays all these colors over the width of the screen. Note it doesn't do anything fancy like check if it's a different color, just a basic example.
Once again, this is technically not really necessary, you could also use the pixel array directly and not use a special array of colors. But given the code example, you could work it out either way. As the code shows, to get a color from the pixel array you use the line
input.pixels[i].
- color [] colors; // array of colors
- PImage input; // source image, I suggest using a selective GIF for small file size
- int cWidth; // width of a color column
- void setup() {
- size(600,400);
- noStroke();
- input = loadImage("input.gif"); // load image
- colors = new color[input.pixels.length]; // set the array size to the number of pixels in the image
- println("There are " + colors.length + " colors in the input image");
- for (int i=0; i<colors.length; i++) {
- colors[i] = input.pixels[i]; // transfer all the colors from the image to the colors array
- }
- cWidth = width / colors.length; // set the width of the color column
- }
- void draw() {
- // draw all the colors over the width of the screen
- for (int i=0; i<colors.length; i++) {
- fill(colors[i]);
- rect(i*cWidth,0,cWidth,height);
- }
- }