Daily Creative Coding

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

多色のグラデーション

/**
* Gradation of Multiple Colors
*
* @author aa_debdeb
* @date 2015/11/17
*/

color[] colors = {
  color(0, 0, 255), // blue
  color(0, 255, 255), // aqua
  color(0, 255, 0), // green
  color(255, 255, 0), // yellow
  color(255, 165, 0), // orange
  color(255, 0, 0) // red
};
float[] cRange = {0.0, 0.2, 0.4, 0.6, 0.8, 1.0};

void setup(){
  size(500, 500);
  loadPixels();
  float noiseX = random(100);
  float noiseY = random(100);
  for(int x = 0; x < width; x++){
    for(int y = 0; y < height; y++){
      pixels[y * width + x] = getColor(translateValue(noise(noiseX + x * 0.03, noiseY + y * 0.01)));
    }
  }
  updatePixels();  
  noLoop();
}

color getColor(float v){
  for(int i = 0; i < cRange.length - 1; i++){
    if(cRange[i] <= v && v <= cRange[i + 1]){
      float newV = v - cRange[i];
      float r = (red(colors[i + 1]) - red(colors[i])) * (newV / (cRange[i + 1] - cRange[i])) + red(colors[i]);
      float g = (green(colors[i + 1]) - green(colors[i])) * (newV / (cRange[i + 1] - cRange[i])) + green(colors[i]);
      float b = (blue(colors[i + 1]) - blue(colors[i])) * (newV / (cRange[i + 1] - cRange[i])) + blue(colors[i]);
      return color(r, g, b);
    }
  }
  return color(0); // to avoid error
}

float translateValue(float v){
  float vv = map(v, 0, 1, -0.5, 0.5);
  return vv > 0 ? vv : 1.0 + vv;
}