Ensure pdfBackground constructor property is used #4207

Slightly refactor the way background colours are set
This commit is contained in:
Lovell Fuller 2025-04-09 22:21:14 +01:00
parent a642767329
commit 5b5dfbad77
4 changed files with 31 additions and 35 deletions

View File

@ -6,6 +6,11 @@ title: Changelog
Requires libvips v8.16.1
### v0.34.2 - TBD
* Ensure `pdfBackground` constructor property is used.
[#4207](https://github.com/lovell/sharp/pull/4207)
### v0.34.1 - 7th April 2025
* TypeScript: Ensure new `autoOrient` property is optional.

View File

@ -134,6 +134,26 @@ function toColorspace (colorspace) {
return this.toColourspace(colorspace);
}
/**
* Create a RGBA colour array from a given value.
* @private
* @param {string|Object} value
* @throws {Error} Invalid value
*/
function _getBackgroundColourOption (value) {
if (is.object(value) || is.string(value)) {
const colour = color(value);
return [
colour.red(),
colour.green(),
colour.blue(),
Math.round(colour.alpha() * 255)
];
} else {
throw is.invalidParameterError('background', 'object or string', value);
}
}
/**
* Update a colour attribute of the this.options Object.
* @private
@ -143,17 +163,7 @@ function toColorspace (colorspace) {
*/
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);
}
this.options[key] = _getBackgroundColourOption(value);
}
}
@ -173,6 +183,7 @@ module.exports = function (Sharp) {
toColourspace,
toColorspace,
// Private
_getBackgroundColourOption,
_setBackgroundColourOption
});
// Class attributes

View File

@ -3,7 +3,6 @@
'use strict';
const color = require('color');
const is = require('./is');
const sharp = require('./sharp');
@ -249,7 +248,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
}
// PDF background colour
if (is.defined(inputOptions.pdfBackground)) {
this._setBackgroundColourOption('pdfBackground', inputOptions.pdfBackground);
inputDescriptor.pdfBackground = this._getBackgroundColourOption(inputOptions.pdfBackground);
}
// Create new image
if (is.defined(inputOptions.create)) {
@ -288,13 +287,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
if (!is.inRange(inputOptions.create.channels, 3, 4)) {
throw is.invalidParameterError('create.channels', 'number between 3 and 4', inputOptions.create.channels);
}
const background = color(inputOptions.create.background);
inputDescriptor.createBackground = [
background.red(),
background.green(),
background.blue(),
Math.round(background.alpha() * 255)
];
inputDescriptor.createBackground = this._getBackgroundColourOption(inputOptions.create.background);
} else {
throw new Error('Expected valid noise or background to create a new input image');
}
@ -410,13 +403,7 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
}
}
if (is.defined(inputOptions.join.background)) {
const background = color(inputOptions.join.background);
inputDescriptor.joinBackground = [
background.red(),
background.green(),
background.blue(),
Math.round(background.alpha() * 255)
];
inputDescriptor.joinBackground = this._getBackgroundColourOption(inputOptions.join.background);
}
if (is.defined(inputOptions.join.halign)) {
if (is.string(inputOptions.join.halign) && is.string(this.constructor.align[inputOptions.join.halign])) {

View File

@ -3,7 +3,6 @@
'use strict';
const color = require('color');
const is = require('./is');
/**
@ -67,13 +66,7 @@ function rotate (angle, options) {
} else if (is.number(angle)) {
this.options.rotationAngle = angle;
if (is.object(options) && options.background) {
const backgroundColour = color(options.background);
this.options.rotationBackground = [
backgroundColour.red(),
backgroundColour.green(),
backgroundColour.blue(),
Math.round(backgroundColour.alpha() * 255)
];
this._setBackgroundColourOption('rotationBackground', options.background);
}
} else {
throw is.invalidParameterError('angle', 'numeric', angle);