diff --git a/src/js/controller/settings/exportimage/GifExportController.js b/src/js/controller/settings/exportimage/GifExportController.js index d1793b21..ead09a61 100644 --- a/src/js/controller/settings/exportimage/GifExportController.js +++ b/src/js/controller/settings/exportimage/GifExportController.js @@ -117,10 +117,10 @@ ns.GifExportController.prototype.renderAsImageDataAnimatedGIF = function(zoom, fps, cb) { var currentColors = pskl.app.currentColorsService.computeCurrentColors(); - + var preserveColors = currentColors.length < MAX_GIF_COLORS; var transparentColor = this.getTransparentColor(currentColors); - + var gif = new window.GIF({ workers: 5, quality: 1, @@ -155,17 +155,16 @@ ns.GifExportController.prototype.getTransparentColor = function(currentColors) { var transparentColor = pskl.utils.ColorUtils.getUnusedColor(currentColors); + if (!transparentColor) { console.error('Unable to find unused color to use as transparent color in the current sprite'); transparentColor = MAGIC_PINK; - } else { - transparentColor = window.tinycolor(transparentColor).toHexString(); } + return transparentColor; }; - // FIXME : HORRIBLE COPY/PASTA - + // FIXME : JD : HORRIBLE COPY/PASTA (JD later : where???) ns.GifExportController.prototype.updateStatus_ = function (imageUrl, error) { if (imageUrl) { var linkTpl = "{{shortLink}}"; diff --git a/src/js/utils/ColorUtils.js b/src/js/utils/ColorUtils.js index a1635fe3..34ac60f3 100644 --- a/src/js/utils/ColorUtils.js +++ b/src/js/utils/ColorUtils.js @@ -3,25 +3,25 @@ ns.ColorUtils = { getUnusedColor : function (usedColors) { - // start with white - var color = { - r : 255, - g : 255, - b : 255 - }; - + usedColors = usedColors || []; // create check map var colorMap = {}; usedColors.forEach(function (color) { colorMap[color.toUpperCase()] = true; }); + // start with white + var color = { + r : 255, + g : 255, + b : 255 + }; var match = null; while (true) { var hex = tinycolor(color).toHexString().toUpperCase(); if (!colorMap[hex]) { - match = color; + match = hex; break; } else { // pick a non null component to decrease its value diff --git a/test/js/utils/ColorUtilsTest.js b/test/js/utils/ColorUtilsTest.js new file mode 100644 index 00000000..bcc68e1c --- /dev/null +++ b/test/js/utils/ColorUtilsTest.js @@ -0,0 +1,29 @@ +describe("Color utils", function() { + + beforeEach(function() {}); + afterEach(function() {}); + + it("returns a color when provided with array of colors", function() { + // when/then + var unusedColor = pskl.utils.ColorUtils.getUnusedColor(['#ffffff', '#feffff', '#fdffff']); + // verify + expect(unusedColor).toBe('#FCFFFF'); + + // when/then + unusedColor = pskl.utils.ColorUtils.getUnusedColor(['#fcffff', '#feffff', '#fdffff']); + // verify + expect(unusedColor).toBe('#FFFFFF'); + }); + + it("returns a color for an empty array", function() { + // when/then + var unusedColor = pskl.utils.ColorUtils.getUnusedColor([]); + // verify + expect(unusedColor).toBe('#FFFFFF'); + + // when/then + unusedColor = pskl.utils.ColorUtils.getUnusedColor(); + // verify + expect(unusedColor).toBe('#FFFFFF'); + }); +}); \ No newline at end of file