处理中的 RGB 渐变

RGB Gradient in Processing

我正在尝试找出一种方法来生成应如下所示的渐变:

但是,我不知道如何一次组合多种颜色(红色、蓝色和绿色)。这是我的代码目前的样子:

color red;
color blue;
color green;

void setup() {
  size(255, 255);
  
  red = color(255,0,0);
  blue = color(0,0,255);
  green = color(0,255,0);
  
  for(int x = 0; x < width; x++) {
    float n = map(x, 0, width, 0, 1);
    color newc = lerpColor(blue, red, n);
    stroke(newc);
    line(0, x, height, x);
  }
}
void draw() {
} 

如您所见,目前未使用绿色,因为我找不到在保留红色和蓝色渐变的同时插入这些颜色的方法。我是 Processing 的初学者,所以在此方面我将不胜感激。我应该如何编写代码?

我会使用嵌套循环来执行此操作,并分别执行三个值,如下所示:

void setup() {
  size(255, 255);
  
  for (int x = 0; x < width; x++) {
    for(int y = 0; y < height; y++) {
      stroke(y, x, 255 - y);
      point(x, y);
    }
  }
}

以下是您希望颜色如何更改:

  • 红色:顶部为 0(y = 0),底部为 255(y = 255
  • 绿色:左侧为 0(其中 x = 0),右侧为 255(其中 x = 255
  • 蓝色:顶部为 255(y = 0),底部为 0(y = 255

如果你想要不同的尺寸 canvas,你可以使用 map():

stroke(map(y, 0, height, 0, 255), map(x, 0, width, 0, 255), map(y, 0, height, 255, 0));

lerp():

float xprop = float(x) / width;
float yprop = float(y) / height;
stroke(lerp(0, 255, yprop), lerp(0, 255, xprop), lerp(255, 0, yprop));