From da4e05c118231fb12bb15198c4dedc181c94f2af Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Fri, 16 Aug 2019 20:37:17 +0100 Subject: [PATCH] Better validation and test coverage for background colours --- lib/colour.js | 28 ++++++++++++++++------------ lib/operation.js | 2 +- lib/platform.js | 1 + lib/resize.js | 4 ++-- package.json | 2 +- test/unit/alpha.js | 12 ++++++++++++ 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/colour.js b/lib/colour.js index 6475f1b5..bf0fc8a5 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -83,18 +83,22 @@ function toColorspace (colorspace) { * Update a colour attribute of the this.options Object. * @private * @param {String} key - * @param {String|Object} val - * @throws {Error} Invalid key + * @param {String|Object} value + * @throws {Error} Invalid value */ -function _setColourOption (key, val) { - if (is.object(val) || is.string(val)) { - const colour = color(val); - this.options[key] = [ - colour.red(), - colour.green(), - colour.blue(), - Math.round(colour.alpha() * 255) - ]; +function _setBackgroundColourOption (key, value) { + if (is.defined(value)) { + if (is.object(value) || is.string(value)) { + const colour = color(value); + this.options[key] = [ + colour.red(), + colour.green(), + colour.blue(), + Math.round(colour.alpha() * 255) + ]; + } else { + throw is.invalidParameterError('background', 'object or string', value); + } } } @@ -111,7 +115,7 @@ module.exports = function (Sharp) { toColourspace, toColorspace, // Private - _setColourOption + _setBackgroundColourOption }); // Class attributes Sharp.colourspace = colourspace; diff --git a/lib/operation.js b/lib/operation.js index 58f7a1b5..90244fc1 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -179,7 +179,7 @@ function blur (sigma) { function flatten (options) { this.options.flatten = is.bool(options) ? options : true; if (is.object(options)) { - this._setColourOption('flattenBackground', options.background); + this._setBackgroundColourOption('flattenBackground', options.background); } return this; } diff --git a/lib/platform.js b/lib/platform.js index d3342e34..9dc4abae 100644 --- a/lib/platform.js +++ b/lib/platform.js @@ -7,6 +7,7 @@ const env = process.env; module.exports = function () { const arch = env.npm_config_arch || process.arch; const platform = env.npm_config_platform || process.platform; + /* istanbul ignore next */ const libc = (platform === 'linux' && detectLibc.isNonGlibcLinux) ? detectLibc.family : ''; const platformId = [`${platform}${libc}`]; diff --git a/lib/resize.js b/lib/resize.js index 76680ad4..215dec29 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -239,7 +239,7 @@ function resize (width, height, options) { } // Background if (is.defined(options.background)) { - this._setColourOption('resizeBackground', options.background); + this._setBackgroundColourOption('resizeBackground', options.background); } // Kernel if (is.defined(options.kernel)) { @@ -305,7 +305,7 @@ function extend (extend) { this.options.extendBottom = extend.bottom; this.options.extendLeft = extend.left; this.options.extendRight = extend.right; - this._setColourOption('extendBackground', extend.background); + this._setBackgroundColourOption('extendBackground', extend.background); } else { throw is.invalidParameterError('extend', 'integer or object', extend); } diff --git a/package.json b/package.json index d0d6a544..5c45c237 100644 --- a/package.json +++ b/package.json @@ -119,7 +119,7 @@ "nyc": "^14.1.1", "prebuild": "^9.0.1", "prebuild-ci": "^3.1.0", - "rimraf": "^2.6.3", + "rimraf": "^3.0.0", "semistandard": "^13.0.1" }, "license": "Apache-2.0", diff --git a/test/unit/alpha.js b/test/unit/alpha.js index 38c2ad2f..598ab8f2 100644 --- a/test/unit/alpha.js +++ b/test/unit/alpha.js @@ -81,6 +81,18 @@ describe('Alpha transparency', function () { }); }); + it('Flatten with options but without colour does not throw', () => { + assert.doesNotThrow(() => { + sharp().flatten({}); + }); + }); + + it('Flatten to invalid colour throws', () => { + assert.throws(() => { + sharp().flatten({ background: 1 }); + }); + }); + it('Enlargement with non-nearest neighbor interpolation shouldn’t cause dark edges', function () { const base = 'alpha-premultiply-enlargement-2048x1536-paper.png'; const actual = fixtures.path('output.' + base);