Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.MouseEvent;
- /**
- * ...
- * @author Feffers
- */
- public class Main extends Sprite {
- [Embed(source = "../lib/Picture.png")]
- private var pictureClass:Class;
- private var pictureBitmap:Bitmap = new pictureClass();
- private var newPixelChange:Array = new Array();
- private var pictureData:BitmapData = new BitmapData(500, 400);
- private var counter:int = 0;
- private var storePixelChecks:Array = new Array();
- public function Main():void {
- addChild(pictureBitmap);
- pictureData.draw(pictureBitmap);
- stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
- this.addEventListener(Event.ENTER_FRAME, enterFrame);
- }
- private function enterFrame(ev:Event):void {
- counter = 0;
- var currentPixelChecks:Array = storePixelChecks.concat();
- storePixelChecks = [];
- for (var n:uint = 0; n < currentPixelChecks.length; n += 2) {
- pixelCheck(currentPixelChecks[n], currentPixelChecks[n + 1], 0xFFFFFF);
- }
- finishFill();
- if (currentPixelChecks.length == 0) {
- pictureBitmap.bitmapData = pictureData;
- }
- }
- private function mouseDown(Event:MouseEvent):void {
- floodFill(stage.mouseX, stage.mouseY, 0xFFFFFF);
- }
- private function floodFill(x:int, y:int, colour:int):void {
- newPixelChange = new Array();
- storePixelChecks = [];
- counter = 0;
- pixelCheck(x, y, colour);
- }
- private function finishFill():void {
- for (var n:uint = 0; n < newPixelChange.length; n += 2) {
- pictureData.setPixel(newPixelChange[n], newPixelChange[n + 1], 0x00FF00);
- }
- newPixelChange = [];
- }
- private function pixelCheck(x:int, y:int, colour:int):void {
- var surroundingPoints:Array = getSurroundingPoints(x, y, colour).concat();
- if (surroundingPoints[0] != null) {
- for (var i:uint = 0; i < surroundingPoints.length; i += 2) {
- if (surroundingPoints[i] != -1 && surroundingPoints[i + 1] != -1) {
- newPixelChange.push(surroundingPoints[i], surroundingPoints[i + 1]);
- ++counter;
- if (counter <= 20) {
- pixelCheck(surroundingPoints[i], surroundingPoints[i + 1], 0xFFFFFF);
- }else {
- storePixelChecks.push(surroundingPoints[i], surroundingPoints[i + 1]);
- }
- }
- }
- }
- }
- private function getSurroundingPoints(x:int, y:int, colour:int):Array {
- var sameColourArray:Array = new Array( -1, -1, -1, -1, -1, -1, -1, -1);
- if (x > 0) {
- if (pictureData.getPixel(x - 1, y) == colour) {
- sameColourArray[0] = x - 1;
- sameColourArray[1] = y;
- }
- }
- if (x < 500) {
- if (pictureData.getPixel(x + 1, y) == colour) {
- sameColourArray[2] = x + 1;
- sameColourArray[3] = y;
- }
- }
- if (y > 0) {
- if (pictureData.getPixel(x, y - 1) == colour) {
- sameColourArray[4] = x;
- sameColourArray[5] = y - 1;
- }
- }
- if (y < 400) {
- if (pictureData.getPixel(x, y + 1) == colour) {
- sameColourArray[6] = x;
- sameColourArray[7] = y + 1;
- }
- }
- for (var l:uint = 0; l < newPixelChange.length; l += 2) {
- for (var n:uint = 0; n < sameColourArray.length; n += 2) {
- if (newPixelChange[l] == sameColourArray[n]) {
- if (newPixelChange[l + 1] == sameColourArray[n + 1]) {
- sameColourArray[n] = -1;
- sameColourArray[n + 1] = -1;
- }
- }
- }
- }
- return sameColourArray;
- }
- }
- }
Add Comment
Please, Sign In to add comment