From 255d60c2b2f0db40057356de92168b3fd716024f Mon Sep 17 00:00:00 2001 From: weepy Date: Mon, 10 Aug 2015 21:26:08 +0100 Subject: [PATCH 1/2] added handling of numbers for Pixi.js hex format with tests --- test/test.js | 11 +++++++++++ tinycolor.js | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 6ab1c9a..0412280 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(0, 0, 86)").toNumber(), 0x134) +}) \ No newline at end of file diff --git a/tinycolor.js b/tinycolor.js index 4af951c..0a5a492 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -18,15 +18,19 @@ function tinycolor (color, opts) { color = (color) ? color : ''; opts = opts || { }; + + // If input is already a tinycolor, return itself if (color instanceof tinycolor) { return color; } + // If we are called as a function, call using new instead if (!(this instanceof tinycolor)) { return new tinycolor(color, opts); } + var rgb = inputToRGB(color); this._originalInput = color, this._r = rgb.r, @@ -164,6 +168,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 +311,12 @@ function inputToRGB(color) { var ok = false; var format = false; - if (typeof color == "string") { + if (typeof color == "number") { + color = numberInputToObject(color) + // color.toString(16); + // color = '#' + ('000000'+color).substring(color.length); + } + else if (typeof color == "string") { color = stringInputToObject(color); } @@ -1064,6 +1076,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}` From 57f6de4758ebab1ca81d4eaaaf835ac9d8c46e34 Mon Sep 17 00:00:00 2001 From: weepy Date: Tue, 11 Aug 2015 07:51:23 +0100 Subject: [PATCH 2/2] removed extraneous whitespace, fixed toNumber() test, added missing semicolons --- test/test.js | 2 +- tinycolor.js | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/test/test.js b/test/test.js index 0412280..feb0b87 100644 --- a/test/test.js +++ b/test/test.js @@ -738,5 +738,5 @@ test("NumberHex", function () { equal( tinycolor("rgb(0, 52, 0)").toString(), tinycolor(0x3400).toString()) equal( tinycolor("rgb(0, 0, 86)").toString(), tinycolor(0x56).toString()) - equal( tinycolor("rgb(0, 0, 86)").toNumber(), 0x134) + 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 0a5a492..2393403 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -17,20 +17,16 @@ function tinycolor (color, opts) { color = (color) ? color : ''; opts = opts || { }; - - - + // If input is already a tinycolor, return itself if (color instanceof tinycolor) { return color; } - // If we are called as a function, call using new instead if (!(this instanceof tinycolor)) { return new tinycolor(color, opts); } - var rgb = inputToRGB(color); this._originalInput = color, this._r = rgb.r, @@ -169,7 +165,7 @@ 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)) + return (mathRound(this._r)<<16) + (mathRound(this._g)<<8) + (mathRound(this._b)); }, toString: function(format) { var formatSet = !!format; @@ -312,9 +308,7 @@ function inputToRGB(color) { var format = false; if (typeof color == "number") { - color = numberInputToObject(color) - // color.toString(16); - // color = '#' + ('000000'+color).substring(color.length); + color = numberInputToObject(color); } else if (typeof color == "string") { color = stringInputToObject(color); @@ -1084,7 +1078,7 @@ function numberInputToObject(color) { r: color >> 16, g: (color&0xff00) >> 8, b: color&0xff - } + }; } // `stringInputToObject`