Using the get() function and comparing the color of the pixel that the mouse is over is definitely not a good way to do hit testing. Anybody who suggests this is making an unfortunate mistake. There are numerous things that could cause this to fail such as anti-aliasing, how opacities and blending are implemented, the complexities of color spaces and how graphics cards handle them, etc. Additionally this method restricts you to only having one instance of a clickable thing of a certain color. It also wouldn’t work any more if you decided to do something like introduce texturing to whatever you’re doing hit testing for.
In this specific case you are running into two issues:
Using the == with two colors is not going to work. The == operator in JavaScript generally only works for value types such as numbers and strings. With Objects that operator will only return true when the Objects are the exact same instance (i.e. cookie == cookie) when you create two separate but equivalent objects, this will be false (i.e. cookie == color(132, 86, 60, 255))
For some reason the color 132, 86, 80 is not representable in the canvas and gets converted to 133, 87, 80. As I said there could be all sorts of complicated low level reasons for this.
I suggest you check my StackOverflow answer and use that technique. However if you were dead set on using color (bad idea), then this would technically work:
Also, it is still a sub optimal approach in my opinion for all the other issues I mentioned in my original response.
I would personally never use such an approach regardless of whether somebody considered it an “old and well established concept.” It my personal opinion it is a very bad “old and well established concept” if it is indeed the latter.
I was also struggling with the same issue today.
For example, this did not work, even though the compared values are the same:
function draw() {
let m = get(mouseX, mouseY);
let cp = get(50, 50);// reference color
// if mouse color equals the reference color, do something
if (m == cp) {
fill(0, 255, 0);
} else fill(0, 0, 255);
print(m + " - " + cp);// debug
square(100, 100, 50);
}
I will use one of the other suggestions instead. Thanks!
ps. It does work with Processing though, not with P5js.