views:

224

answers:

2

I am trying to loop through a bitmap and determine if each pixel is lighter or darker than gray using getPixel(). Problem is, I am not sure how to tell whether the value returned by getPixel() is darker or lighter than gray.

Neutral gray is about 0x808080 or R:127, G:127, B:127. How would I need to modify the code below to accurately determine this?

for (var dx:int=0; dx < objectWidth; dx++)
{  
    for (var dy:int=0; dy < objectHeight; dy++)
    {
         if (testBmd.getPixel(dx, dy) > GRAY)
         {
             trace("Lighter than gray!");
         } else {
             trace("Darker than gray!");
         }
    }
}
+3  A: 

Luminance is the answer - Math needed and explanation here:

http://www.scantips.com/lumin.html

you know how to continue :)

Edit:

on livedocs (livedocs - BitmapData - getPixel32()), you can see in example, how they get r,g,b, values from getPixel32() return value. Maybe you can use i:]

Also, Richard's answer looks like it already does what you need, although if you combine it with example from above - voilla - you've got yourself an luminance comparison :]

Adam Kiss
Excellent reading material. Thank you much.
cmal
Part of my problem is that I don't even understand what format the values returned by getPixel() are in. They don't seem to be standard hex values. Without even knowing this, any discussion of luminance in relation to RGB values won't help me.
cmal
@cmal getPixel() returns an RGB value as a hexadecimal, getPixel32() returns ARGB value as hexadecimal. Check you are getting a pixel at the correct location if values are not as you expect.
Allan
The values I'm getting back just don't seem to be standard hex values to me. I expect non-alpha hex values to be 6 characters, like 1AFF00, but the values I'm getting back are 8421504, 16777215, 15790320, and so on. I have no idea what this type of value represents or what to do with it. Anybody know?
cmal
I case you can't tell, I'm very new to working with numeric colors. I'm pretty sure now that the values I'm getting back are decimal colors, created through the formula: (r * 65536) + (g * 256) + (b). However, I have no idea how to solve that formula to get individual, 3-digit numbers for R, G, and B. Once I had that I could use a luminance formula and be set. Please help me!
cmal
cmal, numbers are numbers - 1AFF00 is the same number as 1769216, except displayed in base 16 instead of base 10. The numbers you get from getPixel are regular colors. To display them in hex format, try:trace( bmd.getPixel(x,y).toString(16) );
fenomas
Thanks for helping out, everyone. I get it now.
cmal
+4  A: 

To extend Adam's answer a bit further, you could generate a luminance value using a function like this...

function luminance(myRGB:int):int {
//returns a luminance value between 0 and 255
var R:int = (myRGB / 65536) % 256;
var G:int = (myRGB / 256) % 256;
var B:int = myRGB % 256;
return ((0.3*R)+(0.59*G)+(0.11*B));
}

Then you can test for your 50% grey threshold like this:

if (luminance(testBmd.getPixel(dx, dy)) > 127)
Richard Inglis
great simplification :] Together, we're helpful :]
Adam Kiss
Super helpful. Thank you.
cmal