imagescript 3.16.1 → 3.18.0

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.
package/dist/image.js CHANGED
@@ -30,11 +30,26 @@ import { encode as utf8 } from "./util/utf8.js";
30
30
  import { resample, BOX, TRIANGLE, CUBIC } from "./util/resample.js";
31
31
  import { rotateBilinear } from "./util/rotate.js";
32
32
  import { gaussianBlur, boxBlurRGBA, premultiply, unpremultiply } from "./util/blur.js";
33
+ import { Colors, wrapHue, hue2rgb, rgbaToHSLA } from "./colors.js";
33
34
  import { isOptions, finite, color as checkColor, quality as checkQuality, effort as checkEffort, compressionLevel } from "./util/options.js";
34
35
  import { MAX_PIXELS, decoding } from "./util/limits.js";
35
36
  import { GainMap } from "./gainmap.js";
36
37
  import * as gain from "./util/gain.js";
37
38
  import * as gainMapCodec from "./codecs/gainmap.js";
39
+ export class ResizeModes {
40
+ static NEAREST_NEIGHBOR = 'nearest-neighbor';
41
+ static BILINEAR = 'bilinear';
42
+ static BICUBIC = 'bicubic';
43
+ static AREA = 'area';
44
+ static AUTO = -1;
45
+ constructor() { }
46
+ }
47
+ export class SVGScaleModes {
48
+ static SCALE = 'scale';
49
+ static WIDTH = 'width';
50
+ static HEIGHT = 'height';
51
+ constructor() { }
52
+ }
38
53
  function featureList(features) {
39
54
  if (typeof features !== 'object' || features === null)
40
55
  throw new TypeError('Features must be an object');
@@ -100,88 +115,47 @@ export class Image {
100
115
  get height() {
101
116
  return this.__height__;
102
117
  }
103
- *[Symbol.iterator]() {
118
+ *coordinates() {
104
119
  for (let y = 0; y < this.height; y++)
105
120
  for (let x = 0; x < this.width; x++)
106
121
  yield [x, y];
107
122
  }
108
- *iterateWithColors() {
123
+ *pixels() {
109
124
  const view = new DataView(this.bitmap.buffer, this.bitmap.byteOffset, this.bitmap.byteLength);
110
125
  let offset = 0;
111
126
  for (let y = 0; y < this.height; y++)
112
127
  for (let x = 0; x < this.width; x++, offset += 4)
113
128
  yield [x, y, view.getUint32(offset, false)];
114
129
  }
130
+ *[Symbol.iterator]() {
131
+ yield* this.coordinates();
132
+ }
133
+ *iterateWithColors() {
134
+ yield* this.pixels();
135
+ }
115
136
  static rgbaToColor(r, g, b, a) {
116
- r = finite(r, 'red');
117
- g = finite(g, 'green');
118
- b = finite(b, 'blue');
119
- a = finite(a, 'alpha');
120
- return (channel(r) << 24 | channel(g) << 16 | channel(b) << 8 | channel(a)) >>> 0;
137
+ return Colors.fromRGBA(r, g, b, a);
121
138
  }
122
139
  static rgbToColor(r, g, b) {
123
- return Image.rgbaToColor(r, g, b, 0xff);
140
+ return Colors.fromRGB(r, g, b);
124
141
  }
125
142
  static hslaToColor(h, s, l, a) {
126
- h = wrapHue(finite(h, 'hue'));
127
- s = finite(s, 'saturation');
128
- l = finite(l, 'lightness');
129
- a = finite(a, 'alpha');
130
- s = Math.min(1, Math.max(0, s));
131
- l = Math.min(1, Math.max(0, l));
132
- a = Math.min(1, Math.max(0, a));
133
- let r, g, b;
134
- if (s === 0) {
135
- r = g = b = l;
136
- }
137
- else {
138
- const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
139
- const p = 2 * l - q;
140
- r = hue2rgb(p, q, h + 1 / 3);
141
- g = hue2rgb(p, q, h);
142
- b = hue2rgb(p, q, h - 1 / 3);
143
- }
144
- return Image.rgbaToColor(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), Math.round(a * 255));
143
+ return Colors.fromHSLA(h, s, l, a);
145
144
  }
146
145
  static hslToColor(h, s, l) {
147
- return Image.hslaToColor(h, s, l, 1);
146
+ return Colors.fromHSL(h, s, l);
148
147
  }
149
148
  static rgbaToHSLA(r, g, b, a) {
150
- r = finite(r, 'red') / 255;
151
- g = finite(g, 'green') / 255;
152
- b = finite(b, 'blue') / 255;
153
- a = finite(a, 'alpha');
154
- const max = Math.max(r, g, b), min = Math.min(r, g, b);
155
- let h = 0, s;
156
- const l = (max + min) / 2;
157
- if (max === min) {
158
- h = s = 0;
159
- }
160
- else {
161
- const d = max - min;
162
- s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
163
- switch (max) {
164
- case r:
165
- h = (g - b) / d + (g < b ? 6 : 0);
166
- break;
167
- case g:
168
- h = (b - r) / d + 2;
169
- break;
170
- case b:
171
- h = (r - g) / d + 4;
172
- break;
173
- }
174
- h /= 6;
175
- }
176
- return [h, s, l, a / 255];
149
+ return rgbaToHSLA(r, g, b, a);
177
150
  }
178
151
  static colorToRGBA(color) {
179
- color = checkColor(color);
180
- return [(color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff];
152
+ return Colors.toRGBA(color);
181
153
  }
182
154
  static colorToRGB(color) {
183
- const [r, g, b] = Image.colorToRGBA(color);
184
- return [r, g, b];
155
+ return Colors.toRGB(color);
156
+ }
157
+ static gradient(colors) {
158
+ return Colors.gradient(colors);
185
159
  }
186
160
  getPixelAt(x, y) {
187
161
  x = ~~finite(x, 'x');
@@ -252,33 +226,24 @@ export class Image {
252
226
  image.__gain_map__ = this.__gain_map__ ? this.__gain_map__.clone() : null;
253
227
  return image;
254
228
  }
255
- static get RESIZE_NEAREST_NEIGHBOR() {
256
- return 'RESIZE_NEAREST_NEIGHBOR';
257
- }
258
- static get RESIZE_BILINEAR() {
259
- return 'RESIZE_BILINEAR';
260
- }
261
- static get RESIZE_BICUBIC() {
262
- return 'RESIZE_BICUBIC';
263
- }
264
- static get RESIZE_AREA() {
265
- return 'RESIZE_AREA';
266
- }
267
- static get RESIZE_AUTO() {
268
- return -1;
269
- }
229
+ static RESIZE_NEAREST_NEIGHBOR = 'RESIZE_NEAREST_NEIGHBOR';
230
+ static RESIZE_BILINEAR = 'RESIZE_BILINEAR';
231
+ static RESIZE_BICUBIC = 'RESIZE_BICUBIC';
232
+ static RESIZE_AREA = 'RESIZE_AREA';
233
+ static RESIZE_AUTO = ResizeModes.AUTO;
270
234
  scale(factor, modeOrOptions) {
271
235
  const { mode, fast } = resizeOptions(modeOrOptions);
272
236
  const image = this.__scale__(factor, mode, fast);
273
237
  this.__gain_map__?.__rescale__(this.width, this.height, image.width, image.height);
274
238
  return this.__apply__(image);
275
239
  }
276
- __scale__(factor, mode = Image.RESIZE_BILINEAR, fast = true) {
240
+ __scale__(factor, mode = ResizeModes.BILINEAR, fast = true) {
277
241
  if (factor === 1)
278
242
  return this;
279
243
  return this.__resize__(this.width * factor, this.height * factor, mode, fast);
280
244
  }
281
- resize(width, height, modeOrOptions) {
245
+ resize(widthOrOptions, heightArgument, modeArgument) {
246
+ const [width, height, modeOrOptions] = resizeArguments(widthOrOptions, heightArgument, modeArgument);
282
247
  const { mode, fast } = resizeOptions(modeOrOptions);
283
248
  const image = this.__resize__(width, height, mode, fast);
284
249
  this.__gain_map__?.__rescale__(this.width, this.height, image.width, image.height);
@@ -310,7 +275,7 @@ export class Image {
310
275
  throw new RangeError('Image has to be at least 1 pixel high');
311
276
  const scale = Math.max(width / this.width, height / this.height);
312
277
  const resizedWidth = Math.max(width, Math.round(this.width * scale)), resizedHeight = Math.max(height, Math.round(this.height * scale));
313
- if (options.mode !== Image.RESIZE_NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
278
+ if (options.mode !== ResizeModes.NEAREST_NEIGHBOR && width >= 1 && height >= 1) {
314
279
  const region = { x: ~~((resizedWidth - width) / 2), y: ~~((resizedHeight - height) / 2), width, height };
315
280
  this.__rescaled__?.(resizedWidth, resizedHeight);
316
281
  const fx = resizedWidth / this.width, fy = resizedHeight / this.height;
@@ -320,12 +285,12 @@ export class Image {
320
285
  this.resize(resizedWidth, resizedHeight, options);
321
286
  return this.crop((this.width - width) / 2, (this.height - height) / 2, width, height);
322
287
  }
323
- __resize__(width, height, mode = Image.RESIZE_BILINEAR, fast = true, region) {
324
- if (width === Image.RESIZE_AUTO && height === Image.RESIZE_AUTO)
288
+ __resize__(width, height, mode = ResizeModes.BILINEAR, fast = true, region) {
289
+ if (width === ResizeModes.AUTO && height === ResizeModes.AUTO)
325
290
  throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
326
- else if (width === Image.RESIZE_AUTO)
291
+ else if (width === ResizeModes.AUTO)
327
292
  width = Math.max(1, Math.round(this.width / this.height * Math.floor(height)));
328
- else if (height === Image.RESIZE_AUTO)
293
+ else if (height === ResizeModes.AUTO)
329
294
  height = Math.max(1, Math.round(this.height / this.width * Math.floor(width)));
330
295
  width = Math.floor(width);
331
296
  height = Math.floor(height);
@@ -333,9 +298,9 @@ export class Image {
333
298
  throw new RangeError('Image has to be at least 1 pixel wide');
334
299
  if (height < 1)
335
300
  throw new RangeError('Image has to be at least 1 pixel high');
336
- if (mode === Image.RESIZE_NEAREST_NEIGHBOR)
301
+ if (mode === ResizeModes.NEAREST_NEIGHBOR)
337
302
  return this.__resize_nearest_neighbor__(width, height);
338
- const filter = mode === Image.RESIZE_BILINEAR ? TRIANGLE : mode === Image.RESIZE_BICUBIC ? CUBIC : mode === Image.RESIZE_AREA ? BOX : null;
303
+ const filter = mode === ResizeModes.BILINEAR ? TRIANGLE : mode === ResizeModes.BICUBIC ? CUBIC : mode === ResizeModes.AREA ? BOX : null;
339
304
  if (!filter)
340
305
  throw new Error('Invalid resize mode');
341
306
  const image = new Image(region?.width ?? width, region?.height ?? height);
@@ -383,7 +348,8 @@ export class Image {
383
348
  throw new RangeError('Crop region does not overlap the image');
384
349
  return [left, top, right - left, bottom - top];
385
350
  }
386
- trim(color, options = {}) {
351
+ trim(colorOrOptions, optionsArgument = {}) {
352
+ const { color, ...options } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
387
353
  const bounds = this.__trim_bounds__(color, options);
388
354
  if (!bounds)
389
355
  return this;
@@ -519,10 +485,12 @@ export class Image {
519
485
  b[i + 3] = a * 255;
520
486
  }
521
487
  cropCircle(maxOrOptions = false, featheringArgument = 0) {
522
- const { max = false, feathering = 0 } = typeof maxOrOptions === 'object' && maxOrOptions !== null
488
+ const { max = false, size = max ? 'cover' : 'contain', feathering = 0 } = typeof maxOrOptions === 'object' && maxOrOptions !== null
523
489
  ? maxOrOptions : { max: maxOrOptions, feathering: featheringArgument };
524
490
  finite(feathering, 'feathering');
525
- const radius = (max ? Math.max : Math.min)(this.width, this.height) / 2;
491
+ if (size !== 'contain' && size !== 'cover')
492
+ throw new TypeError(`Invalid circle size: ${String(size)}`);
493
+ const radius = (size === 'cover' ? Math.max : Math.min)(this.width, this.height) / 2;
526
494
  const cx = this.width / 2, cy = this.height / 2;
527
495
  const band = Math.min(1, Math.max(0, feathering)) * radius;
528
496
  for (let y = 0; y < this.height; y++) {
@@ -540,6 +508,18 @@ export class Image {
540
508
  }
541
509
  return this;
542
510
  }
511
+ recolor(color) {
512
+ if (this.__gain_map__)
513
+ return this.__recolor__(image => image.recolor(color));
514
+ color = checkColor(color);
515
+ const r = color >>> 24, g = color >> 16 & 0xff, b = color >> 8 & 0xff;
516
+ for (let i = 0; i < this.bitmap.length; i += 4) {
517
+ this.bitmap[i] = r;
518
+ this.bitmap[i + 1] = g;
519
+ this.bitmap[i + 2] = b;
520
+ }
521
+ return this;
522
+ }
543
523
  opacity(opacity, absoluteOrOptions = false) {
544
524
  const absolute = isOptions(absoluteOrOptions) ? absoluteOrOptions.absolute ?? false : absoluteOrOptions;
545
525
  opacity = finite(opacity, 'opacity value');
@@ -877,8 +857,9 @@ export class Image {
877
857
  }
878
858
  return this;
879
859
  }
880
- colorToAlpha(color, options = {}) {
881
- const { transparencyThreshold = 0, opacityThreshold = 1 } = options;
860
+ colorToAlpha(colorOrOptions, optionsArgument = {}) {
861
+ const { color: colorOption, transparencyThreshold = 0, opacityThreshold = 1 } = isOptions(colorOrOptions) ? colorOrOptions : { ...optionsArgument, color: colorOrOptions ?? optionsArgument.color };
862
+ let color = colorOption;
882
863
  if (!(transparencyThreshold >= 0 && opacityThreshold <= 1 && transparencyThreshold < opacityThreshold))
883
864
  throw new RangeError('Invalid thresholds, they have to satisfy 0 <= transparencyThreshold < opacityThreshold <= 1');
884
865
  const bitmap = this.bitmap;
@@ -924,8 +905,8 @@ export class Image {
924
905
  weight += a;
925
906
  }
926
907
  if (!weight)
927
- return Image.rgbaToColor(0, 0, 0, 0xff);
928
- return Image.rgbaToColor(Math.round(r / weight), Math.round(g / weight), Math.round(b / weight), 0xff);
908
+ return Colors.fromRGBA(0, 0, 0, 0xff);
909
+ return Colors.fromRGBA(Math.round(r / weight), Math.round(g / weight), Math.round(b / weight), 0xff);
929
910
  }
930
911
  dominantColor(ignoreBlackOrOptions = true, ignoreWhiteArgument = true, thresholdArgument = 0xf) {
931
912
  const { ignoreBlack = true, ignoreWhite = true, threshold: bwThreshold = 0xf } = typeof ignoreBlackOrOptions === 'object' && ignoreBlackOrOptions !== null
@@ -934,7 +915,7 @@ export class Image {
934
915
  const pixels = this.__bytes__();
935
916
  const color = quantize.dominantColor(pixels, (r, g, b) => !(ignoreBlack && Math.max(r, g, b) < threshold) && !(ignoreWhite && Math.min(r, g, b) > 255 - threshold))
936
917
  || quantize.dominantColor(pixels, () => true);
937
- return color ? Image.rgbToColor(...color) : Image.rgbaToColor(0, 0, 0, 0xff);
918
+ return color ? Colors.fromRGB(...color) : Colors.fromRGBA(0, 0, 0, 0xff);
938
919
  }
939
920
  rotate(angle, resizeOrOptions = true) {
940
921
  const { resize = true, fast = true } = typeof resizeOrOptions === 'object' && resizeOrOptions !== null ? resizeOrOptions : { resize: resizeOrOptions };
@@ -1086,51 +1067,6 @@ export class Image {
1086
1067
  this.__u32__ = image.__u32__;
1087
1068
  return this;
1088
1069
  }
1089
- static gradient(colors) {
1090
- const entries = Object.entries(colors).sort((a, b) => Number(a[0]) - Number(b[0]));
1091
- const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
1092
- const values = entries.map(e => checkColor(e[1]));
1093
- if (positions.length === 0)
1094
- throw new RangeError('Invalid gradient point count');
1095
- else if (positions.length === 1) {
1096
- return () => values[0];
1097
- }
1098
- else if (positions.length === 2) {
1099
- const gradient = this.__gradient__(values[0], values[1]);
1100
- return position => {
1101
- if (position <= positions[0])
1102
- return values[0];
1103
- if (position >= positions[1])
1104
- return values[1];
1105
- return gradient((position - positions[0]) / (positions[1] - positions[0]));
1106
- };
1107
- }
1108
- const minDef = Math.min(...positions);
1109
- const maxDef = Math.max(...positions);
1110
- const gradients = [];
1111
- for (let i = 0; i < positions.length; i++) {
1112
- const minPos = positions[i - 1];
1113
- if (minPos === undefined)
1114
- continue;
1115
- const maxPos = positions[i];
1116
- let minVal = values[i - 1];
1117
- if (minVal === undefined)
1118
- minVal = values[i];
1119
- const maxVal = values[i];
1120
- const gradient = this.__gradient__(minVal, maxVal);
1121
- gradients.push({ min: minPos, max: maxPos, gradient });
1122
- }
1123
- return position => {
1124
- if (position <= minDef)
1125
- return gradients[0].gradient(0);
1126
- if (position >= maxDef)
1127
- return gradients[gradients.length - 1].gradient(1);
1128
- for (const gradient of gradients)
1129
- if (position >= gradient.min && position <= gradient.max)
1130
- return gradient.gradient((position - gradient.min) / (gradient.max - gradient.min));
1131
- throw new RangeError(`Invalid gradient position: ${position}`);
1132
- };
1133
- }
1134
1070
  roundCorners(radiusOrOptions) {
1135
1071
  let radius = (isOptions(radiusOrOptions) ? radiusOrOptions.radius : radiusOrOptions) ?? Math.min(this.width, this.height) / 4;
1136
1072
  radius = finite(radius, 'radius');
@@ -1148,23 +1084,6 @@ export class Image {
1148
1084
  }
1149
1085
  return this;
1150
1086
  }
1151
- static __gradient__(startColor, endColor) {
1152
- const sr = startColor >>> 24;
1153
- const sg = startColor >> 16 & 0xff;
1154
- const sb = startColor >> 8 & 0xff;
1155
- const sa = startColor & 0xff;
1156
- const er = (endColor >>> 24) - sr;
1157
- const eg = (endColor >> 16 & 0xff) - sg;
1158
- const eb = (endColor >> 8 & 0xff) - sb;
1159
- const ea = (endColor & 0xff) - sa;
1160
- return position => {
1161
- const r = sr + position * er;
1162
- const g = sg + position * eg;
1163
- const b = sb + position * eb;
1164
- const a = sa + position * ea;
1165
- return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff)) >>> 0;
1166
- };
1167
- }
1168
1087
  fisheye(strengthOrOptions) {
1169
1088
  const strength = finite((isOptions(strengthOrOptions) ? strengthOrOptions.strength : strengthOrOptions) ?? 2, 'strength');
1170
1089
  const r = new Image(this.width, this.height);
@@ -1198,7 +1117,7 @@ export class Image {
1198
1117
  });
1199
1118
  return this.__apply__(r);
1200
1119
  }
1201
- async encode(compressionOrOptions = 4, metadataArgument = {}) {
1120
+ async encodePNG(compressionOrOptions = 4, metadataArgument = {}) {
1202
1121
  let compression = 4, metadata = metadataArgument;
1203
1122
  if (!isOptions(compressionOrOptions))
1204
1123
  compression = compressionOrOptions;
@@ -1208,8 +1127,8 @@ export class Image {
1208
1127
  metadata = compressionOrOptions;
1209
1128
  return png.encodeRGBA(this.__bytes__(), this.width, this.height, compressionLevel(compression), pngText(resolveMetadata(metadata)));
1210
1129
  }
1211
- encodePNG(compressionOrOptions, metadata) {
1212
- return this.encode(compressionOrOptions, metadata);
1130
+ encode(compressionOrOptions, metadata) {
1131
+ return this.encodePNG(compressionOrOptions, metadata);
1213
1132
  }
1214
1133
  async encodeJPEG(qualityOrOptions = 90) {
1215
1134
  const { quality = 90, metadata = {}, gainMap: gainMapOption = true } = isOptions(qualityOrOptions) ? qualityOrOptions : { quality: qualityOrOptions };
@@ -1418,23 +1337,18 @@ export class Image {
1418
1337
  }
1419
1338
  return this.__apply__(out);
1420
1339
  }
1421
- static get SVG_MODE_SCALE() {
1422
- return 1;
1423
- }
1424
- static get SVG_MODE_WIDTH() {
1425
- return 2;
1426
- }
1427
- static get SVG_MODE_HEIGHT() {
1428
- return 3;
1429
- }
1430
- static async renderSVG(svg, sizeOrOptions = 1, modeArgument = Image.SVG_MODE_SCALE) {
1431
- const { size = 1, mode = Image.SVG_MODE_SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1432
- if (![1, 2, 3].includes(mode))
1340
+ static SVG_MODE_SCALE = 1;
1341
+ static SVG_MODE_WIDTH = 2;
1342
+ static SVG_MODE_HEIGHT = 3;
1343
+ static async renderSVG(svg, sizeOrOptions = 1, modeArgument = SVGScaleModes.SCALE) {
1344
+ const { size = 1, mode: givenMode = SVGScaleModes.SCALE, fonts = [] } = isOptions(sizeOrOptions) ? sizeOrOptions : { size: sizeOrOptions, mode: modeArgument };
1345
+ const mode = LEGACY_SVG_MODES.get(givenMode) ?? givenMode;
1346
+ if (mode !== 'scale' && mode !== 'width' && mode !== 'height')
1433
1347
  throw new Error('Invalid SVG scaling mode');
1434
1348
  finite(size, 'SVG size');
1435
- if (mode === Image.SVG_MODE_SCALE && size <= 0)
1349
+ if (mode === SVGScaleModes.SCALE && size <= 0)
1436
1350
  throw new RangeError('SVG scale must be > 0');
1437
- if (mode !== Image.SVG_MODE_SCALE && size < 1)
1351
+ if (mode !== SVGScaleModes.SCALE && size < 1)
1438
1352
  throw new RangeError('SVG size must be >= 1');
1439
1353
  const source = typeof svg === 'string' ? svg : view(svg);
1440
1354
  const images = new Map();
@@ -1459,9 +1373,22 @@ export class Image {
1459
1373
  image.bitmap.set(pixels);
1460
1374
  return image;
1461
1375
  }
1462
- static async renderText(font, size, text, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
1463
- const options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
1464
- const { color = 0xffffffff, layout = new TextLayout(), strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
1376
+ static async renderText(fontOrText, sizeOrOptions, textArgument, colorOrOptions = 0xffffffff, layoutArgument, styleArgument = {}) {
1377
+ let font, size, text, options;
1378
+ if (typeof fontOrText === 'string') {
1379
+ if (!isOptions(sizeOrOptions) || sizeOrOptions.font === undefined)
1380
+ throw new TypeError('renderText(text, options) needs the font in the options');
1381
+ ({ font, size, ...options } = sizeOrOptions);
1382
+ text = fontOrText;
1383
+ }
1384
+ else {
1385
+ font = fontOrText;
1386
+ size = sizeOrOptions;
1387
+ text = textArgument;
1388
+ options = isOptions(colorOrOptions) ? colorOrOptions : { ...styleArgument, color: colorOrOptions, layout: layoutArgument };
1389
+ }
1390
+ const { color = 0xffffffff, strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {} } = options;
1391
+ const layout = options.layout instanceof TextLayout ? options.layout : new TextLayout(options.layout);
1465
1392
  const axes = textRenderer.axisValues(variations);
1466
1393
  size = finite(size, 'font size');
1467
1394
  if (size < 0)
@@ -1471,7 +1398,7 @@ export class Image {
1471
1398
  const fonts = Array.isArray(font) ? font : [font];
1472
1399
  if (!fonts.length)
1473
1400
  throw new RangeError('No font given');
1474
- const [r, g, b, a] = Image.colorToRGBA(color);
1401
+ const [r, g, b, a] = Colors.toRGBA(color);
1475
1402
  const rendered = await textRenderer.render(fonts.map(view), size, text, layout, hinting, (r << 24 | g << 16 | b << 8 | 255) >>> 0, featureList(features), axes);
1476
1403
  const pad = Math.ceil(strokeWidth);
1477
1404
  const width = rendered.width + 2 * pad, height = rendered.height + 2 * pad;
@@ -1503,7 +1430,7 @@ export class Image {
1503
1430
  shape[i] = Math.max(shape[i], colored[i * 4 + 3]);
1504
1431
  }
1505
1432
  const stroke = strokeWidth ? outline(shape, width, height, strokeWidth) : null;
1506
- const [sr, sg, sb, sa] = Image.colorToRGBA(strokeColor);
1433
+ const [sr, sg, sb, sa] = Colors.toRGBA(strokeColor);
1507
1434
  for (let i = 0; i < coverage.length; i++) {
1508
1435
  let af = coverage[i] / 255 * a / 255, pr = r * af, pg = g * af, pb = b * af;
1509
1436
  if (colored && colored[i * 4 + 3]) {
@@ -1547,30 +1474,23 @@ function circleCoverage(px, py, cx, cy, radius) {
1547
1474
  return 0;
1548
1475
  return pixelCoverage(px, py, (x, y) => (x - cx) ** 2 + (y - cy) ** 2 <= radius * radius);
1549
1476
  }
1550
- function channel(value) {
1551
- return value > 255 ? 255 : value > 0 ? Math.round(value) : 0;
1552
- }
1553
- function wrapHue(h) {
1554
- const t = h - Math.trunc(h) + 1;
1555
- return t < 1 ? t : t < 2 ? t - 1 : 0;
1556
- }
1557
- function hue2rgb(p, q, t) {
1558
- if (t < 0)
1559
- t += 1;
1560
- if (t > 1)
1561
- t -= 1;
1562
- if (t < 1 / 6)
1563
- return p + (q - p) * 6 * t;
1564
- if (t < 1 / 2)
1565
- return q;
1566
- if (t < 2 / 3)
1567
- return p + (q - p) * (2 / 3 - t) * 6;
1568
- return p;
1477
+ const LEGACY_RESIZE_MODES = new Map([
1478
+ ['RESIZE_NEAREST_NEIGHBOR', 'nearest-neighbor'], ['RESIZE_BILINEAR', 'bilinear'], ['RESIZE_BICUBIC', 'bicubic'], ['RESIZE_AREA', 'area']
1479
+ ]);
1480
+ export function resizeArguments(widthOrOptions, height, modeOrOptions) {
1481
+ if (!isOptions(widthOrOptions))
1482
+ return [widthOrOptions, height, modeOrOptions];
1483
+ const { width, height: optionsHeight, ...options } = widthOrOptions;
1484
+ if (width === undefined && optionsHeight === undefined)
1485
+ throw new TypeError('resize() needs a width, a height or both');
1486
+ return [width ?? ResizeModes.AUTO, optionsHeight ?? ResizeModes.AUTO, options];
1569
1487
  }
1570
1488
  function resizeOptions(modeOrOptions) {
1571
1489
  const options = isOptions(modeOrOptions) ? modeOrOptions : { mode: modeOrOptions };
1572
- return { mode: options.mode ?? Image.RESIZE_BILINEAR, fast: options.fast ?? true };
1490
+ const mode = options.mode ?? ResizeModes.BILINEAR;
1491
+ return { mode: LEGACY_RESIZE_MODES.get(mode) ?? mode, fast: options.fast ?? true };
1573
1492
  }
1493
+ const LEGACY_SVG_MODES = new Map([[1, 'scale'], [2, 'width'], [3, 'height']]);
1574
1494
  const hardLight = (b, s) => s <= 0.5 ? b * 2 * s : 1 - (1 - b) * (1 - (2 * s - 1));
1575
1495
  const BLEND_MODES = {
1576
1496
  'normal': (_b, s) => s,
package/dist/index.d.ts CHANGED
@@ -2,12 +2,13 @@ import { BinaryData } from './util/mem.js';
2
2
  import { Image, DecodeOptions } from './image.js';
3
3
  import { GIF } from './gif.js';
4
4
  import { Animation, AnimationDecodeOptions } from './animation.js';
5
- export { Image } from './image.js';
6
- export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
7
- export { Frame } from './frame.js';
5
+ export { Image, ResizeModes, SVGScaleModes } from './image.js';
6
+ export { Colors } from './colors.js';
7
+ export type { Color, ColorFunction, ResizeMode, SVGScaleMode, PNGMetadata, TextStyle, DecodeOptions, CompositeOptions, BlendMode, CropCircleOptions, DominantColorOptions, RotateOptions, ColorToAlphaOptions, ResizeOptions, ResizeToOptions, AdjustOptions, SharpenOptions, PNGEncodeOptions, JPEGEncodeOptions, WebPEncodeOptions, AVIFEncodeOptions, JXLEncodeOptions, EncodeOptions, SVGOptions, TextOptions, RenderTextOptions, RoundCornersOptions, FisheyeOptions, TrimOptions } from './image.js';
8
+ export { Frame, FrameDisposalModes } from './frame.js';
8
9
  export type { FrameDisposalModeName, FrameDisposalModeId, FrameOptions } from './frame.js';
9
10
  export { GainMap } from './gainmap.js';
10
- export type { GainMapOptions } from './gainmap.js';
11
+ export type { GainMapOptions, GainMapEncodeOptions } from './gainmap.js';
11
12
  export { GIF } from './gif.js';
12
13
  export { Animation } from './animation.js';
13
14
  export type { AnimationOptions, AnimationDecodeOptions, AnimationEncodeOptions, WebPAnimationEncodeOptions, GIFEncodeOptions, APNGEncodeOptions } from './animation.js';
package/dist/index.js CHANGED
@@ -5,8 +5,9 @@ import { isOptions } from "./util/options.js";
5
5
  import * as webp from "./codecs/webp.js";
6
6
  import { view } from "./util/mem.js";
7
7
  import { ImageType } from "./image-type.js";
8
- export { Image } from "./image.js";
9
- export { Frame } from "./frame.js";
8
+ export { Image, ResizeModes, SVGScaleModes } from "./image.js";
9
+ export { Colors } from "./colors.js";
10
+ export { Frame, FrameDisposalModes } from "./frame.js";
10
11
  export { GainMap } from "./gainmap.js";
11
12
  export { GIF } from "./gif.js";
12
13
  export { Animation } from "./animation.js";
@@ -63,11 +63,11 @@ export function render(source, mode, size, images = new Map(), fonts = []) {
63
63
  if (!(width > 0) || !(height > 0))
64
64
  throw new Error('SVG: invalid dimensions');
65
65
  let outWidth, outHeight;
66
- if (mode === 2) {
66
+ if (mode === 'width') {
67
67
  outWidth = Math.round(size);
68
68
  outHeight = Math.round(height * size / width);
69
69
  }
70
- else if (mode === 3) {
70
+ else if (mode === 'height') {
71
71
  outHeight = Math.round(size);
72
72
  outWidth = Math.round(width * size / height);
73
73
  }
@@ -14,12 +14,14 @@ export interface TextLayoutOptions {
14
14
  wrapStyle?: WrapStyle;
15
15
  /**
16
16
  * Alignment of the lines relative to the widest line. Default: `'left'`
17
- * (before 3.0 this was called verticalAlign, values are accepted under either name)
17
+ * (before 3.0 this was called verticalAlign, the values of the other axis (`'top'`, `'middle'`, `'bottom'`) are
18
+ * accepted for compatibility, which will be removed in 4.0)
18
19
  */
19
20
  horizontalAlign?: HorizontalAlign | VerticalAlign;
20
21
  /**
21
22
  * Which lines to keep when cutting off at `maxHeight`. Default: `'top'`
22
- * (before 3.0 this was called horizontalAlign, values are accepted under either name)
23
+ * (before 3.0 this was called horizontalAlign, the values of the other axis (`'left'`, `'center'`, `'right'`) are
24
+ * accepted for compatibility, which will be removed in 4.0)
23
25
  */
24
26
  verticalAlign?: VerticalAlign | HorizontalAlign;
25
27
  /** Whether to break lines at newline characters. Default: `true` */
package/dist/version.js CHANGED
@@ -1 +1 @@
1
- export const VERSION = '3.16.1';
1
+ export const VERSION = '3.18.0';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "imagescript",
3
- "version": "3.16.1",
3
+ "version": "3.18.0",
4
4
  "description": "Pure TypeScript image processing that runs everywhere",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",