flash - How can I set a color to transparency for an import image in AS2 -
how can set color black: 0x000000 transparent, normaly magic pink transparent want set black be.
if don't understand: http://j.imagehost.org/0829/woodygx_0.jpg
i have image, , when converting 80x80 sprite want background transparent, meaning: no background, character.
notice: answer in actionscript 3, in case either decide migrate actionscript 3, moreso other people , general info.
can create new bitmapdata source bitmapdata black pixels removed (converted alpha channel).
i've created function you:
// takes source bitmapdata , converts new bitmapdata, ignoring // dark pixels below specified sensitivity. function removedarkness(source:bitmapdata, sensitivity:uint = 10000):bitmapdata { // define new bitmapdata, size constraints ensure loop // doesn't time out / crash. // demonstration only, consider creating class manages // portions of bitmapdata @ time (up 50,000 iterations per // frame) , dispatches event new bitmapdata when done. var fresh:bitmapdata = new bitmapdata( math.min(600, source.width), math.min(400, source.height), true, 0xffffffff ); fresh.lock(); // remove listed colors. for(var v:int = 0; v < fresh.height; v++) { for(var h:int = 0; h < fresh.width; h++) { // select relevant pixel iteration. var pixel:uint = source.getpixel(h, v); // check against colors remove. if(pixel <= sensitivity) { // match - delete pixel (fill transparent pixel). fresh.setpixel32(h, v, 0x00000000); continue; } // no match, fill expected color. fresh.setpixel(h, v, pixel); } } // we're done modifying new bitmapdata. fresh.unlock(); return fresh; } as can see, takes:
- bitmapdata want remove darker pixels from.
- uint representing how many shades of black / grey want have removed.
and here's demo using source image:
var original:loader = new loader(); original.load( new urlrequest("http://j.imagehost.org/0829/woodygx_0.jpg") ); original.contentloaderinfo.addeventlistener(event.complete, imageloaded); // original image has loaded, continue. function imageloaded(e:event):void { // capture pixels loaded bitmap. var obmd:bitmapdata = new bitmapdata(original.width, original.height, false, 0); obmd.draw(original); // create new bitmapdata without black pixels. var herosheet:bitmapdata = removedarkness(obmd, 1200000); addchild( new bitmap(herosheet) ); }
Comments
Post a Comment