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_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,
ANIMATED_PREVIEW_WIDTH : 200,

View File

@ -70,52 +70,26 @@
return result;
};
var frameCache = {};
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 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);
}.bind(this);
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) {
}.bind(this))
.then(function (results) {
var colors = {};
results.forEach(function (result) {
Object.keys(result).forEach(function (color) {
colors[color] = true;
});
});
// Remove transparent color from used colors
delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)];
@ -123,13 +97,7 @@
return pskl.utils.intToHex(color);
});
this.setCurrentColors(hexColors);
}.bind(this, colors);
if (framesToBatch.length === 0) {
batchAllThen([colors]);
} else {
batchAll(framesToBatch, job).then(batchAllThen);
}
}.bind(this));
};
ns.CurrentColorsService.prototype.isCurrentColorsPaletteSelected_ = function () {
@ -150,7 +118,6 @@
ns.CurrentColorsService.prototype.getFrameColors_ = function (frame, processorCallback) {
var frameColorsWorker = new pskl.worker.framecolors.FrameColors(frame,
function (event) {
frameCache[frame.getHash()] = event.data.colors;
processorCallback(event.data.colors);
},
function () {},

View File

@ -15,7 +15,7 @@
ns.FrameColors.prototype.process = function () {
this.worker.postMessage([
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 transparentColorInt = this.TRANSPARENT_COLOR;
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];
if (color !== transparentColorInt) {
if (!frameColors[color]) {
@ -64,7 +64,7 @@
this.onmessage = function(event) {
try {
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 colors = getFrameColors(frame);
this.postMessage({