Add caching to CurrentColorsService

This commit is contained in:
Dávid Szabó
2016-08-26 01:40:13 +02:00
committed by Julian Descottes
parent bb7d5c862f
commit 0c0aa5f2c9
2 changed files with 38 additions and 4 deletions

View File

@@ -65,6 +65,28 @@
return result;
};
// TODO Move to utils
var componentToHex = function (c) {
var hex = c.toString(16);
return hex.length == 1 ? '0' + hex : hex;
};
var rgbToHex = function (r, g, b) {
return '#' + componentToHex(r) + componentToHex(g) + componentToHex(b);
};
var intHexCache = {};
var intToHex = function(int) {
if (intHexCache[int]) {
return intHexCache[int];
}
var hex = rgbToHex(int & 0xff, int >> 8 & 0xff, int >> 16 & 0xff);
intHexCache[int] = hex;
return hex;
};
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);});
@@ -73,6 +95,13 @@
return this.cachedFrameProcessor.get(frame);
}.bind(this);
// TODO: Cache frame -> color
for (var i = 0, length = frames.length; i < length; i++) {
var frame = frames[i];
var hash = frame.getHash();
}
batchAll(frames, job).then(function (results) {
var colors = {};
results.forEach(function (result) {
@@ -81,8 +110,14 @@
});
});
// Remove transparent color from used colors
delete colors[Constants.TRANSPARENT_COLOR];
this.setCurrentColors(Object.keys(colors));
delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)];
var hexColors = [];
for (var i in colors) {
hexColors.push(intToHex(i));
}
this.setCurrentColors(hexColors);
}.bind(this));
};

View File

@@ -51,8 +51,7 @@
for (var i = 0; i < frame.length; i++) {
var color = frame[i];
if (color !== transparentColorInt) {
var hexColor = intToHex(color);
frameColors[hexColor] = true;
frameColors[color] = true;
}
}
return frameColors;