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;
|
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 () {
|
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 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);
|
return this.cachedFrameProcessor.get(frame);
|
||||||
}.bind(this);
|
}.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) {
|
batchAll(frames, job).then(function (results) {
|
||||||
var colors = {};
|
var colors = {};
|
||||||
results.forEach(function (result) {
|
results.forEach(function (result) {
|
||||||
@@ -81,8 +110,14 @@
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
// Remove transparent color from used colors
|
// Remove transparent color from used colors
|
||||||
delete colors[Constants.TRANSPARENT_COLOR];
|
delete colors[pskl.utils.colorToInt(Constants.TRANSPARENT_COLOR)];
|
||||||
this.setCurrentColors(Object.keys(colors));
|
|
||||||
|
var hexColors = [];
|
||||||
|
for (var i in colors) {
|
||||||
|
hexColors.push(intToHex(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.setCurrentColors(hexColors);
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -51,8 +51,7 @@
|
|||||||
for (var i = 0; i < frame.length; i++) {
|
for (var i = 0; i < frame.length; i++) {
|
||||||
var color = frame[i];
|
var color = frame[i];
|
||||||
if (color !== transparentColorInt) {
|
if (color !== transparentColorInt) {
|
||||||
var hexColor = intToHex(color);
|
frameColors[color] = true;
|
||||||
frameColors[hexColor] = true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return frameColors;
|
return frameColors;
|
||||||
|
Reference in New Issue
Block a user