将彩色图像转换为单个颜色as3

时间:2010-06-10 07:35:03

标签: flash actionscript-3

如何将彩色图像转换为单个颜色as3?

2 个答案:

答案 0 :(得分:1)

我认为你的意思就像下面的例子:Multi color image 在这个例子中,我拍摄了一张图像并使其成为灰度(上左)。右上角是相同的灰度图像,但后面是红色叠加,并将混合模式设置为变暗。 Samen有蓝色和黄色。要在闪光灯中制作灰度图像,必须为红色蓝色和绿色通道设置相同的值。你可以在flash中轻松完成。首先,你必须有一个bitmapData对象,其中包含图片。然后将其制作灰度,最后制作叠加层并将其混合在一起。

要获取带有图像的bitmapdata,您可以按照示例@ adobe reference

进行操作

我做了一个示例课程(来自示例@adobe),这正是我上面所说的:

    package {
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.display.BitmapDataChannel;
    import flash.display.BlendMode;

    import flash.events.*;

    import flash.geom.Point;
    import flash.geom.Rectangle;

    import flash.net.URLRequest;

    [SWF(width='900', height='481', backgroundColor='#FFFFFF', frameRate='30')]
    public class BitmapExample extends Sprite {
        private var url:String = "test.jpg";
        private var fillColor:uint = 0xFF0000; //0xFF0000 = Red

        public function BitmapExample() {
            configureAssets();
        }

        private function configureAssets():void {
            var loader:Loader = new Loader();
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

            var request:URLRequest = new URLRequest(url);
            loader.load(request);
        }

        private function completeHandler(event:Event):void {
            var loader:Loader = Loader(event.target.loader);
            var image:Bitmap = Bitmap(loader.content);
            var bitmapData:BitmapData = image.bitmapData.clone();

            //Now we have the bitmapData we can make it grayscale.
            //First lock the data so it shows no changes while we are doing the changes.
            bitmapData.lock();
            //We now copy the red channel to the blue and the green channel.
            bitmapData.copyChannel(bitmapData,bitmapData.rect,new Point(),BitmapDataChannel.RED,BitmapDataChannel.BLUE);
            bitmapData.copyChannel(bitmapData,bitmapData.rect,new Point(),BitmapDataChannel.RED,BitmapDataChannel.GREEN);
            //After the change we can unlock the bitmapData.
            bitmapData.unlock();


            //Create the overlay with the color set in the private var fillColor
            var overlay:Bitmap = this.makeOverlayBitMap(bitmapData.width, bitmapData.height, fillColor);
            //Set the blendMode to darken so it will will be just like the picture in the post.
            overlay.blendMode = BlendMode.DARKEN;

            //Add original to the stage
            image.x = 0;
            image.y = 0;
            addChild(image);

            //Add a copy next to it with the grayscale data
            var image2:Bitmap = new Bitmap(bitmapData);
            image2.x = image.width;
            image2.y = 0;
            addChild(image2);

            //Add the overlay to the stage at position of the grayscale
            overlay.x = image2.x;
            overlay.y = image2.y;
            addChild(overlay);

        }

        private function makeOverlayBitMap(theWidth:int, theHeight:int, theColor:uint):Bitmap{
            var bitmapData:BitmapData = new BitmapData(theWidth,theHeight,false, theColor);
            var returnBit:Bitmap = new Bitmap(bitmapData);
            return returnBit;
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Unable to load image: " + url);
        }
    }
}

我希望其中的代码和评论能够进行讨论。如果您有疑问,请询问他们。

答案 1 :(得分:0)