# HTML5 - Canvas - how to find a shape in a canvas and count the number of red pixels

Hi..
Any idea how I can find a shape in a canvas (image) and count the number of pixels of the shape.
Say I have an image with a circle and an a square.
How can I find / or count the number of red pixels the square takes up.
square.png
LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
If you can determine the dimensions of the square then calculate the number of dquare inches - and from your screen resolution - you should be able to calculate the  number of pixels.  This assumes that all the pixels are red.
Commented:
If it is your own page, then you put the square in the canvas with scripting and the Object for the square should have height and wisth properties (which may be dynamic) so if you multiple the offsetWidth by the offsetHeight that should give you the total number of pixels based on the current state of the object being rendered as a square.  If you want to know how of a specific colour you would have to script a scan of the rendered area of the object and count the occurrence of the specific value.

If this is from a page you don't control then you might be able to get an approximation by diong a screen shot and counting the pixels in a graphics program.

Cd&
Commented:
You might also want to consider the definition of "red" since it might mean strictly RGB = 255,0,0 but a visual red could be many variants of the RGB values and still look quite red to the observer.  For example, in the attached image file, the red area contains RGB = 237,28,36.
Author Commented:

Experts Exchange Solution brought to you by