日常の進捗

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

Mod: Generative Design / P_1_2_1_01

f:id:takawo:20180101111522p:plain

色Aと色Bの中間の色をグラデーションとして補完するもの.カラーパレットのようなものを出力できるようだったが,Processing 3.XだとどうもASEファイルが適切に書き出されないようだ

任意の色AとBの中間色をlerpColor()という関数で取得するのだけど,RGBの色空間でRGBの各成分をXYZのような位置座標としたとき,色ABを結ぶ線分上の任意の点Cが中間色として考えられる.コード内で色AとBを設定する方法の部分などを変更した.

コード

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

boolean savePDF = false;

int tileCountX = 2;
int tileCountY = 50;

color[] colorsLeft = new color[tileCountY];
color[] colorsRight = new color[tileCountY];
color[] colors;

boolean interpolateShortest = true;

void setup() {
  settings();
  colorMode(HSB, 360, 100, 100, 100);
  noStroke();
  shakeColors();
}

void draw() {
  if (savePDF) {
    beginRecord(PDF, timestamp() + ".pdf");
    noStroke();
    colorMode(HSB, 360, 100, 100, 100);
  }

  tileCountX = (int) map(mouseX, 0, width, 2, 100);
  tileCountY = (int) map(mouseY, 0, height, 2, 50);
  float tileWidth = width / (float)tileCountX;
  float tileHeight = height / (float)tileCountY;
  color interColor;

  colors = new color[tileCountX * tileCountY];
  int n = 0;

  for (int j = 0; j < tileCountY; j++) {
    color color1 = colorsLeft[j];
    color color2 = colorsRight[j];
    for (int i = 0; i < tileCountX; i++) {
      float t = map(i, 0, tileCountX-1, 0, 1);
      if (interpolateShortest) {
        colorMode(RGB, 255, 255, 255, 255);
        interColor = lerpColor(color1, color2, t);
        colorMode(HSB, 360, 100, 100, 100);
      } else {
        interColor = lerpColor(color1, color2, t);
      }
      fill(interColor);
      float x = tileWidth * i;
      float y = tileHeight * j;
      rect(x, y, tileWidth, tileHeight);
      colors[n] = interColor;
      n++;
    }
  }

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

void shakeColors() {
  for (int i=0; i<tileCountY; i++) {
    colorsLeft[i] = color(random(0, 360), random(100), random(100));
    float hue = hue(colorsLeft[i]);
    colorsRight[i] = color((hue+random(-180, 180))%360, random(100), random(100));
  }
}

String timestamp() {
  Calendar now = Calendar.getInstance();
  return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}
void settings() {
  int dpi=72; 
  float width_mm=210;
  float height_mm=297;
  int width_px=int(width_mm*0.03937*dpi);
  int height_px=int(height_mm*0.03937*dpi);
  size(width_px, height_px);
}

void keyPressed() {
  if (key == 'c' || key == 'C') GenerativeDesign.saveASE(this, colors, timestamp()+".ase");
  if (key == 's' || key == 'S') saveFrame(timestamp()+"_##.png");
  if (key == 'p' || key == 'P') savePDF = true;

  if (key == '1') interpolateShortest = true;
  if (key == '2') interpolateShortest = false;
}

void mousePressed() {
  shakeColors();
}

リファレンス