在处理中创建颜色渐变
Create Colour Gradiente on Processing
我有一个代码可以绘制出颜色,以便在圆形中创建径向渐变。就知道它会从黑到白。如何切换颜色并使用其他值?
我试图为不同的颜色创建值,但它不起作用。
有谁知道该怎么做?
这是一段代码:
// function to draw the gradient
void desenhar_grad(float posX, float posY, int raio) {
pushStyle();
noStroke();
for (int r = raio; r > 0; r--) {
int tom = round(map(r, raio, 0, 255, 0)); // the last 2 values are the colours. first is the center, second is the exterior
fill(tom);
circle(posX, posY, r * 2);
}
popStyle();
}
您的方法适用于灰度颜色。
要轻松地在具有多个通道的两种颜色之间进行插值,您可以使用 lerpColor()
。它采用两种颜色作为前两个参数(要插值的颜色和要插值的颜色),第一个参数是插值量(介于 0.0 和 1.0 之间的值,其中 0 表示它是第一种颜色,1.0 是第二种颜色,例如 0.5 是两者之间的 50% 混合)
然后您可以像在 0.0 到 1.0 范围内那样映射 r:
void setup(){
desenhar_grad(50, 50, 50, color(0, 192, 192), color(192, 0, 192));
}
void desenhar_grad(float posX, float posY, int raio, color color1, color color2) {
pushStyle();
noStroke();
for (int r = raio; r > 0; r--) {
// the last 2 values are the colours. first is the center, second is the exterior
int tom = lerpColor(color1, color2, map(r, 0, raio, 0.0, 1.0));
fill(tom);
circle(posX, posY, r * 2);
}
popStyle();
}
我有一个代码可以绘制出颜色,以便在圆形中创建径向渐变。就知道它会从黑到白。如何切换颜色并使用其他值? 我试图为不同的颜色创建值,但它不起作用。 有谁知道该怎么做? 这是一段代码:
// function to draw the gradient
void desenhar_grad(float posX, float posY, int raio) {
pushStyle();
noStroke();
for (int r = raio; r > 0; r--) {
int tom = round(map(r, raio, 0, 255, 0)); // the last 2 values are the colours. first is the center, second is the exterior
fill(tom);
circle(posX, posY, r * 2);
}
popStyle();
}
您的方法适用于灰度颜色。
要轻松地在具有多个通道的两种颜色之间进行插值,您可以使用 lerpColor()
。它采用两种颜色作为前两个参数(要插值的颜色和要插值的颜色),第一个参数是插值量(介于 0.0 和 1.0 之间的值,其中 0 表示它是第一种颜色,1.0 是第二种颜色,例如 0.5 是两者之间的 50% 混合)
然后您可以像在 0.0 到 1.0 范围内那样映射 r:
void setup(){
desenhar_grad(50, 50, 50, color(0, 192, 192), color(192, 0, 192));
}
void desenhar_grad(float posX, float posY, int raio, color color1, color color2) {
pushStyle();
noStroke();
for (int r = raio; r > 0; r--) {
// the last 2 values are the colours. first is the center, second is the exterior
int tom = lerpColor(color1, color2, map(r, 0, raio, 0.0, 1.0));
fill(tom);
circle(posX, posY, r * 2);
}
popStyle();
}