From 03f37d46ac3a27f261febc3d168427f6d91e957b Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Fri, 13 Jan 2017 03:31:02 +0100 Subject: [PATCH] add unit tests for colorToInt --- src/js/utils/core.js | 57 +++++++++------------------------------ test/js/utils/CoreTest.js | 37 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 test/js/utils/CoreTest.js diff --git a/src/js/utils/core.js b/src/js/utils/core.js index e245899a..39fd3c9e 100644 --- a/src/js/utils/core.js +++ b/src/js/utils/core.js @@ -167,45 +167,20 @@ if (!Uint32Array.prototype.fill) { return colorCache[color]; } - var intValue = 0; - var r; - var g; - var b; - var a; - - // Hexadecimal - if ((color.length == 9 || color.length == 7 || color.length == 3) && color[0] == '#') { - var hex = parseInt(color.substr(1), 16); - if (color.length == 9) { - r = hex >> 24 & 0xff; - g = hex >> 16 & 0xff; - b = hex >> 8 & 0xff; - a = hex & 0xff; - } else if (color.length == 7) { - r = hex >> 16 & 0xff; - g = hex >> 8 & 0xff; - b = hex & 0xff; - a = 255; - } else { - r = hex >> 8 & 0xf * 16; - g = hex >> 4 & 0xf * 16; - b = hex & 0xf * 16; - a = 255; + var tc = window.tinycolor(color); + if (tc && tc.ok) { + var rgb = tc.toRgb(); + var a = Math.round(rgb.a * 255); + var intValue = (a << 24 >>> 0) + (rgb.b << 16) + (rgb.g << 8) + rgb.r; + if (a === 0) { + // assign all 'transparent' colors to 0, theoretically mapped to rgba(0,0,0,0) only + intValue = 0; } - } else if (color.length > 5 && color.substr(0, 5) == 'rgba(') { // RGBA - var rgba = color.substr(5).slice(0, -1).split(','); - r = parseInt(rgba[0]); - g = parseInt(rgba[1]); - b = parseInt(rgba[2]); - a = Math.floor(parseFloat(rgba[3]) * 255); - } else if (color.length > 4 && color.substr(0, 4) == 'rgb(') { // RGB - var rgb = color.substr(4).slice(0, -1).split(','); - r = parseInt(rgb[0]); - g = parseInt(rgb[1]); - b = parseInt(rgb[2]); - a = 255; - } else { // NO HOPE - // Determine color by using the browser itself + colorCache[color] = intValue; + colorCacheReverse[intValue] = color; + return intValue; + } else { + // If tinycolor failed, determine color by using the browser var d = document.createElement('div'); d.style.color = color; document.body.appendChild(d); @@ -216,12 +191,6 @@ if (!Uint32Array.prototype.fill) { return pskl.utils.colorToInt(color); } - - intValue = (a << 24 >>> 0) + (b << 16) + (g << 8) + r; - - colorCache[color] = intValue; - colorCacheReverse[intValue] = color; - return intValue; }; ns.intToColor = function(intValue) { diff --git a/test/js/utils/CoreTest.js b/test/js/utils/CoreTest.js new file mode 100644 index 00000000..441bc883 --- /dev/null +++ b/test/js/utils/CoreTest.js @@ -0,0 +1,37 @@ +describe("Core utils tests", function() { + + beforeEach(function() {}); + afterEach(function() {}); + + it("colorToInt parses red", function() { + var RED = 4278190335; + + expect(pskl.utils.colorToInt("red")).toBe(RED); + expect(pskl.utils.colorToInt("rgb(255,0,0)")).toBe(RED); + expect(pskl.utils.colorToInt("rgba(255,0,0,1)")).toBe(RED); + expect(pskl.utils.colorToInt("#FF0000")).toBe(RED); + expect(pskl.utils.colorToInt("#ff0000")).toBe(RED); + expect(pskl.utils.colorToInt("#f00")).toBe(RED); + expect(pskl.utils.colorToInt("#f00")).toBe(RED); + }); + + it("colorToInt parses white", function() { + var WHITE = 4294967295; + + expect(pskl.utils.colorToInt("white")).toBe(WHITE); + expect(pskl.utils.colorToInt("rgb(255,255,255)")).toBe(WHITE); + expect(pskl.utils.colorToInt("rgba(255,255,255,1)")).toBe(WHITE); + expect(pskl.utils.colorToInt("#FFFFFF")).toBe(WHITE); + expect(pskl.utils.colorToInt("#ffffff")).toBe(WHITE); + expect(pskl.utils.colorToInt("#FFF")).toBe(WHITE); + expect(pskl.utils.colorToInt("#fff")).toBe(WHITE); + }); + + it("colorToInt parses transparent", function() { + var TRANSPARENT = 0; + + expect(pskl.utils.colorToInt("transparent")).toBe(TRANSPARENT); + expect(pskl.utils.colorToInt("rgba(100,120,150, 0)")).toBe(TRANSPARENT); + expect(pskl.utils.colorToInt("rgba(255,255,255,0)")).toBe(TRANSPARENT); + }); +}); \ No newline at end of file