BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

tint.js 12.8 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) lightens the assigned tint toward white for shiny highlights:
11 * mix(tint, vec3(1), 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 // Leather + cloth (red = leather, blue = cloth trim).
148 if (definition.dualTintLeatherCloth)
149 {
150 const leatherTier = definition.leatherTier ?? null;
151 const clothTier = definition.clothTier ?? null;
152 if (!leatherTier || !clothTier)
153 { return null; }
154 return {
155 shaft: tierTintColor(leatherTier),
156 tip: clothChannelTint(definition, stack, clothTier)
157 };
158 }
159
160 // Tools / arrows: metal primary tier + matching wood shaft (red = wood, blue = metal).
161 if (definition.woodTier)
162 {
163 const metalTier = definition.tier ?? definition.equipment?.tier ?? null;
164 const woodTier = definition.woodTier;
165 if (!metalTier || !woodTier)
166 { return null; }
167 const dual = {
168 shaft: tierTintColor(woodTier),
169 tip: tierTintColor(metalTier)
170 };
171 if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element)
172 { dual.coat = magicElementTint(stack.instance.element); }
173 return dual;
174 }
175
176 // Swords: red = metal, blue = leather wrap.
177 if (definition.leatherTier)
178 {
179 const metalTier = definition.tier ?? definition.equipment?.tier ?? null;
180 const leatherTier = definition.leatherTier;
181 if (!metalTier || !leatherTier)
182 { return null; }
183 return {
184 shaft: tierTintColor(metalTier),
185 tip: tierTintColor(leatherTier)
186 };
187 }
188
189 // Magic staff: wood shaft (red) + gem tip (blue).
190 if (definition.gemTier)
191 {
192 const woodTier = definition.tier ?? definition.equipment?.tier ?? null;
193 const gemTier = definition.gemTier;
194 if (!woodTier || !gemTier)
195 { return null; }
196 return {
197 shaft: tierTintColor(woodTier),
198 tip: tierTintColor(gemTier)
199 };
200 }
201
202 // Strung bow: wood frame + cloth string.
203 if (definition.clothTier)
204 {
205 const woodTier = definition.tier ?? null;
206 const clothTier = definition.clothTier;
207 if (!woodTier || !clothTier)
208 { return null; }
209 return {
210 shaft: tierTintColor(woodTier),
211 tip: tierTintColor(clothTier)
212 };
213 }
214
215 // Unstrung bow (and similar): wood on red only; tip unused in art.
216 const woodTier = definition.tier ?? definition.equipment?.tier ?? null;
217 if (!woodTier)
218 { return null; }
219 const wood = tierTintColor(woodTier);
220 return { shaft: wood, tip: wood };
221}
222
223/** Max |R − B| in 0–255 channel units for near-magenta pixels. */
224export const MAGENTA_CHANNEL_TOLERANCE = 2;
225
226/** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */
227export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255;
228
229/** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */
230export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js";
231
232export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 })
233{
234 if (!color)
235 { return { r: fallback.r, g: fallback.g, b: fallback.b }; }
236 return {
237 r: clamp01(color.r ?? fallback.r),
238 g: clamp01(color.g ?? fallback.g),
239 b: clamp01(color.b ?? fallback.b)
240 };
241}
242
243export function tierTintColor (tier, { ore = false } = {})
244{
245 return normalizeRgb(materialTintColor(tier, { ore }));
246}
247
248/** Max towards grey between charcoal and the wood tier colour (shared ash sprite). */
249export function ashTintColor (woodTier)
250{
251 const wood = tierTintColor(woodTier);
252 const dark = { r: 0.22, g: 0.22, b: 0.22 };
253 return {
254 r: dark.r * 0.65 + wood.r * 0.35,
255 g: dark.g * 0.65 + wood.g * 0.35,
256 b: dark.b * 0.65 + wood.b * 0.35
257 };
258}
259
260/**
261 * Resolve tint for an inventory/ground stack or item def.
262 * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides).
263 */
264export function resolveItemTint (stackOrDef, def = null)
265{
266 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
267 const definition = def
268 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
269 ?? null;
270 if (stack?.instance?.kind === "potion" && stack.instance.color)
271 { return normalizeRgb(stack.instance.color); }
272 if (stack?.instance?.kind === "dye" && stack.instance.color)
273 { return normalizeRgb(stack.instance.color); }
274 if (stack?.instance?.kind === "flower" && stack.instance.color)
275 { return normalizeRgb(stack.instance.color); }
276 if (stack?.instance?.dyeColor && !definition?.dualTintLeatherCloth)
277 { return normalizeRgb(stack.instance.dyeColor); }
278 if (definition?.tint)
279 { return normalizeRgb(definition.tint); }
280 if (definition?.dualTintFromTier && !definition?.iconTintFromTier)
281 { return null; }
282 if (definition?.tintFromTier === "ash" && definition.tier)
283 { return ashTintColor(definition.tier); }
284 const tier = resolveTintTier(definition);
285 if (tier)
286 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
287 return null;
288}
289
290export function resolveEquipTint (itemIdOrDef, instance = null)
291{
292 const definition = typeof itemIdOrDef === "number"
293 ? itemDefinition(itemIdOrDef)
294 : itemIdOrDef;
295 if (!definition)
296 { return null; }
297 if (instance?.dyeColor && !definition?.dualTintLeatherCloth)
298 { return normalizeRgb(instance.dyeColor); }
299 if (definition?.tint)
300 { return normalizeRgb(definition.tint); }
301 // Dual worn art wins; magenta-only equip (e.g. plant clippers) still uses single tint.
302 if (definition?.dualTintFromTier && !equipUsesMagentaOnly(definition))
303 { return null; }
304 if (definition?.tintFromTier === "ash" && definition.tier)
305 { return ashTintColor(definition.tier); }
306 const tier = resolveTintTier(definition);
307 if (tier)
308 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
309 return null;
310}
311
312/** Dual tint for worn overlays. Null when equip art is single-channel magenta. */
313export function resolveEquipDualTint (stackOrDef, def = null)
314{
315 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
316 const definition = def
317 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
318 ?? null;
319 if (equipUsesMagentaOnly(definition))
320 { return null; }
321 return resolveItemDualTint(stackOrDef, definition);
322}
323
324/** Magenta equip sprite with dual bag icon (grips hidden in hand). */
325function equipUsesMagentaOnly (definition)
326{
327 return !!definition?.equipment?.tintFromTier && !definition?.equipment?.dualTintFromTier;
328}
329
330function clothChannelTint (definition, stack, clothTier)
331{
332 if (stack?.instance?.dyeColor)
333 { return normalizeRgb(stack.instance.dyeColor); }
334 return tierTintColor(clothTier);
335}
336
337function isOreItemDefinition (definition)
338{
339 return definition?.defaultState === "ore";
340}
341
342function resolveTintTier (definition)
343{
344 if (!definition)
345 { return null; }
346 if (definition.tintFromTier === "ash")
347 { return null; }
348 if (typeof definition.tintFromTier === "string")
349 { return definition.tintFromTier; }
350 if (definition.tintFromTier === true)
351 { return definition.equipment?.tier ?? definition.tier ?? null; }
352 if (definition.equipment?.tintFromTier)
353 { return definition.equipment.tier ?? null; }
354 return null;
355}
356
357/** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */
358export function magentaTintedRgb (r, g, b, tintRgb)
359{
360 if (!isMagentaTintable(r, g, b))
361 { return null; }
362 const L = r;
363 const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0;
364 const tint = normalizeRgb(tintRgb);
365 return {
366 r: clamp01(mix(tint.r, 1, gBlend) * L),
367 g: clamp01(mix(tint.g, 1, gBlend) * L),
368 b: clamp01(mix(tint.b, 1, gBlend) * L)
369 };
370}
371
372export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE)
373{
374 // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs.
375 const R = Math.round(r * 255);
376 const G = Math.round(g * 255);
377 const B = Math.round(b * 255);
378 return Math.abs(R - B) <= tolerance && G < R;
379}
380
381/** Mutate ImageData in place with magenta→tint (alpha unchanged). */
382export function applyMagentaTintToImageData (imageData, tintRgb)
383{
384 const data = imageData.data;
385 const tint = normalizeRgb(tintRgb);
386 for (let i = 0; i < data.length; i += 4)
387 {
388 if (data[i + 3] < 8)
389 { continue; }
390 const r = data[i] / 255;
391 const g = data[i + 1] / 255;
392 const b = data[i + 2] / 255;
393 const next = magentaTintedRgb(r, g, b, tint);
394 if (!next)
395 { continue; }
396 data[i] = Math.round(next.r * 255);
397 data[i + 1] = Math.round(next.g * 255);
398 data[i + 2] = Math.round(next.b * 255);
399 }
400 return imageData;
401}
402
403export function listTierTintPreview ()
404{
405 return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) }));
406}
407
408export { GatherMaterialId };
409
410function clamp01 (n)
411{
412 return Math.min(1, Math.max(0, Number(n) || 0));
413}
414
415function mix (a, b, t)
416{
417 return a + (b - a) * t;
418}
419
420/** Rec. 709 luminance for desaturating toward the tint's own gray. */
421function luminance (r, g, b)
422{
423 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
424}