1

I am drawing a colored object with random RGB color values via the background() function in Processing, waiting half a second, and then changing the position of the RGB values (e.g. swapping R with B, etc):

void draw() {
    z = float(random(255));
    x = float(random(255));
    c = float(random(255));
    background(z, x, c);
    delay(500);
    background(c, x, z);
}

However, the way I am declaring the color values randomly doesn't seem to be constant within the function, despite storing the values as variables; rather, the second background call is generating entirely new values for the RGB variables.

I want the z, x, and c variables to not change upon being called into the 2nd background() command, but can't quite figure it out.

I tried making other variables, using frameRate(1), and moving the delay(500) command past the 2nd background(), but unfortunately the variables change upon being called the 2nd time, and I want them to keep their value until the next void draw() loop.

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    The tag renaming was already requested a few years ago https://meta.stackoverflow.com/questions/286694/rename-processing-to-processing-language-or-similar/424165#424165 -- please consider weighing in on a good renaming (such as [tag:processing-lang]). – TylerH Apr 13 '23 at 20:35
  • @TylerH He showed all of background() that there is to show: https://processing.org/reference/background_.html – apodidae Apr 14 '23 at 00:04
  • Based on the discussion and the answers below it seems this is not reproducible; in other words it is behaving the way you want it to behave. As such it should probably not be reopened, since it would then just be closed again as 'not reproducible'. – TylerH Apr 14 '23 at 15:36
  • The r,g,b values don't change during the life of a frame; only one background() call is used and the color may not be the r,g,b value in the parentheses. – apodidae Apr 15 '23 at 03:22

3 Answers3

1

Even with the delay() call (which I don't recommend), the second time you call background() you'd overwrite the values set during the 1st call. You probably want to change the colour once per frame (not twice).

If you'd like to toggle between the two calls ((z,x,c) / (c, x, z)) you can use a separate boolean value or expression to swap between the two (thus avoiding changing the background twice in the same frame).

For example you can check frameCount and the modulo(%) to check if when frames alternate.

e.g.

if (frameCount % 2 == 0){   
  background(z, x, c);
}else{
  background(c, x, z);
}

Warning: If you're on a high frame rate this will essentially look strobey / flashy.

Given the r,g,b values are random anyway, perceptually it will be hard to tell the difference.

(It's unclear what the overall effect you're trying to achieve is, visually, but at least you have a hint above on how you'd swap between two colours)

George Profenza
  • 50,687
  • 19
  • 144
  • 218
0

Assuming you have a boolean variable called initialized, with its initial value being false, then you can check for its value and set it to true if it was false. This will make sure your variables are initialized at first call, but will not change their value afterwards:

void draw() {
    if (!initialized) {
        z = float(random(255));
        x = float(random(255));
        c = float(random(255));
        initialized = true;
    }
    background(z, x, c);
    delay(500);
    background(c, x, z);
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

If you slow your demo way down it is easier to see what is happening. You can't change the background color in a single frame; you only get one shot/frame. But what you can do is set the background color only on the even frame counts and then use the swapped values for the odd frame counts. The console output confirms that the middle value stays the same while value 1 and value 3 swap places for the alternate background color.

float z, x, c;

void setup() {
  size(200, 200);
  background(255);
  frameRate(1);
}

void draw() {
  println("===============================");
  if (frameCount % 2 == 0) {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
  } else {
    println("2:", c, x, z);
    background(c, x, z);
    delay(5000);
  }  
}

console output:

enter image description here

Addendum re: original post

I think the premise that the rgb values are changing during the life of a single frame is incorrect. The following slowed down version of the original post shows that the values do not change. In addition, the second background() call has no effect. The background color of the window does not change until a new frame is called and a new set of random r,g,b values are generated. This occurs with the first background() call only.

float z,x,c;

void setup(){
 frameRate(1); 
}

void draw() {
    z = random(255);
    x = random(255);
    c = random(255);
    println("1:", z, x, c);
    background(z, x, c);
    delay(5000);
    println("2:", c, x, z);
    background(c, x, z);
    println("====================");
    delay(5000);
}
apodidae
  • 1,988
  • 2
  • 5
  • 9
  • Based on your results does this not mean that OP's problem is not reproducible? In other words, that it is actually behaving exactly as they want? – TylerH Apr 14 '23 at 14:20
  • I thought it was a valid question. This is the second post on the same subject so we knew what the user was working on (other post had only a Processing tag). My interpretation is that the user wanted to change the background color of a window in the middle of a frame by swapping two rgb values and was having trouble doing it. There are quite a few young coders who visit the Processing forums and they are usually not talkative, probably due to fear of being criticized. It is sometimes difficult to tell what they are thinking, but in this case I thought the question had merit. – apodidae Apr 14 '23 at 15:16
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee Apr 24 '23 at 13:41
  • @tripleee The console output was important in solving this problem. It is not possible in the Processing editor to copy console output. Posting an image is the only way to show the output. – apodidae Apr 24 '23 at 14:32