mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 15:25:07 +01:00
Better validation and test coverage for background colours
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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}`];
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user