From 5b5dfbad776a2d1aba0739eb52f23b7996821682 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Wed, 9 Apr 2025 22:21:14 +0100 Subject: [PATCH] Ensure pdfBackground constructor property is used #4207 Slightly refactor the way background colours are set --- docs/src/content/docs/changelog.md | 5 +++++ lib/colour.js | 33 ++++++++++++++++++++---------- lib/input.js | 19 +++-------------- lib/operation.js | 9 +------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/docs/src/content/docs/changelog.md b/docs/src/content/docs/changelog.md index 559c5e36..81a4ec1d 100644 --- a/docs/src/content/docs/changelog.md +++ b/docs/src/content/docs/changelog.md @@ -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. diff --git a/lib/colour.js b/lib/colour.js index d8a4de4f..ad283830 100644 --- a/lib/colour.js +++ b/lib/colour.js @@ -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 diff --git a/lib/input.js b/lib/input.js index 4ef9f94d..d9d29f3a 100644 --- a/lib/input.js +++ b/lib/input.js @@ -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])) { diff --git a/lib/operation.js b/lib/operation.js index d6cbbdf8..f76f65dc 100644 --- a/lib/operation.js +++ b/lib/operation.js @@ -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);