UNPKG

3.42 kBJavaScriptView Raw
1'use strict';
2
3const color = require('color');
4const is = require('./is');
5
6/**
7 * Colourspaces.
8 * @private
9 */
10const colourspace = {
11 multiband: 'multiband',
12 'b-w': 'b-w',
13 bw: 'b-w',
14 cmyk: 'cmyk',
15 srgb: 'srgb'
16};
17
18/**
19 * Tint the image using the provided chroma while preserving the image luminance.
20 * An alpha channel may be present and will be unchanged by the operation.
21 *
22 * @param {string|Object} rgb - parsed by the [color](https://www.npmjs.org/package/color) module to extract chroma values.
23 * @returns {Sharp}
24 * @throws {Error} Invalid parameter
25 */
26function tint (rgb) {
27 const colour = color(rgb);
28 this.options.tintA = colour.a();
29 this.options.tintB = colour.b();
30 return this;
31}
32
33/**
34 * Convert to 8-bit greyscale; 256 shades of grey.
35 * 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.
36 * By default the output image will be web-friendly sRGB and contain three (identical) color channels.
37 * This may be overridden by other sharp operations such as `toColourspace('b-w')`,
38 * which will produce an output image containing one color channel.
39 * An alpha channel may be present, and will be unchanged by the operation.
40 * @param {Boolean} [greyscale=true]
41 * @returns {Sharp}
42 */
43function greyscale (greyscale) {
44 this.options.greyscale = is.bool(greyscale) ? greyscale : true;
45 return this;
46}
47
48/**
49 * Alternative spelling of `greyscale`.
50 * @param {Boolean} [grayscale=true]
51 * @returns {Sharp}
52 */
53function grayscale (grayscale) {
54 return this.greyscale(grayscale);
55}
56
57/**
58 * Set the output colourspace.
59 * By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
60 * @param {string} [colourspace] - output colourspace e.g. `srgb`, `rgb`, `cmyk`, `lab`, `b-w` [...](https://github.com/libvips/libvips/blob/master/libvips/iofuncs/enumtypes.c#L568)
61 * @returns {Sharp}
62 * @throws {Error} Invalid parameters
63 */
64function toColourspace (colourspace) {
65 if (!is.string(colourspace)) {
66 throw is.invalidParameterError('colourspace', 'string', colourspace);
67 }
68 this.options.colourspace = colourspace;
69 return this;
70}
71
72/**
73 * Alternative spelling of `toColourspace`.
74 * @param {string} [colorspace] - output colorspace.
75 * @returns {Sharp}
76 * @throws {Error} Invalid parameters
77 */
78function toColorspace (colorspace) {
79 return this.toColourspace(colorspace);
80}
81
82/**
83 * Update a colour attribute of the this.options Object.
84 * @private
85 * @param {string} key
86 * @param {string|Object} value
87 * @throws {Error} Invalid value
88 */
89function _setBackgroundColourOption (key, value) {
90 if (is.defined(value)) {
91 if (is.object(value) || is.string(value)) {
92 const colour = color(value);
93 this.options[key] = [
94 colour.red(),
95 colour.green(),
96 colour.blue(),
97 Math.round(colour.alpha() * 255)
98 ];
99 } else {
100 throw is.invalidParameterError('background', 'object or string', value);
101 }
102 }
103}
104
105/**
106 * Decorate the Sharp prototype with colour-related functions.
107 * @private
108 */
109module.exports = function (Sharp) {
110 Object.assign(Sharp.prototype, {
111 // Public
112 tint,
113 greyscale,
114 grayscale,
115 toColourspace,
116 toColorspace,
117 // Private
118 _setBackgroundColourOption
119 });
120 // Class attributes
121 Sharp.colourspace = colourspace;
122 Sharp.colorspace = colourspace;
123};