Flex / Actionscript White to Transparent

I'm trying to write something in my Flex 3 application using ActionScript that will have an image, and when the user clicks the button, it will strip all the white (ish) pixels and convert them to transparent, I say white (ish) because I tried exactly white, but I have a lot of artifacts around the edges. I got a little closer using the following code:

targetBitmapData.threshold(sourceBitmapData, sourceBitmapData.rect, new Point(0,0), ">=", 0xFFf7f0f2, 0x00FFFFFF, 0xFFFFFFFF, true);

However, it also fades red or yellow. Why is he doing this? I'm not quite sure how to do this. Is there another feature that works best for my needs?

+3
source share
4 answers

, ActionScript . , PixelBender ( Flash 10, AS).

- :

input image4 src;
output float4 dst;

// How close of a match you want
parameter float threshold
<
  minValue:     0.0;
  maxValue:     1.0;
  defaultValue: 0.4;
>;

// Color you are matching against.
parameter float3 color
<
  defaultValue: float3(1.0, 1.0, 1.0);
>;

void evaluatePixel()
{
  float4 current = sampleNearest(src, outCoord());
  dst = float4((distance(current.rgb, color) < threshold) ? 0.0 : current);
}

AS, - :

function threshold(source:BitmapData, dest:BitmapData, color:uint, threshold:Number) {
  dest.lock();

  var x:uint, y:uint;
  for (y = 0; y < source.height; y++) {
    for (x = 0; x < source.width; x++) {
      var c1:uint = source.getPixel(x, y);
      var c2:uint = color;
      var rx:uint = Math.abs(((c1 & 0xff0000) >> 16) - ((c2 & 0xff0000) >> 16));
      var gx:uint = Math.abs(((c1 & 0xff00) >> 8) - ((c2 & 0xff00) >> 8));
      var bx:uint = Math.abs((c1 & 0xff) - (c2 & 0xff));

      var dist = Math.sqrt(rx*rx + gx*gx + bx*bx);

      if (dist <= threshold)
        dest.setPixel(x, y, 0x00ffffff);
      else
        dest.setPixel(x, y, c1);
    }
  }
  dest.unlock();
}
+1

pixelbender :

// Creates a new transparent BitmapData (in case the source is opaque)
var dest:BitmapData = new BitmapData(source.width,source.height,true,0x00000000);

// Copies the source pixels onto it
dest.draw(source);

// Replaces all the pixels greater than 0xf1f1f1 by transparent pixels
dest.threshold(source, source.rect, new Point(), ">", 0xfff1f1f1,0x00000000);

// And here you go ...  
addChild(new Bitmap(dest));     
+1

, .

:
        targetBitmapData
                                            # 00FFFFFF

something like this will never be perfect because you lose any light colors I would find an online color picker that you can use to see exactly which colors will change.

0
source

1 pixel response:

dst = float4 ((distance (current.rgb, color) <threshold)? 0.0: current);

it should be:

dst = (distance (current .rgb, color) <threshold)? float4 (0.0): current;

or

if (distance (current.rgb, color) <threshold) dst = float4 (0,0); another dst = float4 (current);

0
source

Source: https://habr.com/ru/post/1704159/


All Articles