public class Monochrome extends Painting{
PImage grey;
PImage sat;
PImage rand;
Monochrome(PImage p){
work = p;
clone = 2;
grey = createImage(p.width, p.height, RGB);
sat = createImage(p.width, p.height, RGB);
rand = createImage(p.width, p.height, RGB);
}
//Make everything grayscale first
/* According to Wikipedia the formula to convert is as follows:
30% of red, 59% green, 11% blue
needs to be gamma compressed???
*/
void grayscale(){
work.loadPixels();
grey.loadPixels();
for(int i = 0; i< work.pixels.length; i++){
color cp = work.pixels[i];
color cpp = (int)(.30*red(cp)+ .59 *green(cp)+ .11*blue(cp));
grey.pixels[i] = color(cpp);
}
grey.updatePixels();
}
//then saturate things to either black, mid-gray, or white
void saturate(){
grayscale();
grey.loadPixels();
sat.loadPixels();
for(int i = 0; i< grey.pixels.length; i++){
color cp = grey.pixels[i];
int cpp;
if(cp >= 0 && cp< 86){
cpp= 0;
}
else if( cp>= 86 && cp<170){
cpp= 128;
}
else{
cpp= 255;
}
sat.pixels[i] = color(cpp);
}
sat.updatePixels();
}
//then color it in with a random color
void ink(){
}
void display(){
background(120);
image(work, 0, 0, width/clone,height/clone);
grayscale();
image(grey, width/clone, 0, width/clone, height/clone);
saturate();
image(sat, 0, height/clone, width/clone, height/clone);
}
}
Processing Forum