UNPKG

5.11 kBJavaScriptView Raw
1// Copyright 2013 Lovell Fuller and others.
2// SPDX-License-Identifier: Apache-2.0
3
4'use strict';
5
6const color = require('color');
7const is = require('./is');
8
9/**
10 * Colourspaces.
11 * @private
12 */
13const 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 */
34function 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) color channels.
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.
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 */
53function 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 */
63function 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://github.com/libvips/libvips/blob/41cff4e9d0838498487a00623462204eb10ee5b8/libvips/iofuncs/enumtypes.c#L774)
84 * @returns {Sharp}
85 * @throws {Error} Invalid parameters
86 */
87function 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 */
101function 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://github.com/libvips/libvips/blob/3c0bfdf74ce1dc37a6429bed47fa76f16e2cd70a/libvips/iofuncs/enumtypes.c#L777-L794)
116 * @returns {Sharp}
117 * @throws {Error} Invalid parameters
118 */
119function 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 */
133function toColorspace (colorspace) {
134 return this.toColourspace(colorspace);
135}
136
137/**
138 * Update a colour attribute of the this.options Object.
139 * @private
140 * @param {string} key
141 * @param {string|Object} value
142 * @throws {Error} Invalid value
143 */
144function _setBackgroundColourOption (key, value) {
145 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 }
157 }
158}
159
160/**
161 * Decorate the Sharp prototype with colour-related functions.
162 * @private
163 */
164module.exports = function (Sharp) {
165 Object.assign(Sharp.prototype, {
166 // Public
167 tint,
168 greyscale,
169 grayscale,
170 pipelineColourspace,
171 pipelineColorspace,
172 toColourspace,
173 toColorspace,
174 // Private
175 _setBackgroundColourOption
176 });
177 // Class attributes
178 Sharp.colourspace = colourspace;
179 Sharp.colorspace = colourspace;
180};