From 0c0aa5f2c95ff668e0384f94e53773061d67d51d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1vid=20Szab=C3=B3?= Date: Fri, 26 Aug 2016 01:40:13 +0200 Subject: [PATCH] Add caching to CurrentColorsService --- src/js/service/CurrentColorsService.js | 39 ++++++++++++++++++- .../worker/framecolors/FrameColorsWorker.js | 3 +- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/js/service/CurrentColorsService.js b/src/js/service/CurrentColorsService.js index dacad519..141ea1af 100644 --- a/src/js/service/CurrentColorsService.js +++ b/src/js/service/CurrentColorsService.js @@ -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)); }; diff --git a/src/js/worker/framecolors/FrameColorsWorker.js b/src/js/worker/framecolors/FrameColorsWorker.js index 7a02c253..352370b8 100644 --- a/src/js/worker/framecolors/FrameColorsWorker.js +++ b/src/js/worker/framecolors/FrameColorsWorker.js @@ -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;