首页 > 解决方案 > 彩色字母与 Colorcycle | 加工

问题描述

我找到了一个程序,它在网格中生成随机字母并给它们随机颜色。程序运行时,如何使字母颜色或亮度变化?(源代码:https ://happycoding.io/examples/processing/for-loops/letters )

我尝试让填充(r,g,b)有一个从1到255循环的'r',而'g'和'b'为0,但我无法让它更新颜色。我对编程很陌生,所以我很想知道如何才能做到这一点。

标签: processing

解决方案


首先,让我们更改填充方法以接受 RGB 值:

fill(random(256),random(256),random(256));

要在程序运行时更改颜色,必须在方法内部进行更改,该draw()方法将不断循环和更新画布。有关在此处绘制的更多信息我相信以下代码会输出您所要求的内容:

int rows = 10;
int cols = 10;

int cellHeight;
int cellWidth;

void setup(){
  size(500, 500);
  cellHeight = height/rows;
  cellWidth = width/cols;
  textAlign(CENTER, CENTER);
  textSize(28);
}

void draw(){
  background(32);
  for(int y = 0; y < rows; y++){
    for(int x = 0; x < cols; x++){
         
      //get a random ascii letter
      char c = '!';
      c += random(93);
      
      //calculate cell position
      int pixelX = cellWidth * x;
      int pixelY = cellHeight * y;
      
      //add half to center letters
      pixelX += cellWidth/2;
      pixelY += cellHeight/2;
      
      fill(random(256),random(256),random(256));
      text(c, pixelX, pixelY);
    }
  }
  delay(100);
}

推荐阅读