我试图找出一种方法来生成一个看起来像这样的渐变:
但是,我不知道如何一次组合多种颜色(红色、蓝色和绿色)。这是我的代码目前的样子:
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);
}
}
}
以下是您希望颜色更改的方式:y = 0
)和底部 255(其中 y = 255
)x = 0
),右侧为 255(其中 x = 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));
关于java - 处理中的 RGB 渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67499659/