处理:只让一些随机像素改变颜色

Processing: Make only some random pixels change colour

我制造了一些白噪声,我想随着时间的推移逐渐减少(2 秒后开始变化,10 秒后增强等),慢慢趋于黑屏。

我想不通的是,我怎样才能让同一帧内的一些(比如所有像素的 50%)随机像素改变颜色,而其余的只是黑色?

到目前为止,我只能让它们全部随机变化,或者全部保持黑色。任何帮助将不胜感激,谢谢!!

void setup() {
  size(1000, 800);
}

void draw() {
  if (millis() < 2000) {
    loadPixels();
    for ( int i=0; i<pixels.length; i++)
      pixels[i] = color(random(255));
    updatePixels();
  }

  if (millis() > 2000) {
    loadPixels();
    if (random(1) >= 0.5) {
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color(random(255));
      updatePixels();
    } else {
      loadPixels();
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color(0);
      updatePixels();
    }
  }

  if (millis() > 10000) {
    loadPixels();
    for ( int i=0; i<pixels.length; i++)
      pixels[i] = color(random(255));
    updatePixels();
  }
}

与其 post 在外部网站中输入所有代码,不如将问题归结为 MCVE 并将其直接包含在问题中。

也就是说,您有两个选择:

选项 1:将所有像素存储在某种数据结构中。您可能有一个 MyPixel 对象的二维数组,其中 MyPixel 是您创建的 class,它包含您需要知道的所有信息,以了解该数组中的哪些实例要更改其颜色。

选项 2:直接绘制到 PImage。然后你可以遍历那个 PImage 找到一个非黑色像素并改变它。

采用哪种方法完全取决于您。我个人会选择第一个选项,但这只是我个人的喜好。尝试其中一种方法,当你遇到困难时 post 一个 MCVE。请注意,这应该是尽可能少的行,同时仍能证明问题,而不是你的整个草图——例如,我们不需要看到你的时序逻辑。

一个简单的方法是考虑 random() returns 一个范围内的随机值。如果你给它一个低值,你就会有一个低随机值。 如果您将该值用作颜色,则值越低,您越接近黑色,这可能适合您的情况。

如果随机数为255,则增加亮像素的变化,否则(随机值低)像素将变暗:

//noise image
PImage noise;
//amount of noise image image 0 = 0%, 255 = 100%
int noiseAmt = 255;

void setup(){
  noise = createImage(width,height,RGB);
}
void draw(){
  //decrease noise over time
  noiseAmt--;
  if(noiseAmt < 0) noiseAmt = 255;

  //apply noise based on noise amount
  noiseImage();
  //render image
  image(noise,0,0);
}

void noiseImage(){
  int numPixels = noise.pixels.length;
  for(int i = 0 ; i < numPixels; i++){
    //random(noiseAmt) is the key - low values = darker pixels
    noise.pixels[i] = color(random(noiseAmt));
  }
  noise.updatePixels();
}

为了掌握这一点,这里有一个稍微修改过的代码版本,它使用 UP/DOWN 箭头键来控制噪音:

//noise image
PImage noise;
//amount of noise image image 0 = 0%, 255 = 100%
int noiseAmt = 127;

void setup() {
  noise = createImage(width, height, RGB);
}
void draw() {
  //apply noise based on noise amount
  noiseImage();
  //render image
  image(noise, 0, 0);
}

void noiseImage() {
  int numPixels = noise.pixels.length;
  for (int i = 0; i < numPixels; i++) {
    //random(noiseAmt) is the key - low values = darker pixels
    noise.pixels[i] = color(random(noiseAmt));
  }
  noise.updatePixels();
}
void keyPressed(){
  if(keyCode == UP)   noiseAmt += 5;
  if(keyCode == DOWN) noiseAmt -= 5;
  noiseAmt = constrain(noiseAmt,0,255);
  println("noiseAmt: " + noiseAmt);
}

回到时间问题,你可以看看this answer,其中涵盖了使用millis()跟踪时间。唯一额外的部分是将淡入淡出时间映射到噪声量,这将是某个比率。如果我们将传递的时间映射为标准化值(从 0.0 到 1.0)可能会更容易,只需乘以 255 即可轻松缩放到 0.0 到 255.0:

//noise image
PImage noise;
//amount of noise image image 0 = 0%, 255 = 100%
int noiseAmt = 255;

int timestamp,fadeTime = 10000;//fade to black in 10s

void setup(){
  noise = createImage(width,height,RGB);
  timestamp = millis();
}
void draw(){
  //decrease noise over time
  int now = millis();
  //if the difference between an initial timestamp and the current time is less than 10s
  if(now - timestamp <= fadeTime){
    //compute the ratio between the time difference and total fadeTime which will be from 0.0 to 1.0
    //subtract this difference from 1.0 to flip the ratio direction from 0.0 -> 1.0 to 1.0 -> 0.0 
    float fadeRatio = 1.0 - ((float)(now-timestamp)/fadeTime);
    //this ratio multiplied to 255 will be
    noiseAmt = (int)(fadeRatio * 255);
  } 

  //apply noise based on noise amount
  noiseImage();
  //render image
  image(noise,0,0);
}

void noiseImage(){
  int numPixels = noise.pixels.length;
  for(int i = 0 ; i < numPixels; i++){
    //random(noiseAmt) is the key - low values = darker pixels
    noise.pixels[i] = color(random(noiseAmt));
  }
  noise.updatePixels();
}

Processing 有一些处理 mapping and constraining 数字范围的好函数:

//noise image
PImage noise;
//amount of noise image image 0 = 0%, 255 = 100%
int noiseAmt = 255;
int timestamp,fadeTime = 10000;//fade to black in 10s

void setup(){
  noise = createImage(width,height,RGB);
  timestamp = millis();
}
void draw(){
  //decrease noise over time
  int now = millis();
  //if the difference between an initial timestamp and the current time is less than 10s
  noiseAmt = (int)map(now - timestamp,0,fadeTime,255,0);
  noiseAmt = constrain(noiseAmt,0,255); 

  //apply noise based on noise amount
  int numPixels = noise.pixels.length;
  for(int i = 0 ; i < numPixels; i++){
    //random(noiseAmt) is the key - low values = darker pixels
    noise.pixels[i] = color(random(noiseAmt));
  }
  noise.updatePixels();

  //render image
  image(noise,0,0);
}

请注意 fadeTime 设置为 10 秒(10000 毫秒)。随意修改 fadeTime 值。