/**
 * Brightness Thresholding 
 * by Golan Levin. 
 * 
 * Determines whether a test location (such as the cursor) is contained within
 * the silhouette of a dark object. 
 */


/** 
 * Serial Call-Response 
 * by Tom Igoe. 
 * 
 * Sends a byte out the serial port, and reads 3 bytes in. 
 * Sets foregound color, xpos, and ypos of a circle onstage
 * using the values returned from the serial port. 
 * Thanks to Daniel Shiffman for the improvements. 
 */
 
/// All this code Meshed together by Oscar G. Torres


import processing.video.*;
import processing.serial.*;

Serial port;                         // The serial port
int serialM= 0;

// Size of each cell in the grid
int cellSize = 2;
// Number of columns and rows in our system
int cols, rows;

PImage img;  

color black = color(0);
color white = color(255);
int numPixels;
int numPixelsB;
Capture video;
Capture videoB;

int bitchSlapCt;
float carX, carY;
int ctA;


void setup() {
  size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
  // Uses the default video input, see the reference if this causes an error
  video = new Capture(this, width, height);
  //videoB = new Capture(this, width, height);
  numPixels = video.width * video.height;
  //numPixelsB = videoB.width* videoB.height;
  ctA=0;
  bitchSlapCt=0;

  cols = width / cellSize;
  rows = height / cellSize;

  colorMode(RGB, 255, 255, 255, 100);
  
  img = loadImage("flowerB.jpg");     // Load the image!!!!!!
  //COLS = img.width/cellsize;            // Calculate # of columns
//ROWS = img.height/cellsize;           // Calculate # of rows

  // Print a list of the serial ports, for debugging purposes:
  // println(Serial.list());

  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // On Windows machines, this generally opens COM1.
  // Open whatever port is the one you're using.
  port = new Serial(this, Serial.list()[0], 9600);
  port.write('L');    // Send a capital A to start the microcontroller sending


}


void mousePressed() {
  print( red(video.pixels[ (video.width - mouseX - 1) + mouseY*video.width ]) );
  print( green(video.pixels[ (video.width - mouseX - 1) + mouseY*video.width ]) );
  print( blue(video.pixels[ (video.width - mouseX - 1) + mouseY*video.width ]) );

}

void draw() {

  int trackedX = 0; // X-coordinate of the brightest video pixel
  int trackedY = 0; // Y-coordinate of the brightest video pixel
  int index = 0;
  float brightestValue = 0;

  /////////////// Color Capture ///////////////

  if (video.available()) {
    video.read();
    video.loadPixels();
   // videoB.read();
    //videoB.loadPixels();
    // Turn each pixel in the video frame black or white depending on its brightness
    //loadPixels();

    for (int i = 0; i < cols; i++) {
      // Begin loop for rows
      for (int j = 0; j < rows; j++) {

        // Where are we, pixel-wise?
        int x = i*cellSize;
        int y = j*cellSize;
        int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
       
        //int locB = (img.width - x - 1) + y*img.width; // Reversing x to mirror the image

        //float r = red(video.pixels[loc]);
        // float g = green(video.pixels[loc]);
         //float b = blue(video.pixels[loc]);
        // float br = round(brightness(video.pixels[loc]));
        // Make a new color with an alpha component

        //  int pixelValue =round(brightness(video.pixels[loc]));
        float pixelBrightness =brightness(video.pixels[loc]);

        if (pixelBrightness > brightestValue) {
          brightestValue = pixelBrightness;
          trackedY = y;
          trackedX = x;
          /////Original Brightness Tracker
          carY = trackedY;
          carX = trackedX;
        }
       
        //color c = color(r, g, b, 100);
        // Code for drawing a single rect
        rectMode(CENTER);
        fill(pixelBrightness);
        noStroke();
        // Rects are larger than the cell for some overlap
        rect(x+cellSize/2, y+cellSize/2, cellSize, cellSize);

        //  float r = red(img.pixels[locB]);
       //  float g = green(img.pixels[locB]);
       //  float b = blue(img.pixels[locB]);
        //println(rr);
//int bbr = color(0,0);
        // Make a new color with an alpha component

      //  if (r < 48 && g < 90 && b < 140 ){         //    Threshhold Adjustment 
     //     bbr=  color(0,0,0,100);
      //    if (x == carX && y == carY){
            
      //       port.write(5);              // send an H to indicate mouse is over square
            // delay(200);
      
           // println("I got Hit on!"+ bitchSlapCt);
    //      }
    //    }
     //  else{
     //     bbr=  color(100,100,0,0);
          //port.write(1);    // Send a capital A to start the microcontroller sending
    //    }
        //color c = color(r, g, b, 75);
        //color cB = bbr;
        // Code for drawing a single rect
    //    rectMode(CENTER);
    //    fill(bbr);
     //   noStroke();
    //    // Rects are larger than the cell for some overlap
       // rect((x+cellSize/2), (y+cellSize/2), cellSize, cellSize);

      }
    }
    
      for (int i = 0; i < cols; i++) {                ///////////////////////////////////////
      // Begin loop for rows
      for (int j = 0; j < rows; j++) {

        // Where are we, pixel-wise?
        int x = i*cellSize;
        int y = j*cellSize;
        //int loc = (video.width - x - 1) + y*video.width; // Reversing x to mirror the image
       
        int locB = (img.width - x - 1) + y*img.width; // Reversing x to mirror the image

        //float r = red(video.pixels[loc]);
        // float g = green(video.pixels[loc]);
         //float b = blue(video.pixels[loc]);
        // float br = round(brightness(video.pixels[loc]));
        // Make a new color with an alpha component

        //  int pixelValue =round(brightness(video.pixels[loc]));
      //  float pixelBrightness =brightness(video.pixels[loc]);

    //    if (pixelBrightness > brightestValue) {
     //     brightestValue = pixelBrightness;
    //      trackedY = y;
     //     trackedX = x;
          /////Original Brightness Tracker
     //     carY = trackedY;
      //    carX = trackedX;
    //    }
       
        //color c = color(r, g, b, 100);
        // Code for drawing a single rect
     //   rectMode(CENTER);
     //   fill(pixelBrightness);
     //   noStroke();
        // Rects are larger than the cell for some overlap
     //   rect(x+cellSize/2, y+cellSize/2, cellSize, cellSize);

          float r = red(img.pixels[locB]);
         float g = green(img.pixels[locB]);
         float b = blue(img.pixels[locB]);
        //println(rr);
        int bbr = color(0,0);
        // Make a new color with an alpha component

        if (r > 80 && g <150 && b > 150 ){         //    Threshhold Adjustment 
          bbr=  color(20,140,190,100);
          if (x == carX && y == carY){
            
             port.write(5);              // send an H to indicate mouse is over square
            // delay(200);
      
           // println("I got Hit on!"+ bitchSlapCt);
          }
        }
        else{
          bbr=  color(100,100,0,0);
          //port.write(1);    // Send a capital A to start the microcontroller sending
        }
        //color c = color(r, g, b, 75);
        //color cB = bbr;
        // Code for drawing a single rect
        rectMode(CENTER);
        fill(bbr);
        noStroke();
        // Rects are larger than the cell for some overlap
        rect((x+cellSize/2), (y+cellSize/2), cellSize, cellSize);

      }
    }

  }

  //updatePixels();  ///??????
  fill(255);
  ellipse(carX, carY, 10, 10);

}



