User Tools

Site Tools


cs276:modifycolors

This sketch includes a function decreaseRed which is hardcoded to decrease the red component of each pixel of an image by 50%.

To run this sketch you'll need either a copy of the JPEG file used in the loadImage call or replace it with one of your own.

  import java.awt.*;
 
  PImage pi;
 
  void setup() {
    Color c = Color.black;
    println(c);
    c =   Color.white;
    println(c);
 
    size(800, 800);
    pi = loadImage("myImage.jpg");
    image(pi, 0, 0);
    noLoop();
  }
 
  void draw() {
    decreaseRed(pi);
    image(pi, 0, 350);
  }
 
  void decreaseRed(PImage pi) {
    float value = 0; 
    color c;
    pi.loadPixels();
    for (int x = 0; x < pi.width; x++) {
      for (int y = 0; y < pi.height; y++) { 
        c = pi.get(x, y);
        value = red(c);
        value *= 0.5;
        c = color(value, green(c), blue(c));
        pi.set(x,y,c);
      }
    }
    pi.updatePixels();
  }
cs276/modifycolors.txt · Last modified: 2008/09/08 16:26 by scarl