[英]Choosing a color from an array of them just returns black?
这是我的情况。 我正在使用Processing 2.0,并且试图使以45度角旋转的正方形网格。 每个正方形将填充从五个调色板中随机选择的颜色。 我的问题是 由于某些原因,当我使用COLORS[int(random(COLORS.length))]
从调色板中获得颜色时,我只会得到黑色! 奇怪的是,黑色不是我调色板中的颜色之一! 我已经测试了floodFill()
函数,并且可以确认它是否正常工作,因为使用未从数组中提取的单一颜色进行的测试可以正常进行。 有小费吗? 我的代码如下:
final int DX = 16, DY = DX;
final color DEFAULT_BG = color(50, 50, 50);
final color[] COLORS = {
color(#ff3333),
color(#4fff55),
color(#585eff),
color(#ebff55),
color(#FF55D5),
};
void setup() {
size(800, 480);
background(DEFAULT_BG);
noSmooth();
for (int x = 0; x < width; x += DX) {
for (int y = 0; y < height; y += DY) {
line(x, y, x + 16, y + 16);
line(x + 16, y, x, y + 16);
}
}
for (int x = 0; x < width; x += 4) {
for (int y = 0; y < height; y += 4) {
if (get(x, y) == DEFAULT_BG) {
color f = COLORS[int(random(COLORS.length))];
floodFill(x, y, DEFAULT_BG, color(f));
}
}
}
}
void floodFill(final int x, final int y, final color from, final color to) {
if (!(x < 0 || y < 0 || x >= width || y >= height || from == to || get(x, y) != from)) {
set(x, y, to);
floodFill(x, y + 1, from, to);
floodFill(x, y - 1, from, to);
floodFill(x + 1, y, from, to);
floodFill(x - 1, y, from, to);
}
}
当我更换#rrggbb
与符号0xrrggbb
,我得到的白色来代替。 当我用0xrrggbbaa
替换它时,我得到黑白(不是灰度)。
现在使用标准的color(r, g, b)
表示法可以正常工作。 但是,我仍然想知道是什么破坏了十六进制版本,因此我将保留这个问题。
color()不接受十六进制值。 它接受的是介于0到255之间的简单int值。尝试打印其中一个具有十六进制值的color()调用之一的值,您会得到一些疯狂的负值。
送入color()方法的任何负值都将解释为0,等于黑色。 如果您想查看其影响,请在处理中尝试。
如您所想,使用color(r,g,b)分别对r,g和b使用介于0-255之间的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.