for (int p : img.pixels)
in
Programming Questions
•
1 year ago
Hi, I've seen this line: for(int p: img.pixels) in a code. I´ve never seen this kind of for before. I´ve always seen the for (;;) kind. It seems p takes all the picture's pixels values, but I would like to see in general the way of using this for(:) structure
- PImage picture;
- Integer[] palette;
- void setup(){
- size(466, 520);
- background(0);
- picture = loadImage("kadinsky.jpeg");
- image(picture,0,0);
- palette = getPalette(picture);
- for(int i = 0; i < palette.length; i++){
- set(i % width, 330 + (i / width), palette[i]);
- }
- fill(255);
- text("Number of pixels in image = " + picture.width * picture.height, 10, height - 40);
- text("Number of unique colours = " + palette.length, 10, height - 20);
- }
- Integer[] getPalette(PImage img){
- HashSet<Integer> colours = new HashSet<Integer>();
- img.loadPixels();
- for(int p : img.pixels){
- colours.add(new Integer(p));
- }
- Integer[] p = colours.toArray(new Integer[colours.size()]);
- Arrays.sort(p);
- return p;
- }
1