@n3rdw1z4rd/core 0.18.4 → 0.19.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 +72 -10
- package/dist/astar.d.ts +44 -0
- package/dist/astar.js +118 -0
- package/dist/canvas-renderer.d.ts +154 -0
- package/dist/canvas-renderer.js +200 -0
- package/dist/clock.d.ts +69 -0
- package/dist/clock.js +108 -0
- package/dist/color-palette.d.ts +25 -0
- package/dist/color-palette.js +112 -0
- package/dist/color.d.ts +90 -0
- package/dist/color.js +224 -0
- package/dist/css/main.css +1 -3
- package/dist/disposable.d.ts +27 -0
- package/dist/disposable.js +32 -0
- package/dist/dungeon-generator.d.ts +142 -0
- package/dist/dungeon-generator.js +288 -0
- package/dist/ecs.d.ts +87 -0
- package/dist/ecs.js +101 -0
- package/dist/emitter.d.ts +40 -0
- package/dist/emitter.js +54 -0
- package/dist/event-queue.d.ts +29 -0
- package/dist/event-queue.js +40 -0
- package/dist/index.cjs +3813 -0
- package/dist/index.d.cts +2655 -0
- package/dist/index.d.ts +35 -873
- package/dist/index.js +34 -3986
- package/dist/input/action-map.d.ts +79 -0
- package/dist/input/action-map.js +131 -0
- package/dist/input/bindings.d.ts +273 -0
- package/dist/input/bindings.js +250 -0
- package/dist/input/gestures.d.ts +108 -0
- package/dist/input/gestures.js +159 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input.d.ts +297 -0
- package/dist/input/input.js +695 -0
- package/dist/log.d.ts +8 -0
- package/dist/log.js +9 -0
- package/dist/map2d.d.ts +83 -0
- package/dist/map2d.js +120 -0
- package/dist/map3d-CHDJiq5T.d.cts +236 -0
- package/dist/map3d.d.ts +94 -0
- package/dist/map3d.js +148 -0
- package/dist/math.d.ts +176 -0
- package/dist/math.js +206 -0
- package/dist/misc.d.ts +46 -0
- package/dist/misc.js +71 -0
- package/dist/observable.d.ts +43 -0
- package/dist/observable.js +53 -0
- package/dist/particle-renderer.d.ts +47 -0
- package/dist/particle-renderer.js +82 -0
- package/dist/particle-system-2d.d.ts +60 -0
- package/dist/particle-system-2d.js +147 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.js +74 -0
- package/dist/rectangle.d.ts +44 -0
- package/dist/rectangle.js +69 -0
- package/dist/rng.d.ts +72 -0
- package/dist/rng.js +152 -0
- package/dist/signal.d.ts +34 -0
- package/dist/signal.js +41 -0
- package/dist/spatial-partition-2d.d.ts +42 -0
- package/dist/spatial-partition-2d.js +52 -0
- package/dist/spatial-partition-3d-Ba-VBveM.cjs +1324 -0
- package/dist/spatial-partition-3d.d.ts +44 -0
- package/dist/spatial-partition-3d.js +57 -0
- package/dist/three/asteroid-mesh.d.ts +41 -0
- package/dist/three/asteroid-mesh.js +44 -0
- package/dist/three/camera-rig.d.ts +140 -0
- package/dist/three/camera-rig.js +197 -0
- package/dist/three/cube-sphere-geometry.d.ts +55 -0
- package/dist/three/cube-sphere-geometry.js +239 -0
- package/dist/three/ecs.d.ts +75 -0
- package/dist/three/ecs.js +64 -0
- package/dist/three/index.d.ts +12 -0
- package/dist/three/index.js +12 -0
- package/dist/three/particle-system-3d.d.ts +53 -0
- package/dist/three/particle-system-3d.js +224 -0
- package/dist/three/renderer.d.ts +38 -0
- package/dist/three/renderer.js +57 -0
- package/dist/three/starfield.d.ts +23 -0
- package/dist/three/starfield.js +64 -0
- package/dist/three/texture-atlas.d.ts +76 -0
- package/dist/three/texture-atlas.js +84 -0
- package/dist/three/texture-utils.d.ts +31 -0
- package/dist/three/texture-utils.js +44 -0
- package/dist/three/voxel-geometry.d.ts +72 -0
- package/dist/three/voxel-geometry.js +231 -0
- package/dist/three/voxel-mesh.d.ts +67 -0
- package/dist/three/voxel-mesh.js +71 -0
- package/dist/three.cjs +1307 -0
- package/dist/three.d.cts +655 -0
- package/dist/tilemap.d.ts +17 -0
- package/dist/tilemap.js +26 -0
- package/dist/types.d.ts +34 -0
- package/dist/types.js +0 -0
- package/dist/vector.d.ts +78 -0
- package/dist/vector.js +169 -0
- package/dist/webgl-renderer.d.ts +89 -0
- package/dist/webgl-renderer.js +140 -0
- package/dist/webgl.d.ts +357 -0
- package/dist/webgl.js +628 -0
- package/dist/worker-interface.d.ts +30 -0
- package/dist/worker-interface.js +39 -0
- package/package.json +55 -13
- package/dist/css/index.ts +0 -1
- package/dist/index.css +0 -104
- package/dist/index.d.mts +0 -873
- package/dist/index.mjs +0 -3982
package/dist/clock.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
//#region src/clock.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Milliseconds in one second.
|
|
4
|
+
*
|
|
5
|
+
* @category Events & Timing
|
|
6
|
+
*/
|
|
7
|
+
export declare const ONE_SECOND = 1000;
|
|
8
|
+
/**
|
|
9
|
+
* Called by {@link Clock.start} every animation frame with the capped delta time and the elapsed
|
|
10
|
+
* time, both in seconds.
|
|
11
|
+
*
|
|
12
|
+
* @category Events & Timing
|
|
13
|
+
*/
|
|
14
|
+
export type ClockCallback = (deltaTime: number, elapsedTime: number) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Frame timing: delta time, elapsed time and FPS.
|
|
17
|
+
*
|
|
18
|
+
* Either let the clock run the loop:
|
|
19
|
+
*
|
|
20
|
+
* clock.start((dt) => { update(dt); render(); });
|
|
21
|
+
*
|
|
22
|
+
* or advance it from a loop something else drives (three.js, WebXR):
|
|
23
|
+
*
|
|
24
|
+
* renderer.setAnimationLoop((time) => { const dt = clock.tick(time); ... });
|
|
25
|
+
*
|
|
26
|
+
* @category Events & Timing
|
|
27
|
+
*/
|
|
28
|
+
export declare class Clock {
|
|
29
|
+
/**
|
|
30
|
+
* seconds; caps the delta time after a stall or a hidden tab (browsers pause the frame loop
|
|
31
|
+
* in background tabs, so the first frame back would otherwise cover the whole absence)
|
|
32
|
+
*/
|
|
33
|
+
maxDeltaTime: number;
|
|
34
|
+
private _callback?;
|
|
35
|
+
private _rafId;
|
|
36
|
+
private _isRunning;
|
|
37
|
+
private _lastTime;
|
|
38
|
+
private _deltaTime;
|
|
39
|
+
private _elapsedTime;
|
|
40
|
+
private _fps;
|
|
41
|
+
private _fpsFrames;
|
|
42
|
+
private _fpsTime;
|
|
43
|
+
/** seconds since the previous tick, capped at `maxDeltaTime`; 0 on the first tick */
|
|
44
|
+
get deltaTime(): number;
|
|
45
|
+
/**
|
|
46
|
+
* seconds since start() (or the first tick), summed from the capped delta times,
|
|
47
|
+
* so it stays in step with the simulation rather than wall-clock time
|
|
48
|
+
*/
|
|
49
|
+
get elapsedTime(): number;
|
|
50
|
+
/** frames per second, rounded; recomputed about once a second, 0 until the first second has passed */
|
|
51
|
+
get fps(): number;
|
|
52
|
+
/** whether {@link Clock.start} is driving the loop (manual {@link Clock.tick} calls don't count) */
|
|
53
|
+
get isRunning(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Advances the clock and returns the delta time in seconds. The first tick returns 0.
|
|
56
|
+
*
|
|
57
|
+
* @param time timestamp in milliseconds, e.g. from requestAnimationFrame or setAnimationLoop
|
|
58
|
+
*/
|
|
59
|
+
tick(time?: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* Runs `callback` every animation frame with the delta and elapsed time in seconds.
|
|
62
|
+
* Restarts timing from zero; calling it while running just swaps the callback.
|
|
63
|
+
*/
|
|
64
|
+
start(callback: ClockCallback): void;
|
|
65
|
+
/** Stops the loop started by {@link Clock.start}. Timing state is kept until the next `start()`. */
|
|
66
|
+
stop(): void;
|
|
67
|
+
private _reset;
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
package/dist/clock.js
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//#region src/clock.ts
|
|
2
|
+
/**
|
|
3
|
+
* Milliseconds in one second.
|
|
4
|
+
*
|
|
5
|
+
* @category Events & Timing
|
|
6
|
+
*/
|
|
7
|
+
const ONE_SECOND = 1e3;
|
|
8
|
+
/**
|
|
9
|
+
* Frame timing: delta time, elapsed time and FPS.
|
|
10
|
+
*
|
|
11
|
+
* Either let the clock run the loop:
|
|
12
|
+
*
|
|
13
|
+
* clock.start((dt) => { update(dt); render(); });
|
|
14
|
+
*
|
|
15
|
+
* or advance it from a loop something else drives (three.js, WebXR):
|
|
16
|
+
*
|
|
17
|
+
* renderer.setAnimationLoop((time) => { const dt = clock.tick(time); ... });
|
|
18
|
+
*
|
|
19
|
+
* @category Events & Timing
|
|
20
|
+
*/
|
|
21
|
+
var Clock = class {
|
|
22
|
+
/**
|
|
23
|
+
* seconds; caps the delta time after a stall or a hidden tab (browsers pause the frame loop
|
|
24
|
+
* in background tabs, so the first frame back would otherwise cover the whole absence)
|
|
25
|
+
*/
|
|
26
|
+
maxDeltaTime = .1;
|
|
27
|
+
_callback;
|
|
28
|
+
_rafId = 0;
|
|
29
|
+
_isRunning = false;
|
|
30
|
+
_lastTime = -1;
|
|
31
|
+
_deltaTime = 0;
|
|
32
|
+
_elapsedTime = 0;
|
|
33
|
+
_fps = 0;
|
|
34
|
+
_fpsFrames = 0;
|
|
35
|
+
_fpsTime = 0;
|
|
36
|
+
/** seconds since the previous tick, capped at `maxDeltaTime`; 0 on the first tick */
|
|
37
|
+
get deltaTime() {
|
|
38
|
+
return this._deltaTime;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* seconds since start() (or the first tick), summed from the capped delta times,
|
|
42
|
+
* so it stays in step with the simulation rather than wall-clock time
|
|
43
|
+
*/
|
|
44
|
+
get elapsedTime() {
|
|
45
|
+
return this._elapsedTime;
|
|
46
|
+
}
|
|
47
|
+
/** frames per second, rounded; recomputed about once a second, 0 until the first second has passed */
|
|
48
|
+
get fps() {
|
|
49
|
+
return this._fps;
|
|
50
|
+
}
|
|
51
|
+
/** whether {@link Clock.start} is driving the loop (manual {@link Clock.tick} calls don't count) */
|
|
52
|
+
get isRunning() {
|
|
53
|
+
return this._isRunning;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Advances the clock and returns the delta time in seconds. The first tick returns 0.
|
|
57
|
+
*
|
|
58
|
+
* @param time timestamp in milliseconds, e.g. from requestAnimationFrame or setAnimationLoop
|
|
59
|
+
*/
|
|
60
|
+
tick(time = performance.now()) {
|
|
61
|
+
if (this._lastTime < 0) {
|
|
62
|
+
this._lastTime = time;
|
|
63
|
+
this._fpsTime = time;
|
|
64
|
+
this._deltaTime = 0;
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
this._deltaTime = Math.min(Math.max(0, (time - this._lastTime) / ONE_SECOND), this.maxDeltaTime);
|
|
68
|
+
this._elapsedTime += this._deltaTime;
|
|
69
|
+
this._lastTime = time;
|
|
70
|
+
this._fpsFrames++;
|
|
71
|
+
if (time - this._fpsTime >= 1e3) {
|
|
72
|
+
this._fps = Math.round(this._fpsFrames * ONE_SECOND / (time - this._fpsTime));
|
|
73
|
+
this._fpsFrames = 0;
|
|
74
|
+
this._fpsTime = time;
|
|
75
|
+
}
|
|
76
|
+
return this._deltaTime;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Runs `callback` every animation frame with the delta and elapsed time in seconds.
|
|
80
|
+
* Restarts timing from zero; calling it while running just swaps the callback.
|
|
81
|
+
*/
|
|
82
|
+
start(callback) {
|
|
83
|
+
this._callback = callback;
|
|
84
|
+
if (this._isRunning) return;
|
|
85
|
+
this._isRunning = true;
|
|
86
|
+
this._reset();
|
|
87
|
+
const frame = (time) => {
|
|
88
|
+
this.tick(time);
|
|
89
|
+
this._callback?.(this._deltaTime, this._elapsedTime);
|
|
90
|
+
if (this._isRunning) this._rafId = requestAnimationFrame(frame);
|
|
91
|
+
};
|
|
92
|
+
this._rafId = requestAnimationFrame(frame);
|
|
93
|
+
}
|
|
94
|
+
/** Stops the loop started by {@link Clock.start}. Timing state is kept until the next `start()`. */
|
|
95
|
+
stop() {
|
|
96
|
+
this._isRunning = false;
|
|
97
|
+
cancelAnimationFrame(this._rafId);
|
|
98
|
+
}
|
|
99
|
+
_reset() {
|
|
100
|
+
this._lastTime = -1;
|
|
101
|
+
this._deltaTime = 0;
|
|
102
|
+
this._elapsedTime = 0;
|
|
103
|
+
this._fps = 0;
|
|
104
|
+
this._fpsFrames = 0;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
//#endregion
|
|
108
|
+
export { Clock, ONE_SECOND };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/color-palette.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Color as `[r, g, b]`, each 0-255.
|
|
4
|
+
* @category Color
|
|
5
|
+
*/
|
|
6
|
+
export type RGB = readonly [number, number, number];
|
|
7
|
+
/**
|
|
8
|
+
* Flat list of colors, each packed as a `0xRRGGBB` integer, as returned by {@link buildColorPalette}.
|
|
9
|
+
* @category Color
|
|
10
|
+
*/
|
|
11
|
+
export type ColorPalette = readonly number[];
|
|
12
|
+
/**
|
|
13
|
+
* Builds a flat color palette of packed `0xRRGGBB` integers: a grayscale ramp (black -> white)
|
|
14
|
+
* followed by a light -> dark ramp for each supplied color. Length is
|
|
15
|
+
* `grayscaleSteps + colors.length * colorSteps`.
|
|
16
|
+
*
|
|
17
|
+
* @param grayscaleSteps number of grayscale entries, black -> white (a single step is black)
|
|
18
|
+
* @param colorSteps number of steps per color, from the full color down to 28% of it (never black)
|
|
19
|
+
* @param colors base RGB colors to ramp; defaults to 14 common colors (red, orange, yellow,
|
|
20
|
+
* green, cyan, blue, purple, magenta, pink, brown, tan, olive, teal, maroon).
|
|
21
|
+
* White is always included via the grayscale ramp.
|
|
22
|
+
* @category Color
|
|
23
|
+
*/
|
|
24
|
+
export declare function buildColorPalette(grayscaleSteps?: number, colorSteps?: number, ...colors: RGB[]): ColorPalette;
|
|
25
|
+
//#endregion
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
//#region src/color-palette.ts
|
|
2
|
+
const DEFAULT_COLORS = [
|
|
3
|
+
[
|
|
4
|
+
237,
|
|
5
|
+
28,
|
|
6
|
+
36
|
|
7
|
+
],
|
|
8
|
+
[
|
|
9
|
+
255,
|
|
10
|
+
127,
|
|
11
|
+
39
|
|
12
|
+
],
|
|
13
|
+
[
|
|
14
|
+
255,
|
|
15
|
+
242,
|
|
16
|
+
0
|
|
17
|
+
],
|
|
18
|
+
[
|
|
19
|
+
34,
|
|
20
|
+
177,
|
|
21
|
+
76
|
|
22
|
+
],
|
|
23
|
+
[
|
|
24
|
+
0,
|
|
25
|
+
183,
|
|
26
|
+
239
|
|
27
|
+
],
|
|
28
|
+
[
|
|
29
|
+
63,
|
|
30
|
+
72,
|
|
31
|
+
204
|
|
32
|
+
],
|
|
33
|
+
[
|
|
34
|
+
163,
|
|
35
|
+
73,
|
|
36
|
+
164
|
|
37
|
+
],
|
|
38
|
+
[
|
|
39
|
+
236,
|
|
40
|
+
0,
|
|
41
|
+
140
|
|
42
|
+
],
|
|
43
|
+
[
|
|
44
|
+
255,
|
|
45
|
+
174,
|
|
46
|
+
201
|
|
47
|
+
],
|
|
48
|
+
[
|
|
49
|
+
139,
|
|
50
|
+
90,
|
|
51
|
+
43
|
|
52
|
+
],
|
|
53
|
+
[
|
|
54
|
+
214,
|
|
55
|
+
181,
|
|
56
|
+
129
|
|
57
|
+
],
|
|
58
|
+
[
|
|
59
|
+
128,
|
|
60
|
+
128,
|
|
61
|
+
0
|
|
62
|
+
],
|
|
63
|
+
[
|
|
64
|
+
0,
|
|
65
|
+
128,
|
|
66
|
+
128
|
|
67
|
+
],
|
|
68
|
+
[
|
|
69
|
+
128,
|
|
70
|
+
0,
|
|
71
|
+
32
|
|
72
|
+
]
|
|
73
|
+
];
|
|
74
|
+
function packRGB(r, g, b) {
|
|
75
|
+
r = Math.min(255, Math.max(0, Math.round(r)));
|
|
76
|
+
g = Math.min(255, Math.max(0, Math.round(g)));
|
|
77
|
+
b = Math.min(255, Math.max(0, Math.round(b)));
|
|
78
|
+
return r << 16 | g << 8 | b;
|
|
79
|
+
}
|
|
80
|
+
function colorRamp(rgb, steps) {
|
|
81
|
+
const [r, g, b] = rgb;
|
|
82
|
+
const out = [];
|
|
83
|
+
for (let i = 0; i < steps; i++) {
|
|
84
|
+
const scale = 1 - (steps === 1 ? 0 : i / (steps - 1)) * .72;
|
|
85
|
+
out.push(packRGB(r * scale, g * scale, b * scale));
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Builds a flat color palette of packed `0xRRGGBB` integers: a grayscale ramp (black -> white)
|
|
91
|
+
* followed by a light -> dark ramp for each supplied color. Length is
|
|
92
|
+
* `grayscaleSteps + colors.length * colorSteps`.
|
|
93
|
+
*
|
|
94
|
+
* @param grayscaleSteps number of grayscale entries, black -> white (a single step is black)
|
|
95
|
+
* @param colorSteps number of steps per color, from the full color down to 28% of it (never black)
|
|
96
|
+
* @param colors base RGB colors to ramp; defaults to 14 common colors (red, orange, yellow,
|
|
97
|
+
* green, cyan, blue, purple, magenta, pink, brown, tan, olive, teal, maroon).
|
|
98
|
+
* White is always included via the grayscale ramp.
|
|
99
|
+
* @category Color
|
|
100
|
+
*/
|
|
101
|
+
function buildColorPalette(grayscaleSteps = 8, colorSteps = 4, ...colors) {
|
|
102
|
+
const palette = [];
|
|
103
|
+
for (let i = 0; i < grayscaleSteps; i++) {
|
|
104
|
+
const v = grayscaleSteps === 1 ? 0 : i / (grayscaleSteps - 1) * 255;
|
|
105
|
+
palette.push(packRGB(v, v, v));
|
|
106
|
+
}
|
|
107
|
+
const colorList = colors.length > 0 ? colors : DEFAULT_COLORS;
|
|
108
|
+
for (const rgb of colorList) palette.push(...colorRamp(rgb, colorSteps));
|
|
109
|
+
return palette;
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
export { buildColorPalette };
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
//#region src/color.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* RGBA color with 8-bit channels (integers 0-255), kept in sync with an 8-digit hex string.
|
|
4
|
+
* Setting a channel rounds and clamps it to 0-255.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* new Color(255, 128, 0); // opaque orange
|
|
8
|
+
* new Color('#ff8000'); // same, from a hex string
|
|
9
|
+
* Color.fromHsv(0.5, 1, 1).hexStr; // '#00ffffff'
|
|
10
|
+
* ```
|
|
11
|
+
* @category Color
|
|
12
|
+
*/
|
|
13
|
+
export declare class Color {
|
|
14
|
+
private _hex;
|
|
15
|
+
private _r;
|
|
16
|
+
private _g;
|
|
17
|
+
private _b;
|
|
18
|
+
private _a;
|
|
19
|
+
/** red, 0-255 */
|
|
20
|
+
get r(): number;
|
|
21
|
+
/** rounded and clamped to 0-255 */
|
|
22
|
+
set r(value: number);
|
|
23
|
+
/** green, 0-255 */
|
|
24
|
+
get g(): number;
|
|
25
|
+
/** rounded and clamped to 0-255 */
|
|
26
|
+
set g(value: number);
|
|
27
|
+
/** blue, 0-255 */
|
|
28
|
+
get b(): number;
|
|
29
|
+
/** rounded and clamped to 0-255 */
|
|
30
|
+
set b(value: number);
|
|
31
|
+
/** alpha, 0-255 (255 = opaque) */
|
|
32
|
+
get a(): number;
|
|
33
|
+
/** rounded and clamped to 0-255 */
|
|
34
|
+
set a(value: number);
|
|
35
|
+
/**
|
|
36
|
+
* lowercase `'#rrggbbaa'`. Accepts `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` when set (alpha
|
|
37
|
+
* defaults to ff); an invalid string is logged and the color is left unchanged.
|
|
38
|
+
*/
|
|
39
|
+
get hexStr(): string;
|
|
40
|
+
/** `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`; invalid strings are logged and ignored */
|
|
41
|
+
set hexStr(value: string);
|
|
42
|
+
/** a new `[r, g, b, a]` array, each 0-255 */
|
|
43
|
+
get rgba(): number[];
|
|
44
|
+
/**
|
|
45
|
+
* Creates a color from a hex string (as accepted by {@link Color.hexStr}) or from channel
|
|
46
|
+
* values, each rounded and clamped to 0-255 like the channel setters. An invalid string is
|
|
47
|
+
* logged and gives opaque white. Defaults to opaque white.
|
|
48
|
+
*/
|
|
49
|
+
constructor(r?: string | number, g?: number, b?: number, a?: number);
|
|
50
|
+
private static _channel;
|
|
51
|
+
private _to_hex;
|
|
52
|
+
private static _parse_hex_color_string;
|
|
53
|
+
/**
|
|
54
|
+
* Creates a color from hue, saturation and value, each in [0, 1]
|
|
55
|
+
* (hues outside [0, 1) wrap around).
|
|
56
|
+
* @param a alpha, 0-255 (default 255)
|
|
57
|
+
*/
|
|
58
|
+
static fromHsv(h: number, s: number, v: number, a?: number): Color;
|
|
59
|
+
/** new opaque black */
|
|
60
|
+
static get BLACK(): Color;
|
|
61
|
+
/** new opaque gray (128, 128, 128) */
|
|
62
|
+
static get GRAY(): Color;
|
|
63
|
+
/** new opaque white */
|
|
64
|
+
static get WHITE(): Color;
|
|
65
|
+
/** new opaque red (255, 0, 0) */
|
|
66
|
+
static get RED(): Color;
|
|
67
|
+
/** new opaque green (0, 255, 0) */
|
|
68
|
+
static get GREEN(): Color;
|
|
69
|
+
/** new opaque blue (0, 0, 255) */
|
|
70
|
+
static get BLUE(): Color;
|
|
71
|
+
/** new opaque yellow (255, 255, 0) */
|
|
72
|
+
static get YELLOW(): Color;
|
|
73
|
+
/** new opaque orange (255, 128, 0) */
|
|
74
|
+
static get ORANGE(): Color;
|
|
75
|
+
/** new opaque purple (128, 0, 128) */
|
|
76
|
+
static get PURPLE(): Color;
|
|
77
|
+
/** new opaque cyan (0, 255, 255) */
|
|
78
|
+
static get CYAN(): Color;
|
|
79
|
+
/** new opaque magenta (255, 0, 255) */
|
|
80
|
+
static get MAGENTA(): Color;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a color from any CSS color string (e.g. `'rebeccapurple'`, `'rgb(0 0 0 / 50%)'`) by
|
|
83
|
+
* letting the browser compute it. Needs a DOM: it briefly appends an element to
|
|
84
|
+
* `document.body`. An unrecognised name is logged and gives {@link Color.TRANSPARENT}.
|
|
85
|
+
*/
|
|
86
|
+
static fromName(name: string): Color;
|
|
87
|
+
/** new fully transparent black (0, 0, 0, 0) */
|
|
88
|
+
static get TRANSPARENT(): Color;
|
|
89
|
+
}
|
|
90
|
+
//#endregion
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { clamp } from "./math.js";
|
|
2
|
+
import { log } from "./log.js";
|
|
3
|
+
//#region src/color.ts
|
|
4
|
+
/**
|
|
5
|
+
* RGBA color with 8-bit channels (integers 0-255), kept in sync with an 8-digit hex string.
|
|
6
|
+
* Setting a channel rounds and clamps it to 0-255.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* new Color(255, 128, 0); // opaque orange
|
|
10
|
+
* new Color('#ff8000'); // same, from a hex string
|
|
11
|
+
* Color.fromHsv(0.5, 1, 1).hexStr; // '#00ffffff'
|
|
12
|
+
* ```
|
|
13
|
+
* @category Color
|
|
14
|
+
*/
|
|
15
|
+
var Color = class Color {
|
|
16
|
+
_hex = "#ffffffff";
|
|
17
|
+
_r = 255;
|
|
18
|
+
_g = 255;
|
|
19
|
+
_b = 255;
|
|
20
|
+
_a = 255;
|
|
21
|
+
/** red, 0-255 */
|
|
22
|
+
get r() {
|
|
23
|
+
return this._r;
|
|
24
|
+
}
|
|
25
|
+
/** rounded and clamped to 0-255 */
|
|
26
|
+
set r(value) {
|
|
27
|
+
this._r = Color._channel(value);
|
|
28
|
+
this._hex = this._to_hex();
|
|
29
|
+
}
|
|
30
|
+
/** green, 0-255 */
|
|
31
|
+
get g() {
|
|
32
|
+
return this._g;
|
|
33
|
+
}
|
|
34
|
+
/** rounded and clamped to 0-255 */
|
|
35
|
+
set g(value) {
|
|
36
|
+
this._g = Color._channel(value);
|
|
37
|
+
this._hex = this._to_hex();
|
|
38
|
+
}
|
|
39
|
+
/** blue, 0-255 */
|
|
40
|
+
get b() {
|
|
41
|
+
return this._b;
|
|
42
|
+
}
|
|
43
|
+
/** rounded and clamped to 0-255 */
|
|
44
|
+
set b(value) {
|
|
45
|
+
this._b = Color._channel(value);
|
|
46
|
+
this._hex = this._to_hex();
|
|
47
|
+
}
|
|
48
|
+
/** alpha, 0-255 (255 = opaque) */
|
|
49
|
+
get a() {
|
|
50
|
+
return this._a;
|
|
51
|
+
}
|
|
52
|
+
/** rounded and clamped to 0-255 */
|
|
53
|
+
set a(value) {
|
|
54
|
+
this._a = Color._channel(value);
|
|
55
|
+
this._hex = this._to_hex();
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* lowercase `'#rrggbbaa'`. Accepts `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` when set (alpha
|
|
59
|
+
* defaults to ff); an invalid string is logged and the color is left unchanged.
|
|
60
|
+
*/
|
|
61
|
+
get hexStr() {
|
|
62
|
+
return this._hex;
|
|
63
|
+
}
|
|
64
|
+
/** `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`; invalid strings are logged and ignored */
|
|
65
|
+
set hexStr(value) {
|
|
66
|
+
const rgba = Color._parse_hex_color_string(value);
|
|
67
|
+
if (rgba) {
|
|
68
|
+
[this._r, this._g, this._b, this._a] = rgba;
|
|
69
|
+
this._hex = this._to_hex();
|
|
70
|
+
} else log(`Color: invalid color string: ${value}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Keeping the current color.`);
|
|
71
|
+
}
|
|
72
|
+
/** a new `[r, g, b, a]` array, each 0-255 */
|
|
73
|
+
get rgba() {
|
|
74
|
+
return [
|
|
75
|
+
this._r,
|
|
76
|
+
this._g,
|
|
77
|
+
this._b,
|
|
78
|
+
this._a
|
|
79
|
+
];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Creates a color from a hex string (as accepted by {@link Color.hexStr}) or from channel
|
|
83
|
+
* values, each rounded and clamped to 0-255 like the channel setters. An invalid string is
|
|
84
|
+
* logged and gives opaque white. Defaults to opaque white.
|
|
85
|
+
*/
|
|
86
|
+
constructor(r = 255, g = 255, b = 255, a = 255) {
|
|
87
|
+
if (typeof r === "string") {
|
|
88
|
+
const rgba = Color._parse_hex_color_string(r);
|
|
89
|
+
if (rgba) [r, g, b, a] = rgba;
|
|
90
|
+
else {
|
|
91
|
+
log(`Color: invalid color string: ${r}. Should be a hexadecimal color string (i.e.: '#ffffff' or '#ffffffff'). Defaulting to opaque white.`);
|
|
92
|
+
r = g = b = a = 255;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
this.r = r;
|
|
96
|
+
this.g = g;
|
|
97
|
+
this.b = b;
|
|
98
|
+
this.a = a;
|
|
99
|
+
}
|
|
100
|
+
static _channel(value) {
|
|
101
|
+
return clamp(Math.round(value), 0, 255);
|
|
102
|
+
}
|
|
103
|
+
_to_hex() {
|
|
104
|
+
return `#${this.r.toString(16).padStart(2, "0")}${this.g.toString(16).padStart(2, "0")}${this.b.toString(16).padStart(2, "0")}${this.a.toString(16).padStart(2, "0")}`;
|
|
105
|
+
}
|
|
106
|
+
static _parse_hex_color_string(hex) {
|
|
107
|
+
let digits = /^#([a-f\d]{3,4}|[a-f\d]{6}|[a-f\d]{8})$/i.exec(hex)?.[1];
|
|
108
|
+
if (!digits) return void 0;
|
|
109
|
+
if (digits.length <= 4) digits = [...digits].map((d) => d + d).join("");
|
|
110
|
+
return [
|
|
111
|
+
parseInt(digits.slice(0, 2), 16),
|
|
112
|
+
parseInt(digits.slice(2, 4), 16),
|
|
113
|
+
parseInt(digits.slice(4, 6), 16),
|
|
114
|
+
digits.length === 8 ? parseInt(digits.slice(6, 8), 16) : 255
|
|
115
|
+
];
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Creates a color from hue, saturation and value, each in [0, 1]
|
|
119
|
+
* (hues outside [0, 1) wrap around).
|
|
120
|
+
* @param a alpha, 0-255 (default 255)
|
|
121
|
+
*/
|
|
122
|
+
static fromHsv(h, s, v, a = 255) {
|
|
123
|
+
let r = 0;
|
|
124
|
+
let g = 0;
|
|
125
|
+
let b = 0;
|
|
126
|
+
h = (h % 1 + 1) % 1;
|
|
127
|
+
let i = Math.floor(h * 6);
|
|
128
|
+
let f = h * 6 - i;
|
|
129
|
+
let p = v * (1 - s);
|
|
130
|
+
let q = v * (1 - f * s);
|
|
131
|
+
let t = v * (1 - (1 - f) * s);
|
|
132
|
+
switch (i % 6) {
|
|
133
|
+
case 0:
|
|
134
|
+
r = v, g = t, b = p;
|
|
135
|
+
break;
|
|
136
|
+
case 1:
|
|
137
|
+
r = q, g = v, b = p;
|
|
138
|
+
break;
|
|
139
|
+
case 2:
|
|
140
|
+
r = p, g = v, b = t;
|
|
141
|
+
break;
|
|
142
|
+
case 3:
|
|
143
|
+
r = p, g = q, b = v;
|
|
144
|
+
break;
|
|
145
|
+
case 4:
|
|
146
|
+
r = t, g = p, b = v;
|
|
147
|
+
break;
|
|
148
|
+
case 5: r = v, g = p, b = q;
|
|
149
|
+
}
|
|
150
|
+
return new Color(Math.round(r * 255), Math.round(g * 255), Math.round(b * 255), a);
|
|
151
|
+
}
|
|
152
|
+
/** new opaque black */
|
|
153
|
+
static get BLACK() {
|
|
154
|
+
return new Color(0, 0, 0, 255);
|
|
155
|
+
}
|
|
156
|
+
/** new opaque gray (128, 128, 128) */
|
|
157
|
+
static get GRAY() {
|
|
158
|
+
return new Color(128, 128, 128, 255);
|
|
159
|
+
}
|
|
160
|
+
/** new opaque white */
|
|
161
|
+
static get WHITE() {
|
|
162
|
+
return new Color(255, 255, 255, 255);
|
|
163
|
+
}
|
|
164
|
+
/** new opaque red (255, 0, 0) */
|
|
165
|
+
static get RED() {
|
|
166
|
+
return new Color(255, 0, 0, 255);
|
|
167
|
+
}
|
|
168
|
+
/** new opaque green (0, 255, 0) */
|
|
169
|
+
static get GREEN() {
|
|
170
|
+
return new Color(0, 255, 0, 255);
|
|
171
|
+
}
|
|
172
|
+
/** new opaque blue (0, 0, 255) */
|
|
173
|
+
static get BLUE() {
|
|
174
|
+
return new Color(0, 0, 255, 255);
|
|
175
|
+
}
|
|
176
|
+
/** new opaque yellow (255, 255, 0) */
|
|
177
|
+
static get YELLOW() {
|
|
178
|
+
return new Color(255, 255, 0, 255);
|
|
179
|
+
}
|
|
180
|
+
/** new opaque orange (255, 128, 0) */
|
|
181
|
+
static get ORANGE() {
|
|
182
|
+
return new Color(255, 128, 0, 255);
|
|
183
|
+
}
|
|
184
|
+
/** new opaque purple (128, 0, 128) */
|
|
185
|
+
static get PURPLE() {
|
|
186
|
+
return new Color(128, 0, 128, 255);
|
|
187
|
+
}
|
|
188
|
+
/** new opaque cyan (0, 255, 255) */
|
|
189
|
+
static get CYAN() {
|
|
190
|
+
return new Color(0, 255, 255, 255);
|
|
191
|
+
}
|
|
192
|
+
/** new opaque magenta (255, 0, 255) */
|
|
193
|
+
static get MAGENTA() {
|
|
194
|
+
return new Color(255, 0, 255, 255);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Creates a color from any CSS color string (e.g. `'rebeccapurple'`, `'rgb(0 0 0 / 50%)'`) by
|
|
198
|
+
* letting the browser compute it. Needs a DOM: it briefly appends an element to
|
|
199
|
+
* `document.body`. An unrecognised name is logged and gives {@link Color.TRANSPARENT}.
|
|
200
|
+
*/
|
|
201
|
+
static fromName(name) {
|
|
202
|
+
const element = document.createElement("div");
|
|
203
|
+
element.style.color = name;
|
|
204
|
+
document.body.appendChild(element);
|
|
205
|
+
const computedColor = getComputedStyle(element).color;
|
|
206
|
+
document.body.removeChild(element);
|
|
207
|
+
const rgba = computedColor.match(/[\d.]+/g)?.map(Number);
|
|
208
|
+
if (element.style.color && rgba && (rgba.length === 3 || rgba.length === 4)) {
|
|
209
|
+
const [r, g, b, a = 1] = rgba;
|
|
210
|
+
const color = new Color(r, g, b);
|
|
211
|
+
color.a = a * 255;
|
|
212
|
+
return color;
|
|
213
|
+
} else {
|
|
214
|
+
log(`Color: invalid CSS color name: ${name}. Defaulting to transparent.`);
|
|
215
|
+
return Color.TRANSPARENT;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/** new fully transparent black (0, 0, 0, 0) */
|
|
219
|
+
static get TRANSPARENT() {
|
|
220
|
+
return new Color(0, 0, 0, 0);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
//#endregion
|
|
224
|
+
export { Color };
|
package/dist/css/main.css
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//#region src/disposable.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Collects cleanup functions (e.g. the unsubscribe functions returned by {@link Observable.subscribe}
|
|
4
|
+
* and {@link Signal.subscribe}) and runs them all at once.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const subscriptions = new Disposable();
|
|
8
|
+
*
|
|
9
|
+
* subscriptions.add(health.subscribe(updateHealthBar));
|
|
10
|
+
* subscriptions.add(onLoaded.subscribe(uploadToGpu));
|
|
11
|
+
*
|
|
12
|
+
* subscriptions.dispose(); // later
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @category Events & Timing
|
|
16
|
+
*/
|
|
17
|
+
export declare class Disposable {
|
|
18
|
+
private readonly disposers;
|
|
19
|
+
/** Adds a cleanup function to run on {@link Disposable.dispose}. */
|
|
20
|
+
add(disposer: () => void): void;
|
|
21
|
+
/**
|
|
22
|
+
* Runs and removes every cleanup function, in reverse order of adding. The instance can be
|
|
23
|
+
* reused afterwards.
|
|
24
|
+
*/
|
|
25
|
+
dispose(): void;
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
//#region src/disposable.ts
|
|
2
|
+
/**
|
|
3
|
+
* Collects cleanup functions (e.g. the unsubscribe functions returned by {@link Observable.subscribe}
|
|
4
|
+
* and {@link Signal.subscribe}) and runs them all at once.
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* const subscriptions = new Disposable();
|
|
8
|
+
*
|
|
9
|
+
* subscriptions.add(health.subscribe(updateHealthBar));
|
|
10
|
+
* subscriptions.add(onLoaded.subscribe(uploadToGpu));
|
|
11
|
+
*
|
|
12
|
+
* subscriptions.dispose(); // later
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @category Events & Timing
|
|
16
|
+
*/
|
|
17
|
+
var Disposable = class {
|
|
18
|
+
disposers = [];
|
|
19
|
+
/** Adds a cleanup function to run on {@link Disposable.dispose}. */
|
|
20
|
+
add(disposer) {
|
|
21
|
+
this.disposers.push(disposer);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Runs and removes every cleanup function, in reverse order of adding. The instance can be
|
|
25
|
+
* reused afterwards.
|
|
26
|
+
*/
|
|
27
|
+
dispose() {
|
|
28
|
+
while (this.disposers.length) this.disposers.pop()?.();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
//#endregion
|
|
32
|
+
export { Disposable };
|