[AS3] Chromaque effect
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | /* * This work is licensed under a Creative Commons Attribution 3.0 License. * http://creativecommons.org/licenses/by/3.0/ */ /* * Chromaque * Chromaque class combines two images * Pixels with the specifics color will be replaced with pixels of a target image * * @author: Han Sanghun (http://hangunsworld.com/blog) * @last modified: 2007-05-26 * @language version: ActionScript 3.0 */ package{ import flash.display.*; import flash.geom.*; public class Chromaque extends MovieClip{ // image dimmesions private const WID:uint = 139; private const HGT:uint = 173; // original bitmapData private var ori_bd:BitmapData; private var ori_bmp:Bitmap; // target bitmapData private var image_bd:BitmapData; // rectangles private var rect:Rectangle; private var pnt:Point; private var area_rect:Rectangle; private var area_mc:MovieClip; // chrom bitmapData private var chrom_bd:BitmapData; private var chrom_bmp:Bitmap; // result bitmapData private var result_bd:BitmapData; private var result_bmp:Bitmap; // color to be replaced var chromColor:uint = 0xFFD9D9D9; /* * Chromaque class constructor */ public function Chromaque(){ // draw original image ori_bd = new BitmapData(WID, HGT, true, 0×00000000); ori_bd.draw(source1_mc); ori_bmp = new Bitmap(ori_bd); addChild(ori_bmp); ori_bmp.y = 180; // draw target image image_bd = new BitmapData(WID, HGT, true, 0×00000000); image_bd.draw(source2_mc); // create chrom bitmapDate // you can use chrom as a mask chrom_bd = new BitmapData(WID, HGT, true, 0×00000000); // clone original bitmapDate result_bd = ori_bd.clone(); // get rectangle filled with the color to be replaced area_rect = ori_bd.getColorBoundsRect(0xFFFFFFFF, chromColor, true); // show me the area area_mc = new MovieClip(); area_mc.y = 180; area_mc.graphics.clear(); area_mc.graphics.beginFill(0xFF0000, .5); area_mc.graphics.drawRect(area_rect.x, area_rect.y, area_rect.width, area_rect.height); area_mc.graphics.endFill(); addChild(area_mc); // area_rect is used in order to minimize the “for” roof var startX:uint = area_rect.x; var startY:uint = area_rect.y; var endX:uint = startX + area_rect.width; var endY:uint = startY + area_rect.height; // lock up bitmapData chrom_bd.lock(); result_bd.lock(); for(var i:uint=startX; i<=endX; i++){ for(var k:uint=startY; k<=endY; k++){ // when the color matchs the chromColor if(ori_bd.getPixel32(i, k) == chromColor){ // paint chrom with red chrom_bd.setPixel32(i, k, 0xFFFF0000); // paint result with target image result_bd.setPixel32(i, k, image_bd.getPixel32(i, k)); } } } // unlock bitmapData chrom_bd.unlock(); result_bd.unlock(); // display chrom chrom_bmp = new Bitmap(chrom_bd); addChild(chrom_bmp); chrom_bmp.x = 150; chrom_bmp.y = 180; // display result result_bmp = new Bitmap(result_bd); addChild(result_bmp); result_bmp.x = 300; result_bmp.y = 180; }// end of Chromaque }// end of class }// end of package |
이 글을 보고 있으니 예전에 openGL 을 공부할 때가 생각나네요. stencil buffer 를 이용해서 위와 같이 렌더링하는 과제가 있었기 때문에… ^^;;