Not Understanding This

Why does set(int(X),int(Y),255); work in Setup but not in draw and yet set(int(X),int(Y),0xffffffff); does work in draw ?

float X,Y;

void setup(){
  size(512,512);
  background(0);
  X=256;Y=256;
  //set(int(X),int(Y),255);
  
}

void draw(){
  //set(int(X),int(Y),255);
  set(int(X),int(Y),0xffffffff);
}
2 Likes

Does:

void draw() {
  background(0);
  set(int(X), int(Y), 255);
}

Work for you?

1 Like

Because it expects parameters like set(int, int, color);

float X,Y;
color yourColor = color(255, 100, 0);

void setup(){
size(512,512);
background(0);
X=256;Y=256;
set(int(X),int(Y),yourColor);

}

void draw(){
set(int(X), int(Y), yourColor);
}
1 Like

Yes, that’s it Fritz. Always wondered what data type color was for as it is seems to be exactly like int.
Now I see it’s ‘special’. :slight_smile:

1 Like

It still does not explain why it works in setup and not in draw.

1 Like

Color actually is really close to an int, because it is represented by the numbers -1 to -16million~, that’s also why 255 doesn’t work. What you could do is use color(255) instead of 255. This will return -16million as an int, while color(0) would give out -1.

1 Like

That’s odd. Maybe @GoToLoop might know more?

1 Like

I did notice that P5.js and processing.js were very strict about having to declare the var as a color. So even being able to just use 0x… as a color is is a plus for processing.

I think that’s due to the fact that int and color both are similar and to avoid confusing ints with color and viceversa that might have to be set explicitly, to avoid a color being used as normal int… although i can’t think of a case in which that would apply… so take what i said with a grain of salt(or a spoon of salt actually :sweat_smile:)

1 Like
  • Processing’s color is merely a syntactic sugar for the Java’s keyword int.
  • Not sure about the initial reason for its inclusion.
  • Maybe b/c Processing is a multi-language library, and the actual datatype can be diff. for each 1.
  • And having its own color keyword allows Processing to represent a webcolor value across diff. flavors under the same label.
4 Likes

Color-related functions interpret the passed color argument as gray if it’s within a 0 to 255 range. :black_flag:
Otherwise it treats it as a full 32-bit aRGB color value. :paintbrush: