BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

tint.js 9.0 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 // Strung bow: wood frame + cloth string.
116 if (definition.clothTier)
117 {
118 const woodTier = definition.tier ?? null;
119 const clothTier = definition.clothTier;
120 if (!woodTier || !clothTier)
121 { return null; }
122 return {
123 shaft: tierTintColor(woodTier),
124 tip: tierTintColor(clothTier)
125 };
126 }
127
128 // Unstrung bow (and similar): wood on red only; tip unused in art.
129 const woodTier = definition.tier ?? definition.equipment?.tier ?? null;
130 if (!woodTier)
131 { return null; }
132 const wood = tierTintColor(woodTier);
133 return { shaft: wood, tip: wood };
134}
135
136/** Max |R − B| in 0–255 channel units for near-magenta pixels. */
137export const MAGENTA_CHANNEL_TOLERANCE = 2;
138
139/** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */
140export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255;
141
142/** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */
143export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js";
144
145export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 })
146{
147 if (!color)
148 { return { r: fallback.r, g: fallback.g, b: fallback.b }; }
149 return {
150 r: clamp01(color.r ?? fallback.r),
151 g: clamp01(color.g ?? fallback.g),
152 b: clamp01(color.b ?? fallback.b)
153 };
154}
155
156export function tierTintColor (tier, { ore = false } = {})
157{
158 return normalizeRgb(materialTintColor(tier, { ore }));
159}
160
161/**
162 * Resolve tint for an inventory/ground stack or item def.
163 * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides).
164 */
165export function resolveItemTint (stackOrDef, def = null)
166{
167 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
168 const definition = def
169 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
170 ?? null;
171 if (stack?.instance?.kind === "potion" && stack.instance.color)
172 { return normalizeRgb(stack.instance.color); }
173 if (stack?.instance?.kind === "dye" && stack.instance.color)
174 { return normalizeRgb(stack.instance.color); }
175 if (stack?.instance?.kind === "flower" && stack.instance.color)
176 { return normalizeRgb(stack.instance.color); }
177 if (stack?.instance?.dyeColor)
178 { return normalizeRgb(stack.instance.dyeColor); }
179 if (definition?.tint)
180 { return normalizeRgb(definition.tint); }
181 if (definition?.dualTintFromTier)
182 { return null; }
183 const tier = resolveTintTier(definition);
184 if (tier)
185 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
186 return null;
187}
188
189export function resolveEquipTint (itemIdOrDef, instance = null)
190{
191 const definition = typeof itemIdOrDef === "number"
192 ? itemDefinition(itemIdOrDef)
193 : itemIdOrDef;
194 if (!definition)
195 { return null; }
196 if (instance?.dyeColor)
197 { return normalizeRgb(instance.dyeColor); }
198 if (definition?.tint)
199 { return normalizeRgb(definition.tint); }
200 if (definition?.dualTintFromTier)
201 { return null; }
202 const tier = resolveTintTier(definition);
203 if (tier)
204 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
205 return null;
206}
207
208function isOreItemDefinition (definition)
209{
210 return definition?.defaultState === "ore";
211}
212
213function resolveTintTier (definition)
214{
215 if (!definition)
216 { return null; }
217 if (typeof definition.tintFromTier === "string")
218 { return definition.tintFromTier; }
219 if (definition.tintFromTier === true)
220 { return definition.equipment?.tier ?? definition.tier ?? null; }
221 if (definition.equipment?.tintFromTier)
222 { return definition.equipment.tier ?? null; }
223 return null;
224}
225
226/** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */
227export function magentaTintedRgb (r, g, b, tintRgb)
228{
229 if (!isMagentaTintable(r, g, b))
230 { return null; }
231 const L = r;
232 const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0;
233 const tint = normalizeRgb(tintRgb);
234 const tintLuma = luminance(tint.r, tint.g, tint.b);
235 return {
236 r: clamp01(mix(tint.r, tintLuma, gBlend) * L),
237 g: clamp01(mix(tint.g, tintLuma, gBlend) * L),
238 b: clamp01(mix(tint.b, tintLuma, gBlend) * L)
239 };
240}
241
242export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE)
243{
244 // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs.
245 const R = Math.round(r * 255);
246 const G = Math.round(g * 255);
247 const B = Math.round(b * 255);
248 return Math.abs(R - B) <= tolerance && G < R;
249}
250
251/** Mutate ImageData in place with magenta→tint (alpha unchanged). */
252export function applyMagentaTintToImageData (imageData, tintRgb)
253{
254 const data = imageData.data;
255 const tint = normalizeRgb(tintRgb);
256 for (let i = 0; i < data.length; i += 4)
257 {
258 if (data[i + 3] < 8)
259 { continue; }
260 const r = data[i] / 255;
261 const g = data[i + 1] / 255;
262 const b = data[i + 2] / 255;
263 const next = magentaTintedRgb(r, g, b, tint);
264 if (!next)
265 { continue; }
266 data[i] = Math.round(next.r * 255);
267 data[i + 1] = Math.round(next.g * 255);
268 data[i + 2] = Math.round(next.b * 255);
269 }
270 return imageData;
271}
272
273export function listTierTintPreview ()
274{
275 return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) }));
276}
277
278export { GatherMaterialId };
279
280function clamp01 (n)
281{
282 return Math.min(1, Math.max(0, Number(n) || 0));
283}
284
285function mix (a, b, t)
286{
287 return a + (b - a) * t;
288}
289
290/** Rec. 709 luminance for desaturating toward the tint's own gray. */
291function luminance (r, g, b)
292{
293 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
294}