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

ACTIONSCRIPT:
  1. import flash.display.BitmapData;
  2. var bitmap_bg:BitmapData = BitmapData.loadBitmap("bg");
  3. _root.createEmptyMovieClip("background_image",_root.getNextHighestDepth());
  4. background_image.attachBitmap(bitmap_bg,_root.getNextHighestDepth());
  5. precision = 1;
  6. r = 0;
  7. g = 0;
  8. b = 0;
  9. other = 0;
  10. total = 0;
  11. for (x=0; x<500; x += precision) {
  12.     for (y=0; y<400; y += precision) {
  13.         switch (bitmap_bg.getPixel(x, y).toString(16)) {
  14.             case "ff" :
  15.                 b++;
  16.                 break;
  17.             case "ff00" :
  18.                 g++;
  19.                 break;
  20.             case "ff0000" :
  21.                 r++;
  22.                 break;
  23.             default :
  24.                 other++;
  25.         }
  26.         total++;
  27.     }
  28. }
  29. r = r/total*100;
  30. g = g/total*100;
  31. b = b/total*100;
  32. other = other/total*100;
  33. 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?

Improve the blog rating this post
Tell me what do you think about this post. I'll write better and better entries.
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.5 out of 5)
Loading ... Loading ...

» Flash Templates provided by Template Monster are pre-made web design products developed using Flash technology.
They can be easily customized to meet the unique requirements of your project.

5 Responses to “Playing with getPixel”

  1. Shadow Scythe on June 30th, 2008 2:05 am

    I’m afaid I don’t quite understand it.

  2. Mr. F. on June 30th, 2008 3:54 am

    Ohter.

    Cut and paste isn’t your friend.

  3. Kesh on June 30th, 2008 4:13 am

    i equally am confused at what it does.

  4. Michael on June 30th, 2008 6:26 am

    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.

  5. Markus on June 30th, 2008 10:12 pm

    Instead of comparing strings you could compare against integers:

    switch (bitmap_bg.getPixel(x, y))
    {
    case 0×0000FF: b++; break;
    case 0×00FF00: g++; break;
    case 0xFF0000: r++; break;
    default: other++; break;
    }

Leave a Reply