diff --git a/test/test.js b/test/test.js index 6ab1c9a..feb0b87 100644 --- a/test/test.js +++ b/test/test.js @@ -729,3 +729,14 @@ test("tetrad", function() { var combination = tinycolor("red").tetrad(); equal(colorsToHexString(combination), "ff0000,80ff00,00ffff,7f00ff", "Correct Combination"); }); + + +test("NumberHex", function () { + equal( tinycolor(0x0).toString(), "#000000") + + equal( tinycolor("rgb(18, 0, 0)").toString(), tinycolor(0x120000).toString()) + equal( tinycolor("rgb(0, 52, 0)").toString(), tinycolor(0x3400).toString()) + equal( tinycolor("rgb(0, 0, 86)").toString(), tinycolor(0x56).toString()) + + equal( tinycolor("rgb(1, 1, 1)").toNumber(), (1<<16)+(1<<8)+1) +}) \ No newline at end of file diff --git a/tinycolor.js b/tinycolor.js index 4af951c..2393403 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -17,7 +17,7 @@ function tinycolor (color, opts) { color = (color) ? color : ''; opts = opts || { }; - + // If input is already a tinycolor, return itself if (color instanceof tinycolor) { return color; @@ -164,6 +164,9 @@ tinycolor.prototype = { return "progid:DXImageTransform.Microsoft.gradient("+gradientType+"startColorstr="+hex8String+",endColorstr="+secondHex8String+")"; }, + toNumber: function() { + return (mathRound(this._r)<<16) + (mathRound(this._g)<<8) + (mathRound(this._b)); + }, toString: function(format) { var formatSet = !!format; format = format || this._format; @@ -304,7 +307,10 @@ function inputToRGB(color) { var ok = false; var format = false; - if (typeof color == "string") { + if (typeof color == "number") { + color = numberInputToObject(color); + } + else if (typeof color == "string") { color = stringInputToObject(color); } @@ -1064,6 +1070,17 @@ var matchers = (function() { }; })(); + +// `numberInputToObject` +// Take in a number +function numberInputToObject(color) { + return { + r: color >> 16, + g: (color&0xff00) >> 8, + b: color&0xff + }; +} + // `stringInputToObject` // Permissive string parsing. Take in a number of formats, and output an object // based on detected format. Returns `{ r, g, b }` or `{ h, s, l }` or `{ h, s, v}`