読者です 読者をやめる 読者になる 読者になる

Daily Creative Coding

元「30 min. Processing」。毎日、Creative Codingします。

色相 ± 60°

/**
* hue +- 60
*
* @author aa_debdeb
* @date 2016/04/04
*/

color c0, c1, c2, cbg;
ArrayList<Ball> balls;

void setup(){
  size(640, 640);  
  colorMode(HSB, 360, 100, 100);
  noStroke();
  mousePressed();
  
  balls = new ArrayList<Ball>();
}

void mousePressed(){
  cbg = color(random(360), 0, random(100));
  float h = random(360);
  float s = random(30, 100);
  float b = random(40, 100);
  c0 = color(h, s, b);
  c1 = color(h + 60 < 360 ? h + 60: h + 60 - 360, s, b);
  c2 = color(h - 60 > 0 ? h - 60: h - 60 + 360, s, b);
}

void draw(){
  background(cbg);
  for(Ball ball: balls){
    ball.update();
    ball.draw();
  }
  
  ArrayList<Ball> newBalls = new ArrayList<Ball>();
  for(Ball ball: balls){
    if(ball.isShown()){
      newBalls.add(ball);
    }
  }
  balls = newBalls;
  
  if(random(1) < 0.2){
    balls.add(new Ball());
  }
  
}

class Ball {
  
  PVector position;
  int type;
  float radious;
  
  Ball(){
    float r = random(1);
    if(r < 0.6){
      type = 0;
    } else if(r < 0.8){
      type = 1;
    } else {
      type = 2;
    }
    if(type == 0){
      radious = map(random(1), 0, 1, 10, 30);
    } else {
      radious = map(random(1), 0, 1, 5, 15);      
    }
    this.position = new PVector(random(-radious, width + radious), - radious);

  }
  
  void update(){
    position.y += 2; 
  }
  
  void draw(){
    switch(type){
      case 0:
        fill(c0);
        break;
      case 1:
        fill(c1);
        break;
      case 2:
        fill(c2);
        break;
    }
    ellipse(position.x, position.y, radious * 2, radious * 2);
  }
  
  boolean isShown(){
    if(position.y < height + radious){
      return true;
    } else {
      return false;
    }
  }
}