sort colors on image import for palette

This commit is contained in:
juliandescottes 2014-09-27 01:22:20 +02:00
parent d805e13d57
commit 37aa6c3d72

View File

@ -8,6 +8,7 @@
this.cachedFrameProcessor.setFrameProcessor(this.getFrameColors_.bind(this)); this.cachedFrameProcessor.setFrameProcessor(this.getFrameColors_.bind(this));
this.colorSorter = new pskl.service.color.ColorSorter(); this.colorSorter = new pskl.service.color.ColorSorter();
this.paletteService = pskl.app.paletteService;
this.framesColorsCache_ = {}; this.framesColorsCache_ = {};
}; };
@ -15,24 +16,35 @@
ns.CurrentColorsService.prototype.init = function () { ns.CurrentColorsService.prototype.init = function () {
$.subscribe(Events.PISKEL_RESET, this.onPiskelUpdated_.bind(this)); $.subscribe(Events.PISKEL_RESET, this.onPiskelUpdated_.bind(this));
$.subscribe(Events.TOOL_RELEASED, 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 () { ns.CurrentColorsService.prototype.getCurrentColors = function () {
return this.currentColors; return this.currentColors;
}; };
ns.CurrentColorsService.prototype.getFrameColors_ = function (frame) { ns.CurrentColorsService.prototype.onPiskelUpdated_ = function (evt) {
var frameColors = {}; if (this.isCurrentColorsPaletteSelected_()) {
frame.forEachPixel(function (color, x, y) { this.updateCurrentColors_();
var hexColor = this.toHexColor_(color); }
frameColors[hexColor] = true;
}.bind(this));
return frameColors;
}; };
ns.CurrentColorsService.prototype.onPiskelUpdated_ = function (evt) { ns.CurrentColorsService.prototype.updateCurrentColors_ = function () {
var layers = this.piskelController.getLayers(); var layers = this.piskelController.getLayers();
var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);}); var frames = layers.map(function (l) {return l.getFrames();}).reduce(function (p, n) {return p.concat(n);});
var colors = {}; var colors = {};
@ -50,12 +62,20 @@
// limit the array to the max colors to display // limit the array to the max colors to display
var colorsArray = Object.keys(colors).slice(0, Constants.MAX_CURRENT_COLORS_DISPLAYED); var colorsArray = Object.keys(colors).slice(0, Constants.MAX_CURRENT_COLORS_DISPLAYED);
this.currentColors = this.colorSorter.sort(colorsArray); this.currentColors = this.colorSorter.sort(colorsArray);
console.log(this.currentColors);
// TODO : only fire if there was a change // TODO : only fire if there was a change
$.publish(Events.CURRENT_COLORS_UPDATED); $.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) { ns.CurrentColorsService.prototype.toHexColor_ = function (color) {
if (color === Constants.TRANSPARENT_COLOR) { if (color === Constants.TRANSPARENT_COLOR) {
return color; return color;