BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

tint.js 12.1 KiB Download Raw
1import { MATERIAL_TIER_ORDER, materialTintColor, GatherMaterialId } from "../definitions/materialTiers.js";
2import { itemDefinition } from "../definitions/items.js";
3import { magicElementTint } from "../definitions/attackTypes.js";
4
5/**
6 * Artist convention for tintable pixels (source sprites):
7 * - Tintable when R ≈ B and G < R (magenta family).
8 * - |R − B| may be up to {@link MAGENTA_CHANNEL_TOLERANCE} (AA / export drift).
9 * - Lightness L = R (= B): (50,0,50) dark … (255,0,255) max light.
10 * - G > 0 (still G < R) desaturates toward the tint's own gray (not white),
11 * so dark target colours stay dark: mix(tint, vec3(luma(tint)), G/R) * L.
12 * - Otherwise leave RGB as-is. World shaders apply darkness after this pass.
13 *
14 * Dual / triple channel ({@link applyDualChannelTintToImageData}):
15 * - Dominant red (R > G,B) with G ≈ B: shaft; lightness = R.
16 * - Dominant blue (B > R,G) with R ≈ G: tip; lightness = B.
17 * - Dominant green (G > R,B) with R ≈ B: coat; lightness = G.
18 * - Equal non-zero secondaries lighten the assigned tint toward white.
19 * - If the primary wins but the other two channels differ, leave the pixel as-is.
20 * - Neutral / white (fletching): neither channel dominates — RGB unchanged.
21 */
22
23const SECONDARY_EQUAL_TOL = 0.04;
24
25/** Mask strength when a paint channel dominates with equal secondaries (matches world dual-tint shader). */
26export function channelDominanceMask (r, g, b, channel)
27{
28 const [o1, o2] = channelSecondaries(r, g, b, channel);
29 if (Math.abs(o1 - o2) > SECONDARY_EQUAL_TOL)
30 { return 0; }
31 if (channel === "shaft")
32 { return clamp01((r - Math.max(g, b)) * 1.75); }
33 if (channel === "coat")
34 { return clamp01((g - Math.max(r, b)) * 1.75); }
35 return clamp01((b - Math.max(r, g)) * 1.75);
36}
37
38function channelLightness (r, g, b, channel)
39{
40 if (channel === "shaft")
41 { return r; }
42 if (channel === "coat")
43 { return g; }
44 return b;
45}
46
47function channelSecondaries (r, g, b, channel)
48{
49 if (channel === "shaft")
50 { return [g, b]; }
51 if (channel === "coat")
52 { return [r, b]; }
53 return [r, g];
54}
55
56/**
57 * Tint pixels where `channel` dominates with equal secondaries, preserving lightness.
58 * Equal non-zero secondaries → blend assigned tint toward white.
59 */
60export function channelTintedRgb (r, g, b, channel, tintRgb)
61{
62 const mask = channelDominanceMask(r, g, b, channel);
63 if (mask <= 0)
64 { return null; }
65 const L = channelLightness(r, g, b, channel);
66 const [o1] = channelSecondaries(r, g, b, channel);
67 const whiteBlend = L > 0 ? Math.min(1, Math.max(0, o1 / L)) : 0;
68 const tint = normalizeRgb(tintRgb);
69 return {
70 r: clamp01(mix(tint.r, 1, whiteBlend) * L),
71 g: clamp01(mix(tint.g, 1, whiteBlend) * L),
72 b: clamp01(mix(tint.b, 1, whiteBlend) * L),
73 mask
74 };
75}
76
77/** Shaft + tip (+ optional coat) channel tints. */
78export function dualChannelTintedRgb (r, g, b, shaftTint, tipTint, coatTint = null)
79{
80 let rgb = { r, g, b };
81 if (coatTint)
82 {
83 const coat = channelTintedRgb(r, g, b, "coat", coatTint);
84 if (coat)
85 {
86 rgb = {
87 r: mix(r, coat.r, coat.mask),
88 g: mix(g, coat.g, coat.mask),
89 b: mix(b, coat.b, coat.mask)
90 };
91 }
92 }
93 const tip = channelTintedRgb(r, g, b, "tip", tipTint);
94 if (tip)
95 {
96 rgb = {
97 r: mix(rgb.r, tip.r, tip.mask),
98 g: mix(rgb.g, tip.g, tip.mask),
99 b: mix(rgb.b, tip.b, tip.mask)
100 };
101 }
102 const shaft = channelTintedRgb(r, g, b, "shaft", shaftTint);
103 if (shaft)
104 {
105 rgb = {
106 r: mix(rgb.r, shaft.r, shaft.mask),
107 g: mix(rgb.g, shaft.g, shaft.mask),
108 b: mix(rgb.b, shaft.b, shaft.mask)
109 };
110 }
111 return rgb;
112}
113
114/** Mutate ImageData: red = shaft, blue = tip, optional green = coat. */
115export function applyDualChannelTintToImageData (imageData, { shaft, tip, coat = null })
116{
117 const data = imageData.data;
118 const shaftTint = normalizeRgb(shaft);
119 const tipTint = normalizeRgb(tip);
120 const coatTint = coat ? normalizeRgb(coat) : null;
121 for (let i = 0; i < data.length; i += 4)
122 {
123 if (data[i + 3] < 8)
124 { continue; }
125 const r = data[i] / 255;
126 const g = data[i + 1] / 255;
127 const b = data[i + 2] / 255;
128 const next = dualChannelTintedRgb(r, g, b, shaftTint, tipTint, coatTint);
129 data[i] = Math.round(next.r * 255);
130 data[i + 1] = Math.round(next.g * 255);
131 data[i + 2] = Math.round(next.b * 255);
132 }
133 return imageData;
134}
135
136/** Dual shaft/tip tier tints for assembled arrows, hafted tools, and bows (`dualTintFromTier`).
137 * Red channel = shaft (wood); blue channel = tip/string (metal or cloth). */
138export function resolveItemDualTint (stackOrDef, def = null)
139{
140 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
141 const definition = def
142 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
143 ?? null;
144 if (!definition?.dualTintFromTier)
145 { return null; }
146
147 // Tools / arrows: metal primary tier + matching wood shaft (red = wood, blue = metal).
148 if (definition.woodTier)
149 {
150 const metalTier = definition.tier ?? definition.equipment?.tier ?? null;
151 const woodTier = definition.woodTier;
152 if (!metalTier || !woodTier)
153 { return null; }
154 const dual = {
155 shaft: tierTintColor(woodTier),
156 tip: tierTintColor(metalTier)
157 };
158 if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element)
159 { dual.coat = magicElementTint(stack.instance.element); }
160 return dual;
161 }
162
163 // Swords: red = metal, blue = leather wrap.
164 if (definition.leatherTier)
165 {
166 const metalTier = definition.tier ?? definition.equipment?.tier ?? null;
167 const leatherTier = definition.leatherTier;
168 if (!metalTier || !leatherTier)
169 { return null; }
170 return {
171 shaft: tierTintColor(metalTier),
172 tip: tierTintColor(leatherTier)
173 };
174 }
175
176 // Magic staff: wood shaft (red) + gem tip (blue).
177 if (definition.gemTier)
178 {
179 const woodTier = definition.tier ?? definition.equipment?.tier ?? null;
180 const gemTier = definition.gemTier;
181 if (!woodTier || !gemTier)
182 { return null; }
183 return {
184 shaft: tierTintColor(woodTier),
185 tip: tierTintColor(gemTier)
186 };
187 }
188
189 // Strung bow: wood frame + cloth string.
190 if (definition.clothTier)
191 {
192 const woodTier = definition.tier ?? null;
193 const clothTier = definition.clothTier;
194 if (!woodTier || !clothTier)
195 { return null; }
196 return {
197 shaft: tierTintColor(woodTier),
198 tip: tierTintColor(clothTier)
199 };
200 }
201
202 // Unstrung bow (and similar): wood on red only; tip unused in art.
203 const woodTier = definition.tier ?? definition.equipment?.tier ?? null;
204 if (!woodTier)
205 { return null; }
206 const wood = tierTintColor(woodTier);
207 return { shaft: wood, tip: wood };
208}
209
210/** Max |R − B| in 0–255 channel units for near-magenta pixels. */
211export const MAGENTA_CHANNEL_TOLERANCE = 2;
212
213/** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */
214export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255;
215
216/** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */
217export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js";
218
219export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 })
220{
221 if (!color)
222 { return { r: fallback.r, g: fallback.g, b: fallback.b }; }
223 return {
224 r: clamp01(color.r ?? fallback.r),
225 g: clamp01(color.g ?? fallback.g),
226 b: clamp01(color.b ?? fallback.b)
227 };
228}
229
230export function tierTintColor (tier, { ore = false } = {})
231{
232 return normalizeRgb(materialTintColor(tier, { ore }));
233}
234
235/** Max towards grey between charcoal and the wood tier colour (shared ash sprite). */
236export function ashTintColor (woodTier)
237{
238 const wood = tierTintColor(woodTier);
239 const dark = { r: 0.22, g: 0.22, b: 0.22 };
240 return {
241 r: dark.r * 0.65 + wood.r * 0.35,
242 g: dark.g * 0.65 + wood.g * 0.35,
243 b: dark.b * 0.65 + wood.b * 0.35
244 };
245}
246
247/**
248 * Resolve tint for an inventory/ground stack or item def.
249 * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides).
250 */
251export function resolveItemTint (stackOrDef, def = null)
252{
253 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
254 const definition = def
255 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
256 ?? null;
257 if (stack?.instance?.kind === "potion" && stack.instance.color)
258 { return normalizeRgb(stack.instance.color); }
259 if (stack?.instance?.kind === "dye" && stack.instance.color)
260 { return normalizeRgb(stack.instance.color); }
261 if (stack?.instance?.kind === "flower" && stack.instance.color)
262 { return normalizeRgb(stack.instance.color); }
263 if (stack?.instance?.dyeColor)
264 { return normalizeRgb(stack.instance.dyeColor); }
265 if (definition?.tint)
266 { return normalizeRgb(definition.tint); }
267 if (definition?.dualTintFromTier && !definition?.iconTintFromTier)
268 { return null; }
269 if (definition?.tintFromTier === "ash" && definition.tier)
270 { return ashTintColor(definition.tier); }
271 const tier = resolveTintTier(definition);
272 if (tier)
273 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
274 return null;
275}
276
277export function resolveEquipTint (itemIdOrDef, instance = null)
278{
279 const definition = typeof itemIdOrDef === "number"
280 ? itemDefinition(itemIdOrDef)
281 : itemIdOrDef;
282 if (!definition)
283 { return null; }
284 if (instance?.dyeColor)
285 { return normalizeRgb(instance.dyeColor); }
286 if (definition?.tint)
287 { return normalizeRgb(definition.tint); }
288 // Dual icon art may still use a magenta equip sprite (`equipment.tintFromTier`).
289 if (definition?.dualTintFromTier && !definition?.equipment?.tintFromTier)
290 { return null; }
291 if (definition?.tintFromTier === "ash" && definition.tier)
292 { return ashTintColor(definition.tier); }
293 const tier = resolveTintTier(definition);
294 if (tier)
295 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
296 return null;
297}
298
299/** Dual tint for worn overlays. Null when equip art is single-channel magenta. */
300export function resolveEquipDualTint (stackOrDef, def = null)
301{
302 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
303 const definition = def
304 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
305 ?? null;
306 if (definition?.equipment?.tintFromTier)
307 { return null; }
308 return resolveItemDualTint(stackOrDef, definition);
309}
310
311function isOreItemDefinition (definition)
312{
313 return definition?.defaultState === "ore";
314}
315
316function resolveTintTier (definition)
317{
318 if (!definition)
319 { return null; }
320 if (definition.tintFromTier === "ash")
321 { return null; }
322 if (typeof definition.tintFromTier === "string")
323 { return definition.tintFromTier; }
324 if (definition.tintFromTier === true)
325 { return definition.equipment?.tier ?? definition.tier ?? null; }
326 if (definition.equipment?.tintFromTier)
327 { return definition.equipment.tier ?? null; }
328 return null;
329}
330
331/** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */
332export function magentaTintedRgb (r, g, b, tintRgb)
333{
334 if (!isMagentaTintable(r, g, b))
335 { return null; }
336 const L = r;
337 const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0;
338 const tint = normalizeRgb(tintRgb);
339 const tintLuma = luminance(tint.r, tint.g, tint.b);
340 return {
341 r: clamp01(mix(tint.r, tintLuma, gBlend) * L),
342 g: clamp01(mix(tint.g, tintLuma, gBlend) * L),
343 b: clamp01(mix(tint.b, tintLuma, gBlend) * L)
344 };
345}
346
347export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE)
348{
349 // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs.
350 const R = Math.round(r * 255);
351 const G = Math.round(g * 255);
352 const B = Math.round(b * 255);
353 return Math.abs(R - B) <= tolerance && G < R;
354}
355
356/** Mutate ImageData in place with magenta→tint (alpha unchanged). */
357export function applyMagentaTintToImageData (imageData, tintRgb)
358{
359 const data = imageData.data;
360 const tint = normalizeRgb(tintRgb);
361 for (let i = 0; i < data.length; i += 4)
362 {
363 if (data[i + 3] < 8)
364 { continue; }
365 const r = data[i] / 255;
366 const g = data[i + 1] / 255;
367 const b = data[i + 2] / 255;
368 const next = magentaTintedRgb(r, g, b, tint);
369 if (!next)
370 { continue; }
371 data[i] = Math.round(next.r * 255);
372 data[i + 1] = Math.round(next.g * 255);
373 data[i + 2] = Math.round(next.b * 255);
374 }
375 return imageData;
376}
377
378export function listTierTintPreview ()
379{
380 return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) }));
381}
382
383export { GatherMaterialId };
384
385function clamp01 (n)
386{
387 return Math.min(1, Math.max(0, Number(n) || 0));
388}
389
390function mix (a, b, t)
391{
392 return a + (b - a) * t;
393}
394
395/** Rec. 709 luminance for desaturating toward the tint's own gray. */
396function luminance (r, g, b)
397{
398 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
399}