BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

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