mirror of
https://github.com/piskelapp/piskel.git
synced 2023-08-10 21:12:52 +03:00
Add caching to CurrentColorsService
This commit is contained in:

committed by
Julian Descottes

parent
bb7d5c862f
commit
0c0aa5f2c9
@@ -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));
|
||||
};
|
||||
|
||||
|
@@ -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;
|
||||
|
Reference in New Issue
Block a user