imagescript 3.16.1 → 3.17.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/README.md +4 -4
- package/dist/animation-encoder.d.ts +10 -2
- package/dist/animation-encoder.js +6 -3
- package/dist/animation.d.ts +15 -3
- package/dist/animation.js +7 -7
- package/dist/colors.d.ts +79 -0
- package/dist/colors.js +161 -0
- package/dist/frame.d.ts +47 -11
- package/dist/frame.js +12 -13
- package/dist/gainmap.d.ts +4 -0
- package/dist/gif.d.ts +1 -0
- package/dist/image-type.d.ts +52 -13
- package/dist/image.d.ts +181 -106
- package/dist/image.js +63 -181
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -2
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,10 +27,10 @@ Without a bundler, import it from jsDelivr in the browser:
|
|
|
27
27
|
|
|
28
28
|
```html
|
|
29
29
|
<script type="module">
|
|
30
|
-
import { Image } from 'https://cdn.jsdelivr.net/npm/imagescript@3/+esm';
|
|
30
|
+
import { Image, ResizeModes } from 'https://cdn.jsdelivr.net/npm/imagescript@3/+esm';
|
|
31
31
|
|
|
32
32
|
const image = await Image.decode(await fetch('photo.jpg').then(r => r.arrayBuffer()));
|
|
33
|
-
image.resize(256,
|
|
33
|
+
image.resize(256, ResizeModes.AUTO);
|
|
34
34
|
const url = URL.createObjectURL(new Blob([await image.encode()], { type: 'image/png' }));
|
|
35
35
|
document.body.append(Object.assign(document.createElement('img'), { src: url }));
|
|
36
36
|
</script>
|
|
@@ -41,11 +41,11 @@ Without a bundler, import it from jsDelivr in the browser:
|
|
|
41
41
|
### Example
|
|
42
42
|
|
|
43
43
|
```js
|
|
44
|
-
import { Image } from 'imagescript';
|
|
44
|
+
import { Image, ResizeModes } from 'imagescript';
|
|
45
45
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
46
46
|
|
|
47
47
|
const image = await Image.decode(await readFile('input.jpg'));
|
|
48
|
-
image.resize(256,
|
|
48
|
+
image.resize(256, ResizeModes.AUTO).roundCorners(16);
|
|
49
49
|
await writeFile('output.png', await image.encode());
|
|
50
50
|
```
|
|
51
51
|
|
|
@@ -43,14 +43,22 @@ export interface APNGAnimationEncoderOptions extends AnimationEncoderOptions {
|
|
|
43
43
|
* format depends on all of them
|
|
44
44
|
*/
|
|
45
45
|
export declare class AnimationEncoder {
|
|
46
|
-
readonly width: number;
|
|
47
|
-
readonly height: number;
|
|
48
46
|
private readonly format;
|
|
49
47
|
private optimizer;
|
|
50
48
|
private queue;
|
|
51
49
|
private count;
|
|
52
50
|
private finished;
|
|
51
|
+
/** The canvas width */
|
|
52
|
+
readonly width: number;
|
|
53
|
+
/** The canvas height */
|
|
54
|
+
readonly height: number;
|
|
53
55
|
/** How many frames were added */
|
|
56
|
+
get frameCount(): number;
|
|
57
|
+
/**
|
|
58
|
+
* How many frames were added
|
|
59
|
+
* @deprecated Use {@link AnimationEncoder.frameCount}, `frames` will be removed in 4.0
|
|
60
|
+
* @group Deprecated
|
|
61
|
+
*/
|
|
54
62
|
get frames(): number;
|
|
55
63
|
/**
|
|
56
64
|
* Adds the next frame: a {@link Frame} (with its duration, offsets and disposal) or an image (a frame of 100 ms by
|
|
@@ -9,19 +9,22 @@ import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
|
9
9
|
import { finite, compressionLevel } from "./util/options.js";
|
|
10
10
|
const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
11
11
|
export class AnimationEncoder {
|
|
12
|
-
width;
|
|
13
|
-
height;
|
|
14
12
|
format;
|
|
15
13
|
optimizer;
|
|
16
14
|
queue = Promise.resolve();
|
|
17
15
|
count = 0;
|
|
18
16
|
finished = false;
|
|
17
|
+
width;
|
|
18
|
+
height;
|
|
19
19
|
constructor(width, height, format, optimize) {
|
|
20
|
+
this.format = format;
|
|
20
21
|
this.width = width;
|
|
21
22
|
this.height = height;
|
|
22
|
-
this.format = format;
|
|
23
23
|
this.optimizer = optimize ? new Optimizer(width, height, format.optimize) : null;
|
|
24
24
|
}
|
|
25
|
+
get frameCount() {
|
|
26
|
+
return this.count;
|
|
27
|
+
}
|
|
25
28
|
get frames() {
|
|
26
29
|
return this.count;
|
|
27
30
|
}
|
package/dist/animation.d.ts
CHANGED
|
@@ -86,7 +86,10 @@ export declare class Animation extends Array<Frame> {
|
|
|
86
86
|
* setting an index), and can be replaced as a whole
|
|
87
87
|
*/
|
|
88
88
|
get frames(): Frame[];
|
|
89
|
-
/**
|
|
89
|
+
/**
|
|
90
|
+
* Replaces the animation's frames
|
|
91
|
+
* @param frames The new frames (copied into the animation, the array itself isn't kept)
|
|
92
|
+
*/
|
|
90
93
|
set frames(frames: Frame[]);
|
|
91
94
|
/** The animation's width */
|
|
92
95
|
get width(): number;
|
|
@@ -99,18 +102,27 @@ export declare class Animation extends Array<Frame> {
|
|
|
99
102
|
/**
|
|
100
103
|
* Returns a new animation of the same class containing a range of this one's frames
|
|
101
104
|
* (same semantics as Array.prototype.slice)
|
|
105
|
+
* @param start The index of the first frame (negative counts from the end). Default: `0`
|
|
106
|
+
* @param end The index after the last frame (negative counts from the end). Default: the frame count
|
|
102
107
|
* @deprecated Use `new Animation(animation.frames.slice(start, end), animation.loopCount)`: in 4.0 animations
|
|
103
108
|
* aren't arrays anymore
|
|
109
|
+
* @group Deprecated
|
|
104
110
|
*/
|
|
105
111
|
slice(start?: number, end?: number): this;
|
|
106
112
|
/**
|
|
107
113
|
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
|
|
108
|
-
* Use {@link
|
|
114
|
+
* Use {@link ResizeModes.AUTO} as either width or height to automatically preserve the aspect ratio.
|
|
115
|
+
* @param width The new width
|
|
116
|
+
* @param height The new height
|
|
117
|
+
* @param options See {@link ResizeOptions}
|
|
109
118
|
*/
|
|
110
119
|
resize(width: number, height: number, options?: ResizeOptions): this;
|
|
111
120
|
/**
|
|
112
121
|
* Resizes the animation to the given dimensions, scaling every frame and its offsets proportionally.
|
|
113
|
-
* Use {@link
|
|
122
|
+
* Use {@link ResizeModes.AUTO} as either width or height to automatically preserve the aspect ratio.
|
|
123
|
+
* @param width The new width
|
|
124
|
+
* @param height The new height
|
|
125
|
+
* @param mode The resizing mode to use. Default: {@link ResizeModes.BILINEAR}
|
|
114
126
|
*/
|
|
115
127
|
resize(width: number, height: number, mode?: ResizeMode): this;
|
|
116
128
|
/**
|
package/dist/animation.js
CHANGED
|
@@ -5,8 +5,8 @@ import * as webp from "./codecs/webp.js";
|
|
|
5
5
|
import * as jxl from "./codecs/jxl.js";
|
|
6
6
|
import { availableThreads } from "./util/threads.js";
|
|
7
7
|
import { view } from "./util/mem.js";
|
|
8
|
-
import { Image } from "./image.js";
|
|
9
|
-
import { Frame } from "./frame.js";
|
|
8
|
+
import { Image, ResizeModes } from "./image.js";
|
|
9
|
+
import { Frame, FrameDisposalModes } from "./frame.js";
|
|
10
10
|
import { ImageType } from "./image-type.js";
|
|
11
11
|
import { optimize } from "./util/optimize.js";
|
|
12
12
|
import { resolveMetadata, pngText, exifData, gifComments } from "./metadata.js";
|
|
@@ -17,7 +17,7 @@ const toPlays = (loopCount) => loopCount < 0 ? 0 : loopCount + 1;
|
|
|
17
17
|
const fromPlays = (plays) => plays === 0 ? -1 : plays - 1;
|
|
18
18
|
function canvasFrames(width, height, frames) {
|
|
19
19
|
return frames.map(({ pixels, duration }) => {
|
|
20
|
-
const frame = new Frame(width, height, duration, 0, 0,
|
|
20
|
+
const frame = new Frame(width, height, duration, 0, 0, FrameDisposalModes.BACKGROUND);
|
|
21
21
|
frame.bitmap = pixels;
|
|
22
22
|
return frame;
|
|
23
23
|
});
|
|
@@ -79,11 +79,11 @@ export class Animation extends Array {
|
|
|
79
79
|
}
|
|
80
80
|
resize(width, height, modeOrOptions) {
|
|
81
81
|
const originalWidth = this.width, originalHeight = this.height;
|
|
82
|
-
if (width ===
|
|
82
|
+
if (width === ResizeModes.AUTO && height === ResizeModes.AUTO)
|
|
83
83
|
throw new Error('RESIZE_AUTO can only be used for either width or height, not for both');
|
|
84
|
-
if (width ===
|
|
84
|
+
if (width === ResizeModes.AUTO)
|
|
85
85
|
width = originalWidth / originalHeight * height;
|
|
86
|
-
if (height ===
|
|
86
|
+
if (height === ResizeModes.AUTO)
|
|
87
87
|
height = originalHeight / originalWidth * width;
|
|
88
88
|
const sx = width / originalWidth, sy = height / originalHeight;
|
|
89
89
|
for (const frame of this) {
|
|
@@ -225,7 +225,7 @@ export class Animation extends Array {
|
|
|
225
225
|
const frames = [];
|
|
226
226
|
let loopCount = -1, icc, space;
|
|
227
227
|
for await (const decoded of jxl.frames(data, maxPixels, 'reinhard', threads)) {
|
|
228
|
-
const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0,
|
|
228
|
+
const frame = new Frame(decoded.width, decoded.height, decoded.duration, 0, 0, FrameDisposalModes.BACKGROUND);
|
|
229
229
|
frame.bitmap = decoded.pixels;
|
|
230
230
|
frames.push(frame.__orient__(decoded.orientation));
|
|
231
231
|
loopCount = fromPlays(decoded.loopCount);
|
package/dist/colors.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { Color } from './image.js';
|
|
2
|
+
/** Clamps and rounds a channel value to 0..255 */
|
|
3
|
+
export declare function channel(value: number): number;
|
|
4
|
+
export declare function wrapHue(h: number): number;
|
|
5
|
+
export declare function hue2rgb(p: number, q: number, t: number): number;
|
|
6
|
+
/**
|
|
7
|
+
* Converts an RGBA value to an array of HSLA values
|
|
8
|
+
* @param r {number} (0..255)
|
|
9
|
+
* @param g {number} (0..255)
|
|
10
|
+
* @param b {number} (0..255)
|
|
11
|
+
* @param a {number} (0..255)
|
|
12
|
+
* @returns The HSLA values ([H, S, L, A])
|
|
13
|
+
*/
|
|
14
|
+
export declare function rgbaToHSLA(r: number, g: number, b: number, a: number): [h: number, s: number, l: number, a: number];
|
|
15
|
+
/**
|
|
16
|
+
* Conversions between RGBA, HSLA and the 32-bit color values ({@link Color}, `0xRRGGBBAA`) that the image methods take
|
|
17
|
+
* and return, and gradients between colors
|
|
18
|
+
*/
|
|
19
|
+
export declare class Colors {
|
|
20
|
+
private constructor();
|
|
21
|
+
/**
|
|
22
|
+
* Converts RGBA components to an RGBA value, values are rounded and clamped to 0..255
|
|
23
|
+
* @param r red (0..255)
|
|
24
|
+
* @param g green (0..255)
|
|
25
|
+
* @param b blue (0..255)
|
|
26
|
+
* @param a alpha (0..255)
|
|
27
|
+
* @returns RGBA value
|
|
28
|
+
*/
|
|
29
|
+
static fromRGBA(r: number, g: number, b: number, a: number): Color;
|
|
30
|
+
/**
|
|
31
|
+
* Converts RGB components to an RGBA value (assuming alpha = 255)
|
|
32
|
+
* @param r red (0..255)
|
|
33
|
+
* @param g green (0..255)
|
|
34
|
+
* @param b blue (0..255)
|
|
35
|
+
* @returns RGBA value
|
|
36
|
+
*/
|
|
37
|
+
static fromRGB(r: number, g: number, b: number): Color;
|
|
38
|
+
/**
|
|
39
|
+
* Converts HSLA colors to RGBA colors
|
|
40
|
+
* @param h hue (0..1)
|
|
41
|
+
* @param s saturation (0..1)
|
|
42
|
+
* @param l lightness (0..1)
|
|
43
|
+
* @param a opacity (0..1)
|
|
44
|
+
* @returns color
|
|
45
|
+
*/
|
|
46
|
+
static fromHSLA(h: number, s: number, l: number, a: number): Color;
|
|
47
|
+
/**
|
|
48
|
+
* Converts HSL colors to RGBA colors (assuming an opacity of 255)
|
|
49
|
+
* @param h hue (0..1)
|
|
50
|
+
* @param s saturation (0..1)
|
|
51
|
+
* @param l lightness (0..1)
|
|
52
|
+
* @returns color
|
|
53
|
+
*/
|
|
54
|
+
static fromHSL(h: number, s: number, l: number): Color;
|
|
55
|
+
/**
|
|
56
|
+
* Converts a color value to an array of RGBA values
|
|
57
|
+
* @param color The color value to convert
|
|
58
|
+
* @returns The RGBA values ([R, G, B, A])
|
|
59
|
+
*/
|
|
60
|
+
static toRGBA(color: Color): [r: number, g: number, b: number, a: number];
|
|
61
|
+
/**
|
|
62
|
+
* Converts a color value to an array of RGB values (ignoring the colors alpha)
|
|
63
|
+
* @param color The color value to convert
|
|
64
|
+
* @returns The RGB values ([R, G, B])
|
|
65
|
+
*/
|
|
66
|
+
static toRGB(color: Color): [r: number, g: number, b: number];
|
|
67
|
+
/**
|
|
68
|
+
* Converts a color to an array of HSLA values
|
|
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
|
|
71
|
+
*/
|
|
72
|
+
static toHSLA(color: Color): [h: number, s: number, l: number, a: number];
|
|
73
|
+
/**
|
|
74
|
+
* Creates a multi-point gradient generator
|
|
75
|
+
* @param colors The gradient points to use (e.g. `{0: 0xff0000ff, 1: 0x00ff00ff}`)
|
|
76
|
+
* @return The gradient generator. The function argument is the position in the gradient (0..1).
|
|
77
|
+
*/
|
|
78
|
+
static gradient(colors: Record<number, Color>): (position: number) => Color;
|
|
79
|
+
}
|
package/dist/colors.js
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { finite, color as checkColor } from "./util/options.js";
|
|
2
|
+
export function channel(value) {
|
|
3
|
+
return value > 255 ? 255 : value > 0 ? Math.round(value) : 0;
|
|
4
|
+
}
|
|
5
|
+
export function wrapHue(h) {
|
|
6
|
+
const t = h - Math.trunc(h) + 1;
|
|
7
|
+
return t < 1 ? t : t < 2 ? t - 1 : 0;
|
|
8
|
+
}
|
|
9
|
+
export function hue2rgb(p, q, t) {
|
|
10
|
+
if (t < 0)
|
|
11
|
+
t += 1;
|
|
12
|
+
if (t > 1)
|
|
13
|
+
t -= 1;
|
|
14
|
+
if (t < 1 / 6)
|
|
15
|
+
return p + (q - p) * 6 * t;
|
|
16
|
+
if (t < 1 / 2)
|
|
17
|
+
return q;
|
|
18
|
+
if (t < 2 / 3)
|
|
19
|
+
return p + (q - p) * (2 / 3 - t) * 6;
|
|
20
|
+
return p;
|
|
21
|
+
}
|
|
22
|
+
export function rgbaToHSLA(r, g, b, a) {
|
|
23
|
+
r = finite(r, 'red') / 255;
|
|
24
|
+
g = finite(g, 'green') / 255;
|
|
25
|
+
b = finite(b, 'blue') / 255;
|
|
26
|
+
a = finite(a, 'alpha');
|
|
27
|
+
const max = Math.max(r, g, b), min = Math.min(r, g, b);
|
|
28
|
+
let h = 0, s;
|
|
29
|
+
const l = (max + min) / 2;
|
|
30
|
+
if (max === min) {
|
|
31
|
+
h = s = 0;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
const d = max - min;
|
|
35
|
+
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
36
|
+
switch (max) {
|
|
37
|
+
case r:
|
|
38
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
39
|
+
break;
|
|
40
|
+
case g:
|
|
41
|
+
h = (b - r) / d + 2;
|
|
42
|
+
break;
|
|
43
|
+
case b:
|
|
44
|
+
h = (r - g) / d + 4;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
h /= 6;
|
|
48
|
+
}
|
|
49
|
+
return [h, s, l, a / 255];
|
|
50
|
+
}
|
|
51
|
+
function gradientBetween(startColor, endColor) {
|
|
52
|
+
const sr = startColor >>> 24;
|
|
53
|
+
const sg = startColor >> 16 & 0xff;
|
|
54
|
+
const sb = startColor >> 8 & 0xff;
|
|
55
|
+
const sa = startColor & 0xff;
|
|
56
|
+
const er = (endColor >>> 24) - sr;
|
|
57
|
+
const eg = (endColor >> 16 & 0xff) - sg;
|
|
58
|
+
const eb = (endColor >> 8 & 0xff) - sb;
|
|
59
|
+
const ea = (endColor & 0xff) - sa;
|
|
60
|
+
return position => {
|
|
61
|
+
const r = sr + position * er;
|
|
62
|
+
const g = sg + position * eg;
|
|
63
|
+
const b = sb + position * eb;
|
|
64
|
+
const a = sa + position * ea;
|
|
65
|
+
return (((r & 0xff) << 24) | ((g & 0xff) << 16) | ((b & 0xff) << 8) | (a & 0xff)) >>> 0;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
export class Colors {
|
|
69
|
+
constructor() { }
|
|
70
|
+
static fromRGBA(r, g, b, a) {
|
|
71
|
+
r = finite(r, 'red');
|
|
72
|
+
g = finite(g, 'green');
|
|
73
|
+
b = finite(b, 'blue');
|
|
74
|
+
a = finite(a, 'alpha');
|
|
75
|
+
return (channel(r) << 24 | channel(g) << 16 | channel(b) << 8 | channel(a)) >>> 0;
|
|
76
|
+
}
|
|
77
|
+
static fromRGB(r, g, b) {
|
|
78
|
+
return Colors.fromRGBA(r, g, b, 0xff);
|
|
79
|
+
}
|
|
80
|
+
static fromHSLA(h, s, l, a) {
|
|
81
|
+
h = wrapHue(finite(h, 'hue'));
|
|
82
|
+
s = finite(s, 'saturation');
|
|
83
|
+
l = finite(l, 'lightness');
|
|
84
|
+
a = finite(a, 'alpha');
|
|
85
|
+
s = Math.min(1, Math.max(0, s));
|
|
86
|
+
l = Math.min(1, Math.max(0, l));
|
|
87
|
+
a = Math.min(1, Math.max(0, a));
|
|
88
|
+
let r, g, b;
|
|
89
|
+
if (s === 0) {
|
|
90
|
+
r = g = b = l;
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
94
|
+
const p = 2 * l - q;
|
|
95
|
+
r = hue2rgb(p, q, h + 1 / 3);
|
|
96
|
+
g = hue2rgb(p, q, h);
|
|
97
|
+
b = hue2rgb(p, q, h - 1 / 3);
|
|
98
|
+
}
|
|
99
|
+
return Colors.fromRGBA(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), Math.round(a * 255));
|
|
100
|
+
}
|
|
101
|
+
static fromHSL(h, s, l) {
|
|
102
|
+
return Colors.fromHSLA(h, s, l, 1);
|
|
103
|
+
}
|
|
104
|
+
static toRGBA(color) {
|
|
105
|
+
color = checkColor(color);
|
|
106
|
+
return [(color >> 24) & 0xff, (color >> 16) & 0xff, (color >> 8) & 0xff, color & 0xff];
|
|
107
|
+
}
|
|
108
|
+
static toRGB(color) {
|
|
109
|
+
const [r, g, b] = Colors.toRGBA(color);
|
|
110
|
+
return [r, g, b];
|
|
111
|
+
}
|
|
112
|
+
static toHSLA(color) {
|
|
113
|
+
const [r, g, b, a] = Colors.toRGBA(color);
|
|
114
|
+
return rgbaToHSLA(r, g, b, a);
|
|
115
|
+
}
|
|
116
|
+
static gradient(colors) {
|
|
117
|
+
const entries = Object.entries(colors).sort((a, b) => Number(a[0]) - Number(b[0]));
|
|
118
|
+
const positions = entries.map(e => finite(parseFloat(e[0]), 'gradient position'));
|
|
119
|
+
const values = entries.map(e => checkColor(e[1]));
|
|
120
|
+
if (positions.length === 0)
|
|
121
|
+
throw new RangeError('Invalid gradient point count');
|
|
122
|
+
else if (positions.length === 1) {
|
|
123
|
+
return () => values[0];
|
|
124
|
+
}
|
|
125
|
+
else if (positions.length === 2) {
|
|
126
|
+
const gradient = gradientBetween(values[0], values[1]);
|
|
127
|
+
return position => {
|
|
128
|
+
if (position <= positions[0])
|
|
129
|
+
return values[0];
|
|
130
|
+
if (position >= positions[1])
|
|
131
|
+
return values[1];
|
|
132
|
+
return gradient((position - positions[0]) / (positions[1] - positions[0]));
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
const minDef = Math.min(...positions);
|
|
136
|
+
const maxDef = Math.max(...positions);
|
|
137
|
+
const gradients = [];
|
|
138
|
+
for (let i = 0; i < positions.length; i++) {
|
|
139
|
+
const minPos = positions[i - 1];
|
|
140
|
+
if (minPos === undefined)
|
|
141
|
+
continue;
|
|
142
|
+
const maxPos = positions[i];
|
|
143
|
+
let minVal = values[i - 1];
|
|
144
|
+
if (minVal === undefined)
|
|
145
|
+
minVal = values[i];
|
|
146
|
+
const maxVal = values[i];
|
|
147
|
+
const gradient = gradientBetween(minVal, maxVal);
|
|
148
|
+
gradients.push({ min: minPos, max: maxPos, gradient });
|
|
149
|
+
}
|
|
150
|
+
return position => {
|
|
151
|
+
if (position <= minDef)
|
|
152
|
+
return gradients[0].gradient(0);
|
|
153
|
+
if (position >= maxDef)
|
|
154
|
+
return gradients[gradients.length - 1].gradient(1);
|
|
155
|
+
for (const gradient of gradients)
|
|
156
|
+
if (position >= gradient.min && position <= gradient.max)
|
|
157
|
+
return gradient.gradient((position - gradient.min) / (gradient.max - gradient.min));
|
|
158
|
+
throw new RangeError(`Invalid gradient position: ${position}`);
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
package/dist/frame.d.ts
CHANGED
|
@@ -3,6 +3,18 @@ import { Image, ResizeMode, ResizeOptions, Color, TrimOptions } from './image.js
|
|
|
3
3
|
export type FrameDisposalModeName = 'any' | 'keep' | 'previous' | 'background';
|
|
4
4
|
/** Ids of the frame disposal modes, as GIFs store them (0 any, 1 keep, 2 previous, 3 background) */
|
|
5
5
|
export type FrameDisposalModeId = 0 | 1 | 2 | 3;
|
|
6
|
+
/** What happens to a frame's area before the next frame is drawn, see {@link Frame.disposalMode} */
|
|
7
|
+
export declare class FrameDisposalModes {
|
|
8
|
+
/** The decoder decides (in practice like {@link FrameDisposalModes.KEEP}) */
|
|
9
|
+
static readonly ANY = "any";
|
|
10
|
+
/** The frame stays, the next one is drawn over it (the default) */
|
|
11
|
+
static readonly KEEP = "keep";
|
|
12
|
+
/** The frame's area goes back to what it was before the frame */
|
|
13
|
+
static readonly PREVIOUS = "previous";
|
|
14
|
+
/** The frame's area is cleared to transparent */
|
|
15
|
+
static readonly BACKGROUND = "background";
|
|
16
|
+
private constructor();
|
|
17
|
+
}
|
|
6
18
|
/** Options of {@link Frame} and {@link Frame.from} */
|
|
7
19
|
export interface FrameOptions {
|
|
8
20
|
/** The frame's duration in milliseconds. Default: `100` */
|
|
@@ -15,7 +27,8 @@ export interface FrameOptions {
|
|
|
15
27
|
disposalMode?: FrameDisposalModeName | FrameDisposalModeId;
|
|
16
28
|
}
|
|
17
29
|
/**
|
|
18
|
-
* Represents a frame of an {@link Animation}
|
|
30
|
+
* Represents a frame of an {@link Animation}: an {@link Image} with a duration, offsets and a disposal mode, so every
|
|
31
|
+
* method of {@link Image} works on it too
|
|
19
32
|
*/
|
|
20
33
|
export declare class Frame extends Image {
|
|
21
34
|
/**
|
|
@@ -27,21 +40,37 @@ export declare class Frame extends Image {
|
|
|
27
40
|
xOffset: number;
|
|
28
41
|
/** The frame's vertical offset in the animation */
|
|
29
42
|
yOffset: number;
|
|
30
|
-
/**
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
/**
|
|
44
|
+
* @deprecated Use {@link FrameDisposalModes.ANY}, `Frame.DISPOSAL_*` will be removed in 4.0
|
|
45
|
+
* @group Deprecated
|
|
46
|
+
*/
|
|
47
|
+
static readonly DISPOSAL_ANY: typeof FrameDisposalModes.ANY;
|
|
48
|
+
/**
|
|
49
|
+
* @deprecated Use {@link FrameDisposalModes.KEEP}, `Frame.DISPOSAL_*` will be removed in 4.0
|
|
50
|
+
* @group Deprecated
|
|
51
|
+
*/
|
|
52
|
+
static readonly DISPOSAL_KEEP: typeof FrameDisposalModes.KEEP;
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use {@link FrameDisposalModes.PREVIOUS}, `Frame.DISPOSAL_*` will be removed in 4.0
|
|
55
|
+
* @group Deprecated
|
|
56
|
+
*/
|
|
57
|
+
static readonly DISPOSAL_PREVIOUS: typeof FrameDisposalModes.PREVIOUS;
|
|
58
|
+
/**
|
|
59
|
+
* @deprecated Use {@link FrameDisposalModes.BACKGROUND}, `Frame.DISPOSAL_*` will be removed in 4.0
|
|
60
|
+
* @group Deprecated
|
|
61
|
+
*/
|
|
62
|
+
static readonly DISPOSAL_BACKGROUND: typeof FrameDisposalModes.BACKGROUND;
|
|
38
63
|
/**
|
|
39
64
|
* Creates a new, blank frame
|
|
65
|
+
* @param width The width in pixels
|
|
66
|
+
* @param height The height in pixels
|
|
40
67
|
* @param options See {@link FrameOptions}
|
|
41
68
|
*/
|
|
42
69
|
constructor(width: number, height: number, options?: FrameOptions);
|
|
43
70
|
/**
|
|
44
71
|
* Creates a new, blank frame
|
|
72
|
+
* @param width The width in pixels
|
|
73
|
+
* @param height The height in pixels
|
|
45
74
|
* @param duration The frame's duration (in ms)
|
|
46
75
|
* @param xOffset The frame's offset on the x-axis
|
|
47
76
|
* @param yOffset The frame's offset on the y-axis
|
|
@@ -50,7 +79,10 @@ export declare class Frame extends Image {
|
|
|
50
79
|
constructor(width: number, height: number, duration?: number, xOffset?: number, yOffset?: number, disposalMode?: FrameDisposalModeName | FrameDisposalModeId);
|
|
51
80
|
/** The frame's disposal mode (`'any'`, `'keep'`, `'previous'` or `'background'`); also settable by id (0-3) */
|
|
52
81
|
get disposalMode(): FrameDisposalModeName;
|
|
53
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* Sets the disposal mode
|
|
84
|
+
* @param disposalMode The mode's name (`'any'`, `'keep'`, `'previous'`, `'background'`) or id (0-3)
|
|
85
|
+
*/
|
|
54
86
|
set disposalMode(disposalMode: FrameDisposalModeName | FrameDisposalModeId);
|
|
55
87
|
/** A short description for debugging, e.g. `Frame<64x64x100ms>` */
|
|
56
88
|
toString(): string;
|
|
@@ -80,7 +112,11 @@ export declare class Frame extends Image {
|
|
|
80
112
|
*/
|
|
81
113
|
trim(color?: Color, options?: TrimOptions): this;
|
|
82
114
|
/**
|
|
83
|
-
* Resizes the frame, scaling its offsets along with it
|
|
115
|
+
* Resizes the frame, scaling its offsets along with it.
|
|
116
|
+
* Use {@link ResizeModes.AUTO} as either width or height to automatically preserve the aspect ratio.
|
|
117
|
+
* @param width The new width
|
|
118
|
+
* @param height The new height
|
|
119
|
+
* @param modeOrOptions The resizing mode (default: {@link ResizeModes.BILINEAR}) or {@link ResizeOptions}
|
|
84
120
|
*/
|
|
85
121
|
resize(width: number, height: number, modeOrOptions?: ResizeMode | ResizeOptions): this;
|
|
86
122
|
}
|
package/dist/frame.js
CHANGED
|
@@ -1,23 +1,22 @@
|
|
|
1
1
|
import { Image } from "./image.js";
|
|
2
2
|
import { isOptions, finite } from "./util/options.js";
|
|
3
3
|
const DISPOSAL_MODES = ['any', 'keep', 'previous', 'background'];
|
|
4
|
+
export class FrameDisposalModes {
|
|
5
|
+
static ANY = 'any';
|
|
6
|
+
static KEEP = 'keep';
|
|
7
|
+
static PREVIOUS = 'previous';
|
|
8
|
+
static BACKGROUND = 'background';
|
|
9
|
+
constructor() { }
|
|
10
|
+
}
|
|
4
11
|
export class Frame extends Image {
|
|
5
12
|
duration;
|
|
6
13
|
xOffset;
|
|
7
14
|
yOffset;
|
|
8
15
|
__disposalMode__ = 1;
|
|
9
|
-
static
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
static
|
|
13
|
-
return 'keep';
|
|
14
|
-
}
|
|
15
|
-
static get DISPOSAL_PREVIOUS() {
|
|
16
|
-
return 'previous';
|
|
17
|
-
}
|
|
18
|
-
static get DISPOSAL_BACKGROUND() {
|
|
19
|
-
return 'background';
|
|
20
|
-
}
|
|
16
|
+
static DISPOSAL_ANY = FrameDisposalModes.ANY;
|
|
17
|
+
static DISPOSAL_KEEP = FrameDisposalModes.KEEP;
|
|
18
|
+
static DISPOSAL_PREVIOUS = FrameDisposalModes.PREVIOUS;
|
|
19
|
+
static DISPOSAL_BACKGROUND = FrameDisposalModes.BACKGROUND;
|
|
21
20
|
static __convert_disposal_mode__(mode) {
|
|
22
21
|
const id = typeof mode === 'string' ? DISPOSAL_MODES.indexOf(mode) : mode;
|
|
23
22
|
if (!(id >= 0 && id <= 3))
|
|
@@ -25,7 +24,7 @@ export class Frame extends Image {
|
|
|
25
24
|
return id;
|
|
26
25
|
}
|
|
27
26
|
constructor(width, height, durationOrOptions, xOffset, yOffset, disposalMode) {
|
|
28
|
-
const { duration = 100, xOffset: x = 0, yOffset: y = 0, disposalMode: disposal =
|
|
27
|
+
const { duration = 100, xOffset: x = 0, yOffset: y = 0, disposalMode: disposal = FrameDisposalModes.KEEP } = isOptions(durationOrOptions) ? durationOrOptions : { duration: durationOrOptions, xOffset, yOffset, disposalMode };
|
|
29
28
|
if (finite(duration, 'frame duration') < 0)
|
|
30
29
|
throw new RangeError('Invalid frame duration');
|
|
31
30
|
finite(x, 'x offset');
|
package/dist/gainmap.d.ts
CHANGED
|
@@ -58,6 +58,10 @@ export declare class GainMap {
|
|
|
58
58
|
private __check__;
|
|
59
59
|
/** The map's pixels. Changes to them are written when the image is encoded */
|
|
60
60
|
get image(): Image;
|
|
61
|
+
/**
|
|
62
|
+
* Replaces the map's pixels
|
|
63
|
+
* @param image The new map, at any resolution: it covers the whole image
|
|
64
|
+
*/
|
|
61
65
|
set image(image: Image);
|
|
62
66
|
/** The map's width */
|
|
63
67
|
get width(): number;
|
package/dist/gif.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ import { Animation, AnimationDecodeOptions, AnimationOptions, GIFEncodeOptions }
|
|
|
6
6
|
* Array methods like map and filter return plain arrays.
|
|
7
7
|
* @deprecated Use {@link Animation}, which works the same and also handles APNG: `new Animation(frames, loopCount)`,
|
|
8
8
|
* {@link Animation.decode} and {@link Animation.encodeGIF}. `GIF` will be removed in 4.0
|
|
9
|
+
* @group Deprecated
|
|
9
10
|
*/
|
|
10
11
|
export declare class GIF extends Animation {
|
|
11
12
|
/**
|
package/dist/image-type.d.ts
CHANGED
|
@@ -10,30 +10,69 @@ export declare class ImageType {
|
|
|
10
10
|
* @returns The image type, or null if it isn't supported
|
|
11
11
|
*/
|
|
12
12
|
static getType(data: BinaryData): ImageTypeName | null;
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* Whether the data starts like a PNG
|
|
15
|
+
* @param data The file's data
|
|
16
|
+
*/
|
|
14
17
|
static isPNG(data: BinaryData): boolean;
|
|
15
|
-
/**
|
|
18
|
+
/**
|
|
19
|
+
* Whether the data starts like a JPEG
|
|
20
|
+
* @param data The file's data
|
|
21
|
+
*/
|
|
16
22
|
static isJPEG(data: BinaryData): boolean;
|
|
17
|
-
/**
|
|
23
|
+
/**
|
|
24
|
+
* Whether the data starts like a TIFF (or BigTIFF)
|
|
25
|
+
* @param data The file's data
|
|
26
|
+
*/
|
|
18
27
|
static isTIFF(data: BinaryData): boolean;
|
|
19
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Whether the data starts like a GIF
|
|
30
|
+
* @param data The file's data
|
|
31
|
+
*/
|
|
20
32
|
static isGIF(data: BinaryData): boolean;
|
|
21
|
-
/**
|
|
33
|
+
/**
|
|
34
|
+
* Whether the data starts like a WebP
|
|
35
|
+
* @param data The file's data
|
|
36
|
+
*/
|
|
22
37
|
static isWebP(data: BinaryData): boolean;
|
|
23
|
-
/**
|
|
38
|
+
/**
|
|
39
|
+
* Whether the data starts like an AVIF (the avif or avis brand)
|
|
40
|
+
* @param data The file's data
|
|
41
|
+
*/
|
|
24
42
|
static isAVIF(data: BinaryData): boolean;
|
|
25
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Whether the data starts like a JPEG XL (a codestream or the container)
|
|
45
|
+
* @param data The file's data
|
|
46
|
+
*/
|
|
26
47
|
static isJXL(data: BinaryData): boolean;
|
|
27
|
-
/**
|
|
48
|
+
/**
|
|
49
|
+
* Whether the data starts like a BMP
|
|
50
|
+
* @param data The file's data
|
|
51
|
+
*/
|
|
28
52
|
static isBMP(data: BinaryData): boolean;
|
|
29
|
-
/**
|
|
53
|
+
/**
|
|
54
|
+
* Whether the data is a Windows icon or cursor (ICO or CUR)
|
|
55
|
+
* @param data The file's data
|
|
56
|
+
*/
|
|
30
57
|
static isICO(data: BinaryData): boolean;
|
|
31
|
-
/**
|
|
58
|
+
/**
|
|
59
|
+
* Whether the data starts like a QOI
|
|
60
|
+
* @param data The file's data
|
|
61
|
+
*/
|
|
32
62
|
static isQOI(data: BinaryData): boolean;
|
|
33
|
-
/**
|
|
63
|
+
/**
|
|
64
|
+
* Whether the data starts like a Netpbm image (PBM, PGM, PPM, PAM or PFM)
|
|
65
|
+
* @param data The file's data
|
|
66
|
+
*/
|
|
34
67
|
static isPNM(data: BinaryData): boolean;
|
|
35
|
-
/**
|
|
68
|
+
/**
|
|
69
|
+
* Whether the data starts like a Radiance HDR
|
|
70
|
+
* @param data The file's data
|
|
71
|
+
*/
|
|
36
72
|
static isHDR(data: BinaryData): boolean;
|
|
37
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Whether the data looks like a TGA (which has no magic number: its header has to be consistent)
|
|
75
|
+
* @param data The file's data
|
|
76
|
+
*/
|
|
38
77
|
static isTGA(data: BinaryData): boolean;
|
|
39
78
|
}
|