imagescript 3.20.2 → 4.0.0-rc.1

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.
@@ -678,7 +678,7 @@ function output(frame, adobe, jfif, override) {
678
678
  const count = components.length;
679
679
  let transform;
680
680
  if (count === 1)
681
- transform = 'grey';
681
+ transform = 'gray';
682
682
  else if (override && (count === 4) === (override === 'cmyk'))
683
683
  transform = override;
684
684
  else if (count === 3) {
@@ -694,15 +694,15 @@ function output(frame, adobe, jfif, override) {
694
694
  else
695
695
  throw new Error(`JPEG: ${count} color components are not supported`);
696
696
  const [p0, p1, p2, p3] = planes;
697
- if (transform === 'grey' || transform === 'rgb' || transform === 'ycc') {
697
+ if (transform === 'gray' || transform === 'rgb' || transform === 'ycc') {
698
698
  const s0 = p0.samples, s1 = (p1 ?? p0).samples, s2 = (p2 ?? p0).samples;
699
- const grey = transform === 'grey', ycc = transform === 'ycc';
699
+ const gray = transform === 'gray', ycc = transform === 'ycc';
700
700
  const words = littleEndian ? new Uint32Array(pixels.buffer) : null;
701
701
  for (let y = 0, o = 0; y < height; y++, o = y * width * 4) {
702
702
  for (const p of planes)
703
703
  p.fill?.(y);
704
704
  const r0 = y * p0.stride, r1 = p1 ? y * p1.stride : r0, r2 = p2 ? y * p2.stride : r0;
705
- if (grey)
705
+ if (gray)
706
706
  for (let x = 0; x < width; x++, o += 4) {
707
707
  pixels[o] = pixels[o + 1] = pixels[o + 2] = s0[r0 + x];
708
708
  pixels[o + 3] = 0xff;
@@ -807,7 +807,7 @@ function reciprocals(table) {
807
807
  }
808
808
  return r;
809
809
  }
810
- export function encode(pixels, width, height, quality = 90, { exif, comment, segments, grey = false, fullChroma = false } = {}) {
810
+ export function encode(pixels, width, height, quality = 90, { exif, comment, segments, gray = false, fullChroma = false, subsampling } = {}) {
811
811
  if (!width || !height || width > 0xffff || height > 0xffff)
812
812
  throw new RangeError('JPEG: invalid dimensions');
813
813
  quality = Math.min(100, Math.max(1, Math.round(quality)));
@@ -815,7 +815,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
815
815
  const scaleTable = (base) => base.map(v => Math.min(255, Math.max(1, Math.floor((v * scale + 50) / 100))));
816
816
  const tables = [scaleTable(LUMA_QUANTIZATION), scaleTable(CHROMA_QUANTIZATION)];
817
817
  const divisors = tables.map(reciprocals);
818
- const subsample = quality <= 90 && !grey && !fullChroma;
818
+ const subsample = !gray && !fullChroma && (subsampling ? subsampling === '4:2:0' : quality <= 90);
819
819
  const hmax = subsample ? 2 : 1;
820
820
  const mcuSize = 8 * hmax;
821
821
  const mcusPerLine = Math.ceil(width / mcuSize);
@@ -870,8 +870,8 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
870
870
  { plane: Cb, stride: CW, h: 1, v: 1, blocksX: chromaBlocksX, blocksY: chromaBlocksY, table: 1 },
871
871
  { plane: Cr, stride: CW, h: 1, v: 1, blocksX: chromaBlocksX, blocksY: chromaBlocksY, table: 1 }
872
872
  ];
873
- const componentCount = grey ? 1 : 3;
874
- const blocksPerMcu = grey ? 1 : hmax * hmax + 2;
873
+ const componentCount = gray ? 1 : 3;
874
+ const blocksPerMcu = gray ? 1 : hmax * hmax + 2;
875
875
  const count = mcusPerLine * mcusPerColumn * blocksPerMcu;
876
876
  const coefficients = new Int16Array(count * 64);
877
877
  const owners = new Uint8Array(count), ends = new Uint8Array(count);
@@ -911,7 +911,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
911
911
  const blocks = { coefficients, owners, ends, count };
912
912
  const frequencies = [new Int32Array(257), new Int32Array(257), new Int32Array(257), new Int32Array(257)];
913
913
  countSymbols(blocks, frequencies);
914
- const huffman = frequencies.slice(0, grey ? 2 : 4).map(buildOptimalTable);
914
+ const huffman = frequencies.slice(0, gray ? 2 : 4).map(buildOptimalTable);
915
915
  const writer = new BitWriter(width * height);
916
916
  writer.bytes(0xff, 0xd8);
917
917
  writer.bytes(0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00);
@@ -930,18 +930,18 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
930
930
  for (const bytes of segments)
931
931
  for (let i = 0; i < bytes.length; i++)
932
932
  writer.bytes(bytes[i]);
933
- for (let t = 0; t < (grey ? 1 : 2); t++) {
933
+ for (let t = 0; t < (gray ? 1 : 2); t++) {
934
934
  writer.bytes(0xff, 0xdb, 0x00, 0x43, t);
935
935
  for (let k = 0; k < 64; k++)
936
936
  writer.bytes(tables[t][ZIGZAG[k]]);
937
937
  }
938
- if (grey)
938
+ if (gray)
939
939
  writer.bytes(0xff, 0xc0, 0x00, 0x0b, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x01, 1, 0x11, 0);
940
940
  else {
941
941
  writer.bytes(0xff, 0xc0, 0x00, 0x11, 0x08, height >> 8, height & 0xff, width >> 8, width & 0xff, 0x03);
942
942
  writer.bytes(1, hmax << 4 | hmax, 0, 2, 0x11, 1, 3, 0x11, 1);
943
943
  }
944
- for (const [index, id] of [[0, 0x00], [1, 0x10], [2, 0x01], [3, 0x11]].slice(0, grey ? 2 : 4)) {
944
+ for (const [index, id] of [[0, 0x00], [1, 0x10], [2, 0x01], [3, 0x11]].slice(0, gray ? 2 : 4)) {
945
945
  const { bits, values } = huffman[index];
946
946
  const length = 19 + values.length;
947
947
  writer.bytes(0xff, 0xc4, length >> 8, length & 0xff, id);
@@ -950,7 +950,7 @@ export function encode(pixels, width, height, quality = 90, { exif, comment, seg
950
950
  for (const value of values)
951
951
  writer.bytes(value);
952
952
  }
953
- if (grey)
953
+ if (gray)
954
954
  writer.bytes(0xff, 0xda, 0x00, 0x08, 0x01, 1, 0x00, 0x00, 0x3f, 0x00);
955
955
  else
956
956
  writer.bytes(0xff, 0xda, 0x00, 0x0c, 0x03, 1, 0x00, 2, 0x11, 3, 0x11, 0x00, 0x3f, 0x00);
@@ -1,7 +1,7 @@
1
1
  import { extensions, unpackSigned } from "./bits.js";
2
2
  import { EntropyDecoder } from "./entropy.js";
3
3
  export const EXTRA_ALPHA = 0, EXTRA_SPOT = 2, EXTRA_BLACK = 4;
4
- export const CS_RGB = 0, CS_GREY = 1, CS_XYB = 2;
4
+ export const CS_RGB = 0, CS_GRAY = 1, CS_XYB = 2;
5
5
  export const WP_D65 = 1, WP_CUSTOM = 2, WP_E = 10, WP_DCI = 11;
6
6
  export const PRI_SRGB = 1, PRI_CUSTOM = 2, PRI_BT2100 = 9, PRI_P3 = 11;
7
7
  export const TF_BT709 = 1, TF_UNKNOWN = 2, TF_LINEAR = 8, TF_SRGB = 13, TF_PQ = 16, TF_DCI = 17, TF_HLG = 18;
@@ -94,7 +94,7 @@ export function colourEncoding(r) {
94
94
  else if (c.whitePoint !== WP_D65 && c.whitePoint !== WP_E && c.whitePoint !== WP_DCI)
95
95
  throw new Error('JPEG XL: invalid white point');
96
96
  }
97
- if (c.colourSpace !== CS_XYB && c.colourSpace !== CS_GREY) {
97
+ if (c.colourSpace !== CS_XYB && c.colourSpace !== CS_GRAY) {
98
98
  c.primaries = r.enum();
99
99
  if (c.primaries === PRI_CUSTOM)
100
100
  for (let i = 0; i < 6; i++)
@@ -1,7 +1,7 @@
1
1
  import { startThreads } from "../util/threads.js";
2
2
  import { MAX_PIXELS, checkPixels } from "../util/limits.js";
3
3
  import { BitReader } from "./jxl/bits.js";
4
- import { CS_GREY, CS_RGB, decodeICC, imageHeader, PRI_BT2100, PRI_CUSTOM, PRI_P3, readICC, TF_DCI, TF_HLG, TF_LINEAR, TF_PQ, WP_CUSTOM, WP_DCI, WP_E } from "./jxl/headers.js";
4
+ import { CS_GRAY, CS_RGB, decodeICC, imageHeader, PRI_BT2100, PRI_CUSTOM, PRI_P3, readICC, TF_DCI, TF_HLG, TF_LINEAR, TF_PQ, WP_CUSTOM, WP_DCI, WP_E } from "./jxl/headers.js";
5
5
  import { frameHeader, toc } from "./jxl/frame-header.js";
6
6
  import { decodeFrame } from "./jxl/frame.js";
7
7
  import { ColorOutput } from "./jxl/color.js";
@@ -85,16 +85,16 @@ const PRIMARIES = {
85
85
  };
86
86
  export function colorSpace(image) {
87
87
  const c = image.colour;
88
- if (image.xybEncoded || c.wantICC || (c.colourSpace !== CS_RGB && c.colourSpace !== CS_GREY))
88
+ if (image.xybEncoded || c.wantICC || (c.colourSpace !== CS_RGB && c.colourSpace !== CS_GRAY))
89
89
  return;
90
90
  if (c.transfer === TF_PQ || c.transfer === TF_HLG)
91
91
  return;
92
92
  const curve = c.transfer === -1 ? (c.gamma > 0 ? v => v ** (1 / c.gamma) : null)
93
93
  : c.transfer === TF_LINEAR ? v => v : c.transfer === TF_DCI ? v => v ** 2.6 : null;
94
94
  const white = c.whitePoint === WP_CUSTOM ? c.white : c.whitePoint === WP_E ? [1 / 3, 1 / 3] : c.whitePoint === WP_DCI ? [0.314, 0.351] : null;
95
- const primaries = c.colourSpace === CS_GREY ? null : c.primaries === PRI_CUSTOM
95
+ const primaries = c.colourSpace === CS_GRAY ? null : c.primaries === PRI_CUSTOM
96
96
  ? [0, 2, 4].map(i => c.primariesXY.slice(i, i + 2)) : PRIMARIES[c.primaries] ?? null;
97
- if (c.colourSpace === CS_GREY)
97
+ if (c.colourSpace === CS_GRAY)
98
98
  return curve ? { xy: null, curve } : undefined;
99
99
  if (!curve && !white && !primaries)
100
100
  return;
@@ -136,7 +136,7 @@ export async function* frames(data, maxPixels = MAX_PIXELS, toneMapping = 'reinh
136
136
  shown++;
137
137
  yield {
138
138
  width: image.width, height: image.height, pixels: state.output.toRGBA(frame.canvas), duration: frame.header.duration * tick,
139
- orientation: image.orientation, loopCount: a ? a.numLoops : 1, icc, colorSpace: space
139
+ orientation: image.orientation, loopCount: a ? a.numLoops : 1, animated: !!a, icc, colorSpace: space
140
140
  };
141
141
  }
142
142
  if (frame.header.isLast)
@@ -186,31 +186,31 @@ export function encode(rgba, width, height) {
186
186
  if (width < 1 || height < 1)
187
187
  throw new RangeError('Netpbm: dimensions must be at least 1');
188
188
  const count = width * height;
189
- let opaque = true, grey = true, bitmap = true;
189
+ let opaque = true, gray = true, bitmap = true;
190
190
  for (let p = 0; p < count * 4; p += 4) {
191
191
  const r = rgba[p], g = rgba[p + 1], b = rgba[p + 2], a = rgba[p + 3];
192
192
  if (a !== 255)
193
193
  opaque = false;
194
194
  if (r !== g || g !== b)
195
- grey = false;
195
+ gray = false;
196
196
  if (r !== 0 && r !== 255 || r !== g || g !== b)
197
197
  bitmap = false;
198
- if (!opaque && !grey)
198
+ if (!opaque && !gray)
199
199
  break;
200
200
  }
201
201
  const ascii = (text) => Uint8Array.from(text, c => c.charCodeAt(0));
202
202
  let header, depth;
203
203
  if (!opaque) {
204
- depth = grey ? 2 : 4;
205
- header = ascii(`P7\nWIDTH ${width}\nHEIGHT ${height}\nDEPTH ${depth}\nMAXVAL 255\nTUPLTYPE ${grey ? 'GRAYSCALE_ALPHA' : 'RGB_ALPHA'}\nENDHDR\n`);
204
+ depth = gray ? 2 : 4;
205
+ header = ascii(`P7\nWIDTH ${width}\nHEIGHT ${height}\nDEPTH ${depth}\nMAXVAL 255\nTUPLTYPE ${gray ? 'GRAYSCALE_ALPHA' : 'RGB_ALPHA'}\nENDHDR\n`);
206
206
  }
207
207
  else if (bitmap) {
208
208
  depth = 0;
209
209
  header = ascii(`P4\n${width} ${height}\n`);
210
210
  }
211
211
  else {
212
- depth = grey ? 1 : 3;
213
- header = ascii(`P${grey ? 5 : 6}\n${width} ${height}\n255\n`);
212
+ depth = gray ? 1 : 3;
213
+ header = ascii(`P${gray ? 5 : 6}\n${width} ${height}\n255\n`);
214
214
  }
215
215
  const stride = depth ? width * depth : width + 7 >> 3;
216
216
  const out = new Uint8Array(header.length + stride * height);
@@ -116,7 +116,7 @@ export function alphaCost(images) {
116
116
  return scanner.cost();
117
117
  }
118
118
  export class AlphaCost {
119
- grey = true;
119
+ gray = true;
120
120
  colors = new Set();
121
121
  done = false;
122
122
  add(rgba) {
@@ -134,20 +134,20 @@ export class AlphaCost {
134
134
  if (rgba[o + 3] === 0)
135
135
  continue;
136
136
  if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
137
- this.grey = false;
137
+ this.gray = false;
138
138
  if (colors.size <= 256)
139
139
  colors.add(value);
140
- else if (!this.grey) {
140
+ else if (!this.gray) {
141
141
  this.done = true;
142
142
  return;
143
143
  }
144
144
  }
145
145
  }
146
146
  cost() {
147
- const { colors, grey } = this;
147
+ const { colors, gray } = this;
148
148
  const palette = (entries) => (entries <= 2 ? 1 : entries <= 4 ? 2 : entries <= 16 ? 4 : 8) / 8;
149
- const transparent = colors.size < 256 ? palette(colors.size + 1) : grey ? 2 : 4;
150
- const opaque = colors.size <= 256 ? palette(colors.size) : grey ? 1 : 3;
149
+ const transparent = colors.size < 256 ? palette(colors.size + 1) : gray ? 2 : 4;
150
+ const opaque = colors.size <= 256 ? palette(colors.size) : gray ? 1 : 3;
151
151
  return transparent / opaque;
152
152
  }
153
153
  }
@@ -159,7 +159,7 @@ function pixelFormat(images) {
159
159
  }
160
160
  export class FormatScanner {
161
161
  opaque = true;
162
- grey = true;
162
+ gray = true;
163
163
  colors = new Map();
164
164
  add(rgba) {
165
165
  const pixels = new Uint32Array(rgba.buffer, rgba.byteOffset, rgba.length >> 2);
@@ -173,22 +173,22 @@ export class FormatScanner {
173
173
  if (rgba[o + 3] !== 255)
174
174
  this.opaque = false;
175
175
  if (rgba[o] !== rgba[o + 1] || rgba[o] !== rgba[o + 2])
176
- this.grey = false;
176
+ this.gray = false;
177
177
  if (this.colors && !this.colors.has(value)) {
178
178
  if (this.colors.size === 256)
179
179
  this.colors = null;
180
180
  else
181
181
  this.colors.set(value, this.colors.size);
182
182
  }
183
- if (!this.colors && !this.opaque && !this.grey)
183
+ if (!this.colors && !this.opaque && !this.gray)
184
184
  return;
185
185
  }
186
186
  }
187
187
  format() {
188
- return formatOf(this.colors, this.opaque, this.grey);
188
+ return formatOf(this.colors, this.opaque, this.gray);
189
189
  }
190
190
  }
191
- function formatOf(colors, opaque, grey) {
191
+ function formatOf(colors, opaque, gray) {
192
192
  if (colors) {
193
193
  const entries = [...colors.keys()].map(value => new Uint8Array(new Uint32Array([value]).buffer));
194
194
  entries.sort((a, b) => a[3] - b[3]);
@@ -221,7 +221,7 @@ function formatOf(colors, opaque, grey) {
221
221
  }
222
222
  };
223
223
  }
224
- const channels = (grey ? 1 : 3) + (opaque ? 0 : 1);
224
+ const channels = (gray ? 1 : 3) + (opaque ? 0 : 1);
225
225
  return {
226
226
  channels, depth: 8,
227
227
  convert(rgba, width, height) {
@@ -540,11 +540,11 @@ export function isAnimated(array) {
540
540
  return false;
541
541
  }
542
542
  }
543
- export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
543
+ export async function decodeAnimation(array, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
544
544
  const p = parse(array);
545
545
  const { width, height } = p.header;
546
546
  const orientation = p.exif ? exifOrientation(p.exif) : await textOrientation(p.text);
547
- checkPixels(width, height, maxPixels, !p.animation || p.animationError || onlyFirstFrame ? 1 : Math.max(1, p.frames.length));
547
+ checkPixels(width, height, maxPixels, !p.animation || p.animationError || firstFrameOnly ? 1 : Math.max(1, p.frames.length));
548
548
  if (!p.animation || p.animationError) {
549
549
  const pixels = await decodeImageData(p, p.idat, width, height);
550
550
  return { width, height, plays: 0, frames: [{ x: 0, y: 0, width, height, duration: 0, dispose: 0, blend: 0, pixels }], icc: await profile(p.iccp), orientation };
@@ -559,7 +559,7 @@ export async function decodeAnimation(array, onlyFirstFrame = false, maxPixels =
559
559
  blend: c.blend,
560
560
  pixels: await decodeImageData(p, data, c.width, c.height)
561
561
  });
562
- if (onlyFirstFrame)
562
+ if (firstFrameOnly)
563
563
  break;
564
564
  }
565
565
  return { width, height, plays: p.animation.plays, frames, icc: await profile(p.iccp), orientation };
@@ -1,4 +1,22 @@
1
1
  const WR = 3, WG = 4, WB = 2;
2
+ const DITHER = { 'none': true, 'floyd-steinberg': true, 'atkinson': true, 'ordered': true };
3
+ export const DITHER_METHODS = Object.keys(DITHER);
4
+ export function paletteOptions(colors, dither, ditherStrength) {
5
+ let count;
6
+ if (colors !== undefined) {
7
+ count = Number(colors);
8
+ if (!Number.isInteger(count) || count < 2 || count > 256)
9
+ throw new RangeError(`colors must be an integer from 2 to 256, not ${colors}`);
10
+ }
11
+ if (!DITHER_METHODS.includes(dither))
12
+ throw new TypeError(`dither must be one of ${DITHER_METHODS.map(name => `'${name}'`).join(', ')}, not ${String(dither)}`);
13
+ const strength = Number(ditherStrength);
14
+ if (!Number.isFinite(strength))
15
+ throw new RangeError(`Invalid ditherStrength: ${ditherStrength}`);
16
+ if (strength < 0 || strength > 1)
17
+ throw new RangeError(`ditherStrength must be 0-1, not ${ditherStrength}`);
18
+ return { colors: count, dither: dither, strength };
19
+ }
2
20
  export function dominantColor(pixels, include, clusters = 8) {
3
21
  const { palette, weights } = medianCut(pixels, clusters, 4, include);
4
22
  if (!weights.length)
@@ -9,7 +27,7 @@ export function dominantColor(pixels, include, clusters = 8) {
9
27
  best = i;
10
28
  return [Math.round(palette[best * 3]), Math.round(palette[best * 3 + 1]), Math.round(palette[best * 3 + 2])];
11
29
  }
12
- export function quantize(pixels, width, { maxColors = 256, quality = 95, transparent = false } = {}) {
30
+ export function quantize(pixels, width, { maxColors = 256, dither = 'floyd-steinberg', ditherStrength = 0.85, transparent = false } = {}) {
13
31
  const length = pixels.length >> 2;
14
32
  let hasTransparency = transparent;
15
33
  for (let i = 3; i < pixels.length; i += 4)
@@ -17,16 +35,14 @@ export function quantize(pixels, width, { maxColors = 256, quality = 95, transpa
17
35
  hasTransparency = true;
18
36
  break;
19
37
  }
20
- quality = Math.min(100, Math.max(1, quality));
21
- const limit = Math.min(maxColors, Math.round(8 * 32 ** (Math.min(quality, 90) / 90)));
22
- const colors = hasTransparency ? limit - 1 : limit;
38
+ const colors = hasTransparency ? maxColors - 1 : maxColors;
23
39
  const exact = exactPalette(pixels, colors);
24
40
  let palette, indices;
25
41
  if (exact)
26
42
  ({ palette, indices } = exact);
27
43
  else {
28
- palette = Uint8Array.from(medianCut(pixels, colors, Math.ceil(quality / 25)).palette, Math.round);
29
- indices = ditherMap(pixels, width, palette, palette.length / 3);
44
+ palette = Uint8Array.from(medianCut(pixels, colors, 4).palette, Math.round);
45
+ indices = dither === 'floyd-steinberg' ? ditherMap(pixels, width, palette, palette.length / 3, ditherStrength) : paletteIndices(pixels, width, palette, dither, ditherStrength);
30
46
  }
31
47
  let transparentIndex = -1;
32
48
  if (hasTransparency) {
@@ -340,13 +356,23 @@ function createLookup(palette, entries) {
340
356
  return index;
341
357
  };
342
358
  }
343
- function ditherMap(pixels, width, palette, entries) {
359
+ function paletteIndices(pixels, width, palette, dither, strength) {
360
+ const mapped = pixels.slice();
361
+ applyPalette(mapped, width, (pixels.length >> 2) / width, palette, dither, strength);
362
+ const lookup = new Map();
363
+ for (let i = palette.length / 3 - 1; i >= 0; i--)
364
+ lookup.set(palette[i * 3] << 16 | palette[i * 3 + 1] << 8 | palette[i * 3 + 2], i);
365
+ const indices = new Uint8Array(pixels.length >> 2);
366
+ for (let p = 0; p < indices.length; p++)
367
+ indices[p] = lookup.get(mapped[p * 4] << 16 | mapped[p * 4 + 1] << 8 | mapped[p * 4 + 2]) ?? 0;
368
+ return indices;
369
+ }
370
+ function ditherMap(pixels, width, palette, entries, strength) {
344
371
  const height = (pixels.length >> 2) / width;
345
372
  const lookup = createLookup(palette, entries);
346
373
  const indices = new Uint8Array(pixels.length >> 2);
347
374
  let current = new Float32Array((width + 2) * 3);
348
375
  let next = new Float32Array((width + 2) * 3);
349
- const strength = 0.85;
350
376
  for (let y = 0; y < height; y++) {
351
377
  const reverse = y & 1;
352
378
  for (let step = 0; step < width; step++) {
@@ -173,7 +173,7 @@ export async function decode(data, maxPixels = MAX_PIXELS) {
173
173
  joined.set(stream.subarray(2), tables.length - 2);
174
174
  stream = joined;
175
175
  }
176
- const decoded = jpeg.decode(stream, { transform });
176
+ const decoded = jpeg.decode(stream, { transform, maxPixels });
177
177
  blitRGBA(decoded.pixels, decoded.width, decoded.height, pixels, width, height, (s % segmentsAcross) * segmentWidth, (s / segmentsAcross | 0) * segmentHeight);
178
178
  }
179
179
  return { width, height, pixels, orientation, icc };
@@ -136,7 +136,7 @@ export function decode(data, maxPixels = MAX_PIXELS) {
136
136
  }
137
137
  return { ...image, orientation, icc };
138
138
  }
139
- export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PIXELS) {
139
+ export function decodeAnimation(data, firstFrameOnly = false, maxPixels = MAX_PIXELS) {
140
140
  const list = container(data);
141
141
  const header = list[0];
142
142
  if (!header || header.type !== VP8X || !list.some(c => c.type === ANMF)) {
@@ -146,7 +146,7 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
146
146
  if (header.data.length < 10)
147
147
  throw new Error('WebP: invalid VP8X chunk');
148
148
  const width = u24(header.data, 4) + 1, height = u24(header.data, 7) + 1;
149
- checkPixels(width, height, maxPixels, onlyFirstFrame ? 1 : list.filter(c => c.type === ANMF).length);
149
+ checkPixels(width, height, maxPixels, firstFrameOnly ? 1 : list.filter(c => c.type === ANMF).length);
150
150
  let loopCount = 0, background = 0;
151
151
  const frames = [];
152
152
  for (const chunk of list) {
@@ -170,7 +170,7 @@ export function decodeAnimation(data, onlyFirstFrame = false, maxPixels = MAX_PI
170
170
  dispose: (d[15] & 1) === 1,
171
171
  pixels: frame.pixels
172
172
  });
173
- if (onlyFirstFrame)
173
+ if (firstFrameOnly)
174
174
  break;
175
175
  }
176
176
  }
package/dist/colors.d.ts CHANGED
@@ -37,7 +37,7 @@ export declare class Colors {
37
37
  static fromRGB(r: number, g: number, b: number): Color;
38
38
  /**
39
39
  * Converts HSLA colors to RGBA colors
40
- * @param h hue (0..1)
40
+ * @param h hue in degrees (0..360, like CSS' `hsl()`: 0 red, 120 green, 240 blue; other values wrap around)
41
41
  * @param s saturation (0..1)
42
42
  * @param l lightness (0..1)
43
43
  * @param a opacity (0..1)
@@ -45,8 +45,8 @@ export declare class Colors {
45
45
  */
46
46
  static fromHSLA(h: number, s: number, l: number, a: number): Color;
47
47
  /**
48
- * Converts HSL colors to RGBA colors (assuming an opacity of 255)
49
- * @param h hue (0..1)
48
+ * Converts HSL colors to RGBA colors (fully opaque)
49
+ * @param h hue in degrees (0..360, like CSS' `hsl()`; other values wrap around)
50
50
  * @param s saturation (0..1)
51
51
  * @param l lightness (0..1)
52
52
  * @returns color
@@ -59,7 +59,7 @@ export declare class Colors {
59
59
  */
60
60
  static toRGBA(color: Color): [r: number, g: number, b: number, a: number];
61
61
  /**
62
- * Converts a color value to an array of RGB values (ignoring the colors alpha)
62
+ * Converts a color value to an array of RGB values (ignoring the color's alpha)
63
63
  * @param color The color value to convert
64
64
  * @returns The RGB values ([R, G, B])
65
65
  */
@@ -67,13 +67,14 @@ export declare class Colors {
67
67
  /**
68
68
  * Converts a color to an array of HSLA values
69
69
  * @param color The color value to convert
70
- * @returns The HSLA values ([H, S, L, A]), H and S and L and A in 0..1
70
+ * @returns The HSLA values ([H, S, L, A]): H in degrees (0..360, like CSS' `hsl()`), S, L and A in 0..1
71
71
  */
72
72
  static toHSLA(color: Color): [h: number, s: number, l: number, a: number];
73
73
  /**
74
74
  * Creates a multi-point gradient generator
75
- * @param colors The gradient points to use (e.g. `{0: 0xff0000ff, 1: 0x00ff00ff}`)
75
+ * @param stops The colors along the gradient: their positions (0..1) and colors (`{ 0: 0xff0000ff, 1: 0x00ff00ff }`),
76
+ * or just the colors, evenly spaced (`[0xff0000ff, 0x00ff00ff]`), like {@link GradientOptions.stops}
76
77
  * @return The gradient generator. The function argument is the position in the gradient (0..1).
77
78
  */
78
- static gradient(colors: Record<number, Color>): (position: number) => Color;
79
+ static gradient(stops: Record<number, Color> | Color[]): (position: number) => Color;
79
80
  }
package/dist/colors.js CHANGED
@@ -78,7 +78,7 @@ export class Colors {
78
78
  return Colors.fromRGBA(r, g, b, 0xff);
79
79
  }
80
80
  static fromHSLA(h, s, l, a) {
81
- h = wrapHue(finite(h, 'hue'));
81
+ h = wrapHue(finite(h, 'hue') / 360);
82
82
  s = finite(s, 'saturation');
83
83
  l = finite(l, 'lightness');
84
84
  a = finite(a, 'alpha');
@@ -111,9 +111,19 @@ export class Colors {
111
111
  }
112
112
  static toHSLA(color) {
113
113
  const [r, g, b, a] = Colors.toRGBA(color);
114
- return rgbaToHSLA(r, g, b, a);
114
+ const [h, s, l, alpha] = rgbaToHSLA(r, g, b, a);
115
+ return [h * 360, s, l, alpha];
115
116
  }
116
- static gradient(colors) {
117
+ static gradient(stops) {
118
+ let colors;
119
+ if (Array.isArray(stops)) {
120
+ if (!stops.length)
121
+ throw new RangeError('Invalid gradient point count');
122
+ const list = stops.length === 1 ? [stops[0], stops[0]] : stops;
123
+ colors = Object.fromEntries(list.map((color, i) => [i / (list.length - 1), color]));
124
+ }
125
+ else
126
+ colors = stops;
117
127
  const entries = Object.entries(colors).sort((a, b) => Number(a[0]) - Number(b[0]));
118
128
  const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
119
129
  const values = entries.map(e => checkColor(e[1]));