Flex / Actionscript白色到透明

时间:2009-03-03 20:41:30

标签: flex actionscript-3 colors

我正在尝试使用动作脚本在我的Flex 3应用程序中编写一些内容,该动作脚本会拍摄图像,当用户点击按钮时,它会剥离所有白色(ish)像素并将它们转换为透明,我说白色( ish)因为我试过了白色,但是边缘周围有很多文物。我使用以下代码有点接近:

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

然而,它也会使红色或黄色消失。它为什么这样做?我不确定如何使这项工作。还有其他功能更适合我的需求吗?

4 个答案:

答案 0 :(得分:1)

一位朋友和我试图在一段时间内为一个项目做这件事,发现编写一个在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 :(得分:1)

你实际上可以没有 pixelbender和实时,这要归功于内置的threshold function

// 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));     

答案 2 :(得分:0)

看起来上面的代码会使一系列颜色透明。

伪代码:
    对于targetBitmapData中的每个像素         如果像素的颜色是&gt; =#FFF7F0F2
            将颜色更改为#00FFFFFF

这样的事情永远不会是完美的,因为你会失去任何浅色 我会找到一个在线颜色选择器,你可以用它来确切地看到将改变哪些颜色

答案 3 :(得分:0)

像素弯曲代码中的答案:

dst = float4((距离(current.rgb,颜色)&lt; threshold)?0.0:当前);

应该是:

dst =(距离(current.rgb,颜色)&lt; threshold)? float4(0.0):current;

if(distance(current.rgb,color)&lt; threshold)     dst = float4(0.0); 其他     dst = float4(当前);