diff --git a/tinycolor.js b/tinycolor.js index 9580299..e369b4b 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()); @@ -1004,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