diff --git a/src/js/controller/ToolController.js b/src/js/controller/ToolController.js index 0ea26d64..6b895ffb 100644 --- a/src/js/controller/ToolController.js +++ b/src/js/controller/ToolController.js @@ -18,7 +18,8 @@ toDescriptor('rectangleSelect', 'S', new pskl.drawingtools.RectangleSelect()), toDescriptor('shapeSelect', 'Z', new pskl.drawingtools.ShapeSelect()), toDescriptor('lighten', 'U', new pskl.drawingtools.Lighten()), - toDescriptor('colorPicker', 'O', new pskl.drawingtools.ColorPicker()) + toDescriptor('colorPicker', 'O', new pskl.drawingtools.ColorPicker()), + toDescriptor('colorSwap', 'F', new pskl.drawingtools.ColorSwap()) ]; this.currentSelectedTool = this.tools[0]; diff --git a/src/js/drawingtools/ColorSwap.js b/src/js/drawingtools/ColorSwap.js new file mode 100644 index 00000000..9b4e4307 --- /dev/null +++ b/src/js/drawingtools/ColorSwap.js @@ -0,0 +1,45 @@ +/** + * @provide pskl.drawingtools.ColorSwap + * + * @require pskl.utils + */ +(function() { + var ns = $.namespace("pskl.drawingtools"); + + ns.ColorSwap = function() { + this.toolId = "tool-colorswap"; + this.helpText = "Color swap"; + }; + + pskl.utils.inherit(ns.ColorSwap, ns.BaseTool); + + /** + * @override + */ + ns.ColorSwap.prototype.applyToolAt = function(col, row, color, frame, overlay, event) { + if (frame.containsPixel(col, row)) { + var sampledColor = frame.getPixel(col, row); + console.time('swapColors'); + this.swapColors(sampledColor, color); + console.timeEnd('swapColors'); + + $.publish(Events.PISKEL_SAVE_STATE, { + type : pskl.service.HistoryService.SNAPSHOT + }); + } + }; + + ns.ColorSwap.prototype.swapColors = function(oldColor, newColor) { + var swapPixels = function (pixelColor,x,y,frame) { + if (pixelColor == oldColor) { + frame.pixels[x][y] = newColor; + } + }; + pskl.app.piskelController.getPiskel().getLayers().forEach(function (l) { + l.getFrames().forEach(function (f) { + f.forEachPixel(swapPixels); + f.version++; + }); + }); + }; +})(); diff --git a/src/js/model/Frame.js b/src/js/model/Frame.js index 433ddff4..8de6510e 100644 --- a/src/js/model/Frame.js +++ b/src/js/model/Frame.js @@ -96,16 +96,22 @@ ns.Frame.prototype.getPixel = function (x, y) { if (this.containsPixel(x, y)) { - return this.pixels[x][y]; + return this._unsafeGetPixel(x,y); } else { return null; } }; + ns.Frame.prototype._unsafeGetPixel = function (x, y) { + return this.pixels[x][y]; + }; + ns.Frame.prototype.forEachPixel = function (callback) { - for (var col = 0 ; col < this.getWidth() ; col++) { - for (var row = 0 ; row < this.getHeight() ; row++) { - callback(this.getPixel(col, row), col, row); + var width = this.getWidth(); + var height = this.getHeight(); + for (var col = 0 ; col < width ; col++) { + for (var row = 0 ; row < height ; row++) { + callback(this._unsafeGetPixel(col, row), col, row, this); } } }; diff --git a/src/piskel-script-list.js b/src/piskel-script-list.js index e47c98f6..98ecf14b 100644 --- a/src/piskel-script-list.js +++ b/src/piskel-script-list.js @@ -131,6 +131,7 @@ "js/drawingtools/selectiontools/RectangleSelect.js", "js/drawingtools/selectiontools/ShapeSelect.js", "js/drawingtools/ColorPicker.js", + "js/drawingtools/ColorSwap.js", // Application controller and initialization "js/app.js" ]; \ No newline at end of file