Animated Gradient Colour Responder

edited February 2017 in Library Questions

This project needs an animated gradient, responding to an ambient colour every 5 seconds.

I am having difficulty getting the gradient animation to respond to color as seen by the laptop camera.

The animated gradient loop runs on a laptop. Every 5 seconds or so, the webcam will sample the ambient colour using the camera. What I am trying is to have the gradient colour update to that colour sampled. and then continue with the cycling of colours from there.

Any suggestions greatly appreciated.

    import processing.video.*;
    Capture cam;

    int Y_AXIS = 1;

    //Gradient colors
    color c1, c2;

    float rd1=100;
    float gr1=100;
    float bl1=255;

    float rd2=100;
    float gr2=255;
    float bl2=100;

    float rd1Change = 0.1; 
    float gr1Change = 0.2; 
    float bl1Change = 0.3; 

    float rd2Change = 0.3; 
    float gr2Change = 0.2; 
    float bl2Change = 0.1; 


    int get=1;

    int timer = 0;


    void setup() {
      size(1200, 800);  
      noStroke();

      //webcam
      String[] cameras = Capture.list();
      cam = new Capture(this, cameras[0]);
      cam.start();
    }

    void draw() {
      //go cam
      if (cam.available() == true) {
        cam.read();
      }
      //draw cam
      image(cam, 0, 0, width, height);

      //get the color
      get = cam.get(width/2, 50); 

      // set the gradient colors
      c1 = color(rd1, gr1, bl1);
      c2 = color(rd2, gr2, bl2);


      // color rotates - top
      rd1=rd1+ rd1Change;
      if (rd1 < 100 || rd1 > 255) {
        rd1Change *= -1;
      }

      gr1=gr1+ gr1Change;
      if (gr1 < 100 || gr1 > 255) {
        gr1Change *= -1;
      }


      bl1=bl1+ bl1Change;
      if (bl1 < 100 || bl1 > 255) {
        bl1Change *= -1;
      }


      // color rotates - bottom
      rd2=rd2+ rd2Change;
      if (rd2 < 100 || rd2 > 255) {
        rd2Change *= -1;
      }

      gr2=gr2+ gr2Change;
      if (gr2 < 100 || gr2 > 255) {
        gr2Change *= -1;
      }

      bl2=bl2+ bl2Change;
      if (bl2 < 100 || bl2 > 255) {
        bl2Change *= -1;
      }

      // make gradient

      setGradient(100, 100, width-200, height-200, c1, c2, Y_AXIS);
    }

    void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {

      if (axis == Y_AXIS) {  // Top to bottom gradient
        for (int i = y; i <= y+h; i++) {
          float inter = map(i, y, y+h, 0, 1);
          color c = lerpColor(c1, c2, inter);
          stroke(c);
          line(x, i, x+w, i);

          // Timer
          if (millis()-timer > 5000) {
            timer = millis();

            // how do I  implement get colour back into gradient cycle?

          }
        }
      }
    }
Tagged:
Sign In or Register to comment.