BlueGit

timfa questward

Files

questward / shared / render / tint.js

tint.js

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