# Getting an images rgb value at click

I'm not sure if this is at all possible, but maybe it will spark up an interesting conversation. (Sidenote: working on a small game in Löve).

I'm making an archery shooting kind of game, and I was thinking it might be cool to have critical and regular hits based on where the user shoot the arrow at the enemy. Example, if I have a troll image, and the user clicks the head of the troll, it will count as a critical hit.

Checking to see if the user clicked on the top part of the image vs the bottom, isn't the tricky part. The hard bit will be to check if the user clicked on apart of the image, instead of blank space in the png...

So, main question: Is it possible to get the rgb value of where an image has been clicked, to determine if they hit or missed the target.

Also, if this is at all possible, is there anyone familiar with Löve that can confirm it is doable?

Edit: I'm realizing now it's not the rgb value I want to look for, it is whether or not the clicked pixel has transparency. If it is 100% transparent that means the user missed the image.

• I think the question you should be asking is how assign and detect hit regions on an image. If you're depending on the RGB value, you limit yourself to a range of colors that can only be shown at the critical parts (like requiring the head to be a different color than the neck). Additionally, the hit regions are far more re-usable for other enemies. – MichaelHouse Jan 7 '13 at 22:35
• I see what you're saying, but that's not exactly what I'm trying to say. If the rgb value of a click is nothing, that means it was a miss. So I would use the value to determine if the point is even apart of the actual image or not. – mbreen Jan 7 '13 at 22:40

You can get specific information about the image clicked by accessing the ImageData associated with the Image you're drawing. Specifically the getPixel function. When you detect a click, find which image is being clicked on. Then you need to get the ImageData you created for that image. Finally, convert the click coordinates to image local coordinates and pass them into the getPixel(x, y) function. You'd likely want to test the alpha component to see if it's below whatever threshold you define as "not part of the image".