image-js 0.35.2 → 0.35.4

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 (170) hide show
  1. package/index.d.ts +3 -10
  2. package/lib/browser/environment.js +0 -6
  3. package/lib/browser/worker/extend.js +0 -3
  4. package/lib/browser/worker/process/background.js +0 -12
  5. package/lib/browser/worker/worker.js +0 -17
  6. package/lib/image/Image.js +18 -51
  7. package/lib/image/compute/colorHistogram.js +0 -8
  8. package/lib/image/compute/countAlphaPixels.js +0 -3
  9. package/lib/image/compute/extendedPoints.js +2 -7
  10. package/lib/image/compute/histogram.js +6 -25
  11. package/lib/image/compute/localMaxima.js +2 -10
  12. package/lib/image/compute/max.js +0 -5
  13. package/lib/image/compute/mean.js +0 -4
  14. package/lib/image/compute/median.js +0 -4
  15. package/lib/image/compute/min.js +0 -5
  16. package/lib/image/compute/minimalBoundingRectangle.js +5 -18
  17. package/lib/image/compute/moment.js +0 -3
  18. package/lib/image/compute/monotoneChainConvexHull.js +0 -3
  19. package/lib/image/compute/points.js +0 -3
  20. package/lib/image/compute/relativePosition.js +2 -8
  21. package/lib/image/compute/sum.js +0 -5
  22. package/lib/image/core/bitMethods.js +0 -11
  23. package/lib/image/core/checkProcessable.js +0 -13
  24. package/lib/image/core/environment.js +0 -8
  25. package/lib/image/core/export.js +0 -38
  26. package/lib/image/core/extend.js +0 -13
  27. package/lib/image/core/getRGBAData.js +0 -15
  28. package/lib/image/core/kind.js +4 -28
  29. package/lib/image/core/kindNames.js +1 -0
  30. package/lib/image/core/load.js +1 -53
  31. package/lib/image/core/mediaTypes.js +0 -2
  32. package/lib/image/core/valueMethods.js +0 -13
  33. package/lib/image/extend.js +7 -83
  34. package/lib/image/filter/abs.js +0 -3
  35. package/lib/image/filter/add.js +2 -8
  36. package/lib/image/filter/background.js +0 -9
  37. package/lib/image/filter/blurFilter.js +0 -6
  38. package/lib/image/filter/divide.js +0 -8
  39. package/lib/image/filter/flipX.js +0 -5
  40. package/lib/image/filter/flipY.js +0 -4
  41. package/lib/image/filter/gaussianFilter.js +0 -5
  42. package/lib/image/filter/gradientFilter.js +0 -11
  43. package/lib/image/filter/hypotenuse.js +0 -13
  44. package/lib/image/filter/invert.js +0 -9
  45. package/lib/image/filter/level.js +1 -19
  46. package/lib/image/filter/medianFilter.js +0 -15
  47. package/lib/image/filter/multiply.js +0 -10
  48. package/lib/image/filter/scharrFilter.js +0 -4
  49. package/lib/image/filter/sobelFilter.js +0 -4
  50. package/lib/image/filter/subtract.js +0 -8
  51. package/lib/image/filter/subtractImage.js +0 -10
  52. package/lib/image/internal/checks.js +1 -9
  53. package/lib/image/internal/clamp.js +0 -1
  54. package/lib/image/internal/copy.js +0 -2
  55. package/lib/image/internal/copyAlphaChannel.js +0 -1
  56. package/lib/image/internal/getImageParameters.js +0 -1
  57. package/lib/image/internal/getOutputImage.js +1 -13
  58. package/lib/image/model/model.js +1 -1
  59. package/lib/image/morphology/blackHat.js +0 -5
  60. package/lib/image/morphology/close.js +0 -5
  61. package/lib/image/morphology/dilate.js +0 -40
  62. package/lib/image/morphology/erode.js +0 -40
  63. package/lib/image/morphology/morphologicalGradient.js +0 -5
  64. package/lib/image/morphology/open.js +0 -5
  65. package/lib/image/morphology/topHat.js +0 -5
  66. package/lib/image/operator/cannyEdge.js +0 -3
  67. package/lib/image/operator/convolution.js +9 -34
  68. package/lib/image/operator/convolutionSeparable.js +0 -10
  69. package/lib/image/operator/extract.js +4 -13
  70. package/lib/image/operator/floodFill.js +2 -15
  71. package/lib/image/operator/getSeparatedKernel.js +0 -2
  72. package/lib/image/operator/paintLabels.js +2 -13
  73. package/lib/image/operator/paintMasks.js +2 -12
  74. package/lib/image/operator/paintPoints.js +0 -9
  75. package/lib/image/operator/paintPolygon.js +0 -14
  76. package/lib/image/operator/paintPolygons.js +0 -4
  77. package/lib/image/operator/paintPolyline.js +0 -7
  78. package/lib/image/operator/paintPolylines.js +0 -4
  79. package/lib/image/roi/Roi.js +27 -208
  80. package/lib/image/roi/RoiLayer.js +1 -22
  81. package/lib/image/roi/RoiMap.js +5 -38
  82. package/lib/image/roi/creator/fromMask.js +4 -22
  83. package/lib/image/roi/creator/fromMaskConnectedComponentLabelingAlgorithm.js +2 -20
  84. package/lib/image/roi/creator/fromMaxima.js +10 -43
  85. package/lib/image/roi/creator/fromPoints.js +2 -10
  86. package/lib/image/roi/creator/fromWaterShed.js +6 -17
  87. package/lib/image/roi/feretDiameters.js +9 -21
  88. package/lib/image/roi/manager.js +30 -119
  89. package/lib/image/roi/util/commonBorderLength.js +5 -16
  90. package/lib/image/roi/util/mergeRoi.js +2 -21
  91. package/lib/image/transform/cmyk.js +0 -9
  92. package/lib/image/transform/colorDepth.js +0 -12
  93. package/lib/image/transform/crop.js +0 -20
  94. package/lib/image/transform/cropAlpha.js +0 -11
  95. package/lib/image/transform/grey.js +0 -17
  96. package/lib/image/transform/greyAlgorithms.js +1 -24
  97. package/lib/image/transform/hsl.js +0 -16
  98. package/lib/image/transform/hsv.js +0 -15
  99. package/lib/image/transform/insert.js +0 -6
  100. package/lib/image/transform/mask/huang.js +3 -18
  101. package/lib/image/transform/mask/intermodes.js +3 -14
  102. package/lib/image/transform/mask/isodata.js +0 -14
  103. package/lib/image/transform/mask/li.js +12 -42
  104. package/lib/image/transform/mask/mask.js +1 -13
  105. package/lib/image/transform/mask/maxEntropy.js +8 -25
  106. package/lib/image/transform/mask/mean.js +1 -3
  107. package/lib/image/transform/mask/minError.js +10 -21
  108. package/lib/image/transform/mask/minimum.js +0 -20
  109. package/lib/image/transform/mask/moments.js +4 -17
  110. package/lib/image/transform/mask/otsu.js +1 -7
  111. package/lib/image/transform/mask/percentile.js +0 -8
  112. package/lib/image/transform/mask/renyiEntropy.js +8 -34
  113. package/lib/image/transform/mask/shanbhag.js +8 -25
  114. package/lib/image/transform/mask/thresholdAlgorithms.js +1 -17
  115. package/lib/image/transform/mask/triangle.js +13 -29
  116. package/lib/image/transform/mask/yen.js +2 -14
  117. package/lib/image/transform/pad.js +0 -20
  118. package/lib/image/transform/resize/nearestNeighbor.js +0 -6
  119. package/lib/image/transform/resize/resize.js +0 -16
  120. package/lib/image/transform/rgba.js +0 -9
  121. package/lib/image/transform/rgba8.js +0 -3
  122. package/lib/image/transform/rotate.js +0 -29
  123. package/lib/image/transform/rotateFree.js +0 -31
  124. package/lib/image/transform/warping.js +2 -35
  125. package/lib/image/utility/combineChannels.js +0 -11
  126. package/lib/image/utility/getBestMatch.js +2 -13
  127. package/lib/image/utility/getChannel.js +0 -10
  128. package/lib/image/utility/getClosestCommonParent.js +6 -23
  129. package/lib/image/utility/getColumn.js +0 -4
  130. package/lib/image/utility/getIntersection.js +2 -18
  131. package/lib/image/utility/getMatrix.js +0 -7
  132. package/lib/image/utility/getPixelsArray.js +0 -5
  133. package/lib/image/utility/getPixelsGrid.js +0 -11
  134. package/lib/image/utility/getRow.js +0 -4
  135. package/lib/image/utility/getSimilarity.js +3 -17
  136. package/lib/image/utility/getThreshold.js +0 -4
  137. package/lib/image/utility/setBorder.js +0 -16
  138. package/lib/image/utility/setChannel.js +0 -6
  139. package/lib/image/utility/setMatrix.js +3 -6
  140. package/lib/image/utility/split.js +2 -15
  141. package/lib/index.js +0 -12
  142. package/lib/kernel/kernel.js +0 -3
  143. package/lib/kernel/laplacianOfGaussian.js +1 -7
  144. package/lib/node_modules/test/binary.js +0 -3
  145. package/lib/node_modules/test/oneRoi.js +0 -2
  146. package/lib/stack/Stack.js +2 -35
  147. package/lib/stack/compute/histogram.js +0 -4
  148. package/lib/stack/compute/histograms.js +0 -4
  149. package/lib/stack/compute/max.js +0 -3
  150. package/lib/stack/compute/median.js +0 -4
  151. package/lib/stack/compute/min.js +0 -3
  152. package/lib/stack/extend.js +1 -11
  153. package/lib/stack/transform/matchAndCrop.js +6 -19
  154. package/lib/stack/utility/averageImage.js +0 -9
  155. package/lib/stack/utility/maxImage.js +0 -7
  156. package/lib/stack/utility/minImage.js +0 -7
  157. package/lib/util/Shape.js +3 -40
  158. package/lib/util/base64.js +3 -14
  159. package/lib/util/channel.js +1 -35
  160. package/lib/util/color.js +1 -25
  161. package/lib/util/converter.js +1 -13
  162. package/lib/util/deepValue.js +0 -3
  163. package/lib/util/histogram.js +1 -12
  164. package/lib/util/kernel.js +2 -12
  165. package/lib/util/matrix.js +0 -13
  166. package/lib/util/points.js +12 -35
  167. package/lib/util/value.js +0 -7
  168. package/lib/util/zerosMatrix.js +0 -3
  169. package/package.json +1 -1
  170. package/src/image/utility/setMatrix.js +4 -0
