From 37aa6c3d72c80ac4af0fd56671c8488e930f23bd Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Sat, 27 Sep 2014 01:22:20 +0200 Subject: [PATCH] sort colors on image import for palette --- src/js/service/CurrentColorsService.js | 40 +++++++++++++++++++------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/js/service/CurrentColorsService.js b/src/js/service/CurrentColorsService.js index d32f1aa4..169a67bc 100644 --- a/src/js/service/CurrentColorsService.js +++ b/src/js/service/CurrentColorsService.js @@ -8,6 +8,7 @@ this.cachedFrameProcessor.setFrameProcessor(this.getFrameColors_.bind(this)); this.colorSorter = new pskl.service.color.ColorSorter(); + this.paletteService = pskl.app.paletteService; this.framesColorsCache_ = {}; }; @@ -15,24 +16,35 @@ ns.CurrentColorsService.prototype.init = function () { $.subscribe(Events.PISKEL_RESET, this.onPiskelUpdated_.bind(this)); $.subscribe(Events.TOOL_RELEASED, this.onPiskelUpdated_.bind(this)); + $.subscribe(Events.USER_SETTINGS_CHANGED, $.proxy(this.onUserSettingsChange_, this)); + }; - // this.updateTimer = window.setInterval(this.updateCurrentColors_.bind(this)); + ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () { + var paletteId = pskl.UserSettings.get(pskl.UserSettings.SELECTED_PALETTE); + var palette = this.paletteService.getPaletteById(paletteId); + + return palette.id === Constants.CURRENT_COLORS_PALETTE_ID; + }; + + ns.CurrentColorsService.prototype.onUserSettingsChange_ = function (evt, name, value) { + if (name == pskl.UserSettings.SELECTED_PALETTE) { + if (this.isCurrentColorsPaletteSelected_()) { + this.updateCurrentColors_(); + } + } }; ns.CurrentColorsService.prototype.getCurrentColors = function () { return this.currentColors; }; - ns.CurrentColorsService.prototype.getFrameColors_ = function (frame) { - var frameColors = {}; - frame.forEachPixel(function (color, x, y) { - var hexColor = this.toHexColor_(color); - frameColors[hexColor] = true; - }.bind(this)); - return frameColors; + ns.CurrentColorsService.prototype.onPiskelUpdated_ = function (evt) { + if (this.isCurrentColorsPaletteSelected_()) { + this.updateCurrentColors_(); + } }; - ns.CurrentColorsService.prototype.onPiskelUpdated_ = function (evt) { + ns.CurrentColorsService.prototype.updateCurrentColors_ = function () { var layers = this.piskelController.getLayers(); var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);}); var colors = {}; @@ -50,12 +62,20 @@ // limit the array to the max colors to display var colorsArray = Object.keys(colors).slice(0, Constants.MAX_CURRENT_COLORS_DISPLAYED); this.currentColors = this.colorSorter.sort(colorsArray); - console.log(this.currentColors); // TODO : only fire if there was a change $.publish(Events.CURRENT_COLORS_UPDATED); }; + ns.CurrentColorsService.prototype.getFrameColors_ = function (frame) { + var frameColors = {}; + frame.forEachPixel(function (color, x, y) { + var hexColor = this.toHexColor_(color); + frameColors[hexColor] = true; + }.bind(this)); + return frameColors; + }; + ns.CurrentColorsService.prototype.toHexColor_ = function (color) { if (color === Constants.TRANSPARENT_COLOR) { return color;