sharp 0.33.5 → 0.35.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +12 -18
  2. package/dist/channel.cjs +177 -0
  3. package/{lib/channel.js → dist/channel.mjs} +11 -8
  4. package/dist/colour.cjs +199 -0
  5. package/{lib/colour.js → dist/colour.mjs} +42 -23
  6. package/dist/composite.cjs +213 -0
  7. package/{lib/composite.js → dist/composite.mjs} +14 -11
  8. package/{lib/constructor.js → dist/constructor.cjs} +102 -43
  9. package/dist/constructor.mjs +511 -0
  10. package/dist/index.cjs +25 -0
  11. package/dist/index.d.cts +1999 -0
  12. package/dist/index.d.mts +2046 -0
  13. package/dist/index.mjs +25 -0
  14. package/dist/input.cjs +814 -0
  15. package/{lib/input.js → dist/input.mjs} +232 -76
  16. package/{lib/is.js → dist/is.cjs} +19 -45
  17. package/dist/is.mjs +143 -0
  18. package/{lib/libvips.js → dist/libvips.cjs} +52 -43
  19. package/dist/libvips.mjs +212 -0
  20. package/dist/operation.cjs +998 -0
  21. package/{lib/operation.js → dist/operation.mjs} +131 -91
  22. package/{lib/output.js → dist/output.cjs} +262 -61
  23. package/dist/output.mjs +1788 -0
  24. package/dist/resize.cjs +617 -0
  25. package/{lib/resize.js → dist/resize.mjs} +65 -35
  26. package/dist/sharp.cjs +174 -0
  27. package/dist/sharp.mjs +174 -0
  28. package/{lib/utility.js → dist/utility.cjs} +30 -25
  29. package/dist/utility.mjs +301 -0
  30. package/install/build.js +38 -0
  31. package/lib/index.d.ts +387 -142
  32. package/package.json +90 -85
  33. package/src/binding.gyp +49 -26
  34. package/src/common.cc +203 -111
  35. package/src/common.h +68 -40
  36. package/src/metadata.cc +106 -22
  37. package/src/metadata.h +15 -5
  38. package/src/operations.cc +62 -21
  39. package/src/operations.h +15 -3
  40. package/src/pipeline.cc +391 -201
  41. package/src/pipeline.h +36 -7
  42. package/src/sharp.cc +11 -8
  43. package/src/stats.cc +16 -13
  44. package/src/stats.h +6 -3
  45. package/src/utilities.cc +35 -15
  46. package/src/utilities.h +5 -2
  47. package/install/check.js +0 -41
  48. package/lib/index.js +0 -16
  49. package/lib/sharp.js +0 -114
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # sharp
2
2
 
3
- <img src="https://cdn.jsdelivr.net/gh/lovell/sharp@main/docs/image/sharp-logo.svg" width="160" height="160" alt="sharp logo" align="right">
3
+ <picture><img src="https://sharp.pixelplumbing.com/sharp-logo.svg" width="160" height="160" alt="sharp logo" align="right"></picture>
4
4
 
5
5
  The typical use case for this high speed Node-API module
6
6
  is to convert large images in common formats to
@@ -8,7 +8,7 @@ smaller, web-friendly JPEG, PNG, WebP, GIF and AVIF images of varying dimensions
8
8
 
9
9
  It can be used with all JavaScript runtimes
10
10
  that provide support for Node-API v9, including
11
- Node.js (^18.17.0 or >= 20.3.0), Deno and Bun.
11
+ Node.js (>= 20.9.0), Deno and Bun.
12
12
 
13
13
  Resizing an image is typically 4x-5x faster than using the
14
14
  quickest ImageMagick and GraphicsMagick settings
@@ -38,31 +38,27 @@ npm install sharp
38
38
  ```
39
39
 
40
40
  ```javascript
41
+ // ESM
42
+ import sharp from 'sharp';
43
+
44
+ // CJS
41
45
  const sharp = require('sharp');
42
46
  ```
43
47
 
44
- ### Callback
45
-
46
48
  ```javascript
47
- sharp(inputBuffer)
48
- .resize(320, 240)
49
+ await sharp(inputBuffer)
50
+ .resize({ width: 320, height: 240 })
49
51
  .toFile('output.webp', (err, info) => { ... });
50
52
  ```
51
53
 
52
- ### Promise
53
-
54
54
  ```javascript
