Merge pull request #622 from juliandescottes/fix-current-colors

Fix current colors
This commit is contained in:
Julian Descottes 2017-01-25 15:08:30 +01:00 committed by GitHub
commit c0182952c9
4 changed files with 19 additions and 49 deletions

View File

@ -13,6 +13,9 @@ var Constants = {
MAX_WIDTH : 1024, MAX_WIDTH : 1024,
MAX_PALETTE_COLORS : 100, MAX_PALETTE_COLORS : 100,
// allow current colors service to get up to 256 colors.
// GIF generation is different if the color count goes over 256.
MAX_WORKER_COLORS : 256,
PREVIEW_FILM_SIZE : 96, PREVIEW_FILM_SIZE : 96,
ANIMATED_PREVIEW_WIDTH : 200, ANIMATED_PREVIEW_WIDTH : 200,

View File

@ -70,52 +70,26 @@
return result; return result;
}; };
var frameCache = {};
ns.CurrentColorsService.prototype.updateCurrentColors_ = function () { 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 job = function (frame) { // Concatenate all frames in a single array.
var frames = layers.map(function (l) {
return l.getFrames();
}).reduce(function (p, n) {
return p.concat(n);
});
batchAll(frames, function (frame) {
return this.cachedFrameProcessor.get(frame); return this.cachedFrameProcessor.get(frame);
}.bind(this); }.bind(this))
.then(function (results) {
var colors = {}; var colors = {};
var framesToBatch = [];
var removeColorsIfNotInCurrent = function(hash, color) {
if (!frameCache[hash][color]) {
delete colors[color];
}
};
for (var i = 0, length = frames.length; i < length; ++i) {
var frame = frames[i];
var hash = frame.getHash();
if (frameCache[hash]) {
colors = Object.assign(colors, frameCache[hash]);
var hashParts = hash.split('-');
var hashVersion = parseInt(hashParts.pop());
if (hashVersion > 0) {
var lastColors = frameCache[hashParts.join('-') + '-' + (hashVersion - 1)];
if (lastColors) {
Object.keys(lastColors).forEach(removeColorsIfNotInCurrent.bind(this, hash));
}
}
} else {
framesToBatch.push(frame);
}
}
var batchAllThen = function (colors, results) {
results.forEach(function (result) { results.forEach(function (result) {
Object.keys(result).forEach(function (color) { Object.keys(result).forEach(function (color) {
colors[color] = true; colors[color] = true;
}); });
}); });
// Remove transparent color from used colors // Remove transparent color from used colors
delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)]; delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)];
@ -123,13 +97,7 @@
return pskl.utils.intToHex(color); return pskl.utils.intToHex(color);
}); });
this.setCurrentColors(hexColors); this.setCurrentColors(hexColors);
}.bind(this, colors); }.bind(this));
if (framesToBatch.length === 0) {
batchAllThen([colors]);
} else {
batchAll(framesToBatch, job).then(batchAllThen);
}
}; };
ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () { ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () {
@ -150,7 +118,6 @@
ns.CurrentColorsService.prototype.getFrameColors_ = function (frame, processorCallback) { ns.CurrentColorsService.prototype.getFrameColors_ = function (frame, processorCallback) {
var frameColorsWorker = new pskl.worker.framecolors.FrameColors(frame, var frameColorsWorker = new pskl.worker.framecolors.FrameColors(frame,
function (event) { function (event) {
frameCache[frame.getHash()] = event.data.colors;
processorCallback(event.data.colors); processorCallback(event.data.colors);
}, },
function () {}, function () {},

View File

@ -15,7 +15,7 @@
ns.FrameColors.prototype.process = function () { ns.FrameColors.prototype.process = function () {
this.worker.postMessage([ this.worker.postMessage([
pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR), pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR),
Constants.MAX_PALETTE_COLORS, this.pixels Constants.MAX_WORKER_COLORS, this.pixels
]); ]);
}; };

View File

@ -49,7 +49,7 @@
var frameColors = {}; var frameColors = {};
var transparentColorInt = this.TRANSPARENT_COLOR; var transparentColorInt = this.TRANSPARENT_COLOR;
var colors = 0; var colors = 0;
for (var i = 0, length = frame.length; i < length && colors < this.MAX_PALETTE_COLORS; i++) { for (var i = 0, length = frame.length; i < length && colors < this.MAX_WORKER_COLORS; i++) {
var color = frame[i]; var color = frame[i];
if (color !== transparentColorInt) { if (color !== transparentColorInt) {
if (!frameColors[color]) { if (!frameColors[color]) {
@ -64,7 +64,7 @@
this.onmessage = function(event) { this.onmessage = function(event) {
try { try {
this.TRANSPARENT_COLOR = event.data[0]; this.TRANSPARENT_COLOR = event.data[0];
this.MAX_PALETTE_COLORS = event.data[1]; this.MAX_WORKER_COLORS = event.data[1];
var frame = event.data[2]; var frame = event.data[2];
var colors = getFrameColors(frame); var colors = getFrameColors(frame);
this.postMessage({ this.postMessage({