package/lib/util/Shape.js CHANGED
@@ -4,17 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _Image = _interopRequireDefault(require("../image/Image"));
9
-
10
8
  var _kindNames = require("../image/core/kindNames");
11
-
12
9
  var _zerosMatrix = _interopRequireDefault(require("./zerosMatrix"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  const cross = [[0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0]];
17
12
  const smallCross = [[0, 1, 0], [1, 1, 1], [0, 1, 0]];
13
+
18
14
  /**
19
15
  * Class representing a shape
20
16
  * @class Shape
@@ -28,7 +24,6 @@ const smallCross = [[0, 1, 0], [1, 1, 1], [0, 1, 0]];
28
24
  * @param {number} [options.height=options.size] - width of the shape. Must be odd.
29
25
  * @param {boolean} [options.filled=true] - If false only the border ot the shape is taken into account.
30
26
  */
31
-
32
27
  class Shape {
33
28
  constructor(options = {}) {
34
29
  let {
@@ -39,12 +34,10 @@ class Shape {
39
34
  height,
40
35
  filled = true
41
36
  } = options;
42
-
43
37
  if (size) {
44
38
  width = size;
45
39
  height = size;
46
40
  }
47
-
48
41
  if (shape) {
49
42
  switch (shape.toLowerCase()) {
50
43
  case 'square':
@@ -53,20 +46,17 @@ class Shape {
53
46
  filled
54
47
  });
55
48
  break;
56
-
57
49
  case 'circle':
58
50
  case 'ellipse':
59
51
  this.matrix = ellipse(width, height, {
60
52
  filled
61
53
  });
62
54
  break;
63
-
64
55
  case 'triangle':
65
56
  this.matrix = triangle(width, height, {
66
57
  filled
67
58
  });
68
59
  break;
69
-
70
60
  default:
71
61
  throw new Error(`Shape: unexpected shape: ${shape}`);
72
62
  }
@@ -75,33 +65,28 @@ class Shape {
75
65
  case 'cross':
76
66
  this.matrix = cross;
77
67
  break;
78
-
79
68
  case 'smallcross':
80
69
  this.matrix = smallCross;
81
70
  break;
82
-
83
71
  default:
84
72
  throw new Error(`Shape: unexpected kind: ${kind}`);
85
73
  }
86
74
  } else {
87
75
  throw new Error('Shape: expected a kind or a shape option');
88
76
  }
89
-
90
77
  this.height = this.matrix.length;
91
78
  this.width = this.matrix[0].length;
92
79
  this.halfHeight = this.height / 2 >> 0;
93
80
  this.halfWidth = this.width / 2 >> 0;
94
81
  }
82
+
95
83
  /**
96
84
  * Returns an array of [x,y] points
97
85
  * @return {Array<Array<number>>} - Array of [x,y] points
98
86
  */
99
-
100
-
101
87
  getPoints() {
102
88
  let matrix = this.matrix;
103
89
  let points = [];
104
-
105
90
  for (let y = 0; y < matrix.length; y++) {
106
91
  for (let x = 0; x < matrix[0].length; x++) {
107
92
  if (matrix[y][x]) {
@@ -109,20 +94,17 @@ class Shape {
109
94
  }
110
95
  }
111
96
  }
112
-
113
97
  return points;
114
98
  }
99
+
115
100
  /**
116
101
  * Returns a Mask (1 bit Image) corresponding to this shape.
117
102
  * @return {Image}
118
103
  */
119
-
120
-
121
104
  getMask() {
122
105
  let img = new _Image.default(this.width, this.height, {
123
106
  kind: _kindNames.BINARY
124
107
  });
125
-
126
108
  for (let y = 0; y < this.matrix.length; y++) {
127
109
  for (let x = 0; x < this.matrix[0].length; x++) {
128
110
  if (this.matrix[y][x]) {
@@ -130,17 +112,12 @@ class Shape {
130
112
  }
131
113
  }
132
114
  }
133
-
134
115
  return img;
135
116
  }
136
-
137
117
  }
138
-
139
118
  exports.default = Shape;
140
-
141
119
  function rectangle(width, height, options) {
142
120
  const matrix = (0, _zerosMatrix.default)(height, width);
143
-
144
121
  if (options.filled) {
145
122
  for (let y = 0; y < height; y++) {
146
123
  for (let x = 0; x < width; x++) {
@@ -153,32 +130,25 @@ function rectangle(width, height, options) {
153
130
  matrix[y][x] = 1;
154
131
  }
155
132
  }
156
-
157
133
  for (let y = 0; y < height; y++) {
158
134
  for (let x of [0, width - 1]) {
159
135
  matrix[y][x] = 1;
160
136
  }
161
137
  }
162
138
  }
163
-
164
139
  return matrix;
165
140
  }
166
-
167
141
  function ellipse(width, height, options) {
168
142
  const matrix = (0, _zerosMatrix.default)(height, width, options);
169
143
  let yEven = 1 - height % 2;
170
144
  let xEven = 1 - width % 2;
171
145
  let a = Math.floor((width - 1) / 2); // horizontal ellipse axe
172
-
173
146
  let b = Math.floor((height - 1) / 2); // vertical ellipse axe
174
-
175
147
  let a2 = a * a;
176
148
  let b2 = b * b;
177
-
178
149
  if (options.filled) {
179
150
  for (let y = 0; y <= b; y++) {
180
151
  let shift = Math.floor(Math.sqrt(a2 - a2 * y * y / b2));
181
-
182
152
  for (let x = a - shift; x <= a; x++) {
183
153
  matrix[b - y][x] = 1;
184
154
  matrix[b + y + yEven][x] = 1;
@@ -195,7 +165,6 @@ function ellipse(width, height, options) {
195
165
  matrix[b - y][width - x - 1] = 1;
196
166
  matrix[b + y + yEven][width - x - 1] = 1;
197
167
  }
198
-
199
168
  for (let x = 0; x <= a; x++) {
200
169
  let shift = Math.floor(Math.sqrt(b2 - b2 * x * x / a2));
201
170
  let y = b - shift;
@@ -205,24 +174,18 @@ function ellipse(width, height, options) {
205
174
  matrix[height - y - 1][a + x + xEven] = 1;
206
175
  }
207
176
  }
208
-
209
177
  return matrix;
210
178
  }
211
-
212
179
  function triangle(width, height, options) {
213
180
  if (!options.filled) {
214
181
  throw new Error('Non filled triangle is not implemented');
215
182
  }
216
-
217
183
  const matrix = (0, _zerosMatrix.default)(height, width, options);
218
-
219
184
  for (let y = 0; y < height; y++) {
220
185
  let shift = Math.floor((1 - y / height) * width / 2);
221
-
222
186
  for (let x = shift; x < width - shift; x++) {
223
187
  matrix[y][x] = 1;
224
188
  }
225
189
  }
226
-
227
190
  return matrix;
228
191
  }
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.decode = decode;
7
7
  exports.encode = encode;
8
8
  exports.toBase64URL = toBase64URL;
9
-
10
9
  /*
11
10
  * base64-arraybuffer
12
11
  * https://github.com/niklasvh/base64-arraybuffer
@@ -14,51 +13,43 @@ exports.toBase64URL = toBase64URL;
14
13
  * Copyright (c) 2012 Niklas von Hertzen
15
14
  * Licensed under the MIT license.
16
15
  */
17
- let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; // Use a lookup table to find the index.
18
16
 
19
- let lookup = new Uint8Array(256);
17
+ let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
20
18
 
19
+ // Use a lookup table to find the index.
20
+ let lookup = new Uint8Array(256);
21
21
  for (let i = 0; i < chars.length; i++) {
22
22
  lookup[chars.charCodeAt(i)] = i;
23
23
  }
24
-
25
24
  function encode(bytes) {
26
25
  let i;
27
26
  let len = bytes.length;
28
27
  let base64 = '';
29
-
30
28
  for (i = 0; i < len; i += 3) {
31
29
  base64 += chars[bytes[i] >> 2];
32
30
  base64 += chars[(bytes[i] & 3) << 4 | bytes[i + 1] >> 4];
33
31
  base64 += chars[(bytes[i + 1] & 15) << 2 | bytes[i + 2] >> 6];
34
32
  base64 += chars[bytes[i + 2] & 63];
35
33
  }
36
-
37
34
  if (len % 3 === 2) {
38
35
  base64 = `${base64.substring(0, base64.length - 1)}=`;
39
36
  } else if (len % 3 === 1) {
40
37
  base64 = `${base64.substring(0, base64.length - 2)}==`;
41
38
  }
42
-
43
39
  return base64;
44
40
  }
45
-
46
41
  function decode(base64) {
47
42
  let bufferLength = base64.length * 0.75;
48
43
  let len = base64.length;
49
44
  let p = 0;
50
45
  let encoded1, encoded2, encoded3, encoded4;
51
-
52
46
  if (base64[base64.length - 1] === '=') {
53
47
  bufferLength--;
54
-
55
48
  if (base64[base64.length - 2] === '=') {
56
49
  bufferLength--;
57
50
  }
58
51
  }
59
-
60
52
  const bytes = new Uint8Array(bufferLength);
61
-
62
53
  for (let i = 0; i < len; i += 4) {
63
54
  encoded1 = lookup[base64.charCodeAt(i)];
64
55
  encoded2 = lookup[base64.charCodeAt(i + 1)];
@@ -68,10 +59,8 @@ function decode(base64) {
68
59
  bytes[p++] = (encoded2 & 15) << 4 | encoded3 >> 2;
69
60
  bytes[p++] = (encoded3 & 3) << 6 | encoded4 & 63;
70
61
  }
71
-
72
62
  return bytes;
73
63
  }
74
-
75
64
  function toBase64URL(u8, type) {
76
65
  const base64 = encode(u8);
77
66
  return `data:${type};base64,${base64}`;
@@ -5,13 +5,9 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.validateArrayOfChannels = validateArrayOfChannels;
7
7
  exports.validateChannel = validateChannel;
8
-
9
8
  var Model = _interopRequireWildcard(require("../image/model/model"));
10
-
11
9
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
12
-
13
10
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
14
-
15
11
  /**
16
12
  * Specify which channels should be processed
17
13
  * * undefined : we take all the channels but alpha
@@ -21,70 +17,57 @@ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj &&
21
17
  * * [string] : array of channels as one letter string
22
18
  * @typedef {undefined|number|string|Array<number>|Array<string>} SelectedChannels
23
19
  */
20
+
24
21
  function validateArrayOfChannels(image, options = {}) {
25
22
  let {
26
23
  channels,
27
24
  allowAlpha,
28
25
  // are we allowing the selection of an alpha channel ?
29
26
  defaultAlpha // if no channels are selected should we take the alpha channel ?
30
-
31
27
  } = options;
32
-
33
28
  if (typeof allowAlpha !== 'boolean') {
34
29
  allowAlpha = true;
35
30
  }
36
-
37
31
  if (typeof channels === 'undefined') {
38
32
  return allChannels(image, defaultAlpha);
39
33
  } else {
40
34
  return validateChannels(image, channels, allowAlpha);
41
35
  }
42
36
  }
43
-
44
37
  function allChannels(image, defaultAlpha) {
45
38
  let length = defaultAlpha ? image.channels : image.components;
46
39
  let array = new Array(length);
47
-
48
40
  for (let i = 0; i < length; i++) {
49
41
  array[i] = i;
50
42
  }
51
-
52
43
  return array;
53
44
  }
54
-
55
45
  function validateChannels(image, channels, allowAlpha) {
56
46
  if (!Array.isArray(channels)) {
57
47
  channels = [channels];
58
48
  }
59
-
60
49
  for (let c = 0; c < channels.length; c++) {
61
50
  channels[c] = validateChannel(image, channels[c], allowAlpha);
62
51
  }
63
-
64
52
  return channels;
65
53
  }
66
-
67
54
  function validateChannel(image, channel, allowAlpha = true) {
68
55
  if (channel === undefined) {
69
56
  throw new RangeError(`validateChannel : the channel has to be >=0 and <${image.channels}`);
70
57
  }
71
-
72
58
  if (typeof channel === 'string') {
73
59
  switch (image.colorModel) {
74
60
  case Model.GREY:
75
61
  break;
76
-
77
62
  case Model.RGB:
78
63
  if ('rgb'.includes(channel)) {
79
64
  switch (channel) {
80
65
  case 'r':
81
66
  channel = 0;
82
67
  break;
83
-
84
68
  case 'g':
85
69
  channel = 1;
86
70
  break;
87
-
88
71
  case 'b':
89
72
  channel = 2;
90
73
  break;
@@ -93,18 +76,15 @@ function validateChannel(image, channel, allowAlpha = true) {
93
76
  }
94
77
 
95
78
  break;
96
-
97
79
  case Model.HSL:
98
80
  if ('hsl'.includes(channel)) {
99
81
  switch (channel) {
100
82
  case 'h':
101
83
  channel = 0;
102
84
  break;
103
-
104
85
  case 's':
105
86
  channel = 1;
106
87
  break;
107
-
108
88
  case 'l':
109
89
  channel = 2;
110
90
  break;
@@ -113,18 +93,15 @@ function validateChannel(image, channel, allowAlpha = true) {
113
93
  }
114
94
 
115
95
  break;
116
-
117
96
  case Model.HSV:
118
97
  if ('hsv'.includes(channel)) {
119
98
  switch (channel) {
120
99
  case 'h':
121
100
  channel = 0;
122
101
  break;
123
-
124
102
  case 's':
125
103
  channel = 1;
126
104
  break;
127
-
128
105
  case 'v':
129
106
  channel = 2;
130
107
  break;
@@ -133,22 +110,18 @@ function validateChannel(image, channel, allowAlpha = true) {
133
110
  }
134
111
 
135
112
  break;
136
-
137
113
  case Model.CMYK:
138
114
  if ('cmyk'.includes(channel)) {
139
115
  switch (channel) {
140
116
  case 'c':
141
117
  channel = 0;
142
118
  break;
143
-
144
119
  case 'm':
145
120
  channel = 1;
146
121
  break;
147
-
148
122
  case 'y':
149
123
  channel = 2;
150
124
  break;
151
-
152
125
  case 'k':
153
126
  channel = 3;
154
127
  break;
@@ -157,31 +130,24 @@ function validateChannel(image, channel, allowAlpha = true) {
157
130
  }
158
131
 
159
132
  break;
160
-
161
133
  default:
162
134
  throw new Error(`Unexpected color model: ${image.colorModel}`);
163
135
  }
164
-
165
136
  if (channel === 'a') {
166
137
  if (!image.alpha) {
167
138
  throw new Error('validateChannel : the image does not contain alpha channel');
168
139
  }
169
-
170
140
  channel = image.components;
171
141
  }
172
-
173
142
  if (typeof channel === 'string') {
174
143
  throw new Error(`validateChannel : undefined channel: ${channel}`);
175
144
  }
176
145
  }
177
-
178
146
  if (channel >= image.channels) {
179
147
  throw new RangeError(`validateChannel : the channel has to be >=0 and <${image.channels}`);
180
148
  }
181
-
182
149
  if (!allowAlpha && channel >= image.components) {
183
150
  throw new RangeError('validateChannel : alpha channel may not be selected');
184
151
  }
185
-
186
152
  return channel;
187
153
  }
package/lib/util/color.js CHANGED
@@ -7,43 +7,33 @@ exports.css2array = css2array;
7
7
  exports.getColors = getColors;
8
8
  exports.getDistinctColors = getDistinctColors;
9
9
  exports.getRandomColor = getRandomColor;
10
-
11
10
  var _colorFns = require("@swiftcarrot/color-fns");
12
-
13
11
  function css2array(string) {
14
12
  let color = (0, _colorFns.cssColor)(string);
15
13
  return [color.r, color.g, color.b, Math.round(color.a * 255 / 100)];
16
14
  }
17
-
18
15
  function hue2rgb(p, q, t) {
19
16
  if (t < 0) {
20
17
  t += 1;
21
18
  }
22
-
23
19
  if (t > 1) {
24
20
  t -= 1;
25
21
  }
26
-
27
22
  if (t < 1 / 6) {
28
23
  return p + (q - p) * 6 * t;
29
24
  }
30
-
31
25
  if (t < 1 / 2) {
32
26
  return q;
33
27
  }
34
-
35
28
  if (t < 2 / 3) {
36
29
  return p + (q - p) * (2 / 3 - t) * 6;
37
30
  }
38
-
39
31
  return p;
40
32
  }
41
-
42
33
  function hsl2rgb(h, s, l) {
43
34
  let m1, m2, hue, r, g, b;
44
35
  s /= 100;
45
36
  l /= 100;
46
-
47
37
  if (s === 0) {
48
38
  r = g = b = l * 255;
49
39
  } else {
@@ -52,37 +42,32 @@ function hsl2rgb(h, s, l) {
52
42
  } else {
53
43
  m2 = l + s - l * s;
54
44
  }
55
-
56
45
  m1 = l * 2 - m2;
57
46
  hue = h / 360;
58
47
  r = hue2rgb(m1, m2, hue + 1 / 3);
59
48
  g = hue2rgb(m1, m2, hue);
60
49
  b = hue2rgb(m1, m2, hue - 1 / 3);
61
50
  }
62
-
63
51
  return {
64
52
  r: r,
65
53
  g: g,
66
54
  b: b
67
55
  };
68
56
  }
69
-
70
57
  function getDistinctColors(numColors) {
71
58
  let colors = new Array(numColors);
72
59
  let j = 0;
73
-
74
60
  for (let i = 0; i < 360; i += 360 / numColors) {
75
61
  j++;
76
62
  let color = hsl2rgb(i, 100, 30 + j % 4 * 15);
77
63
  colors[j - 1] = [Math.round(color.r * 255), Math.round(color.g * 255), Math.round(color.b * 255)];
78
64
  }
79
-
80
65
  return colors;
81
66
  }
82
-
83
67
  function getRandomColor() {
84
68
  return [Math.floor(Math.random() * 256), Math.floor(Math.random() * 256), Math.floor(Math.random() * 256)];
85
69
  }
70
+
86
71
  /**
87
72
  * returns an array of colors based on various options
88
73
  * by default this methods return 50 distinct colors
@@ -95,8 +80,6 @@ function getRandomColor() {
95
80
  * @return {Array} Array of colors
96
81
  * @private
97
82
  */
98
-
99
-
100
83
  function getColors(options) {
101
84
  let {
102
85
  color,
@@ -105,33 +88,26 @@ function getColors(options) {
105
88
  // true / false
106
89
  numberColors = 50
107
90
  } = options;
108
-
109
91
  if (color && !Array.isArray(color)) {
110
92
  color = css2array(color);
111
93
  }
112
-
113
94
  if (color) {
114
95
  return [color];
115
96
  }
116
-
117
97
  if (colors) {
118
98
  colors = colors.map(function (color) {
119
99
  if (!Array.isArray(color)) {
120
100
  return css2array(color);
121
101
  }
122
-
123
102
  return color;
124
103
  });
125
104
  return colors;
126
105
  }
127
-
128
106
  if (randomColors) {
129
107
  colors = new Array(numberColors);
130
-
131
108
  for (let i = 0; i < numberColors; i++) {
132
109
  colors[i] = getRandomColor();
133
110
  }
134
111
  }
135
-
136
112
  return getDistinctColors(numberColors);
137
113
  }
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.factorDimensions = factorDimensions;
7
7
  exports.getFactor = getFactor;
8
8
  exports.getThreshold = getThreshold;
9
-
10
9
  /**
11
10
  * Converts a factor value to a number between 0 and 1
12
11
  * @private
@@ -17,14 +16,13 @@ function getFactor(value) {
17
16
  if (typeof value === 'string') {
18
17
  const last = value[value.length - 1];
19
18
  value = parseFloat(value);
20
-
21
19
  if (last === '%') {
22
20
  value /= 100;
23
21
  }
24
22
  }
25
-
26
23
  return value;
27
24
  }
25
+
28
26
  /**
29
27
  * We can specify a threshold as "0.4", "40%" or 123
30
28
  * @private
@@ -32,45 +30,35 @@ function getFactor(value) {
32
30
  * @param {number} maxValue
33
31
  * @return {number}
34
32
  */
35
-
36
-
37
33
  function getThreshold(value, maxValue) {
38
34
  if (!maxValue) {
39
35
  throw Error('getThreshold : the maxValue should be specified');
40
36
  }
41
-
42
37
  if (typeof value === 'string') {
43
38
  let last = value[value.length - 1];
44
-
45
39
  if (last !== '%') {
46
40
  throw Error('getThreshold : if the value is a string it must finish by %');
47
41
  }
48
-
49
42
  return parseFloat(value) / 100 * maxValue;
50
43
  } else if (typeof value === 'number') {
51
44
  if (value < 1) {
52
45
  return value * maxValue;
53
46
  }
54
-
55
47
  return value;
56
48
  } else {
57
49
  throw Error('getThreshold : the value is not valid');
58
50
  }
59
51
  }
60
-
61
52
  function factorDimensions(factor, width, height) {
62
53
  factor = getFactor(factor);
63
54
  let newWidth = Math.round(factor * width);
64
55
  let newHeight = Math.round(factor * height);
65
-
66
56
  if (newWidth <= 0) {
67
57
  newWidth = 1;
68
58
  }
69
-
70
59
  if (newHeight <= 0) {
71
60
  newHeight = 1;
72
61
  }
73
-
74
62
  return {
75
63
  width: newWidth,
76
64
  height: newHeight
@@ -4,14 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = deepValue;
7
-
8
7
  function deepValue(object, path = '') {
9
8
  let parts = path.split('.');
10
-
11
9
  for (let part of parts) {
12
10
  if (object[part] === undefined) return undefined;
13
11
  object = object[part];
14
12
  }
15
-
16
13
  return object;
17
14
  }