处理 - 颜色循环
Processing - Color loop
我正在尝试创建一个 window,当您单击一次时,颜色会发生循环变化:
代码:
package declaringvariables;
import processing.core.PApplet;
public class DeclaringVariables extends PApplet {
public void setup() {
width = 800;
height = 600;
size(width,height);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incremento = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
R += incremento;
if (R > maxcol) incremento = -1;
else if (R > mincol) incremento = random(2);
B += incremento;
if (B > maxcol) incremento = -1;
else if (B < mincol) incremento = +5;
G += incremento;
if (G > maxcol) incremento = -1;
else if (G < mincol) incremento = +10;
frameRate(150);
}
}
public void mousePressed() {
down = true;
}
}
就像现在一样,我点击它开始改变颜色,直到它变成绿色 (1,255,1)RGB,然后停止。我需要它循环,所以它是一个不断变化的背景。
我考虑制作某种 "if statement",当它变成绿色时会重置背景。还考虑了一个 while 循环,但它既降低了我的帧速率又没有使它工作。
行尾,我希望我的 if(down = true) 语句在单击后自行重复。
提前谢谢你:)
首先:你真的是说 if(down = true)
吗?这会将 true 赋值给 down,而不仅仅是测试它是否为 true。我建议您改用 if(down)
。
为了循环颜色,您可以使用模数 (%)。这样,一旦超过 255,就从 0 开始。
除了您的主要颜色插值问题之外,几乎没有什么可以改进的地方:
- 在调用
size()
之前调用 size(800,600);
比直接访问 width,height
更好(它在幕后做了很多事情)
- 您可以在
setup()
中设置一次 frameRate,而不是在 draw()
中一遍又一遍地设置它
- 如果值的行为与您预期的不同,最好将它们打印到控制台或在屏幕上显示,这样您就可以观察它们的行为方式
与您的问题更相关:
- 您正在为 3 个值使用单个增量,但希望 3 个值独立递增:您应该使用 3 个增量变量来保持每个颜色通道值独立
- 在将增量应用到颜色通道之前,您应该检查并调整增量。
以上三点代码如下:
public void setup() {
size(800,600);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incrementoR = +1;
float incrementoG = +1;
float incrementoB = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
R += incrementoR;
if (R > maxcol) incrementoR = -1;
else if (R > mincol) incrementoR = random(2);
B += incrementoB;
if (B > maxcol) incrementoG = -1;
else if (B < mincol) incrementoG = +5;
G += incrementoG;
if (G > maxcol) incrementoB = -1;
else if (G < mincol) incrementoB = +10;
frameRate(150);
}
fill(127);
text("R:"+(int)R+"\tG:"+(int)G+"\tB:"+(int)B,15,15);
}
public void mousePressed() {
down = true;
}
您会注意到值很快就会超出 0-255 范围。
这是调整为首先更新增量,然后是值的代码。
此外,这是在绘图之前完成的,否则您将始终绘制以前的 R、G、B 值:
public void setup() {
size(800,600);
frameRate(150);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incrementoR = +1;
float incrementoG = +1;
float incrementoB = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
//update increments prior to applying them
if(R > maxcol) incrementoR = -1;
if(R < mincol) incrementoR = random(2);
if(G > maxcol) incrementoG = -1;
if(G < mincol) incrementoG = +5;
if(B > maxcol) incrementoB = -1;
if(B < mincol) incrementoB = +10;
//apply increments
R += incrementoR;
G += incrementoG;
B += incrementoB;
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
}
fill(127);
text("R:"+(int)R+"\tG:"+(int)G+"\tB:"+(int)B,15,15);
}
public void mousePressed() {
down = true;
}
如果你想更多地玩颜色,你也应该看看lerpColor() and colorMode(),它们很好玩。
我正在尝试创建一个 window,当您单击一次时,颜色会发生循环变化:
代码:
package declaringvariables;
import processing.core.PApplet;
public class DeclaringVariables extends PApplet {
public void setup() {
width = 800;
height = 600;
size(width,height);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incremento = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
R += incremento;
if (R > maxcol) incremento = -1;
else if (R > mincol) incremento = random(2);
B += incremento;
if (B > maxcol) incremento = -1;
else if (B < mincol) incremento = +5;
G += incremento;
if (G > maxcol) incremento = -1;
else if (G < mincol) incremento = +10;
frameRate(150);
}
}
public void mousePressed() {
down = true;
}
}
就像现在一样,我点击它开始改变颜色,直到它变成绿色 (1,255,1)RGB,然后停止。我需要它循环,所以它是一个不断变化的背景。
我考虑制作某种 "if statement",当它变成绿色时会重置背景。还考虑了一个 while 循环,但它既降低了我的帧速率又没有使它工作。
行尾,我希望我的 if(down = true) 语句在单击后自行重复。
提前谢谢你:)
首先:你真的是说 if(down = true)
吗?这会将 true 赋值给 down,而不仅仅是测试它是否为 true。我建议您改用 if(down)
。
为了循环颜色,您可以使用模数 (%)。这样,一旦超过 255,就从 0 开始。
除了您的主要颜色插值问题之外,几乎没有什么可以改进的地方:
- 在调用
size()
之前调用size(800,600);
比直接访问width,height
更好(它在幕后做了很多事情) - 您可以在
setup()
中设置一次 frameRate,而不是在draw()
中一遍又一遍地设置它
- 如果值的行为与您预期的不同,最好将它们打印到控制台或在屏幕上显示,这样您就可以观察它们的行为方式
与您的问题更相关:
- 您正在为 3 个值使用单个增量,但希望 3 个值独立递增:您应该使用 3 个增量变量来保持每个颜色通道值独立
- 在将增量应用到颜色通道之前,您应该检查并调整增量。
以上三点代码如下:
public void setup() {
size(800,600);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incrementoR = +1;
float incrementoG = +1;
float incrementoB = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
R += incrementoR;
if (R > maxcol) incrementoR = -1;
else if (R > mincol) incrementoR = random(2);
B += incrementoB;
if (B > maxcol) incrementoG = -1;
else if (B < mincol) incrementoG = +5;
G += incrementoG;
if (G > maxcol) incrementoB = -1;
else if (G < mincol) incrementoB = +10;
frameRate(150);
}
fill(127);
text("R:"+(int)R+"\tG:"+(int)G+"\tB:"+(int)B,15,15);
}
public void mousePressed() {
down = true;
}
您会注意到值很快就会超出 0-255 范围。 这是调整为首先更新增量,然后是值的代码。 此外,这是在绘图之前完成的,否则您将始终绘制以前的 R、G、B 值:
public void setup() {
size(800,600);
frameRate(150);
}
boolean down = false;
float R = 255;
float G = 255;
float B = 0;
float incrementoR = +1;
float incrementoG = +1;
float incrementoB = +1;
float mincol = 0, maxcol = 255;
public void draw() {
background (1,255,1);
if(down) {
//update increments prior to applying them
if(R > maxcol) incrementoR = -1;
if(R < mincol) incrementoR = random(2);
if(G > maxcol) incrementoG = -1;
if(G < mincol) incrementoG = +5;
if(B > maxcol) incrementoB = -1;
if(B < mincol) incrementoB = +10;
//apply increments
R += incrementoR;
G += incrementoG;
B += incrementoB;
println("\nFramecount: "+frameCount);
println("The current framerate is: "+frameRate);
background(R,G,B);
}
fill(127);
text("R:"+(int)R+"\tG:"+(int)G+"\tB:"+(int)B,15,15);
}
public void mousePressed() {
down = true;
}
如果你想更多地玩颜色,你也应该看看lerpColor() and colorMode(),它们很好玩。