colour.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2013 Lovell Fuller and others.
  2. // SPDX-License-Identifier: Apache-2.0
  3. 'use strict';
  4. const color = require('color');
  5. const is = require('./is');
  6. /**
  7. * Colourspaces.
  8. * @private
  9. */
  10. const colourspace = {
  11. multiband: 'multiband',
  12. 'b-w': 'b-w',
  13. bw: 'b-w',
  14. cmyk: 'cmyk',
  15. srgb: 'srgb'
  16. };
  17. /**
  18. * Tint the image using the provided colour.
  19. * An alpha channel may be present and will be unchanged by the operation.
  20. *
  21. * @example
  22. * const output = await sharp(input)
  23. * .tint({ r: 255, g: 240, b: 16 })
  24. * .toBuffer();
  25. *
  26. * @param {string|Object} tint - Parsed by the [color](https://www.npmjs.org/package/color) module.
  27. * @returns {Sharp}
  28. * @throws {Error} Invalid parameter
  29. */
  30. function tint (tint) {
  31. this._setBackgroundColourOption('tint', tint);
  32. return this;
  33. }
  34. /**
  35. * Convert to 8-bit greyscale; 256 shades of grey.
  36. * 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.
  37. * By default the output image will be web-friendly sRGB and contain three (identical) color channels.
  38. * This may be overridden by other sharp operations such as `toColourspace('b-w')`,
  39. * which will produce an output image containing one color channel.
  40. * An alpha channel may be present, and will be unchanged by the operation.
  41. *
  42. * @example
  43. * const output = await sharp(input).greyscale().toBuffer();
  44. *
  45. * @param {Boolean} [greyscale=true]
  46. * @returns {Sharp}
  47. */
  48. function greyscale (greyscale) {
  49. this.options.greyscale = is.bool(greyscale) ? greyscale : true;
  50. return this;
  51. }
  52. /**
  53. * Alternative spelling of `greyscale`.
  54. * @param {Boolean} [grayscale=true]
  55. * @returns {Sharp}
  56. */
  57. function grayscale (grayscale) {
  58. return this.greyscale(grayscale);
  59. }
  60. /**
  61. * Set the pipeline colourspace.
  62. *
  63. * The input image will be converted to the provided colourspace at the start of the pipeline.
  64. * All operations will use this colourspace before converting to the output colourspace,
  65. * as defined by {@link #tocolourspace|toColourspace}.
  66. *
  67. * @since 0.29.0
  68. *
  69. * @example
  70. * // Run pipeline in 16 bits per channel RGB while converting final result to 8 bits per channel sRGB.
  71. * await sharp(input)
  72. * .pipelineColourspace('rgb16')
  73. * .toColourspace('srgb')
  74. * .toFile('16bpc-pipeline-to-8bpc-output.png')
  75. *
  76. * @param {string} [colourspace] - pipeline colourspace e.g. `rgb16`, `scrgb`, `lab`, `grey16` [...](https://github.com/libvips/libvips/blob/41cff4e9d0838498487a00623462204eb10ee5b8/libvips/iofuncs/enumtypes.c#L774)
  77. * @returns {Sharp}
  78. * @throws {Error} Invalid parameters
  79. */
  80. function pipelineColourspace (colourspace) {
  81. if (!is.string(colourspace)) {
  82. throw is.invalidParameterError('colourspace', 'string', colourspace);
  83. }
  84. this.options.colourspacePipeline = colourspace;
  85. return this;
  86. }
  87. /**
  88. * Alternative spelling of `pipelineColourspace`.
  89. * @param {string} [colorspace] - pipeline colorspace.
  90. * @returns {Sharp}
  91. * @throws {Error} Invalid parameters
  92. */
  93. function pipelineColorspace (colorspace) {
  94. return this.pipelineColourspace(colorspace);
  95. }
  96. /**
  97. * Set the output colourspace.
  98. * By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.
  99. *
  100. * @example
  101. * // Output 16 bits per pixel RGB
  102. * await sharp(input)
  103. * .toColourspace('rgb16')
  104. * .toFile('16-bpp.png')
  105. *
  106. * @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)
  107. * @returns {Sharp}
  108. * @throws {Error} Invalid parameters
  109. */
  110. function toColourspace (colourspace) {
  111. if (!is.string(colourspace)) {
  112. throw is.invalidParameterError('colourspace', 'string', colourspace);
  113. }
  114. this.options.colourspace = colourspace;
  115. return this;
  116. }
  117. /**
  118. * Alternative spelling of `toColourspace`.
  119. * @param {string} [colorspace] - output colorspace.
  120. * @returns {Sharp}
  121. * @throws {Error} Invalid parameters
  122. */
  123. function toColorspace (colorspace) {
  124. return this.toColourspace(colorspace);
  125. }
  126. /**
  127. * Update a colour attribute of the this.options Object.
  128. * @private
  129. * @param {string} key
  130. * @param {string|Object} value
  131. * @throws {Error} Invalid value
  132. */
  133. function _setBackgroundColourOption (key, value) {
  134. if (is.defined(value)) {
  135. if (is.object(value) || is.string(value)) {
  136. const colour = color(value);
  137. this.options[key] = [
  138. colour.red(),
  139. colour.green(),
  140. colour.blue(),
  141. Math.round(colour.alpha() * 255)
  142. ];
  143. } else {
  144. throw is.invalidParameterError('background', 'object or string', value);
  145. }
  146. }
  147. }
  148. /**
  149. * Decorate the Sharp prototype with colour-related functions.
  150. * @private
  151. */
  152. module.exports = function (Sharp) {
  153. Object.assign(Sharp.prototype, {
  154. // Public
  155. tint,
  156. greyscale,
  157. grayscale,
  158. pipelineColourspace,
  159. pipelineColorspace,
  160. toColourspace,
  161. toColorspace,
  162. // Private
  163. _setBackgroundColourOption
  164. });
  165. // Class attributes
  166. Sharp.colourspace = colourspace;
  167. Sharp.colorspace = colourspace;
  168. };