Skip to content

Instantly share code, notes, and snippets.

@atduskgreg
Created September 5, 2012 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atduskgreg/3631038 to your computer and use it in GitHub Desktop.
Save atduskgreg/3631038 to your computer and use it in GitHub Desktop.
Functions to get the color channels of an image
PImage img;
PImage redImg;
PImage greenImg;
PImage blueImg;
void setup(){
size(250*3, 250*2);
img = loadImage("testImage.png");
redImg = loadImage("testImage.png");
greenImg = loadImage("testImage.png");
blueImg = loadImage("testImage.png");
redImg.loadPixels();
redImg.pixels = red(redImg);
redImg.updatePixels();
greenImg.loadPixels();
greenImg.pixels = green(greenImg);
greenImg.updatePixels();
blueImg.loadPixels();
blueImg.pixels = blue(blueImg);
blueImg.updatePixels();
}
void draw(){
background(0);
image(img,250,0, 250, 250);
image(redImg, 0, 250, 250, 250);
image(greenImg, 250, 250, 250, 250);
image(blueImg, 500, 250, 250,250);
}
int[] red(PImage i){
int[] result = new int[i.width * i.height];
for(int p = 0; p<(i.width * i.height); p++){
color c = color(i.pixels[p]);
result[p] = color(c >> 16 & 0xff);
}
return result;
}
int[] green(PImage i){
int[] result = new int[i.width * i.height];
for(int p = 0; p<(i.width * i.height); p++){
color c = color(i.pixels[p]);
result[p] = color(c >> 8 & 0xff);
}
return result;
}
int[] blue(PImage i){
int[] result = new int[i.width * i.height];
for(int p = 0; p<(i.width * i.height); p++){
color c = color(i.pixels[p]);
result[p] = color(c & 0xff);
}
return result;
}
@atduskgreg
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment