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) {
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 = "<a class='image-link' href='{{link}}' target='_blank'>{{shortLink}}</a>";

View File

@ -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

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