2009-03-03 58 views
3

我正在尝试在ActionScript 3应用程序中使用actionscript编写某些东西,这些东西需要图像,当用户单击某个按钮时,它会去掉所有白色(ish)像素并将它们转换为透明,我说白色(ish),因为我尝试了完全白色,但我得到了很多边缘周围的文物。我已经有点接近使用下面的代码:Flex/Actionscript白色透明

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

但是,它也使红色或黄色消失。它为什么这样做?我不确定如何完成这项工作。有没有更适合我需求的功能?

+0

难道不是RGBA吗,你好像在口罩里做ARGB? – grapefrukt 2009-03-04 08:08:46

回答

1

一位朋友和我正在尝试为项目做一段时间,并发现编写一个内联方法,在ActionScript中这样做的速度令人难以置信。您必须扫描每个像素并对其进行计算,但使用PixelBender进行测试证明闪电般快速(如果您可以使用Flash 10,否则您会陷入缓慢的AS)。

的Pixel Bender的代码如下所示:

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

如果你需要做的,你可以使用类似:

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(); 
} 
+0

你的动作示例中的c1和c2是什么?颜色? – bkildow 2009-03-03 21:48:17

+0

是的,不好意思,我把这段代码从别的地方移开了,不得不修改一下。现在应该修好了。 – Adam 2009-03-03 21:58:57

0

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

伪代码:
       对于每个像素在targetBitmapData
               如果像素的颜色是> =#FFF7F0F2
                       改变颜色为#00FFFFFF

这样的事情永远不会是完美的,因为你会失去任何光线的颜色 我会发现,你可以用它来看看到底是什么颜色将改变在线颜色选择器

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

中的Pixel Bender代码1回答:

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

应该是:

DST =(距离(current.rgb,颜色)<阈值)? float4(0.0):current;

如果(距离(current.rgb,颜色)<阈值) DST = float4变量(0。0); else dst = float4(current);