开发者

Coloring grayscale image as3

I need to dynamically color a grayscal开发者_如何学编程e gradient bitmap with ActionScript 3.0. Gradient goes form white to black, the darkest color has to be 0xFF0000, lightest 0xFFFFFF. Tried paletteMap and ColorMatrixFilter with mixed result and no stable solution.


        var color:uint = 0xFF0000;
        var r:uint = (color >> 16) & 0xFF;
        var g:uint = (color >> 8) & 0xFF;
        var b:uint = color & 0xFF;

        var n:Number = 1/3;

        var matrix:Array = new Array();
        matrix = matrix.concat([n,n,n,0,r]);
        matrix = matrix.concat([n,n,n,0,g]);
        matrix = matrix.concat([n,n,n,0,b]);
        matrix = matrix.concat([0,0,0,1,0]);

        _bitmapData.applyFilter(_bitmapData, _bitmapData.rect, new Point(),  new ColorMatrixFilter(matrix));'


Very possible to do with a ColorMatrixFilter, by building the matrix with a 255 offset on the red channel (but an identity matrix otherwise).

This sample Document class assumes the FLA library has a 720 x 480 exported symbol "GradientBMD" which is a flash.display.BitmapData subclass:

package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Point;
    import flash.filters.*;

    public class GreyscaleDemo extends Sprite
    {
        public function GreyscaleDemo()
        {
            var bmd:BitmapData = new GradientBMD(720,480);

            var matrix:Array = new Array();
            matrix = matrix.concat([1,0,0,0,255]);
            matrix = matrix.concat([0,1,0,0,0]);
            matrix = matrix.concat([0,0,1,0,0]);
            matrix = matrix.concat([0,0,0,1,0]);

            var colorFilter:ColorMatrixFilter = new ColorMatrixFilter(matrix);
            bmd.applyFilter(bmd, bmd.rect, new Point(0,0), colorFilter);

            var bitmap:Bitmap = new Bitmap(bmd);
            this.addChild(bitmap);
        }
    }
}

You could also create a new bitmapData filled with the color you want the gradient to ramp to (in place of black) and copy it into your gradient with BlendMode.ADD:

package
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.BlendMode;
    import flash.geom.Point;

    public class GreyscaleDemo extends Sprite
    {
        public function GreyscaleDemo()
        {
            var bmd:BitmapData = new GradientBMD(720,480);
            var bitmap:Bitmap = new Bitmap(bmd);
            this.addChild(bitmap);

            var bmd2:BitmapData = new BitmapData(720, 480, false, 0xFF0000);
            bmd.draw(bmd2, null, null, BlendMode.ADD, bmd.rect,false);
        }
    }
}


Instead of using the ColorMatrixFilter (described in another answer) you can also use the blendMode="luminosity" option as well.

I have a blog post on grayscale images in ActionScript that might help: http://flexdevtips.blogspot.com/2011/01/grayscale-images-progressbar-rating.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