Cannot invoke red() on the primitive type int

Hello, I here have a problem I do not understand: the code below prints to the console : Cannot invoke red() on the primitive type int

void mousePressed() {
  color m = get(mouseX, mouseY);
  if(m.red() == 255 && m.green() >= 200)
    clicked();
}

There are absolutely no integers involved here, m is of type color, not int !
Thanks to anyone who can help me, this is very confusing.

1 Like

A color is secretly an int. This fact is hidden from most casual users.

The red() function is not called on a color. Instead, pass the color to the function:

if(red(m) == 255){

3 Likes

Oh… Once again, I found my own answer right after posting: replacing red and green by >> 16 & 0xFF and >> 8 & 0xFF (which is something proposed on the reference for red()) solved the problem! I don’t understand what was the issue in the code I first wrote but here I have my correct results.

Oh thanks, I had forgotten about that… :person_facepalming:
And wow, I actually tested out the color / int thing and it just works: you can replace color by int, well at least in my case. That is very interesting!

2 Likes