BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

tint.js 9.6 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/** Halfway between very dark grey and the wood tier colour (shared ash sprite). */
162export function ashTintColor (woodTier)
163{
164 const wood = tierTintColor(woodTier);
165 const dark = { r: 0.12, g: 0.12, b: 0.12 };
166 return {
167 r: (dark.r + wood.r) * 0.5,
168 g: (dark.g + wood.g) * 0.5,
169 b: (dark.b + wood.b) * 0.5
170 };
171}
172
173/**
174 * Resolve tint for an inventory/ground stack or item def.
175 * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides).
176 */
177export function resolveItemTint (stackOrDef, def = null)
178{
179 const stack = stackOrDef?.itemId != null ? stackOrDef : null;
180 const definition = def
181 ?? (stack ? itemDefinition(stack.itemId) : stackOrDef)
182 ?? null;
183 if (stack?.instance?.kind === "potion" && stack.instance.color)
184 { return normalizeRgb(stack.instance.color); }
185 if (stack?.instance?.kind === "dye" && stack.instance.color)
186 { return normalizeRgb(stack.instance.color); }
187 if (stack?.instance?.kind === "flower" && stack.instance.color)
188 { return normalizeRgb(stack.instance.color); }
189 if (stack?.instance?.dyeColor)
190 { return normalizeRgb(stack.instance.dyeColor); }
191 if (definition?.tint)
192 { return normalizeRgb(definition.tint); }
193 if (definition?.dualTintFromTier)
194 { return null; }
195 if (definition?.tintFromTier === "ash" && definition.tier)
196 { return ashTintColor(definition.tier); }
197 const tier = resolveTintTier(definition);
198 if (tier)
199 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
200 return null;
201}
202
203export function resolveEquipTint (itemIdOrDef, instance = null)
204{
205 const definition = typeof itemIdOrDef === "number"
206 ? itemDefinition(itemIdOrDef)
207 : itemIdOrDef;
208 if (!definition)
209 { return null; }
210 if (instance?.dyeColor)
211 { return normalizeRgb(instance.dyeColor); }
212 if (definition?.tint)
213 { return normalizeRgb(definition.tint); }
214 if (definition?.dualTintFromTier)
215 { return null; }
216 if (definition?.tintFromTier === "ash" && definition.tier)
217 { return ashTintColor(definition.tier); }
218 const tier = resolveTintTier(definition);
219 if (tier)
220 { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); }
221 return null;
222}
223
224function isOreItemDefinition (definition)
225{
226 return definition?.defaultState === "ore";
227}
228
229function resolveTintTier (definition)
230{
231 if (!definition)
232 { return null; }
233 if (definition.tintFromTier === "ash")
234 { return null; }
235 if (typeof definition.tintFromTier === "string")
236 { return definition.tintFromTier; }
237 if (definition.tintFromTier === true)
238 { return definition.equipment?.tier ?? definition.tier ?? null; }
239 if (definition.equipment?.tintFromTier)
240 { return definition.equipment.tier ?? null; }
241 return null;
242}
243
244/** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */
245export function magentaTintedRgb (r, g, b, tintRgb)
246{
247 if (!isMagentaTintable(r, g, b))
248 { return null; }
249 const L = r;
250 const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0;
251 const tint = normalizeRgb(tintRgb);
252 const tintLuma = luminance(tint.r, tint.g, tint.b);
253 return {
254 r: clamp01(mix(tint.r, tintLuma, gBlend) * L),
255 g: clamp01(mix(tint.g, tintLuma, gBlend) * L),
256 b: clamp01(mix(tint.b, tintLuma, gBlend) * L)
257 };
258}
259
260export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE)
261{
262 // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs.
263 const R = Math.round(r * 255);
264 const G = Math.round(g * 255);
265 const B = Math.round(b * 255);
266 return Math.abs(R - B) <= tolerance && G < R;
267}
268
269/** Mutate ImageData in place with magenta→tint (alpha unchanged). */
270export function applyMagentaTintToImageData (imageData, tintRgb)
271{
272 const data = imageData.data;
273 const tint = normalizeRgb(tintRgb);
274 for (let i = 0; i < data.length; i += 4)
275 {
276 if (data[i + 3] < 8)
277 { continue; }
278 const r = data[i] / 255;
279 const g = data[i + 1] / 255;
280 const b = data[i + 2] / 255;
281 const next = magentaTintedRgb(r, g, b, tint);
282 if (!next)
283 { continue; }
284 data[i] = Math.round(next.r * 255);
285 data[i + 1] = Math.round(next.g * 255);
286 data[i + 2] = Math.round(next.b * 255);
287 }
288 return imageData;
289}
290
291export function listTierTintPreview ()
292{
293 return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) }));
294}
295
296export { GatherMaterialId };
297
298function clamp01 (n)
299{
300 return Math.min(1, Math.max(0, Number(n) || 0));
301}
302
303function mix (a, b, t)
304{
305 return a + (b - a) * t;
306}
307
308/** Rec. 709 luminance for desaturating toward the tint's own gray. */
309function luminance (r, g, b)
310{
311 return 0.2126 * r + 0.7152 * g + 0.0722 * b;
312}