BlueGit

timfa questward

Files

questward / shared / render / tileAverageColors.js

tileAverageColors.js

feat: add sprite average colours and an editor minimap
timfa project_85689633_bot_760517ee9267c86ff1d6c3a0237abf7d · pushed by timfa · 6 weeks ago
8a24eb51 History
tileAverageColors.js 2.7 KiB Download Raw
1/**
2 * Registry of average sprite colours for grounds and blocks.
3 *
4 * Filled by {@link installTileAverageColors} from a sprite catalog that already
5 * carries `averageColor` / `stateColors` (the server computes those from PNGs
6 * when it builds the catalog). Game code asks {@link groundAverageColor} or
7 * {@link blockAverageColor} rather than sampling textures itself.
8 *
9 * A missing state falls back to the type's default-state colour. A missing
10 * type returns null.
11 */
12
13const groundColors = new Map();
14const blockColors = new Map();
15
16/**
17 * @param {object | null | undefined} catalog
18 */
19export function installTileAverageColors (catalog)
20{
21 groundColors.clear();
22 blockColors.clear();
23 installGroup(groundColors, catalog?.grounds);
24 installGroup(blockColors, catalog?.blocks);
25}
26
27/**
28 * @param {number | string | null | undefined} typeId
29 * @param {string | null | undefined} [state]
30 * @returns {{ r: number, g: number, b: number } | null}
31 */
32export function groundAverageColor (typeId, state = null)
33{
34 return lookupAverageColor(groundColors, typeId, state);
35}
36
37/**
38 * @param {number | string | null | undefined} typeId
39 * @param {string | null | undefined} [state]
40 * @returns {{ r: number, g: number, b: number } | null}
41 */
42export function blockAverageColor (typeId, state = null)
43{
44 return lookupAverageColor(blockColors, typeId, state);
45}
46
47export { AVERAGE_COLOR_ALPHA_THRESHOLD } from "./averageSpriteColor.js";
48
49function installGroup (target, types)
50{
51 for (const type of Object.values(types ?? {}))
52 {
53 if (type?.typeId == null)
54 { continue; }
55 const stateColors = {};
56 for (const [state, color] of Object.entries(type.stateColors ?? {}))
57 {
58 const stored = freezeColor(color);
59 if (stored)
60 { stateColors[state] = stored; }
61 }
62 target.set(type.typeId, {
63 averageColor: freezeColor(type.averageColor) ?? firstColor(stateColors),
64 stateColors
65 });
66 }
67}
68
69function lookupAverageColor (groups, typeId, state)
70{
71 if (typeId == null)
72 { return null; }
73 const entry = groups.get(typeId);
74 if (!entry)
75 { return null; }
76 if (state != null && state !== "")
77 {
78 const specific = entry.stateColors[state];
79 if (specific)
80 { return specific; }
81 }
82 return entry.averageColor ?? null;
83}
84
85function firstColor (stateColors)
86{
87 for (const color of Object.values(stateColors))
88 {
89 if (color)
90 { return color; }
91 }
92 return null;
93}
94
95function freezeColor (color)
96{
97 if (!color)
98 { return null; }
99 const r = clampByte(color.r);
100 const g = clampByte(color.g);
101 const b = clampByte(color.b);
102 if (r == null || g == null || b == null)
103 { return null; }
104 return Object.freeze({ r, g, b });
105}
106
107function clampByte (value)
108{
109 const number = Number(value);
110 if (!Number.isFinite(number))
111 { return null; }
112 return Math.max(0, Math.min(255, Math.round(number)));
113}