I've played with this a little. But as i'm not a programer and totally self learning in processing i always avoid sharing my code cause
it may be dumb, wrong or bad practice. Said that, here goes a little color reader class i started to develop.
It will just read a given image and coordinates color. You will have to change the image name for an image you have in the data folder of your sketch. This would be a start for your sketch, You than test the color to see if it's black and draw yours squares. Here it is reading the pixel under mouse. You would have to make a two dimension array to read the entire image (could do with a one dimension also, but is less easy to visualize) some thing like: ColorReader [][] cr = new ColorReader()[width][height];
Then initialize it for the pixels in your image using two nested for loops. The class it self may contain a display method to draw yours squares if color is black. To get air between them I would skip some lines and columns like;
for(int i=0; i<array.length;i+=6) note you r counting in steps of 6 so if your square is smaller than that it won't overlap.
Hope it helps.
PImage img;
ColorReader cr;
void setup(){
img = loadImage("band.jpg");
size(img.width,img.height);
}
void draw(){
cr = new ColorReader(img,mouseX,mouseY);
println(hex(cr.getColor())+" ___ "+cr.getRed()+"/"+cr.getGreen()+"/"+cr.getBlue());
background(cr.getColor());
}
//// class
class ColorReader{
PImage img;
int xpos,ypos;
ColorReader(){}
ColorReader (PImage _img, int _xpos, int _ypos ){
img = _img;
xpos = _xpos;
ypos = _ypos;
}
color getColor (){
loadPixels();
int x = mouseX;
int y = mouseY;
color c = img.pixels[y*width+x]; /// this y*width+x is converting from two to one dimension...
return c;
}
color getRed (){
return getColor() >> 16 & 0xFF; // those strange are fast way of get(x,y) read the get reference
}
color getGreen (){
return getColor() >> 8 & 0xFF;
}
color getBlue (){
return getColor() & 0xFF;
}
}
//////// THIS CODE ABOVE END HERE!!! Below is another code ;-)
Also there is this "before turning into a class code" that may be easier to understand, but is the same thinking:
PImage img;
void setup(){
img = loadImage("band.jpg");
size(img.width,img.height);
}
void draw(){
loadPixels();
int x = mouseX;
int y = mouseY;
color c = img.pixels[y*width+x];
color r = c >> 16 & 0xFF;
color g = c >> 8 & 0xFF;
color b = c & 0xFF;
println(r+"/"+g+"/"+b);
}