Playing with getPixel
Today I played a bit with getPixel function and I want to share with you some considerations about it.
First, you have to know getPixel(x,y) returns an integer that represents an RGB pixel value from a BitmapData object at a specific point (x, y).
Then, I wanted to know the percentage of a color in an image, maybe to manage level filling in a game like ColorFill (when using complex shapes to fill the level) or maybe to analyze the percentage of a color in a photo.
So I made this picture

and I linked it as bg.
Then I wrote this little actionscript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import flash.display.BitmapData;
var bitmap_bg:BitmapData = BitmapData.loadBitmap("bg");
_root.createEmptyMovieClip("background_image",_root.getNextHighestDepth());
background_image.attachBitmap(bitmap_bg,_root.getNextHighestDepth());
precision = 1;
r = 0;
g = 0;
b = 0;
other = 0;
total = 0;
for (x=0; x<500; x += precision) {
for (y=0; y<400; y += precision) {
switch (bitmap_bg.getPixel(x, y).toString(16)) {
case "ff" :
b++;
break;
case "ff00" :
g++;
break;
case "ff0000" :
r++;
break;
default :
other++;
}
total++;
}
}
r = r/total*100;
g = g/total*100;
b = b/total*100;
other = other/total*100;
trace("Red: "+r+"%\nGreen:"+g+"%\nBlue: "+b+"%\nOhter: "+other+"%"); |
It’s a very easy one:
Line 1: importing BitmapData library
Line 2: declaring a BitmapData variable called bitmap_bg containing the image linked as bg
Line 3: creating an empty movie clip where I will attach the bitmap image loaded at line 2
Line 4: attaching the bitmap, as said at line 3
Line 5: Setting a variable called precision. This is the core of the script. precision indicates the gap in pixels from the last examined pixel and the next pixel I am going to examine. The smaller precision value, the more accurate the percentage, the slower the script.
Lines 6-10: Initializing variables counting the amount of red, green and blue pixels found, and the total of pixels examined. I also count the pixels of other colors. Even if you only see a blue background, a red circle and a green heart, there are other colors due to antialiasing.
Lines 11-28: Scanning all pixels with the precision gap between one pixel and another, and incrementing the color variable according to the color found at line 13.
Lines 29-33: Formatting and showing the results
Look how results change when I play with precision
Precision: 1
Red: 8.91%
Green:7.9145%
Blue: 82.5735%
Ohter: 0.602%
Precision: 2
Red: 8.91%
Green:7.916%
Blue: 82.578%
Ohter: 0.596%
Precision: 5
Red: 8.9125%
Green:7.8625%
Blue: 82.55%
Ohter: 0.675%
Precision: 10
Red: 8.8%
Green:7.95%
Blue: 82.7%
Ohter: 0.55%
Precision: 20
Red: 8.8%
Green:8%
Blue: 82.6%
Ohter: 0.6%
As you can see, you don’t need a low (and cpu expensive) precision value to obtain realistic results.
Download the source, maybe changing the image, and tell me what do you think about. Any idea for a game?
They can be easily customized to meet the unique requirements of your project.















(3 votes, average: 4.67 out of 5)









This post has 6 comments
Shadow Scythe
I’m afaid I don’t quite understand it.
Mr. F.
Ohter.
Cut and paste isn’t your friend.
Kesh
i equally am confused at what it does.
Michael
He is simply showing how to find out how much of the image is comprised of a certain color; for instance, how many pixels out of the entire image is exactly a red pixel. You could also use some threshold to get less precision if exact red isn’t realistic.
Cool stuff, never thought of using something like this for a game. I can see it being useful for your latest ColorFill craze, lol.
Markus
Instead of comparing strings you could compare against integers:
switch (bitmap_bg.getPixel(x, y))
{
case 0x0000FF: b++; break;
case 0x00FF00: g++; break;
case 0xFF0000: r++; break;
default: other++; break;
}
Christopher Stevens
Thanks for sharing your experiment! I’m referencing this for a webcam motion sensing experiment. If the total RGB values change by a certain amount, it’s a great opportunity to capture an image and upload it to my site for future reference. Otherwise, it would save bandwidth and storage by not uploading like images.
Best regards,
Chris