3

I'm trying to replace a color and colors near it in a bitmap.

threshold() seems to work but it seems to be that you have to specify the exact color "==" or all colors before or after the exact color "<" & ">" plus "<=" and ">=". I am hoping that the mask parameter will help me find a way to find a color and a dynamic range of colors before and after it to be replaced. What is its intended usage?

Per the comment below Example 1 and 2:

bit.threshold(bit, bit.rect, point, ">", 0xff000000, 0xffff0000, 0x00FF0000); 

bit.threshold(bit, bit.rect, point, ">", 0xff000000, 0xffff0000, 0x00EE0000);
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

2 Answers2

1

If you're trying to do a flood fill, I don't think the mask parameter will help you. The mask parameter lets you ignore parts of the color in the test. In your case, you want to take into account all the channels of the color, you just want the matching to be fuzzy.

e.g. If you want to replace all pixels where the red component is 0, you can set mask to 0x00FF0000, so it will ignore the other channels.

The implementation pseudo-code probably looks something like this:

input = readPixel()
value = input & mask
if(value operation threshold)
{
    writePixel(color)
}

Neither of your samples will produce anything because the mask limits the values to be between 0x00000000 and 0x00FF0000, then tests if they're greater than 0xFF000000.

Sean Fujiwara
  • 4,506
  • 22
  • 34
  • Can you explain more? Are you saying that a pixel that has no amount of red in it is ignored if I set the mask to 100% 0x00FF0000 (the red channel)? What if I set the mask to 0x00EE0000? I will post an example in a bit in the main post. – 1.21 gigawatts Aug 20 '11 at 19:36
  • No, 0x00FF0000 will ignore everything except red. 0xEE = 1110 1110, so it will ignore every other shade of red and some blocks... it doesn't really make sense for images. I think it's intended use is to filter entire channels. – Sean Fujiwara Aug 20 '11 at 22:43
  • I feel like I'm pushing my luck but I'm still struggling to put that into practice. Could you explain what each of the following do? (added the examples to the question) – 1.21 gigawatts Aug 21 '11 at 01:38
1

I have also done this and eventually, I have found it best to create my own threshold-method. You can find it below. Everything is explained in comment.

//_snapshot is a bitmapData-object
for(var i:int = 0; i <= _snapshot.width; i++)
{
    for(var j:int = 0; j <= _snapshot.height; j++)
    {
        //We get the color of the current pixel.
        var _color:uint = _snapshot.getPixel(i, j);                     

        //If the color of the selected pixel is between certain values set by the user, 
        //set the filtered pixel data to green. 
        //Threshold is a number (can be quite high, up to 50000) to look for adjacent colors in the colorspace.
        //_colorToCompare is the color you want to look for.
        if((_colorToCompare - (100 * _threshold)) <= _color && _color <= (_colorToCompare + (100 * _threshold)))
        {
            //This sets the pixel value.
            _snapshot.setPixel(i, j, 0x00ff00);
        }
        else
        {
            //If the pixel color is not within the desired range, set it's value to black.
            _snapshot.setPixel(i, j, 0x000000);
        }
    }
}
Michiel Standaert
  • 4,096
  • 7
  • 27
  • 49