首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Flex/Actionscript白色到透明

Flex/Actionscript白色到透明
EN

Stack Overflow用户
提问于 2009-03-03 20:41:30
回答 4查看 4.5K关注 0票数 3

我正试图用actionscript在我的Flex3应用程序中编写一些东西,它将获取图像,当用户单击按钮时,它将剥离所有白色(Ish)像素并将它们转换为透明,我之所以说白色(Ish),是因为我尝试过完全白色,但我在边缘周围看到了许多伪像。我已经使用下面的代码比较接近了:

代码语言:javascript
运行
复制
targetBitmapData.threshold(sourceBitmapData, sourceBitmapData.rect, new Point(0,0), ">=", 0xFFf7f0f2, 0x00FFFFFF, 0xFFFFFFFF, true);

但是,它也会使红色或黄色消失。它为什么要这么做?我不太确定该怎么做。有没有更适合我的功能?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-03-03 21:09:31

不久前,我和一位朋友在一个项目中尝试这样做,发现用ActionScript编写一个内联方法来做这件事的速度非常慢。你必须扫描每个像素并对其进行计算,但用PixelBender做这件事被证明是闪电般的快(如果你可以使用Flash10,否则你就会坚持使用慢速AS)。

像素折弯器代码如下所示:

代码语言:javascript
运行
复制
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);
}

如果您需要在中执行此操作,则可以使用以下命令:

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2009-03-03 22:35:07

多亏了内置的threshold function,你实际上可以在没有 pixelbender和实时的情况下做到这一点:

代码语言:javascript
运行
复制
// 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
EN

Stack Overflow用户

发布于 2009-03-03 21:25:26

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

伪代码:

代码语言:javascript
运行
复制
     for each pixel in targetBitmapData
代码语言:javascript
运行
复制
             if pixel's color is >= #FFF7F0F2
代码语言:javascript
运行
复制
                     change color to #00FFFFFF

像这样的东西永远不会是完美的,因为你会失去任何浅色我会找到一个在线颜色选择器,你可以用它来准确地查看哪些颜色将被更改

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/608087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档