add unit tests for colorToInt
This commit is contained in:
parent
0abd779348
commit
03f37d46ac
2 changed files with 50 additions and 44 deletions
|
@ -167,45 +167,20 @@ if (!Uint32Array.prototype.fill) {
|
||||||
return colorCache[color];
|
return colorCache[color];
|
||||||
}
|
}
|
||||||
|
|
||||||
var intValue = 0;
|
var tc = window.tinycolor(color);
|
||||||
var r;
|
if (tc && tc.ok) {
|
||||||
var g;
|
var rgb = tc.toRgb();
|
||||||
var b;
|
var a = Math.round(rgb.a * 255);
|
||||||
var a;
|
var intValue = (a << 24 >>> 0) + (rgb.b << 16) + (rgb.g << 8) + rgb.r;
|
||||||
|
if (a === 0) {
|
||||||
// Hexadecimal
|
// assign all 'transparent' colors to 0, theoretically mapped to rgba(0,0,0,0) only
|
||||||
if ((color.length == 9 || color.length == 7 || color.length == 3) && color[0] == '#') {
|
intValue = 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;
|
|
||||||
}
|
}
|
||||||
} else if (color.length > 5 && color.substr(0, 5) == 'rgba(') { // RGBA
|
colorCache[color] = intValue;
|
||||||
var rgba = color.substr(5).slice(0, -1).split(',');
|
colorCacheReverse[intValue] = color;
|
||||||
r = parseInt(rgba[0]);
|
return intValue;
|
||||||
g = parseInt(rgba[1]);
|
} else {
|
||||||
b = parseInt(rgba[2]);
|
// If tinycolor failed, determine color by using the browser
|
||||||
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
|
|
||||||
var d = document.createElement('div');
|
var d = document.createElement('div');
|
||||||
d.style.color = color;
|
d.style.color = color;
|
||||||
document.body.appendChild(d);
|
document.body.appendChild(d);
|
||||||
|
@ -216,12 +191,6 @@ if (!Uint32Array.prototype.fill) {
|
||||||
|
|
||||||
return pskl.utils.colorToInt(color);
|
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) {
|
ns.intToColor = function(intValue) {
|
||||||
|
|
37
test/js/utils/CoreTest.js
Normal file
37
test/js/utils/CoreTest.js
Normal file
|
@ -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);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue