Let It Snow

I made a program that will simulate a snowy environment around you!
snowgenvideo

Press [SPACE] to begin, it will draw a blue background behind you and as long as you hold down the space bar, snow will fall.

Press ‘r’ to reset the blue background.

A challenge I faced was keeping the blue background off of the foreground. For some reason if I kept making the background blue instead of once in a while, the person in the foreground becomes blue as well. I think it might just be my computer I am not sure.

I placed three different types of snowflake images in my /data folder and the Snowpic class randomly chooses which one to be

The three snowflake images:

snowflake2 snowflake3 snowflake1

Code:

SnowGenerator

import processing.video.*;
Capture video;
PImage backgroundImage;
boolean first = true;
ArrayList<Snowpic> snows = new ArrayList();
// How different must a pixel be to be a foreground pixel
float threshold = 20;
int count = 0;
void setup() {
  size(640, 480);
  video = new Capture(this, width, height);
  video.start();
  // Create an empty image the same size as the video
  backgroundImage = createImage(video.width, video.height, RGB);
}

void captureEvent(Capture video) {
  // Read image from the camera
  video.read();
}

void draw() {
  //background(1);
  loadPixels();
  video.loadPixels(); 
  backgroundImage.loadPixels();
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width; // Step 1, what is the 1D pixel location
      color fgColor = video.pixels[loc]; // Step 2, what is the foreground color

      // Step 3, what is the background color
      color bgColor = backgroundImage.pixels[loc];

      // Step 4, compare the foreground and background color
      float r1 = red(fgColor);
      float g1 = green(fgColor);
      float b1 = blue(fgColor);
      float r2 = red(bgColor);
      float g2 = green(bgColor);
      float b2 = blue(bgColor);
      float diff = dist(r1, g1, b1, r2, g2, b2);

      // Step 5, Is the foreground color different from the background color
      if (diff > threshold) {
        // If so, display the foreground color
        pixels[loc] = fgColor;
      } else {
        //pixels[loc] = bgColor;
        // If not, display green
        pixels[loc] = color(#19325A);
      }
    }
  }
  updatePixels();
  for (int i = 0; i < snows.size(); i++) {
    snows.get(i).draw();
    if (snows.get(i).isDone()) {
      snows.remove(i);
    }
  }
  //updatePixels();
}

void keyPressed() {
  if (key == ' ') {
    count++;
    Snowpic sp = new Snowpic(int(random(0, 640)), 0);
    snows.add(sp);
  }
  if (count == 1 || key == 'r') {
    backgroundImage.copy(video, 0, 0, video.width, video.height, 0, 0, video.width, video.height);
    backgroundImage.updatePixels();
  }
}

SnowPic:

class Snowpic {
  String[] snowflakes = {"snowflake1.png", "snowflake2.png", "snowflake3.png"};
  PImage img;
  int x;
  int y;
  boolean done;

  Snowpic(int x, int y) {
    this.x = x;
    this.y = y;
    done = false;
    img = loadImage(snowflakes[int(random(0, 3))]);
  }

  void draw() {
    image(img, x, y);
    y+=5;
    if (y>=480) {
      done = true;
    }
  }

  boolean isDone() {
    return done;
  }
}

 

One thought on “Let It Snow”

Leave a Reply