BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

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