【问题标题】:Colored Letters with Colorcycle | Processing彩色字母与 Colorcycle |加工
【发布时间】:2021-11-11 21:50:21
【问题描述】:

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

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

【问题讨论】:

  • 你指的代码看起来不像java。是基于processing.org吗?我删除了错误的标签。

标签: processing


【解决方案1】:

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

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

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

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);
}

【讨论】:

    猜你喜欢
    • 2012-10-06
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2021-12-06
    • 2012-11-19
    • 2016-09-02
    相关资源
    最近更新 更多