mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
sort colors on image import for palette
This commit is contained in:
parent
d805e13d57
commit
37aa6c3d72
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user