From 304a5c06daf5089a639f698d353089aa1eb543a3 Mon Sep 17 00:00:00 2001 From: jdescottes Date: Sun, 6 Jul 2014 23:56:50 +0200 Subject: [PATCH] swap color : initial implementation --- src/js/drawingtools/ColorPicker.js | 20 ++++++++++++++++---- src/js/model/Frame.js | 14 +++++++------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/js/drawingtools/ColorPicker.js b/src/js/drawingtools/ColorPicker.js index 5a0de4ee..6fbf8286 100644 --- a/src/js/drawingtools/ColorPicker.js +++ b/src/js/drawingtools/ColorPicker.js @@ -20,10 +20,22 @@ ns.ColorPicker.prototype.applyToolAt = function(col, row, color, frame, overlay, event) { if (frame.containsPixel(col, row)) { var sampledColor = frame.getPixel(col, row); - if (event.button == Constants.LEFT_BUTTON) { - $.publish(Events.SELECT_PRIMARY_COLOR, [sampledColor]); - } else if (event.button == Constants.RIGHT_BUTTON) { - $.publish(Events.SELECT_SECONDARY_COLOR, [sampledColor]); + if (event.ctrlKey) { + pskl.app.piskelController.getPiskel().getLayers().forEach(function (l) { + l.getFrames().forEach(function (f) { + f.forEachPixel(function (pixelColor,x,y) { + if (pixelColor === sampledColor) { + f.setPixel(x, y, pskl.app.paletteController.getPrimaryColor()); + } + }); + }); + }); + } else { + if (event.button == Constants.LEFT_BUTTON) { + $.publish(Events.SELECT_PRIMARY_COLOR, [sampledColor]); + } else if (event.button == Constants.RIGHT_BUTTON) { + $.publish(Events.SELECT_SECONDARY_COLOR, [sampledColor]); + } } } }; diff --git a/src/js/model/Frame.js b/src/js/model/Frame.js index 8fb85ccf..433ddff4 100644 --- a/src/js/model/Frame.js +++ b/src/js/model/Frame.js @@ -84,19 +84,19 @@ return [this.id, this.version].join('-'); }; - ns.Frame.prototype.setPixel = function (col, row, color) { - if (this.containsPixel(col, row)) { - var p = this.pixels[col][row]; + ns.Frame.prototype.setPixel = function (x, y, color) { + if (this.containsPixel(x, y)) { + var p = this.pixels[x][y]; if (p !== color) { - this.pixels[col][row] = color; + this.pixels[x][y] = color; this.version++; } } }; - ns.Frame.prototype.getPixel = function (col, row) { - if (this.containsPixel(col, row)) { - return this.pixels[col][row]; + ns.Frame.prototype.getPixel = function (x, y) { + if (this.containsPixel(x, y)) { + return this.pixels[x][y]; } else { return null; }