加载时如何从图像中获取颜色?

时间:2014-01-28 06:10:24

标签: actionscript-3 flex colors flex4

我正在加载SWF图像文件。每次加载图像时,我都想要图像的颜色。 我使用blendMode.INVERT,它将图像的颜色更改为黑色。就像在Flex中用几行代码获得图像颜色一样。

var imag1:Image=currentReference.getChildAt(0) as Image;
imag1.blendMode = BlendMode.INVERT;
Alert.show("Image Path-->  "+ imag1.source + "  Color-->  "+ imag1.blendMode);

2 个答案:

答案 0 :(得分:0)

阅读BitmapData。它应该有助于引导你朝着正确的方向前进。

答案 1 :(得分:0)

您可以使用BitmapData - getPixel()方法 类似的东西:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoadComplete, false, 0,   true);
loader.load(new URLRequest('imageName.jpg'));

var loadedImageIndexColours:Vector.<uint>;

function onImageLoadComplete( event:Event ):void
{
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onImageLoadComplete);
    loadedImageIndexColours = getIndexColours(Bitmap(loader.contentLoaderInfo.content).bitmapData);
    loader = null;
    trace(loadedImageIndexColours);
}

function getIndexColours(bitmapData:BitmapData, unique:Boolean = true):Vector.<uint>
{
    var colors:Vector.<uint> = new Vector.<uint>();

    for (var x:int = 0; x < bitmapData.width; x++)
    {
        for (var y:int = 0; y < bitmapData.height; y++)
        {
            colors.push(bitmapData.getPixel(x, y));
        }
    }

    return (unique) ? removeDuplicates(colors) : colors;
}

function removeDuplicates(inVector:Vector.<uint>):Vector.<uint>
{
    return inVector.filter(_removeDuplicatesFilter);
}

function _removeDuplicatesFilter(e:uint, i:int, inVector:Vector.<uint>):Boolean
{
    return (i == 0) ? true : inVector.lastIndexOf(e, i - 1) == -1;
}