mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +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
|
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.
|
||||||
|
@ -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
|
||||||
|
19
lib/input.js
19
lib/input.js
@ -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])) {
|
||||||
|
@ -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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user