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 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 ### v0.34.1 - 7th April 2025
* TypeScript: Ensure new `autoOrient` property is optional. * TypeScript: Ensure new `autoOrient` property is optional.

View File

@ -135,17 +135,15 @@ function toColorspace (colorspace) {
} }
/** /**
* Update a colour attribute of the this.options Object. * Create a RGBA colour array from a given value.
* @private * @private
* @param {string} key
* @param {string|Object} value * @param {string|Object} value
* @throws {Error} Invalid value * @throws {Error} Invalid value
*/ */
function _setBackgroundColourOption (key, value) { function _getBackgroundColourOption (value) {
if (is.defined(value)) {
if (is.object(value) || is.string(value)) { if (is.object(value) || is.string(value)) {
const colour = color(value); const colour = color(value);
this.options[key] = [ return [
colour.red(), colour.red(),
colour.green(), colour.green(),
colour.blue(), colour.blue(),
@ -155,6 +153,18 @@ function _setBackgroundColourOption (key, value) {
throw is.invalidParameterError('background', 'object or string', value); throw is.invalidParameterError('background', 'object or string', value);
} }
} }
/**
* Update a colour attribute of the this.options Object.
* @private
* @param {string} key
* @param {string|Object} value
* @throws {Error} Invalid value
*/
function _setBackgroundColourOption (key, value) {
if (is.defined(value)) {
this.options[key] = _getBackgroundColourOption(value);
}
} }
/** /**
@ -173,6 +183,7 @@ module.exports = function (Sharp) {
toColourspace, toColourspace,
toColorspace, toColorspace,
// Private // Private
_getBackgroundColourOption,
_setBackgroundColourOption _setBackgroundColourOption
}); });
// Class attributes // Class attributes

View File

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