@beff/aula 0.0.0-stage → 0.1.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 +8 -2
- package/dist/animations.d.ts +634 -0
- package/dist/animations.js +1793 -0
- package/dist/braces.d.ts +84 -0
- package/dist/braces.js +253 -0
- package/dist/card.d.ts +17 -0
- package/dist/card.js +90 -0
- package/dist/colors-palette.d.ts +2242 -0
- package/dist/colors-palette.js +2241 -0
- package/dist/colors.d.ts +120 -0
- package/dist/colors.js +403 -0
- package/dist/curves.d.ts +10 -0
- package/dist/curves.js +128 -0
- package/dist/engine.d.ts +1595 -0
- package/dist/engine.js +3364 -0
- package/dist/errors.d.ts +19 -0
- package/dist/errors.js +25 -0
- package/dist/generated/aula.conditional-suspending-3-i32-to-void.wasm +0 -0
- package/dist/generated/aula.core.wasm +0 -0
- package/dist/generated/aula.core2.wasm +0 -0
- package/dist/generated/aula.core3.wasm +0 -0
- package/dist/generated/aula.core4.wasm +0 -0
- package/dist/generated/aula.core5.wasm +0 -0
- package/dist/generated/aula.core6.wasm +0 -0
- package/dist/generated/aula.core7.wasm +0 -0
- package/dist/generated/aula.core8.wasm +0 -0
- package/dist/generated/aula.d.ts +80 -0
- package/dist/generated/aula.js +54639 -0
- package/dist/generated/interfaces/aula-engine-engine.d.ts +983 -0
- package/dist/generated/interfaces/aula-engine-host.d.ts +10 -0
- package/dist/generated/interfaces/aula-engine-tts.d.ts +43 -0
- package/dist/generated/interfaces/aula-engine-types.d.ts +60 -0
- package/dist/geometry.d.ts +551 -0
- package/dist/geometry.js +1821 -0
- package/dist/graph.d.ts +117 -0
- package/dist/graph.js +526 -0
- package/dist/graphing.d.ts +597 -0
- package/dist/graphing.js +1504 -0
- package/dist/host.d.ts +19 -0
- package/dist/host.js +85 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +11 -0
- package/dist/licenses/aula-g2p-NOTICE.txt +13 -0
- package/dist/licenses/espeak-ng-COPYING.txt +674 -0
- package/dist/licenses/fonts-OFL-JetBrainsMono.txt +93 -0
- package/dist/licenses/fonts-OFL-NunitoSans.txt +93 -0
- package/dist/load.d.ts +16 -0
- package/dist/load.js +69 -0
- package/dist/logo.d.ts +30 -0
- package/dist/logo.js +186 -0
- package/dist/manim.d.ts +22 -0
- package/dist/manim.js +19 -0
- package/dist/mobjects.d.ts +127 -0
- package/dist/mobjects.js +316 -0
- package/dist/pointcloud.d.ts +144 -0
- package/dist/pointcloud.js +400 -0
- package/dist/rates.d.ts +19 -0
- package/dist/rates.js +54 -0
- package/dist/run.d.ts +82 -0
- package/dist/run.js +245 -0
- package/dist/tables.d.ts +179 -0
- package/dist/tables.js +403 -0
- package/dist/tex.d.ts +8 -0
- package/dist/tex.js +257 -0
- package/dist/text-layouts.d.ts +64 -0
- package/dist/text-layouts.js +225 -0
- package/dist/text.d.ts +35 -0
- package/dist/text.js +52 -0
- package/dist/theme.d.ts +25 -0
- package/dist/theme.js +12 -0
- package/dist/user-code.d.ts +5 -0
- package/dist/user-code.js +20 -0
- package/dist/utils.d.ts +152 -0
- package/dist/utils.js +517 -0
- package/dist/vector-field.d.ts +132 -0
- package/dist/vector-field.js +361 -0
- package/package.json +45 -3
package/dist/colors.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { type Color } from "./engine.ts";
|
|
2
|
+
/** Red, green, blue and alpha from 0 to 1. */
|
|
3
|
+
type Rgba = readonly [number, number, number, number];
|
|
4
|
+
/** What `ManimColor` parses (Manim `ParsableManimColor`): `"#RRGGBB"`, `"#RGB"`, `"#RRGGBBAA"`,
|
|
5
|
+
* `"0xRRGGBB"`, a palette name such as `"blue_c"`, an integer `0xRRGGBB`, `[r, g, b]` or
|
|
6
|
+
* `[r, g, b, a]` (from 0 to 1 when no number is above 1, else from 0 to 255), or a ManimColor. */
|
|
7
|
+
export type ParsableManimColor = string | number | readonly number[] | ManimColorObject;
|
|
8
|
+
/** A colour with an alpha: Manim's `ManimColor`. `toHex()` gives the `"#RRGGBB"` string that
|
|
9
|
+
* shapes take; `String(color)` gives the same. */
|
|
10
|
+
export declare class ManimColorObject {
|
|
11
|
+
#private;
|
|
12
|
+
constructor(rgba: Rgba);
|
|
13
|
+
/** A new colour of the class of this one, from red, green, blue and alpha. */
|
|
14
|
+
protected make(rgba: Rgba): this;
|
|
15
|
+
toInteger(): number;
|
|
16
|
+
toRgb(): [number, number, number];
|
|
17
|
+
toIntRgb(): [number, number, number];
|
|
18
|
+
toRgba(): [number, number, number, number];
|
|
19
|
+
toIntRgba(): [number, number, number, number];
|
|
20
|
+
toRgbaWithAlpha(alpha: number): [number, number, number, number];
|
|
21
|
+
toIntRgbaWithAlpha(alpha: number): [number, number, number, number];
|
|
22
|
+
/** `"#RRGGBB"`, or `"#RRGGBBAA"` with `withAlpha`. */
|
|
23
|
+
toHex(withAlpha?: boolean): Color;
|
|
24
|
+
toHsv(): [number, number, number];
|
|
25
|
+
toHsl(): [number, number, number];
|
|
26
|
+
/** `1 - r`, `1 - g`, `1 - b` (and `1 - alpha` with `withAlpha`). */
|
|
27
|
+
invert(withAlpha?: boolean): this;
|
|
28
|
+
interpolate(other: ManimColorObject, alpha: number): this;
|
|
29
|
+
/** Mixes `blend` of black in, and keeps the alpha. */
|
|
30
|
+
darker(blend?: number): this;
|
|
31
|
+
/** Mixes `blend` of white in, and keeps the alpha. */
|
|
32
|
+
lighter(blend?: number): this;
|
|
33
|
+
/** `light` (white) when the luminance (YIQ) is below `threshold`, else `dark` (black). */
|
|
34
|
+
contrasting(threshold?: number, light?: ManimColorObject, dark?: ManimColorObject): ManimColorObject;
|
|
35
|
+
/** The colour with this alpha. */
|
|
36
|
+
opacity(opacity: number): this;
|
|
37
|
+
/** The colour as another class: `color.into(m.HSV)`. */
|
|
38
|
+
into<T extends ManimColorObject>(classType: (value: ParsableManimColor) => T): T;
|
|
39
|
+
fromRgb(rgb: readonly number[], alpha?: number): ManimColorObject;
|
|
40
|
+
fromRgba(rgba: readonly number[]): ManimColorObject;
|
|
41
|
+
fromHex(hexStr: string, alpha?: number): ManimColorObject;
|
|
42
|
+
fromHsv(hsv: readonly [number, number, number], alpha?: number): ManimColorObject;
|
|
43
|
+
fromHsl(hsl: readonly [number, number, number], alpha?: number): ManimColorObject;
|
|
44
|
+
parse(color: ParsableManimColor, alpha?: number): ManimColorObject;
|
|
45
|
+
/** True when the red, green, blue and alpha are all close (Manim `==`). */
|
|
46
|
+
equals(other: ManimColorObject): boolean;
|
|
47
|
+
toString(): string;
|
|
48
|
+
}
|
|
49
|
+
/** A colour with an alpha (Manim `ManimColor(value, alpha)`). */
|
|
50
|
+
export declare function ManimColor(value?: ParsableManimColor, alpha?: number): ManimColorObject;
|
|
51
|
+
export declare namespace ManimColor {
|
|
52
|
+
var fromRgb: (rgb: readonly number[], alpha?: number) => ManimColorObject;
|
|
53
|
+
var fromRgba: (rgba: readonly number[]) => ManimColorObject;
|
|
54
|
+
var fromHex: (hexStr: string, alpha?: number) => ManimColorObject;
|
|
55
|
+
var fromHsv: (hsv: readonly [number, number, number], alpha?: number) => ManimColorObject;
|
|
56
|
+
var fromHsl: (hsl: readonly [number, number, number], alpha?: number) => ManimColorObject;
|
|
57
|
+
var parse: (color: ParsableManimColor, alpha?: number) => ManimColorObject;
|
|
58
|
+
}
|
|
59
|
+
/** A ManimColor as a type. */
|
|
60
|
+
export type ManimColor = ManimColorObject;
|
|
61
|
+
/** Manim's `RGBA`: another name of `ManimColor`. */
|
|
62
|
+
export declare const RGBA: typeof ManimColor;
|
|
63
|
+
export type RGBA = ManimColorObject;
|
|
64
|
+
/** A ManimColor that keeps hue, saturation and value (Manim `HSV`). */
|
|
65
|
+
export declare class HSVObject extends ManimColorObject {
|
|
66
|
+
#private;
|
|
67
|
+
constructor(rgba: Rgba);
|
|
68
|
+
toRgba(): [number, number, number, number];
|
|
69
|
+
get hue(): number;
|
|
70
|
+
set hue(hue: number);
|
|
71
|
+
get saturation(): number;
|
|
72
|
+
set saturation(saturation: number);
|
|
73
|
+
get value(): number;
|
|
74
|
+
set value(value: number);
|
|
75
|
+
get h(): number;
|
|
76
|
+
set h(hue: number);
|
|
77
|
+
get s(): number;
|
|
78
|
+
set s(saturation: number);
|
|
79
|
+
get v(): number;
|
|
80
|
+
set v(value: number);
|
|
81
|
+
}
|
|
82
|
+
/** A colour from hue, saturation and value, each from 0 to 1 (Manim `HSV`). A ManimColor or
|
|
83
|
+
* another parsable colour becomes HSV too: `HSV(m.BLUE)`. */
|
|
84
|
+
export declare function HSV(hsv: readonly number[] | ParsableManimColor, alpha?: number): HSVObject;
|
|
85
|
+
export type HSV = HSVObject;
|
|
86
|
+
/** `[r, g, b]` from 0 to 1. */
|
|
87
|
+
export declare function colorToRgb(color: ParsableManimColor): [number, number, number];
|
|
88
|
+
export declare function colorToRgba(color: ParsableManimColor, alpha?: number): [number, number, number, number];
|
|
89
|
+
/** `[r, g, b]` from 0 to 255. */
|
|
90
|
+
export declare function colorToIntRgb(color: ParsableManimColor): [number, number, number];
|
|
91
|
+
export declare function colorToIntRgba(color: ParsableManimColor, alpha?: number): [number, number, number, number];
|
|
92
|
+
/** The `"#RRGGBB"` colour of `[r, g, b]` (from 0 to 1, or from 0 to 255 when a number is above 1). */
|
|
93
|
+
export declare function rgbToColor(rgb: readonly number[]): Color;
|
|
94
|
+
export declare function rgbaToColor(rgba: readonly number[]): Color;
|
|
95
|
+
export declare function rgbToHex(rgb: readonly number[]): Color;
|
|
96
|
+
export declare function hexToRgb(hexCode: string): [number, number, number];
|
|
97
|
+
/** `1 - r`, `1 - g`, `1 - b`, as `"#RRGGBB"`. */
|
|
98
|
+
export declare function invertColor(color: ParsableManimColor): Color;
|
|
99
|
+
/** `lengthOfOutput` colours evenly between the reference colours, as `"#RRGGBB"`. */
|
|
100
|
+
export declare function colorGradient(referenceColors: readonly ParsableManimColor[], lengthOfOutput: number): Color[];
|
|
101
|
+
/** The colour `alpha` of the way from `color1` to `color2`, as `"#RRGGBB"`. */
|
|
102
|
+
export declare function interpolateColor(color1: ParsableManimColor, color2: ParsableManimColor, alpha: number): Color;
|
|
103
|
+
/** The mean of the red, green and blue of the colours, as `"#RRGGBB"`. */
|
|
104
|
+
export declare function averageColor(...colors: ParsableManimColor[]): Color;
|
|
105
|
+
/** Picks colours from a list (Manim `RandomColorGenerator`). The same seed gives the same
|
|
106
|
+
* colours; with no seed, the seed is 0, so each build of a video has the same colours. */
|
|
107
|
+
export declare class RandomColorGeneratorObject {
|
|
108
|
+
#private;
|
|
109
|
+
constructor(seed: number, colors: readonly Color[]);
|
|
110
|
+
/** The next colour, as `"#RRGGBB"`. */
|
|
111
|
+
next(): Color;
|
|
112
|
+
}
|
|
113
|
+
/** `RandomColorGenerator(seed, sampleColors)`: `sampleColors` is the CE palette by default. */
|
|
114
|
+
export declare function RandomColorGenerator(seed?: number, sampleColors?: readonly ParsableManimColor[]): RandomColorGeneratorObject;
|
|
115
|
+
export type RandomColorGenerator = RandomColorGeneratorObject;
|
|
116
|
+
/** A colour of the CE palette, as `"#RRGGBB"`, from one generator with seed 0. */
|
|
117
|
+
export declare function randomColor(): Color;
|
|
118
|
+
/** `randomColor()` mixed half with white. */
|
|
119
|
+
export declare function randomBrightColor(): Color;
|
|
120
|
+
export {};
|
package/dist/colors.js
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// Manim CE's colour functions (manim/utils/color/core.py): `ManimColor`, `HSV` and the
|
|
2
|
+
// conversions. A colour that a shape takes is a `"#RRGGBB"` string; a ManimColor gives one with
|
|
3
|
+
// `toHex()`. The conversions that CE gives as a ManimColor give the string here, so that they go
|
|
4
|
+
// straight into a `color` option.
|
|
5
|
+
import * as palette from "./colors-palette.js";
|
|
6
|
+
import { BLACK, PURE_YELLOW, WHITE, YELLOW } from "./engine.js";
|
|
7
|
+
import { fault } from "./errors.js";
|
|
8
|
+
const HEX = /(?<=#|0x)[A-F0-9]{3,8}/i;
|
|
9
|
+
/** The palette names of `ManimColor("name")`: the CE palette, upper case. */
|
|
10
|
+
const names = new Map([
|
|
11
|
+
...Object.entries(palette).flatMap(([k, v]) => (typeof v === "string" ? [[k, v]] : [])),
|
|
12
|
+
["WHITE", WHITE],
|
|
13
|
+
["BLACK", BLACK],
|
|
14
|
+
["YELLOW", YELLOW],
|
|
15
|
+
["PURE_YELLOW", PURE_YELLOW],
|
|
16
|
+
]);
|
|
17
|
+
function fromHex(hex, alpha) {
|
|
18
|
+
let h = hex.length === 3 || hex.length === 4 ? hex.replace(/./g, "$&$&") : hex;
|
|
19
|
+
let a = alpha;
|
|
20
|
+
if (h.length === 6)
|
|
21
|
+
h += "FF";
|
|
22
|
+
else if (h.length === 8)
|
|
23
|
+
a = (Number.parseInt(h, 16) & 255) / 255;
|
|
24
|
+
else
|
|
25
|
+
return fault(`ManimColor: ${JSON.stringify(hex)} is not a hex colour`, "give 6 or 8 hex digits after # or 0x");
|
|
26
|
+
const v = Number.parseInt(h, 16);
|
|
27
|
+
return [((v >>> 24) & 255) / 255, ((v >>> 16) & 255) / 255, ((v >>> 8) & 255) / 255, a];
|
|
28
|
+
}
|
|
29
|
+
function parse(value, alpha) {
|
|
30
|
+
if (value === undefined)
|
|
31
|
+
return [0, 0, 0, alpha];
|
|
32
|
+
if (value instanceof ManimColorObject)
|
|
33
|
+
return value.toRgba();
|
|
34
|
+
if (typeof value === "number")
|
|
35
|
+
return [((value >> 16) & 255) / 255, ((value >> 8) & 255) / 255, (value & 255) / 255, alpha];
|
|
36
|
+
if (typeof value === "string") {
|
|
37
|
+
const hex = HEX.exec(value)?.[0];
|
|
38
|
+
if (hex !== undefined)
|
|
39
|
+
return fromHex(hex, alpha);
|
|
40
|
+
const named = names.get(value.toUpperCase());
|
|
41
|
+
if (named === undefined)
|
|
42
|
+
return fault(`ManimColor: no colour is named ${JSON.stringify(value)}`);
|
|
43
|
+
return fromHex(named.slice(1), alpha);
|
|
44
|
+
}
|
|
45
|
+
if (value.length !== 3 && value.length !== 4) {
|
|
46
|
+
return fault(`ManimColor: a list of numbers must have 3 or 4 numbers; got ${String(value.length)}`);
|
|
47
|
+
}
|
|
48
|
+
const floats = value.every((x) => x <= 1);
|
|
49
|
+
const scaled = floats ? [...value] : value.map((x) => x / 255);
|
|
50
|
+
return [scaled[0] ?? 0, scaled[1] ?? 0, scaled[2] ?? 0, value.length === 4 ? (scaled[3] ?? 1) : alpha];
|
|
51
|
+
}
|
|
52
|
+
function hex2(x) {
|
|
53
|
+
return Math.trunc(x * 255)
|
|
54
|
+
.toString(16)
|
|
55
|
+
.toUpperCase()
|
|
56
|
+
.padStart(2, "0");
|
|
57
|
+
}
|
|
58
|
+
/** Python's `colorsys.rgb_to_hsv`. */
|
|
59
|
+
function rgbToHsv([r, g, b]) {
|
|
60
|
+
const max = Math.max(r, g, b);
|
|
61
|
+
const min = Math.min(r, g, b);
|
|
62
|
+
if (max === min)
|
|
63
|
+
return [0, 0, max];
|
|
64
|
+
const d = max - min;
|
|
65
|
+
const rc = (max - r) / d;
|
|
66
|
+
const gc = (max - g) / d;
|
|
67
|
+
const bc = (max - b) / d;
|
|
68
|
+
const h = r === max ? bc - gc : g === max ? 2 + rc - bc : 4 + gc - rc;
|
|
69
|
+
return [(((h / 6) % 1) + 1) % 1, d / max, max];
|
|
70
|
+
}
|
|
71
|
+
/** Python's `colorsys.hsv_to_rgb`. */
|
|
72
|
+
function hsvToRgb([h, s, v]) {
|
|
73
|
+
if (s === 0)
|
|
74
|
+
return [v, v, v];
|
|
75
|
+
const i = Math.floor(h * 6);
|
|
76
|
+
const f = h * 6 - i;
|
|
77
|
+
const p = v * (1 - s);
|
|
78
|
+
const q = v * (1 - s * f);
|
|
79
|
+
const t = v * (1 - s * (1 - f));
|
|
80
|
+
const all = [
|
|
81
|
+
[v, t, p],
|
|
82
|
+
[q, v, p],
|
|
83
|
+
[p, v, t],
|
|
84
|
+
[p, q, v],
|
|
85
|
+
[t, p, v],
|
|
86
|
+
[v, p, q],
|
|
87
|
+
];
|
|
88
|
+
return all[((i % 6) + 6) % 6] ?? [v, t, p];
|
|
89
|
+
}
|
|
90
|
+
/** Python's `colorsys.rgb_to_hls`, as `[h, s, l]` (Manim's HSL order). */
|
|
91
|
+
function rgbToHsl([r, g, b]) {
|
|
92
|
+
const max = Math.max(r, g, b);
|
|
93
|
+
const min = Math.min(r, g, b);
|
|
94
|
+
const l = (min + max) / 2;
|
|
95
|
+
if (min === max)
|
|
96
|
+
return [0, 0, l];
|
|
97
|
+
const s = l <= 0.5 ? (max - min) / (max + min) : (max - min) / (2 - max - min);
|
|
98
|
+
const [h] = rgbToHsv([r, g, b]);
|
|
99
|
+
return [h, s, l];
|
|
100
|
+
}
|
|
101
|
+
/** Python's `colorsys.hls_to_rgb`, from `[h, s, l]`. */
|
|
102
|
+
function hslToRgb([h, s, l]) {
|
|
103
|
+
if (s === 0)
|
|
104
|
+
return [l, l, l];
|
|
105
|
+
const m2 = l <= 0.5 ? l * (1 + s) : l + s - l * s;
|
|
106
|
+
const m1 = 2 * l - m2;
|
|
107
|
+
const v = (hue) => {
|
|
108
|
+
const x = ((hue % 1) + 1) % 1;
|
|
109
|
+
if (x < 1 / 6)
|
|
110
|
+
return m1 + (m2 - m1) * x * 6;
|
|
111
|
+
if (x < 0.5)
|
|
112
|
+
return m2;
|
|
113
|
+
if (x < 2 / 3)
|
|
114
|
+
return m1 + (m2 - m1) * (2 / 3 - x) * 6;
|
|
115
|
+
return m1;
|
|
116
|
+
};
|
|
117
|
+
return [v(h + 1 / 3), v(h), v(h - 1 / 3)];
|
|
118
|
+
}
|
|
119
|
+
/** A colour with an alpha: Manim's `ManimColor`. `toHex()` gives the `"#RRGGBB"` string that
|
|
120
|
+
* shapes take; `String(color)` gives the same. */
|
|
121
|
+
export class ManimColorObject {
|
|
122
|
+
#rgba;
|
|
123
|
+
constructor(rgba) {
|
|
124
|
+
this.#rgba = rgba;
|
|
125
|
+
}
|
|
126
|
+
/** A new colour of the class of this one, from red, green, blue and alpha. */
|
|
127
|
+
make(rgba) {
|
|
128
|
+
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- each subclass takes rgba in its constructor
|
|
129
|
+
return new this.constructor(rgba);
|
|
130
|
+
}
|
|
131
|
+
toInteger() {
|
|
132
|
+
const [r, g, b] = this.toIntRgb();
|
|
133
|
+
return (r << 16) | (g << 8) | b;
|
|
134
|
+
}
|
|
135
|
+
toRgb() {
|
|
136
|
+
const [r, g, b] = this.toRgba();
|
|
137
|
+
return [r, g, b];
|
|
138
|
+
}
|
|
139
|
+
toIntRgb() {
|
|
140
|
+
const [r, g, b] = this.toRgb();
|
|
141
|
+
return [Math.trunc(r * 255), Math.trunc(g * 255), Math.trunc(b * 255)];
|
|
142
|
+
}
|
|
143
|
+
toRgba() {
|
|
144
|
+
const [r, g, b, a] = this.#rgba;
|
|
145
|
+
return [r, g, b, a];
|
|
146
|
+
}
|
|
147
|
+
toIntRgba() {
|
|
148
|
+
const [r, g, b, a] = this.toRgba();
|
|
149
|
+
return [Math.trunc(r * 255), Math.trunc(g * 255), Math.trunc(b * 255), Math.trunc(a * 255)];
|
|
150
|
+
}
|
|
151
|
+
toRgbaWithAlpha(alpha) {
|
|
152
|
+
return [...this.toRgb(), alpha];
|
|
153
|
+
}
|
|
154
|
+
toIntRgbaWithAlpha(alpha) {
|
|
155
|
+
const [r, g, b] = this.toIntRgb();
|
|
156
|
+
return [r, g, b, Math.trunc(alpha * 255)];
|
|
157
|
+
}
|
|
158
|
+
/** `"#RRGGBB"`, or `"#RRGGBBAA"` with `withAlpha`. */
|
|
159
|
+
toHex(withAlpha = false) {
|
|
160
|
+
const [r, g, b, a] = this.toRgba();
|
|
161
|
+
return `#${hex2(r)}${hex2(g)}${hex2(b)}${withAlpha ? hex2(a) : ""}`;
|
|
162
|
+
}
|
|
163
|
+
toHsv() {
|
|
164
|
+
return [...rgbToHsv(this.toRgb())];
|
|
165
|
+
}
|
|
166
|
+
toHsl() {
|
|
167
|
+
return [...rgbToHsl(this.toRgb())];
|
|
168
|
+
}
|
|
169
|
+
/** `1 - r`, `1 - g`, `1 - b` (and `1 - alpha` with `withAlpha`). */
|
|
170
|
+
invert(withAlpha = false) {
|
|
171
|
+
const [r, g, b, a] = this.toRgba();
|
|
172
|
+
return this.make([1 - r, 1 - g, 1 - b, withAlpha ? 1 - a : a]);
|
|
173
|
+
}
|
|
174
|
+
interpolate(other, alpha) {
|
|
175
|
+
const [r, g, b, a] = this.toRgba();
|
|
176
|
+
const [r2, g2, b2, a2] = other.toRgba();
|
|
177
|
+
const mix = (x, y) => x * (1 - alpha) + y * alpha;
|
|
178
|
+
return this.make([mix(r, r2), mix(g, g2), mix(b, b2), mix(a, a2)]);
|
|
179
|
+
}
|
|
180
|
+
/** Mixes `blend` of black in, and keeps the alpha. */
|
|
181
|
+
darker(blend = 0.2) {
|
|
182
|
+
return this.interpolate(ManimColor(BLACK), blend).opacity(this.toRgba()[3]);
|
|
183
|
+
}
|
|
184
|
+
/** Mixes `blend` of white in, and keeps the alpha. */
|
|
185
|
+
lighter(blend = 0.2) {
|
|
186
|
+
return this.interpolate(ManimColor(WHITE), blend).opacity(this.toRgba()[3]);
|
|
187
|
+
}
|
|
188
|
+
/** `light` (white) when the luminance (YIQ) is below `threshold`, else `dark` (black). */
|
|
189
|
+
contrasting(threshold = 0.5, light, dark) {
|
|
190
|
+
const [r, g, b] = this.toRgb();
|
|
191
|
+
const luminance = 0.3 * r + 0.59 * g + 0.11 * b;
|
|
192
|
+
if (luminance < threshold)
|
|
193
|
+
return light ?? ManimColor(WHITE);
|
|
194
|
+
return dark ?? ManimColor(BLACK);
|
|
195
|
+
}
|
|
196
|
+
/** The colour with this alpha. */
|
|
197
|
+
opacity(opacity) {
|
|
198
|
+
const [r, g, b] = this.toRgb();
|
|
199
|
+
return this.make([r, g, b, opacity]);
|
|
200
|
+
}
|
|
201
|
+
/** The colour as another class: `color.into(m.HSV)`. */
|
|
202
|
+
into(classType) {
|
|
203
|
+
return classType(this);
|
|
204
|
+
}
|
|
205
|
+
fromRgb(rgb, alpha = 1) {
|
|
206
|
+
return ManimColor.fromRgb(rgb, alpha);
|
|
207
|
+
}
|
|
208
|
+
fromRgba(rgba) {
|
|
209
|
+
return ManimColor.fromRgba(rgba);
|
|
210
|
+
}
|
|
211
|
+
fromHex(hexStr, alpha = 1) {
|
|
212
|
+
return ManimColor.fromHex(hexStr, alpha);
|
|
213
|
+
}
|
|
214
|
+
fromHsv(hsv, alpha = 1) {
|
|
215
|
+
return ManimColor.fromHsv(hsv, alpha);
|
|
216
|
+
}
|
|
217
|
+
fromHsl(hsl, alpha = 1) {
|
|
218
|
+
return ManimColor.fromHsl(hsl, alpha);
|
|
219
|
+
}
|
|
220
|
+
parse(color, alpha = 1) {
|
|
221
|
+
return ManimColor.parse(color, alpha);
|
|
222
|
+
}
|
|
223
|
+
/** True when the red, green, blue and alpha are all close (Manim `==`). */
|
|
224
|
+
equals(other) {
|
|
225
|
+
const p = this.toRgba();
|
|
226
|
+
const q = other.toRgba();
|
|
227
|
+
return p.every((x, i) => Math.abs(x - (q[i] ?? 0)) <= 1e-8 + 1e-5 * Math.abs(q[i] ?? 0));
|
|
228
|
+
}
|
|
229
|
+
toString() {
|
|
230
|
+
return this.toHex();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** A colour with an alpha (Manim `ManimColor(value, alpha)`). */
|
|
234
|
+
export function ManimColor(value, alpha = 1) {
|
|
235
|
+
return new ManimColorObject(parse(value, alpha));
|
|
236
|
+
}
|
|
237
|
+
ManimColor.fromRgb = (rgb, alpha = 1) => ManimColor(rgb.slice(0, 3), alpha);
|
|
238
|
+
ManimColor.fromRgba = (rgba) => ManimColor(rgba);
|
|
239
|
+
ManimColor.fromHex = (hexStr, alpha = 1) => ManimColor(hexStr, alpha);
|
|
240
|
+
ManimColor.fromHsv = (hsv, alpha = 1) => new ManimColorObject([...hsvToRgb(hsv), alpha]);
|
|
241
|
+
ManimColor.fromHsl = (hsl, alpha = 1) => new ManimColorObject([...hslToRgb(hsl), alpha]);
|
|
242
|
+
ManimColor.parse = (color, alpha = 1) => ManimColor(color, alpha);
|
|
243
|
+
/** Manim's `RGBA`: another name of `ManimColor`. */
|
|
244
|
+
export const RGBA = Object.assign((value, alpha = 1) => ManimColor(value, alpha), ManimColor);
|
|
245
|
+
/** A ManimColor that keeps hue, saturation and value (Manim `HSV`). */
|
|
246
|
+
export class HSVObject extends ManimColorObject {
|
|
247
|
+
#hsv;
|
|
248
|
+
constructor(rgba) {
|
|
249
|
+
super(rgba);
|
|
250
|
+
const [r, g, b] = rgba;
|
|
251
|
+
this.#hsv = [...rgbToHsv([r, g, b])];
|
|
252
|
+
}
|
|
253
|
+
toRgba() {
|
|
254
|
+
return [...hsvToRgb(this.#hsv), super.toRgba()[3]];
|
|
255
|
+
}
|
|
256
|
+
get hue() {
|
|
257
|
+
return this.#hsv[0];
|
|
258
|
+
}
|
|
259
|
+
set hue(hue) {
|
|
260
|
+
this.#hsv[0] = hue;
|
|
261
|
+
}
|
|
262
|
+
get saturation() {
|
|
263
|
+
return this.#hsv[1];
|
|
264
|
+
}
|
|
265
|
+
set saturation(saturation) {
|
|
266
|
+
this.#hsv[1] = saturation;
|
|
267
|
+
}
|
|
268
|
+
get value() {
|
|
269
|
+
return this.#hsv[2];
|
|
270
|
+
}
|
|
271
|
+
set value(value) {
|
|
272
|
+
this.#hsv[2] = value;
|
|
273
|
+
}
|
|
274
|
+
get h() {
|
|
275
|
+
return this.hue;
|
|
276
|
+
}
|
|
277
|
+
set h(hue) {
|
|
278
|
+
this.hue = hue;
|
|
279
|
+
}
|
|
280
|
+
get s() {
|
|
281
|
+
return this.saturation;
|
|
282
|
+
}
|
|
283
|
+
set s(saturation) {
|
|
284
|
+
this.saturation = saturation;
|
|
285
|
+
}
|
|
286
|
+
get v() {
|
|
287
|
+
return this.value;
|
|
288
|
+
}
|
|
289
|
+
set v(value) {
|
|
290
|
+
this.value = value;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/** A colour from hue, saturation and value, each from 0 to 1 (Manim `HSV`). A ManimColor or
|
|
294
|
+
* another parsable colour becomes HSV too: `HSV(m.BLUE)`. */
|
|
295
|
+
export function HSV(hsv, alpha = 1) {
|
|
296
|
+
if (Array.isArray(hsv) && (hsv.length === 3 || hsv.length === 4)) {
|
|
297
|
+
const [h = 0, s = 0, v = 0, a = alpha] = hsv;
|
|
298
|
+
return new HSVObject([...hsvToRgb([h, s, v]), a]);
|
|
299
|
+
}
|
|
300
|
+
return new HSVObject(parse(hsv, alpha));
|
|
301
|
+
}
|
|
302
|
+
/** `[r, g, b]` from 0 to 1. */
|
|
303
|
+
export function colorToRgb(color) {
|
|
304
|
+
return ManimColor(color).toRgb();
|
|
305
|
+
}
|
|
306
|
+
export function colorToRgba(color, alpha = 1) {
|
|
307
|
+
return ManimColor(color).toRgbaWithAlpha(alpha);
|
|
308
|
+
}
|
|
309
|
+
/** `[r, g, b]` from 0 to 255. */
|
|
310
|
+
export function colorToIntRgb(color) {
|
|
311
|
+
return ManimColor(color).toIntRgb();
|
|
312
|
+
}
|
|
313
|
+
export function colorToIntRgba(color, alpha = 1) {
|
|
314
|
+
return ManimColor(color).toIntRgbaWithAlpha(alpha);
|
|
315
|
+
}
|
|
316
|
+
/** The `"#RRGGBB"` colour of `[r, g, b]` (from 0 to 1, or from 0 to 255 when a number is above 1). */
|
|
317
|
+
export function rgbToColor(rgb) {
|
|
318
|
+
return ManimColor.fromRgb(rgb).toHex();
|
|
319
|
+
}
|
|
320
|
+
export function rgbaToColor(rgba) {
|
|
321
|
+
return ManimColor.fromRgba(rgba).toHex();
|
|
322
|
+
}
|
|
323
|
+
export function rgbToHex(rgb) {
|
|
324
|
+
return ManimColor.fromRgb(rgb).toHex();
|
|
325
|
+
}
|
|
326
|
+
export function hexToRgb(hexCode) {
|
|
327
|
+
return ManimColor(hexCode).toRgb();
|
|
328
|
+
}
|
|
329
|
+
/** `1 - r`, `1 - g`, `1 - b`, as `"#RRGGBB"`. */
|
|
330
|
+
export function invertColor(color) {
|
|
331
|
+
return ManimColor(color).invert().toHex();
|
|
332
|
+
}
|
|
333
|
+
/** `lengthOfOutput` colours evenly between the reference colours, as `"#RRGGBB"`. */
|
|
334
|
+
export function colorGradient(referenceColors, lengthOfOutput) {
|
|
335
|
+
if (lengthOfOutput === 0)
|
|
336
|
+
return [];
|
|
337
|
+
const colors = referenceColors.map((c) => ManimColor(c));
|
|
338
|
+
const first = colors[0];
|
|
339
|
+
if (first === undefined)
|
|
340
|
+
return fault("colorGradient: give 1 or more reference colours; got 0");
|
|
341
|
+
if (colors.length === 1)
|
|
342
|
+
return Array.from({ length: lengthOfOutput }, () => first.toHex());
|
|
343
|
+
const rgbs = colors.map((c) => c.toRgb());
|
|
344
|
+
const n = colors.length;
|
|
345
|
+
return Array.from({ length: lengthOfOutput }, (_, k) => {
|
|
346
|
+
const x = lengthOfOutput === 1 ? 0 : ((n - 1) * k) / (lengthOfOutput - 1);
|
|
347
|
+
const last = k === lengthOfOutput - 1;
|
|
348
|
+
const i = last ? n - 2 : Math.trunc(x);
|
|
349
|
+
const alpha = last ? 1 : x % 1;
|
|
350
|
+
const p = rgbs[i] ?? [0, 0, 0];
|
|
351
|
+
const q = rgbs[i + 1] ?? p;
|
|
352
|
+
return rgbToColor(p.map((v, j) => v * (1 - alpha) + (q[j] ?? 0) * alpha));
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
/** The colour `alpha` of the way from `color1` to `color2`, as `"#RRGGBB"`. */
|
|
356
|
+
export function interpolateColor(color1, color2, alpha) {
|
|
357
|
+
return ManimColor(color1).interpolate(ManimColor(color2), alpha).toHex();
|
|
358
|
+
}
|
|
359
|
+
/** The mean of the red, green and blue of the colours, as `"#RRGGBB"`. */
|
|
360
|
+
export function averageColor(...colors) {
|
|
361
|
+
const rgbs = colors.map(colorToRgb);
|
|
362
|
+
const mean = [0, 1, 2].map((i) => rgbs.reduce((sum, c) => sum + (c[i] ?? 0), 0) / rgbs.length);
|
|
363
|
+
return rgbToColor(mean);
|
|
364
|
+
}
|
|
365
|
+
/** A seeded generator: mulberry32. */
|
|
366
|
+
function generator(seed) {
|
|
367
|
+
let a = seed >>> 0;
|
|
368
|
+
return () => {
|
|
369
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
370
|
+
let t = a;
|
|
371
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
372
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
373
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
/** Picks colours from a list (Manim `RandomColorGenerator`). The same seed gives the same
|
|
377
|
+
* colours; with no seed, the seed is 0, so each build of a video has the same colours. */
|
|
378
|
+
export class RandomColorGeneratorObject {
|
|
379
|
+
#random;
|
|
380
|
+
#colors;
|
|
381
|
+
constructor(seed, colors) {
|
|
382
|
+
this.#random = generator(seed);
|
|
383
|
+
this.#colors = colors;
|
|
384
|
+
}
|
|
385
|
+
/** The next colour, as `"#RRGGBB"`. */
|
|
386
|
+
next() {
|
|
387
|
+
return this.#colors[Math.floor(this.#random() * this.#colors.length)] ?? WHITE;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
/** `RandomColorGenerator(seed, sampleColors)`: `sampleColors` is the CE palette by default. */
|
|
391
|
+
export function RandomColorGenerator(seed = 0, sampleColors = [...names.values()]) {
|
|
392
|
+
return new RandomColorGeneratorObject(seed, sampleColors.map((c) => ManimColor(c).toHex()));
|
|
393
|
+
}
|
|
394
|
+
const randoms = RandomColorGenerator();
|
|
395
|
+
/** A colour of the CE palette, as `"#RRGGBB"`, from one generator with seed 0. */
|
|
396
|
+
export function randomColor() {
|
|
397
|
+
return randoms.next();
|
|
398
|
+
}
|
|
399
|
+
/** `randomColor()` mixed half with white. */
|
|
400
|
+
export function randomBrightColor() {
|
|
401
|
+
const rgb = colorToRgb(randomColor());
|
|
402
|
+
return rgbToColor(rgb.map((v) => 0.5 * (v + 1)));
|
|
403
|
+
}
|
package/dist/curves.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Point } from "./engine.ts";
|
|
2
|
+
/** The control points of SVG path data with `M`, `L`, `H`, `V`, `C`, `S` and `Z` (absolute or
|
|
3
|
+
* relative), four for each curve, in the SVG's own coordinates (y down). */
|
|
4
|
+
export declare function svgPathPoints(d: string): Point[];
|
|
5
|
+
/** The part of a path from proportion `a` to `b` of its curves (Manim's
|
|
6
|
+
* `pointwise_become_partial`). */
|
|
7
|
+
export declare function partialPoints(points: readonly Point[], a: number, b: number): Point[];
|
|
8
|
+
/** Splits the curves so that the path has `n` more (Manim `insert_n_curves`): the curves are
|
|
9
|
+
* split evenly, each into equal pieces of its parameter. */
|
|
10
|
+
export declare function insertCurves(points: readonly Point[], n: number): Point[];
|
package/dist/curves.js
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/** The control points of SVG path data with `M`, `L`, `H`, `V`, `C`, `S` and `Z` (absolute or
|
|
2
|
+
* relative), four for each curve, in the SVG's own coordinates (y down). */
|
|
3
|
+
export function svgPathPoints(d) {
|
|
4
|
+
const tokens = d.match(/[MLHVCSZmlhvcsz]|-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/g) ?? [];
|
|
5
|
+
const out = [];
|
|
6
|
+
let pen = [0, 0];
|
|
7
|
+
let start = [0, 0];
|
|
8
|
+
let command = "";
|
|
9
|
+
/** The second handle of the last `C` or `S` curve, for `S`. */
|
|
10
|
+
let handle;
|
|
11
|
+
let i = 0;
|
|
12
|
+
const num = () => Number(tokens[i++]);
|
|
13
|
+
const at = (x, y, relative) => (relative ? [pen[0] + x, pen[1] + y] : [x, y]);
|
|
14
|
+
const line = (to) => {
|
|
15
|
+
const [dx, dy] = [to[0] - pen[0], to[1] - pen[1]];
|
|
16
|
+
out.push(pen, [pen[0] + dx / 3, pen[1] + dy / 3], [pen[0] + (2 * dx) / 3, pen[1] + (2 * dy) / 3], to);
|
|
17
|
+
pen = to;
|
|
18
|
+
};
|
|
19
|
+
while (i < tokens.length) {
|
|
20
|
+
const t = tokens[i] ?? "";
|
|
21
|
+
if (/[A-Za-z]/.test(t)) {
|
|
22
|
+
command = t;
|
|
23
|
+
i++;
|
|
24
|
+
if (command === "z" || command === "Z") {
|
|
25
|
+
if (Math.hypot(pen[0] - start[0], pen[1] - start[1]) > 1e-9)
|
|
26
|
+
line(start);
|
|
27
|
+
pen = start;
|
|
28
|
+
handle = undefined;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const relative = command === command.toLowerCase();
|
|
33
|
+
const previous = handle;
|
|
34
|
+
handle = undefined;
|
|
35
|
+
switch (command.toLowerCase()) {
|
|
36
|
+
case "m":
|
|
37
|
+
pen = at(num(), num(), relative);
|
|
38
|
+
start = pen;
|
|
39
|
+
command = relative ? "l" : "L";
|
|
40
|
+
break;
|
|
41
|
+
case "l":
|
|
42
|
+
line(at(num(), num(), relative));
|
|
43
|
+
break;
|
|
44
|
+
case "h":
|
|
45
|
+
line(relative ? [pen[0] + num(), pen[1]] : [num(), pen[1]]);
|
|
46
|
+
break;
|
|
47
|
+
case "v":
|
|
48
|
+
line(relative ? [pen[0], pen[1] + num()] : [pen[0], num()]);
|
|
49
|
+
break;
|
|
50
|
+
case "c": {
|
|
51
|
+
const a = at(num(), num(), relative);
|
|
52
|
+
const b = at(num(), num(), relative);
|
|
53
|
+
const c = at(num(), num(), relative);
|
|
54
|
+
out.push(pen, a, b, c);
|
|
55
|
+
pen = c;
|
|
56
|
+
handle = b;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
case "s": {
|
|
60
|
+
// The first handle is the last handle of the curve before, mirrored about the pen.
|
|
61
|
+
const a = previous === undefined ? pen : [2 * pen[0] - previous[0], 2 * pen[1] - previous[1]];
|
|
62
|
+
const b = at(num(), num(), relative);
|
|
63
|
+
const c = at(num(), num(), relative);
|
|
64
|
+
out.push(pen, a, b, c);
|
|
65
|
+
pen = c;
|
|
66
|
+
handle = b;
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
default:
|
|
70
|
+
i++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
function lerp(a, b, t) {
|
|
76
|
+
return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
77
|
+
}
|
|
78
|
+
function splitCubic(q, t) {
|
|
79
|
+
const z = [0, 0];
|
|
80
|
+
const [p0, p1, p2, p3] = [q[0] ?? z, q[1] ?? z, q[2] ?? z, q[3] ?? z];
|
|
81
|
+
const a = lerp(p0, p1, t);
|
|
82
|
+
const b = lerp(p1, p2, t);
|
|
83
|
+
const d = lerp(p2, p3, t);
|
|
84
|
+
const e = lerp(a, b, t);
|
|
85
|
+
const f = lerp(b, d, t);
|
|
86
|
+
const g = lerp(e, f, t);
|
|
87
|
+
return [
|
|
88
|
+
[p0, a, e, g],
|
|
89
|
+
[g, f, d, p3],
|
|
90
|
+
];
|
|
91
|
+
}
|
|
92
|
+
/** The part of the cubic `[a, h1, h2, b]` from `t0` to `t1` (de Casteljau). */
|
|
93
|
+
function cubicPart(c, t0, t1) {
|
|
94
|
+
const right = splitCubic(c, t0)[1];
|
|
95
|
+
return t0 >= 1 ? [...right] : splitCubic(right, (t1 - t0) / (1 - t0))[0];
|
|
96
|
+
}
|
|
97
|
+
/** The part of a path from proportion `a` to `b` of its curves (Manim's
|
|
98
|
+
* `pointwise_become_partial`). */
|
|
99
|
+
export function partialPoints(points, a, b) {
|
|
100
|
+
const n = Math.floor(points.length / 4);
|
|
101
|
+
if (n === 0 || b <= a)
|
|
102
|
+
return [];
|
|
103
|
+
const [lo, hi] = [a * n, b * n];
|
|
104
|
+
const out = [];
|
|
105
|
+
for (let i = 0; i < n; i++) {
|
|
106
|
+
if (i + 1 <= lo || i >= hi)
|
|
107
|
+
continue;
|
|
108
|
+
out.push(...cubicPart(points.slice(4 * i, 4 * i + 4), Math.max(0, lo - i), Math.min(1, hi - i)));
|
|
109
|
+
}
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/** Splits the curves so that the path has `n` more (Manim `insert_n_curves`): the curves are
|
|
113
|
+
* split evenly, each into equal pieces of its parameter. */
|
|
114
|
+
export function insertCurves(points, n) {
|
|
115
|
+
const count = Math.floor(points.length / 4);
|
|
116
|
+
if (count === 0 || n <= 0)
|
|
117
|
+
return [...points];
|
|
118
|
+
const target = count + n;
|
|
119
|
+
const pieces = Array.from({ length: count }, () => 0);
|
|
120
|
+
for (let k = 0; k < target; k++) {
|
|
121
|
+
const i = Math.floor((k * count) / target);
|
|
122
|
+
pieces[i] = (pieces[i] ?? 0) + 1;
|
|
123
|
+
}
|
|
124
|
+
return pieces.flatMap((sf, i) => {
|
|
125
|
+
const curve = points.slice(4 * i, 4 * i + 4);
|
|
126
|
+
return Array.from({ length: sf }, (_, j) => cubicPart(curve, j / sf, (j + 1) / sf)).flat();
|
|
127
|
+
});
|
|
128
|
+
}
|