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.
* @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);
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;

View File

@ -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;
}

View File

@ -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}`];

View File

@ -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);
}

View File

@ -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",

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 () {
const base = 'alpha-premultiply-enlargement-2048x1536-paper.png';
const actual = fixtures.path('output.' + base);