test : add unit test for ColorUtils

This commit is contained in:
juliandescottes 2015-04-13 13:02:12 +02:00
parent 0e817a88a7
commit ba491736c1
3 changed files with 42 additions and 14 deletions

View File

@ -155,17 +155,16 @@
ns.GifExportController.prototype.getTransparentColor = function(currentColors) { ns.GifExportController.prototype.getTransparentColor = function(currentColors) {
var transparentColor = pskl.utils.ColorUtils.getUnusedColor(currentColors); var transparentColor = pskl.utils.ColorUtils.getUnusedColor(currentColors);
if (!transparentColor) { if (!transparentColor) {
console.error('Unable to find unused color to use as transparent color in the current sprite'); console.error('Unable to find unused color to use as transparent color in the current sprite');
transparentColor = MAGIC_PINK; transparentColor = MAGIC_PINK;
} else {
transparentColor = window.tinycolor(transparentColor).toHexString();
} }
return transparentColor; return transparentColor;
}; };
// FIXME : HORRIBLE COPY/PASTA // FIXME : JD : HORRIBLE COPY/PASTA (JD later : where???)
ns.GifExportController.prototype.updateStatus_ = function (imageUrl, error) { ns.GifExportController.prototype.updateStatus_ = function (imageUrl, error) {
if (imageUrl) { if (imageUrl) {
var linkTpl = "<a class='image-link' href='{{link}}' target='_blank'>{{shortLink}}</a>"; var linkTpl = "<a class='image-link' href='{{link}}' target='_blank'>{{shortLink}}</a>";

View File

@ -3,25 +3,25 @@
ns.ColorUtils = { ns.ColorUtils = {
getUnusedColor : function (usedColors) { getUnusedColor : function (usedColors) {
// start with white usedColors = usedColors || [];
var color = {
r : 255,
g : 255,
b : 255
};
// create check map // create check map
var colorMap = {}; var colorMap = {};
usedColors.forEach(function (color) { usedColors.forEach(function (color) {
colorMap[color.toUpperCase()] = true; colorMap[color.toUpperCase()] = true;
}); });
// start with white
var color = {
r : 255,
g : 255,
b : 255
};
var match = null; var match = null;
while (true) { while (true) {
var hex = tinycolor(color).toHexString().toUpperCase(); var hex = tinycolor(color).toHexString().toUpperCase();
if (!colorMap[hex]) { if (!colorMap[hex]) {
match = color; match = hex;
break; break;
} else { } else {
// pick a non null component to decrease its value // pick a non null component to decrease its value

View File

@ -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');
});
});