0

Here's my situation. I'm using Processing 2.0, and I'm trying to make a grid of squares turned at a 45-degree angle. Each square will be filled with a color chosen randomly from a palette of five. My problem is this; for some reason, when I use COLORS[int(random(COLORS.length))] to get a color from my palette, I only get black! It's doubly weird because black is not one of the colors in my palette! I have tested my floodFill() function, and I can confirm that it works, as testing with a single color not pulled from an array works all right. Any tips? My code is as follows:

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);
  }
}

When I replace the #rrggbb notation with 0xrrggbb, I get white instead. When I replace it with 0xrrggbbaa, I get black and white (not grayscale).

Now using the standard color(r, g, b) notation works fine. However, I'd still like to know what breaks the hexadecimal versions, so I'll leave this question open.

4

1 回答 1

3

color() 不采用十六进制值。它需要从 0 到 255 的简单 int 值。尝试打印出其中一个带有十六进制值的 color() 调用的值,你会得到一些疯狂的负值。

输入 color() 方法的任何负值都将被解释为 0,即黑色。如果您想查看它的影响,请在处理中尝试它。

就像你想出来的那样,使用 color(r,g,b) 分别为 r、g、b 使用 0 - 255 范围内的值。

于 2013-06-29T22:15:13.677 回答