(Flash) Color range selection

| No Comments | No TrackBacks

It must be very useful of a function equivalent to 'Color Range' selection in Photoshop can be realized with Flash.

First, I thought about majoring the distance of the color of each pixel to the sample color using the Pythagorean theorem, but I abandoned it because the workload to doing it pixel by pixel must be too heavy.

Instead of that, I made it with Bitmap.threshold method. This may be rather rough way of doing it, but the result is satisfactory and fast.



package {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    public class BmpColorRange extends Sprite {
        var src:BitmapData;
        var canvas:BitmapData;
        public function BmpColorRange(s:BitmapData) {
            src = s;
            canvas = new BitmapData(s.width, s.height, true, 0x00000000);
            addChild(new Bitmap(canvas));
        }
        public function select(col:uint):void {
            var tmp:BitmapData = new BitmapData(src.width, src.height, true, col | 0xff000000);
            var r:uint, g:uint, b:uint, rmin:uint, gmin:uint, bmin:uint, rmax:uint, gmax:uint, bmax:uint;
            var min:uint, max:uint;
            var range:uint = 30;
            r = col >> 16 & 0xfff;
            g = col >> 8 & 0xff;
            b = col & 0xff;
           
            rmax = Math.max(0, r - range);
            gmax = Math.max(0, g - range);
            bmax = Math.max(0, b - range);
           
            rmin = Math.min(255, r + range);
            gmin = Math.min(255, g + range);
            bmin = Math.min(255, b + range);
           
            min = rmin << 16 | gmin << 8 | bmin;
            max = rmax << 16 | gmax << 8 | bmax;
           
            tmp.draw(src);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), ">=", min, 0xff000000, 0x00ff0000, true);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), ">=", min, 0xff000000, 0x0000ff00, true);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), ">=", min, 0xff000000, 0x000000ff, true);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), "<=", max, 0xff000000, 0x00ff0000, true);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), "<=", max, 0xff000000, 0x0000ff00, true);
            tmp.threshold(tmp, new Rectangle(0, 0, tmp.width, tmp.height), new Point(0,0), "<=", max, 0xff000000, 0x000000ff, true);
            canvas.draw(tmp);
        }
    }
}

No TrackBacks

TrackBack URL: http://www.kynd.info/cp-bin/mt/mt-tb.cgi/113

Leave a comment