BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

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