Guest User

Untitled

a guest
Aug 16th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.    
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.MouseEvent;
  8.    
  9.     /**
  10.      * ...
  11.      * @author Feffers
  12.      */
  13.     public class Main extends Sprite {
  14.        
  15.         [Embed(source = "../lib/Picture.png")]
  16.         private var pictureClass:Class;
  17.         private var pictureBitmap:Bitmap = new pictureClass();
  18.        
  19.         private var newPixelChange:Array = new Array();
  20.         private var pictureData:BitmapData = new BitmapData(500, 400);
  21.         private var counter:int = 0;
  22.         private var storePixelChecks:Array = new Array();
  23.        
  24.         public function Main():void {
  25.             addChild(pictureBitmap);
  26.             pictureData.draw(pictureBitmap);
  27.            
  28.             stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
  29.             this.addEventListener(Event.ENTER_FRAME, enterFrame);
  30.         }
  31.        
  32.         private function enterFrame(ev:Event):void {
  33.             counter = 0;
  34.            
  35.             var currentPixelChecks:Array = storePixelChecks.concat();
  36.            
  37.             storePixelChecks = [];
  38.            
  39.             for (var n:uint = 0; n < currentPixelChecks.length; n += 2) {
  40.                 pixelCheck(currentPixelChecks[n], currentPixelChecks[n + 1], 0xFFFFFF);
  41.             }
  42.            
  43.             finishFill();
  44.            
  45.             if (currentPixelChecks.length == 0) {
  46.                 pictureBitmap.bitmapData = pictureData;
  47.             }
  48.         }
  49.        
  50.         private function mouseDown(Event:MouseEvent):void {
  51.             floodFill(stage.mouseX, stage.mouseY, 0xFFFFFF);
  52.         }
  53.        
  54.         private function floodFill(x:int, y:int, colour:int):void {
  55.             newPixelChange = new Array();
  56.             storePixelChecks = [];
  57.             counter = 0;
  58.            
  59.             pixelCheck(x, y, colour);
  60.         }
  61.        
  62.         private function finishFill():void {
  63.             for (var n:uint = 0; n < newPixelChange.length; n += 2) {
  64.                 pictureData.setPixel(newPixelChange[n], newPixelChange[n + 1], 0x00FF00);
  65.             }
  66.            
  67.             newPixelChange = [];
  68.         }
  69.        
  70.         private function pixelCheck(x:int, y:int, colour:int):void {
  71.             var surroundingPoints:Array = getSurroundingPoints(x, y, colour).concat();
  72.            
  73.             if (surroundingPoints[0] != null) {
  74.                 for (var i:uint = 0; i < surroundingPoints.length; i += 2) {
  75.                     if (surroundingPoints[i] != -1 && surroundingPoints[i + 1] != -1) {
  76.                         newPixelChange.push(surroundingPoints[i], surroundingPoints[i + 1]);
  77.                        
  78.                         ++counter;
  79.                        
  80.                         if (counter <= 20) {
  81.                             pixelCheck(surroundingPoints[i], surroundingPoints[i + 1], 0xFFFFFF);
  82.                         }else {
  83.                             storePixelChecks.push(surroundingPoints[i], surroundingPoints[i + 1]);
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88.         }
  89.        
  90.         private function getSurroundingPoints(x:int, y:int, colour:int):Array {
  91.             var sameColourArray:Array = new Array( -1, -1, -1, -1, -1, -1, -1, -1);
  92.            
  93.             if (x > 0) {
  94.                 if (pictureData.getPixel(x - 1, y) == colour) {
  95.                     sameColourArray[0] = x - 1;
  96.                     sameColourArray[1] = y;
  97.                 }
  98.             }
  99.             if (x < 500) {
  100.                 if (pictureData.getPixel(x + 1, y) == colour) {
  101.                     sameColourArray[2] = x + 1;
  102.                     sameColourArray[3] = y;
  103.                 }
  104.             }
  105.             if (y > 0) {
  106.                 if (pictureData.getPixel(x, y - 1) == colour) {
  107.                     sameColourArray[4] = x;
  108.                     sameColourArray[5] = y - 1;
  109.                 }
  110.             }
  111.             if (y < 400) {
  112.                 if (pictureData.getPixel(x, y + 1) == colour) {
  113.                     sameColourArray[6] = x;
  114.                     sameColourArray[7] = y + 1;
  115.                 }
  116.             }
  117.            
  118.             for (var l:uint = 0; l < newPixelChange.length; l += 2) {
  119.                 for (var n:uint = 0; n < sameColourArray.length; n += 2) {
  120.                     if (newPixelChange[l] == sameColourArray[n]) {
  121.                         if (newPixelChange[l + 1] == sameColourArray[n + 1]) {
  122.                             sameColourArray[n] = -1;
  123.                             sameColourArray[n + 1] = -1;
  124.                         }
  125.                     }
  126.                 }
  127.             }
  128.            
  129.             return sameColourArray;
  130.         }
  131.     }
  132. }
Add Comment
Please, Sign In to add comment