mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 02:30:12 +02:00
Ensure pdfBackground constructor property is used #4207
Slightly refactor the way background colours are set
This commit is contained in:
parent
a642767329
commit
5b5dfbad77
@ -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.
|
||||
|
@ -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
|
||||
* @param {string} key
|
||||
* @param {string|Object} value
|
||||
* @throws {Error} Invalid value
|
||||
*/
|
||||
function _setBackgroundColourOption (key, value) {
|
||||
if (is.defined(value)) {
|
||||
function _getBackgroundColourOption (value) {
|
||||
if (is.object(value) || is.string(value)) {
|
||||
const colour = color(value);
|
||||
this.options[key] = [
|
||||
return [
|
||||
colour.red(),
|
||||
colour.green(),
|
||||
colour.blue(),
|
||||
@ -155,6 +153,18 @@ function _setBackgroundColourOption (key, 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,
|
||||
toColorspace,
|
||||
// Private
|
||||
_getBackgroundColourOption,
|
||||
_setBackgroundColourOption
|
||||
});
|
||||
// Class attributes
|
||||
|
19
lib/input.js
19
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])) {
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user