topical media & game development

talk show tell print

#javascript-processing-example-topic-image-brightness.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Brightness</h2>
  
  <p>by Daniel Shiffman. 
  
  Adjusts the brightness of part of the image
  Pixels closer to the mouse will appear brighter.</p>
  
  <p><a href="http://processing.org/learning/topics/brightness.html"><b>Original Processing.org Example:</b> Brightness</a><br>
  <script type="application/processing">
  PImage img;
  
  void setup() {
    size(33, 33);
    frameRate(30);
    img = loadImage("cait.jpg");
  }
  
  void draw() {
    loadPixels();
    for (int x = 0; x < img.width; x++) {
      for (int y = 0; y < img.height; y++ ) {
        // Calculate the 1D location from a 2D grid
        int loc = x + y*img.width;
        // Get the R,G,B values from image
        float r,g,b;
        r = red (img.pixels[loc]);
        //g = green (img.pixels[loc]);
        //b = blue (img.pixels[loc]);
        // Calculate an amount to change brightness based on proximity to the mouse
        float maxdist = 15;//dist(0,0,width,height);
        float d = dist(x,y,mouseX,mouseY);
        float adjustbrightness = 255*(maxdist-d)/maxdist;
        r += adjustbrightness;
        //g += adjustbrightness;
        //b += adjustbrightness;
        // Constrain RGB to make sure they are within 0-255 color range
        r = constrain(r,0,255);
        //g = constrain(g,0,255);
        //b = constrain(b,0,255);
        // Make a new color and set pixel in the window
        //color c = color(r,g,b);
        color c = color(r);
        pixels[loc] = c;
      }
    }
    updatePixels();
  }
  </script><canvas width="33" height="33"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"><img src="javascript-processing-example-cait.jpg" id="cait.jpg"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  PImage img;
  
  void setup() {
    size(200, 200);
    frameRate(30);
    img = loadImage("wires.jpg");
  }
  
  void draw() {
    loadPixels();
    for (int x = 0; x &lt; img.width; x++) {
      for (int y = 0; y &lt; img.height; y++ ) {
        // Calculate the 1D location from a 2D grid
        int loc = x + y*img.width;
        // Get the R,G,B values from image
        float r,g,b;
        r = red (img.pixels[loc]);
        //g = green (img.pixels[loc]);
        //b = blue (img.pixels[loc]);
        // Calculate an amount to change brightness based on proximity to the mouse
        float maxdist = 50;//dist(0,0,width,height);
        float d = dist(x,y,mouseX,mouseY);
        float adjustbrightness = 255*(maxdist-d)/maxdist;
        r += adjustbrightness;
        //g += adjustbrightness;
        //b += adjustbrightness;
        // Constrain RGB to make sure they are within 0-255 color range
        r = constrain(r,0,255);
        //g = constrain(g,0,255);
        //b = constrain(b,0,255);
        // Make a new color and set pixel in the window
        //color c = color(r,g,b);
        color c = color(r);
        pixels[loc] = c;
      }
    }
    updatePixels();
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.