Better validation and test coverage for background colours

This commit is contained in:
Lovell Fuller 2019-08-16 20:37:17 +01:00
parent e4333ff6b0
commit da4e05c118
6 changed files with 33 additions and 16 deletions

View File

@ -83,18 +83,22 @@ function toColorspace (colorspace) {
* Update a colour attribute of the this.options Object. * Update a colour attribute of the this.options Object.
* @private * @private
* @param {String} key * @param {String} key
* @param {String|Object} val * @param {String|Object} value
* @throws {Error} Invalid key * @throws {Error} Invalid value
*/ */
function _setColourOption (key, val) { function _setBackgroundColourOption (key, value) {
if (is.object(val) || is.string(val)) { if (is.defined(value)) {
const colour = color(val); if (is.object(value) || is.string(value)) {
this.options[key] = [ const colour = color(value);
colour.red(), this.options[key] = [
colour.green(), colour.red(),
colour.blue(), colour.green(),
Math.round(colour.alpha() * 255) 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, toColourspace,
toColorspace, toColorspace,
// Private // Private
_setColourOption _setBackgroundColourOption
}); });
// Class attributes // Class attributes
Sharp.colourspace = colourspace; Sharp.colourspace = colourspace;

View File

@ -179,7 +179,7 @@ function blur (sigma) {
function flatten (options) { function flatten (options) {
this.options.flatten = is.bool(options) ? options : true; this.options.flatten = is.bool(options) ? options : true;
if (is.object(options)) { if (is.object(options)) {
this._setColourOption('flattenBackground', options.background); this._setBackgroundColourOption('flattenBackground', options.background);
} }
return this; return this;
} }

View File

@ -7,6 +7,7 @@ const env = process.env;
module.exports = function () { module.exports = function () {
const arch = env.npm_config_arch || process.arch; const arch = env.npm_config_arch || process.arch;
const platform = env.npm_config_platform || process.platform; const platform = env.npm_config_platform || process.platform;
/* istanbul ignore next */
const libc = (platform === 'linux' && detectLibc.isNonGlibcLinux) ? detectLibc.family : ''; const libc = (platform === 'linux' && detectLibc.isNonGlibcLinux) ? detectLibc.family : '';
const platformId = [`${platform}${libc}`]; const platformId = [`${platform}${libc}`];

View File

@ -239,7 +239,7 @@ function resize (width, height, options) {
} }
// Background // Background
if (is.defined(options.background)) { if (is.defined(options.background)) {
this._setColourOption('resizeBackground', options.background); this._setBackgroundColourOption('resizeBackground', options.background);
} }
// Kernel // Kernel
if (is.defined(options.kernel)) { if (is.defined(options.kernel)) {
@ -305,7 +305,7 @@ function extend (extend) {
this.options.extendBottom = extend.bottom; this.options.extendBottom = extend.bottom;
this.options.extendLeft = extend.left; this.options.extendLeft = extend.left;
this.options.extendRight = extend.right; this.options.extendRight = extend.right;
this._setColourOption('extendBackground', extend.background); this._setBackgroundColourOption('extendBackground', extend.background);
} else { } else {
throw is.invalidParameterError('extend', 'integer or object', extend); throw is.invalidParameterError('extend', 'integer or object', extend);
} }

View File

@ -119,7 +119,7 @@
"nyc": "^14.1.1", "nyc": "^14.1.1",
"prebuild": "^9.0.1", "prebuild": "^9.0.1",
"prebuild-ci": "^3.1.0", "prebuild-ci": "^3.1.0",
"rimraf": "^2.6.3", "rimraf": "^3.0.0",
"semistandard": "^13.0.1" "semistandard": "^13.0.1"
}, },
"license": "Apache-2.0", "license": "Apache-2.0",

View File

@ -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 shouldnt cause dark edges', function () { it('Enlargement with non-nearest neighbor interpolation shouldnt cause dark edges', function () {
const base = 'alpha-premultiply-enlargement-2048x1536-paper.png'; const base = 'alpha-premultiply-enlargement-2048x1536-paper.png';
const actual = fixtures.path('output.' + base); const actual = fixtures.path('output.' + base);