From 528ddf5e00e84f547708fbb995c7e4b82979a693 Mon Sep 17 00:00:00 2001 From: AngryAndConflict Date: Thu, 16 Feb 2017 16:05:19 +0300 Subject: [PATCH 1/2] Fix rechecking unchanged variable in .toString() Also changed ifs to switch, this way it is more readable and compact --- tinycolor.js | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/tinycolor.js b/tinycolor.js index 9580299..38ce49f 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -167,7 +167,6 @@ tinycolor.prototype = { var formatSet = !!format; format = format || this._format; - var formattedString = false; var hasAlpha = this._a < 1 && this._a >= 0; var needsAlphaFormat = !formatSet && hasAlpha && (format === "hex" || format === "hex6" || format === "hex3" || format === "hex4" || format === "hex8" || format === "name"); @@ -179,35 +178,26 @@ tinycolor.prototype = { } return this.toRgbString(); } - if (format === "rgb") { - formattedString = this.toRgbString(); + switch(format){ + case 'rgb': + return this.toRgbString(); + case "prgb": + return this.toPercentageRgbString(); + case "hex3": + return this.toHexString(true); + case "hex4": + return this.toHex8String(true); + case "hex8": + return this.toHex8String(); + case "name": + return this.toName() || this.toHexString(); + case "hsl": + return this.toHslString(); + case "hsv": + return this.toHsvString(); + // case "hex": case "hex6": case unknown: + default: return this.toHexString(); } - if (format === "prgb") { - formattedString = this.toPercentageRgbString(); - } - if (format === "hex" || format === "hex6") { - formattedString = this.toHexString(); - } - if (format === "hex3") { - formattedString = this.toHexString(true); - } - if (format === "hex4") { - formattedString = this.toHex8String(true); - } - if (format === "hex8") { - formattedString = this.toHex8String(); - } - if (format === "name") { - formattedString = this.toName(); - } - if (format === "hsl") { - formattedString = this.toHslString(); - } - if (format === "hsv") { - formattedString = this.toHsvString(); - } - - return formattedString || this.toHexString(); }, clone: function() { return tinycolor(this.toString()); From a6d1d2b583906392091aabdbfa533828c45e685f Mon Sep 17 00:00:00 2001 From: AngryAndConflict Date: Sat, 18 Feb 2017 21:08:43 +0300 Subject: [PATCH 2/2] generalized clamp pattern In case of using tinycolor on complex math platform, will have more optimized clamp, Or in case using with libs like Ramda or Lodash, they have crazy caching and memoization mechanics, so there clamp version is more performant, and can be easily used in exchange. Also have made it with variable initialization so it is currable out of the box, and params order for same reason. --- tinycolor.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tinycolor.js b/tinycolor.js index 38ce49f..e369b4b 100644 --- a/tinycolor.js +++ b/tinycolor.js @@ -994,9 +994,14 @@ function bound01(n, max) { return (n % max) / parseFloat(max); } +// Force a numeric value within the inclusive lower and upper bounds +var clamp = function(lower, upper, numeric){ + return mathMin(upper, mathMax(lower, numeric)); +}; + // Force a number between 0 and 1 function clamp01(val) { - return mathMin(1, mathMax(0, val)); + return clamp(0, 1, val); } // Parse a base-16 hex value into a base-10 integer