【问题标题】:Flex/Actionscript White to TransparentFlex/Actionscript 白色到透明
【发布时间】:2009-03-03 20:41:29
【问题描述】:

我正在尝试在我的 Flex 3 应用程序中使用将拍摄图像的动作脚本编写一些内容,当用户单击按钮时,它将去除所有白色(ish)像素并将它们转换为透明,我说白色( ish)因为我已经尝试过完全白色,但我在边缘得到了很多伪影。使用以下代码,我已经有点接近了:

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

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

【问题讨论】:

  • 不是RGBA吗,你的面具好像在做ARGB?

标签: apache-flex actionscript-3 colors


【解决方案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();
}

【讨论】:

  • 你的动作脚本示例中的 c1 和 c2 是什么?颜色?
  • 是的,抱歉,我从其他地方移动了这段代码,不得不稍作修改。现在应该修好了。
  • 另请注意,在 dest.setPixel 之后缺少一对行
  • 这太好了,谢谢,谢谢!我唯一看到的是在您的动作脚本示例中,我认为该行: dest.setPixel(x, y, 0x00ffffff);应该是 dest.setPixel32(x, y, 0x00ffffff);设置 Alpha 通道。
【解决方案2】:

借助内置的threshold function,您实际上可以不使用 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));     

【讨论】:

    【解决方案3】:

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

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

    这样的事情永远不会完美,因为你会失去任何浅色 我会找到一个在线颜色选择器,您可以使用它来准确查看要更改的颜色

    【讨论】:

      【解决方案4】:

      像素弯曲代码1中的答案:

      dst = float4((distance(current.rgb, color)

      应该是:

      dst = (距离(current.rgb, color)

      如果(距离(当前.rgb,颜色)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-29
        • 2015-04-25
        • 2015-09-05
        • 1970-01-01
        • 2011-06-25
        • 2014-09-12
        相关资源
        最近更新 更多