日常の進捗

主に自分のための,行為とその習慣化の記録

Mod: Generative Design / P_1_2_3_01

f:id:takawo:20180101191552g:plain

コード

import generativedesign.*;
import processing.pdf.*;
import java.util.Calendar;

boolean savePDF = false;

int tileCountX = 100;
int tileCountY = 50;

float[] hue = new float[tileCountX];
float[] sat = new float[tileCountX];
float[] bri = new float[tileCountX];

void setup() {
  size(960, 540);
  colorMode(HSB, 360, 100, 100, 100);
  noStroke();
  shakeColors();
}

void mousePressed() {
  shakeColors();
}
void shakeColors() {
  for (int i = 0; i < tileCountX; i++) {
    hue[i] = random(360);
    sat[i] = random(100);
    bri[i] = random(100);
  }
}


void draw() {
  if (savePDF) {
    beginRecord("PDF", timestamp() + ".pdf");
    colorMode(HSB, 360, 100, 100, 100);
  }
  background(0, 0, 100);
  int counter = 0;

  int currentTileCountX = int(map(mouseX, 0, width, 1, tileCountX));
  int currentTileCountY = int(map(mouseY, 0, height, 1, tileCountY));
  float tileWidth = width/ (float)currentTileCountX;
  float tileHeight = height/ (float)currentTileCountY;

  for (int gridY = 0; gridY < tileCountY; gridY++) {
    for (int gridX = 0; gridX < tileCountX; gridX++) {
      float posX = tileWidth * gridX;
      float posY = tileHeight * gridY;
      int index = counter%currentTileCountX;

      fill(hue[index], sat[index], bri[index]);
      rect(posX, posY, tileWidth, tileHeight);
      counter++;
    }
  }

  if (savePDF) {
    savePDF = false;
    endRecord();
  }
}


void keyReleased() {
  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == 'p' || key == 'P') savePDF = true;
  if (key == 'c' || key == 'C') {
    color[] colors = new color[hue.length];
    for (int i=0; i<hue.length; i++) {
      colors[i] = color(hue[i], sat[i], bri[i]);
    }
    GenerativeDesign.saveASE(this, colors, timestamp()+".ase");
  }
  if (key == '1') {
    for (int i=0; i<hue.length; i++) {
      hue[i] =   random(360);
      sat[i] = (int) random(100);
      bri[i] = (int) random(100);
    }
  }
  if (key == '2') {
    for (int i=0; i<hue.length; i++) {
      hue[i] = (int) random((i*360/hue.length)%360);
      sat[i] = random(100);
      bri[i] = 100;
    }
  }
  if (key == '3') {
    for (int i=0; i<hue.length; i++) {
      hue[i] = (int) (i*360/hue.length);
      sat[i] = random(100);
      bri[i] = random(100);
    }
  }

  if (key == '4') {
    float s = random(100);
    float b = random(100);
    for (int i=0; i<hue.length; i++) {
      hue[i] = random(360);
      sat[i] = s;
      bri[i] = b;
    }
  }
  if (key == '5') {
    float h = random(360);
    float b = random(100);
    for (int i=0; i<hue.length; i++) {
      hue[i] = h;
      sat[i] = random(100);
      bri[i] = b;
    }
  }
  if (key == '6') {
    float h = random(360);
    float s = random(100);
    for (int i=0; i<hue.length; i++) {
      hue[i] = h;
      sat[i] = s;
      bri[i] = random(100);
    }
  }

  if (key == '7') {
    for (int i=0; i<hue.length; i++) {
      float freq = map(i, 0, hue.length, 0, TWO_PI);
      hue[i] = map(sin(freq), -1, 1, 0, 360);
      sat[i] = 100;
      bri[i] = 100;
    }
  }
  if (key == '8') {
    for (int i=0; i<hue.length; i++) {
      float freq = map(i, 0, hue.length, 0, TWO_PI);
      hue[i] = random(360);
      sat[i] = 100;
      bri[i] =  map(sin(freq), -1, 1, 0, 100);
    }
  }

  if (key == '9') {
    float h = random(360);
    float s = random(100);
    float b = random(100);
    for (int i=0; i<hue.length; i++) {
      if (i%2 == 0) {
        sat[i] = s;
        hue[i] = h;
      } else {
        hue[i] = (h+180)%360;
        bri[i] = (b+50)%100;
      }
    }
  }
}


String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}

リファレンス