Define a function to call another function

edited May 2016 in Questions about Code

How's possibile to define a function that applies another function in a program of color change?

Answers

  • edited May 2016 Answer ✓

    first I rewrite your first, old version

    PImage imgOriginal=null;
    String selectImg=""; 
    PImage imgCopy;
    
    void setup() {
    
      selectInput("olio", "fileSelected");
    
    
      size(1222, 1222);
    }
    void draw() {
    
      // in draw (which runs again and again 60 times per second),
      // we first need to wait for fileSelected(). 
      // When fileSelected() is done, selectImg will not be == "" 
      // anymore.
      // Then we load the image (which is then not == null anymore). 
      // And then we do the normal situation with mouse. 
    
      if (selectImg!="") {
        // the waiting for fileSelected is over 
    
        if (imgOriginal==null) {
          // load image : this happens once 
          imgOriginal = loadImage(selectImg);
          imgCopy = createImage(imgOriginal.width, imgOriginal.height, imgOriginal.RGB);
          imgOriginal.loadPixels();
        } else {
          // the normal situation with mouse. 
    
          spot(imgOriginal);
        } // else
      }// if
    }// func 
    
    void spot(PImage imgOriginal) {
      for (int y = 0; y < imgOriginal.height; y++ ) {
        for (int x = 0; x < imgOriginal.width; x++ ) { 
          float distance = dist(x, y, mouseX, mouseY);
          int loc = x + y*imgOriginal.width;
          int r = imgOriginal.pixels[loc] >> 16 & 0xFF;
          int g = imgOriginal.pixels[loc] >> 8 & 0xFF;
          int b = imgOriginal.pixels[loc] & 0xFF;
          int greyScale=color(max(r, g, b));
          if (distance<50) {
            imgCopy.pixels[loc] = greyScale;
          } else {
            imgCopy.pixels[loc] = imgOriginal.pixels[loc];
          }
        }
      }
      imgCopy.updatePixels();
      image(imgCopy, 0, 0);
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        selectImg=selection.getAbsolutePath();
      }
    }
    
  • Answer ✓

    and here comes the 2nd version with a new function that returns a gray value

    PImage imgOriginal=null;
    String selectImg=""; 
    PImage imgCopy;
    
    void setup() {
    
      selectInput("olio", "fileSelected");
    
    
      size(1222, 1222);
    }
    void draw() {
    
      // in draw (which runs again and again 60 times per second),
      // we first need to wait for fileSelected(). 
      // When fileSelected() is done, selectImg will not be == "" 
      // anymore.
      // Then we load the image (which is then not == null anymore). 
      // And then we do the normal situation with mouse. 
    
      if (selectImg!="") {
        // the waiting for fileSelected is over 
    
        if (imgOriginal==null) {
          // load image : this happens once 
          imgOriginal = loadImage(selectImg);
          imgCopy = createImage(imgOriginal.width, imgOriginal.height, imgOriginal.RGB);
          imgOriginal.loadPixels();
        } else {
          // the normal situation with mouse. 
          spot(imgOriginal);
        } // else
      }// if
    }// func 
    
    void spot(PImage imgOriginal) {
      for (int y = 0; y < imgOriginal.height; y++ ) {
        for (int x = 0; x < imgOriginal.width; x++ ) { 
    
          // we calculate the position of the pixel in the 1D array pixels
          int loc = x + y*imgOriginal.width;
          // calclate the distance to mouse
          float distance = dist(x, y, mouseX, mouseY);
    
          // when around the mouse....      
          if (distance<50) {
            // use gray value 
            imgCopy.pixels[loc] = grayFromColor (imgOriginal.pixels[loc]);
          } else {
            // far from mouse just use color original
            imgCopy.pixels[loc] = imgOriginal.pixels[loc];
          }
        }
      }
      imgCopy.updatePixels();
      image(imgCopy, 0, 0);
    }
    
    color grayFromColor(color colorValue) {
      // returns a gray value from any given color     
      int r = colorValue >> 16 & 0xFF;
      int g = colorValue >> 8 & 0xFF;
      int b = colorValue & 0xFF;
      int greyScale=color(max(r, g, b));
    
      return greyScale;
    }
    
    void fileSelected(File selection) {
      if (selection == null) {
        println("Window was closed or the user hit cancel.");
      } else {
        println("User selected " + selection.getAbsolutePath());
        selectImg=selection.getAbsolutePath();
      }
    }
    
  • probably better with size() being the 1st line in setup()

  • Great, thank you so much!

  • sure!

    you are welcome!

Sign In or Register to comment.