BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

tint.js 4.9 KiB Download Raw
1import { MATERIAL_TIER_ORDER, materialTintColor, GatherMaterialId } from "../definitions/materialTiers.js";
2import { itemDefinition } from "../definitions/items.js";
3
4/**
5 * Artist convention for tintable pixels (source sprites):
6 * - Tintable when R ≈ B and G < R (magenta family).
7 * - |R − B| may be up to {@link MAGENTA_CHANNEL_TOLERANCE} (AA / export drift).
8 * - Lightness L = R (= B): (50,0,50) dark … (255,0,255) max light.
9 * - G > 0 (still G < R) desaturates toward the tint's own gray (not white),
10 * so dark target colours stay dark: mix(tint, vec3(luma(tint)), G/R) * L.
11 * - Otherwise leave RGB as-is. World shaders apply darkness after this pass.
12 */
13
14/** Max |R − B| in 0–255 channel units for near-magenta pixels. */
15export const MAGENTA_CHANNEL_TOLERANCE = 2;
16
17/** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */
18export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255;
19
20/** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */
21export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js";
22
23export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 })
24{
25 if (!color)
26 { return { r: fallback.r, g: fallback.g, b: fallback.b }; }
27 return {
28 r: clamp01(color.r ?? fallback.r),
29 g: clamp01(color.g ?? fallback.g),
30 b: clamp01(color.b ?? fallback.b)
31 };
32}
33
34export function tierTintColor (tier, { ore = false } = {})
35{
36 return normalizeRgb(materialTintColor(tier, { ore }));
37}
38
39/**
40 * Resolve tint for an inventory/ground stack or item def.
41 * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides).
42 */
43export function resolveItemTint (stackOrDef, def = null)
44{
45 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
46 const definition = def
47 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
48 ?? null;
49 if (stack?.instance?.kind === "potion" && stack.instance.color)
50 { return normalizeRgb(stack.instance.color); }
51 if (definition?.tint)
52 { return normalizeRgb(definition.tint); }
53 const tier = resolveTintTier(definition);
54 if (tier)
55 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
56 return null;
57}
58
59export function resolveEquipTint (itemIdOrDef)
60{
61 const definition = typeof itemIdOrDef === "number"
62 ? itemDefinition(itemIdOrDef)
63 : itemIdOrDef;
64 if (!definition)
65 { return null; }
66 if (definition.tint)
67 { return normalizeRgb(definition.tint); }
68 const tier = resolveTintTier(definition);
69 if (tier)
70 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
71 return null;
72}
73
74function isOreItemDefinition (definition)
75{
76 return definition?.defaultState === "ore";
77}
78
79function resolveTintTier (definition)
80{
81 if (!definition)
82 { return null; }
83 if (typeof definition.tintFromTier === "string")
84 { return definition.tintFromTier; }
85 if (definition.tintFromTier === true)
86 { return definition.equipment?.tier ?? definition.tier ?? null; }
87 if (definition.equipment?.tintFromTier)
88 { return definition.equipment.tier ?? null; }
89 return null;
90}
91
92/** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */
93export function magentaTintedRgb (r, g, b, tintRgb)
94{
95 if (!isMagentaTintable(r, g, b))
96 { return null; }
97 const L = r;
98 const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0;
99 const tint = normalizeRgb(tintRgb);
100 const tintLuma = luminance(tint.r, tint.g, tint.b);
101 return {
102 r: clamp01(mix(tint.r, tintLuma, gBlend) * L),
103 g: clamp01(mix(tint.g, tintLuma, gBlend) * L),
104 b: clamp01(mix(tint.b, tintLuma, gBlend) * L)
105 };
106}
107
108export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE)
109{
110 // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs.
111 const R = Math.round(r * 255);
112 const G = Math.round(g * 255);
113 const B = Math.round(b * 255);
114 return Math.abs(R - B) <= tolerance && G < R;
115}
116
117/** Mutate ImageData in place with magenta→tint (alpha unchanged). */
118export function applyMagentaTintToImageData (imageData, tintRgb)
119{
120 const data = imageData.data;
121 const tint = normalizeRgb(tintRgb);
122 for (let i = 0; i < data.length; i += 4)
123 {
124 if (data[i + 3] < 8)
125 { continue; }
126 const r = data[i] / 255;
127 const g = data[i + 1] / 255;
128 const b = data[i + 2] / 255;
129 const next = magentaTintedRgb(r, g, b, tint);
130 if (!next)
131 { continue; }
132 data[i] = Math.round(next.r * 255);
133 data[i + 1] = Math.round(next.g * 255);
134 data[i + 2] = Math.round(next.b * 255);
135 }
136 return imageData;
137}
138
139export function listTierTintPreview ()
140{
141 return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) }));
142}
143
144export { GatherMaterialId };
145
146function clamp01 (n)
147{
148 return Math.min(1, Math.max(0, Number(n) || 0));
149}
150
151function mix (a, b, t)
152{
153 return a + (b - a) * t;
154}
155
156/** Rec. 709 luminance for desaturating toward the tint's own gray. */
157function luminance (r, g, b)
158{
159 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
160}