colour.js 3.3 KB

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