55
- sharp('input.jpg')
56
- .rotate()
57
- .resize(200)
55
+ const output = await sharp('input.jpg')
56
+ .autoOrient()
57
+ .resize({ width: 200 })
58
58
  .jpeg({ mozjpeg: true })
59
- .toBuffer()
60
- .then( data => { ... })
61
- .catch( err => { ... });
59
+ .toBuffer();
62
60
  ```
63
61
 
64
- ### Async/await
65
-
66
62
  ```javascript
67
63
  const semiTransparentRedPng = await sharp({
68
64
  create: {
@@ -76,8 +72,6 @@ const semiTransparentRedPng = await sharp({
76
72
  .toBuffer();
77
73
  ```
78
74
 
79
- ### Stream
80
-
81
75
  ```javascript
82
76
  const roundedCorners = Buffer.from(
83
77
  '<svg><rect x="0" y="0" width="200" height="200" rx="50" ry="50"/></svg>'
@@ -0,0 +1,177 @@
1
+ /*!
2
+ Copyright 2013 Lovell Fuller and others.
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const is = require('./is.cjs');
7
+
8
+ /**
9
+ * Boolean operations for bandbool.
10
+ * @private
11
+ */
12
+ const bool = {
13
+ and: 'and',
14
+ or: 'or',
15
+ eor: 'eor'
16
+ };
17
+
18
+ /**
19
+ * Remove alpha channels, if any. This is a no-op if the image does not have an alpha channel.
20
+ *
21
+ * See also {@link /api-operation/#flatten flatten}.
22
+ *
23
+ * @example
24
+ * sharp('rgba.png')
25
+ * .removeAlpha()
26
+ * .toFile('rgb.png', function(err, info) {
27
+ * // rgb.png is a 3 channel image without an alpha channel
28
+ * });
29
+ *
30
+ * @returns {Sharp}
31
+ */
32
+ function removeAlpha () {
33
+ this.options.removeAlpha = true;
34
+ return this;
35
+ }
36
+
37
+ /**
38
+ * Ensure the output image has an alpha transparency channel.
39
+ * If missing, the added alpha channel will have the specified
40
+ * transparency level, defaulting to fully-opaque (1).
41
+ * This is a no-op if the image already has an alpha channel.
42
+ *
43
+ * @since 0.21.2
44
+ *
45
+ * @example
46
+ * // rgba.png will be a 4 channel image with a fully-opaque alpha channel
47
+ * await sharp('rgb.jpg')
48
+ * .ensureAlpha()
49
+ * .toFile('rgba.png')
50
+ *
51
+ * @example
52
+ * // rgba is a 4 channel image with a fully-transparent alpha channel
53
+ * const rgba = await sharp(rgb)
54
+ * .ensureAlpha(0)
55
+ * .toBuffer();
56
+ *
57
+ * @param {number} [alpha=1] - alpha transparency level (0=fully-transparent, 1=fully-opaque)
58
+ * @returns {Sharp}
59
+ * @throws {Error} Invalid alpha transparency level
60
+ */
61
+ function ensureAlpha (alpha) {
62
+ if (is.defined(alpha)) {
63
+ if (is.number(alpha) && is.inRange(alpha, 0, 1)) {
64
+ this.options.ensureAlpha = alpha;
65
+ } else {
66
+ throw is.invalidParameterError('alpha', 'number between 0 and 1', alpha);
67
+ }
68
+ } else {
69
+ this.options.ensureAlpha = 1;
70
+ }
71
+ return this;
72
+ }
73
+
74
+ /**
75
+ * Extract a single channel from a multi-channel image.
76
+ *
77
+ * The output colourspace will be either `b-w` (8-bit) or `grey16` (16-bit).
78
+ *
79
+ * @example
80
+ * // green.jpg is a greyscale image containing the green channel of the input
81
+ * await sharp(input)
82
+ * .extractChannel('green')
83
+ * .toFile('green.jpg');
84
+ *
85
+ * @example
86
+ * // red1 is the red value of the first pixel, red2 the second pixel etc.
87
+ * const [red1, red2, ...] = await sharp(input)
88
+ * .extractChannel(0)
89
+ * .raw()
90
+ * .toBuffer();
91
+ *
92
+ * @param {number|string} channel - zero-indexed channel/band number to extract, or `red`, `green`, `blue` or `alpha`.
93
+ * @returns {Sharp}
94
+ * @throws {Error} Invalid channel
95
+ */
96
+ function extractChannel (channel) {
97
+ const channelMap = { red: 0, green: 1, blue: 2, alpha: 3 };
98
+ if (Object.keys(channelMap).includes(channel)) {
99
+ channel = channelMap[channel];
100
+ }
101
+ if (is.integer(channel) && is.inRange(channel, 0, 4)) {
102
+ this.options.extractChannel = channel;
103
+ } else {
104
+ throw is.invalidParameterError('channel', 'integer or one of: red, green, blue, alpha', channel);
105
+ }
106
+ return this;
107
+ }
108
+
109
+ /**
110
+ * Join one or more channels to the image.
111
+ * The meaning of the added channels depends on the output colourspace, set with `toColourspace()`.
112
+ * By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
113
+ * Channel ordering follows vips convention:
114
+ * - sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.
115
+ * - CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
116
+ *
117
+ * Buffers may be any of the image formats supported by sharp.
118
+ * For raw pixel input, the `options` object should contain a `raw` attribute, which follows the format of the attribute of the same name in the `sharp()` constructor.
119
+ *
120
+ * @param {Array<string|Buffer>|string|Buffer} images - one or more images (file paths, Buffers).
121
+ * @param {Object} options - image options, see `sharp()` constructor.
122
+ * @returns {Sharp}
123
+ * @throws {Error} Invalid parameters
124
+ */
125
+ function joinChannel (images, options) {
126
+ if (Array.isArray(images)) {
127
+ images.forEach(function (image) {
128
+ this.options.joinChannelIn.push(this._createInputDescriptor(image, options));
129
+ }, this);
130
+ } else {
131
+ this.options.joinChannelIn.push(this._createInputDescriptor(images, options));
132
+ }
133
+ return this;
134
+ }
135
+
136
+ /**
137
+ * Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
138
+ *
139
+ * @example
140
+ * sharp('3-channel-rgb-input.png')
141
+ * .bandbool(sharp.bool.and)
142
+ * .toFile('1-channel-output.png', function (err, info) {
143
+ * // The output will be a single channel image where each pixel `P = R & G & B`.
144
+ * // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]`
145
+ * // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`.
146
+ * });
147
+ *
148
+ * @param {string} boolOp - one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
149
+ * @returns {Sharp}
150
+ * @throws {Error} Invalid parameters
151
+ */
152
+ function bandbool (boolOp) {
153
+ if (is.string(boolOp) && is.inArray(boolOp, ['and', 'or', 'eor'])) {
154
+ this.options.bandBoolOp = boolOp;
155
+ } else {
156
+ throw is.invalidParameterError('boolOp', 'one of: and, or, eor', boolOp);
157
+ }
158
+ return this;
159
+ }
160
+
161
+ /**
162
+ * Decorate the Sharp prototype with channel-related functions.
163
+ * @module Sharp
164
+ * @private
165
+ */
166
+ module.exports = (Sharp) => {
167
+ Object.assign(Sharp.prototype, {
168
+ // Public instance functions
169
+ removeAlpha,
170
+ ensureAlpha,
171
+ extractChannel,
172
+ joinChannel,
173
+ bandbool
174
+ });
175
+ // Class attributes
176
+ Sharp.bool = bool;
177
+ };
@@ -1,9 +1,9 @@
1
- // Copyright 2013 Lovell Fuller and others.
2
- // SPDX-License-Identifier: Apache-2.0
1
+ /*!
2
+ Copyright 2013 Lovell Fuller and others.
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
3
5
 
4
- 'use strict';
5
-
6
- const is = require('./is');
6
+ import is from './is.mjs';
7
7
 
8
8
  /**
9
9
  * Boolean operations for bandbool.
@@ -16,9 +16,9 @@ const bool = {
16
16
  };
17
17
 
18
18
  /**
19
- * Remove alpha channel, if any. This is a no-op if the image does not have an alpha channel.
19
+ * Remove alpha channels, if any. This is a no-op if the image does not have an alpha channel.
20
20
  *
21
- * See also {@link /api-operation#flatten|flatten}.
21
+ * See also {@link /api-operation/#flatten flatten}.
22
22
  *
23
23
  * @example
24
24
  * sharp('rgba.png')
@@ -74,6 +74,8 @@ function ensureAlpha (alpha) {
74
74
  /**
75
75
  * Extract a single channel from a multi-channel image.
76
76
  *
77
+ * The output colourspace will be either `b-w` (8-bit) or `grey16` (16-bit).
78
+ *
77
79
  * @example
78
80
  * // green.jpg is a greyscale image containing the green channel of the input
79
81
  * await sharp(input)
@@ -158,9 +160,10 @@ function bandbool (boolOp) {
158
160
 
159
161
  /**
160
162
  * Decorate the Sharp prototype with channel-related functions.
163
+ * @module Sharp
161
164
  * @private
162
165
  */
163
- module.exports = function (Sharp) {
166
+ export default (Sharp) => {
164
167
  Object.assign(Sharp.prototype, {
165
168
  // Public instance functions
166
169
  removeAlpha,
@@ -0,0 +1,199 @@
1
+ /*!
2
+ Copyright 2013 Lovell Fuller and others.
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const color = require('@img/colour');
7
+ const is = require('./is.cjs');
8
+
9
+ /**
10
+ * Colourspaces.
11
+ * @private
12
+ */
13
+ const colourspace = {
14
+ multiband: 'multiband',
15
+ 'b-w': 'b-w',
16
+ bw: 'b-w',
17
+ cmyk: 'cmyk',
18
+ srgb: 'srgb'
19
+ };
20
+
21
+ /**
22
+ * Tint the image using the provided colour.
23
+ * An alpha channel may be present and will be unchanged by the operation.
24
+ *
25
+ * @example
26
+ * const output = await sharp(input)
27
+ * .tint({ r: 255, g: 240, b: 16 })
28
+ * .toBuffer();
29
+ *
30
+ * @param {string|Object} tint - Parsed by the [color](https://www.npmjs.org/package/color) module.
31
+ * @returns {Sharp}
32
+ * @throws {Error} Invalid parameter
33
+ */
34
+ function tint (tint) {
35
+ this._setBackgroundColourOption('tint', tint);
36
+ return this;
37
+ }
38
+
39
+ /**
40
+ * Convert to 8-bit greyscale; 256 shades of grey.
41
+ * This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results.
42
+ * By default the output image will be web-friendly sRGB and contain three (identical) colour channels.
43
+ * This may be overridden by other sharp operations such as `toColourspace('b-w')`,
44
+ * which will produce an output image containing one colour channel.
45
+ * An alpha channel may be present, and will be unchanged by the operation.
46
+ *
47
+ * @example
48
+ * const output = await sharp(input).greyscale().toBuffer();
49
+ *
50
+ * @param {Boolean} [greyscale=true]
51
+ * @returns {Sharp}
52
+ */
53
+ function greyscale (greyscale) {
54
+ this.options.greyscale = is.bool(greyscale) ? greyscale : true;
55
+ return this;
56
+ }
57
+
58
+ /**
59
+ * Alternative spelling of `greyscale`.
60
+ * @param {Boolean} [grayscale=true]
61
+ * @returns {Sharp}
62
+ */
63
+ function grayscale (grayscale) {
64
+ return this.greyscale(grayscale);
65
+ }
66
+
67
+ /**
68
+ * Set the pipeline colourspace.
69
+ *
70
+ * The input image will be converted to the provided colourspace at the start of the pipeline.
71
+ * All operations will use this colourspace before converting to the output colourspace,
72
+ * as defined by {@link #tocolourspace toColourspace}.
73
+ *
74
+ * @since 0.29.0
75
+ *
76
+ * @example
77
+ * // Run pipeline in 16 bits per channel RGB while converting final result to 8 bits per channel sRGB.
78
+ * await sharp(input)
79
+ * .pipelineColourspace('rgb16')
80
+ * .toColourspace('srgb')
81
+ * .toFile('16bpc-pipeline-to-8bpc-output.png')
82
+ *
83
+ * @param {string} [colourspace] - pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://www.libvips.org/API/current/enum.Interpretation.html)
84
+ * @returns {Sharp}
85
+ * @throws {Error} Invalid parameters
86
+ */
87
+ function pipelineColourspace (colourspace) {
88
+ if (!is.string(colourspace)) {
89
+ throw is.invalidParameterError('colourspace', 'string', colourspace);
90
+ }
91
+ this.options.colourspacePipeline = colourspace;
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * Alternative spelling of `pipelineColourspace`.
97
+ * @param {string} [colorspace] - pipeline colorspace.
98
+ * @returns {Sharp}
99
+ * @throws {Error} Invalid parameters
100
+ */
101
+ function pipelineColorspace (colorspace) {
102
+ return this.pipelineColourspace(colorspace);
103
+ }
104
+
105
+ /**
106
+ * Set the output colourspace.
107
+ * By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
108
+ *
109
+ * @example
110
+ * // Output 16 bits per pixel RGB
111
+ * await sharp(input)
112
+ * .toColourspace('rgb16')
113
+ * .toFile('16-bpp.png')
114
+ *
115
+ * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html)
116
+ * @returns {Sharp}
117
+ * @throws {Error} Invalid parameters
118
+ */
119
+ function toColourspace (colourspace) {
120
+ if (!is.string(colourspace)) {
121
+ throw is.invalidParameterError('colourspace', 'string', colourspace);
122
+ }
123
+ this.options.colourspace = colourspace;
124
+ return this;
125
+ }
126
+
127
+ /**
128
+ * Alternative spelling of `toColourspace`.
129
+ * @param {string} [colorspace] - output colorspace.
130
+ * @returns {Sharp}
131
+ * @throws {Error} Invalid parameters
132
+ */
133
+ function toColorspace (colorspace) {
134
+ return this.toColourspace(colorspace);
135
+ }
136
+
137
+ /**
138
+ * Create a RGBA colour array from a given value.
139
+ * @private
140
+ * @param {string|Object} value
141
+ * @throws {Error} Invalid value
142
+ */
143
+ function _getBackgroundColourOption (value) {
144
+ if (
145
+ is.object(value) ||
146
+ (is.string(value) && value.length >= 3 && value.length <= 200)
147
+ ) {
148
+ const colour = color(value);
149
+ const red = colour.red();
150
+ const green = colour.green();
151
+ const blue = colour.blue();
152
+ const alpha = Math.round(colour.alpha() * 255);
153
+ for (const [channel, component] of [['red', red], ['green', green], ['blue', blue], ['alpha', alpha]]) {
154
+ if (!is.number(component)) {
155
+ throw is.invalidParameterError(`background.${channel}`, 'number', component);
156
+ }
157
+ }
158
+ return [red, green, blue, alpha];
159
+ } else {
160
+ throw is.invalidParameterError('background', 'object or string', value);
161
+ }
162
+ }
163
+
164
+ /**
165
+ * Update a colour attribute of the this.options Object.
166
+ * @private
167
+ * @param {string} key
168
+ * @param {string|Object} value
169
+ * @throws {Error} Invalid value
170
+ */
171
+ function _setBackgroundColourOption (key, value) {
172
+ if (is.defined(value)) {
173
+ this.options[key] = _getBackgroundColourOption(value);
174
+ }
175
+ }
176
+
177
+ /**
178
+ * Decorate the Sharp prototype with colour-related functions.
179
+ * @module Sharp
180
+ * @private
181
+ */
182
+ module.exports = (Sharp) => {
183
+ Object.assign(Sharp.prototype, {
184
+ // Public
185
+ tint,
186
+ greyscale,
187
+ grayscale,
188
+ pipelineColourspace,
189
+ pipelineColorspace,
190
+ toColourspace,
191
+ toColorspace,
192
+ // Private
193
+ _getBackgroundColourOption,
194
+ _setBackgroundColourOption
195
+ });
196
+ // Class attributes
197
+ Sharp.colourspace = colourspace;
198
+ Sharp.colorspace = colourspace;
199
+ };
@@ -1,10 +1,10 @@
1
- // Copyright 2013 Lovell Fuller and others.
2
- // SPDX-License-Identifier: Apache-2.0
1
+ /*!
2
+ Copyright 2013 Lovell Fuller and others.
3
+ SPDX-License-Identifier: Apache-2.0
4
+ */
3
5
 
4
- 'use strict';
5
-
6
- const color = require('color');
7
- const is = require('./is');
6
+ import color from '@img/colour';
7
+ import is from './is.mjs';
8
8
 
9
9
  /**
10
10
  * Colourspaces.
@@ -39,9 +39,9 @@ function tint (tint) {
39
39
  /**
40
40
  * Convert to 8-bit greyscale; 256 shades of grey.
41
41
  * This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use `gamma()` with `greyscale()` for the best results.
42
- * By default the output image will be web-friendly sRGB and contain three (identical) color channels.
42
+ * By default the output image will be web-friendly sRGB and contain three (identical) colour channels.
43
43
  * This may be overridden by other sharp operations such as `toColourspace('b-w')`,
44
- * which will produce an output image containing one color channel.
44
+ * which will produce an output image containing one colour channel.
45
45
  * An alpha channel may be present, and will be unchanged by the operation.
46
46
  *
47
47
  * @example
@@ -69,7 +69,7 @@ function grayscale (grayscale) {
69
69
  *
70
70
  * The input image will be converted to the provided colourspace at the start of the pipeline.
71
71
  * All operations will use this colourspace before converting to the output colourspace,
72
- * as defined by {@link #tocolourspace|toColourspace}.
72
+ * as defined by {@link #tocolourspace toColourspace}.
73
73
  *
74
74
  * @since 0.29.0
75
75
  *
@@ -80,7 +80,7 @@ function grayscale (grayscale) {
80
80
  * .toColourspace('srgb')
81
81
  * .toFile('16bpc-pipeline-to-8bpc-output.png')
82
82
  *
83
- * @param {string} [colourspace] - pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://github.com/libvips/libvips/blob/41cff4e9d0838498487a00623462204eb10ee5b8/libvips/iofuncs/enumtypes.c#L774)
83
+ * @param {string} [colourspace] - pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://www.libvips.org/API/current/enum.Interpretation.html)
84
84
  * @returns {Sharp}
85
85
  * @throws {Error} Invalid parameters
86
86
  */
@@ -112,7 +112,7 @@ function pipelineColorspace (colorspace) {
112
112
  * .toColourspace('rgb16')
113
113
  * .toFile('16-bpp.png')
114
114
  *
115
- * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/3c0bfdf74ce1dc37a6429bed47fa76f16e2cd70a/libvips/iofuncs/enumtypes.c#L777-L794)
115
+ * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://www.libvips.org/API/current/enum.Interpretation.html)
116
116
  * @returns {Sharp}
117
117
  * @throws {Error} Invalid parameters
118
118
  */
@@ -134,6 +134,33 @@ function toColorspace (colorspace) {
134
134
  return this.toColourspace(colorspace);
135
135
  }
136
136
 
137
+ /**
138
+ * Create a RGBA colour array from a given value.
139
+ * @private
140
+ * @param {string|Object} value
141
+ * @throws {Error} Invalid value
142
+ */
143
+ function _getBackgroundColourOption (value) {
144
+ if (
145
+ is.object(value) ||
146
+ (is.string(value) && value.length >= 3 && value.length <= 200)
147
+ ) {
148
+ const colour = color(value);
149
+ const red = colour.red();
150
+ const green = colour.green();
151
+ const blue = colour.blue();
152
+ const alpha = Math.round(colour.alpha() * 255);
153
+ for (const [channel, component] of [['red', red], ['green', green], ['blue', blue], ['alpha', alpha]]) {
154
+ if (!is.number(component)) {
155
+ throw is.invalidParameterError(`background.${channel}`, 'number', component);
156
+ }
157
+ }
158
+ return [red, green, blue, alpha];
159
+ } else {
160
+ throw is.invalidParameterError('background', 'object or string', value);
161
+ }
162
+ }
163
+
137
164
  /**
138
165
  * Update a colour attribute of the this.options Object.
139
166
  * @private
@@ -143,25 +170,16 @@ function toColorspace (colorspace) {
143
170
  */
144
171
  function _setBackgroundColourOption (key, value) {
145
172
  if (is.defined(value)) {
146
- if (is.object(value) || is.string(value)) {
147
- const colour = color(value);
148
- this.options[key] = [
149
- colour.red(),
150
- colour.green(),
151
- colour.blue(),
152
- Math.round(colour.alpha() * 255)
153
- ];
154
- } else {
155
- throw is.invalidParameterError('background', 'object or string', value);
156
- }
173
+ this.options[key] = _getBackgroundColourOption(value);
157
174
  }
158
175
  }
159
176
 
160
177
  /**
161
178
  * Decorate the Sharp prototype with colour-related functions.
179
+ * @module Sharp
162
180
  * @private
163
181
  */
164
- module.exports = function (Sharp) {
182
+ export default (Sharp) => {
165
183
  Object.assign(Sharp.prototype, {
166
184
  // Public
167
185
  tint,
@@ -172,6 +190,7 @@ module.exports = function (Sharp) {
172
190
  toColourspace,
173
191
  toColorspace,
174
192
  // Private
193
+ _getBackgroundColourOption,
175
194
  _setBackgroundColourOption
176
195
  });
177
196
  // Class attributes