| 1 | import { MATERIAL_TIER_ORDER, materialTintColor, GatherMaterialId, woodTierForMetal } from "../definitions/materialTiers.js"; |
| 2 | import { 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 ({@link applyDualChannelTintToImageData}): |
| 14 | * - Dominant red (R > G,B): shaft / handle mask; lightness = R; tint with shaft colour. |
| 15 | * - Dominant blue (B > R,G): tip / metal 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). */ |
| 20 | export 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. */ |
| 28 | export 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. */ |
| 46 | export 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. */ |
| 71 | export 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 and hafted tools (`dualTintFromTier`). */ |
| 92 | export 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 ? woodTierForMetal(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. */ |
| 111 | export const MAGENTA_CHANNEL_TOLERANCE = 2; |
| 112 | |
| 113 | /** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */ |
| 114 | export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255; |
| 115 | |
| 116 | /** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */ |
| 117 | export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js"; |
| 118 | |
| 119 | export 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 | |
| 130 | export 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 | */ |
| 139 | export 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 | |
| 163 | export 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 | if (definition?.dualTintFromTier) |
| 175 | { return null; } |
| 176 | const tier = resolveTintTier(definition); |
| 177 | if (tier) |
| 178 | { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | function isOreItemDefinition (definition) |
| 183 | { |
| 184 | return definition?.defaultState === "ore"; |
| 185 | } |
| 186 | |
| 187 | function resolveTintTier (definition) |
| 188 | { |
| 189 | if (!definition) |
| 190 | { return null; } |
| 191 | if (typeof definition.tintFromTier === "string") |
| 192 | { return definition.tintFromTier; } |
| 193 | if (definition.tintFromTier === true) |
| 194 | { return definition.equipment?.tier ?? definition.tier ?? null; } |
| 195 | if (definition.equipment?.tintFromTier) |
| 196 | { return definition.equipment.tier ?? null; } |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | /** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */ |
| 201 | export function magentaTintedRgb (r, g, b, tintRgb) |
| 202 | { |
| 203 | if (!isMagentaTintable(r, g, b)) |
| 204 | { return null; } |
| 205 | const L = r; |
| 206 | const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0; |
| 207 | const tint = normalizeRgb(tintRgb); |
| 208 | const tintLuma = luminance(tint.r, tint.g, tint.b); |
| 209 | return { |
| 210 | r: clamp01(mix(tint.r, tintLuma, gBlend) * L), |
| 211 | g: clamp01(mix(tint.g, tintLuma, gBlend) * L), |
| 212 | b: clamp01(mix(tint.b, tintLuma, gBlend) * L) |
| 213 | }; |
| 214 | } |
| 215 | |
| 216 | export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE) |
| 217 | { |
| 218 | // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs. |
| 219 | const R = Math.round(r * 255); |
| 220 | const G = Math.round(g * 255); |
| 221 | const B = Math.round(b * 255); |
| 222 | return Math.abs(R - B) <= tolerance && G < R; |
| 223 | } |
| 224 | |
| 225 | /** Mutate ImageData in place with magenta→tint (alpha unchanged). */ |
| 226 | export function applyMagentaTintToImageData (imageData, tintRgb) |
| 227 | { |
| 228 | const data = imageData.data; |
| 229 | const tint = normalizeRgb(tintRgb); |
| 230 | for (let i = 0; i < data.length; i += 4) |
| 231 | { |
| 232 | if (data[i + 3] < 8) |
| 233 | { continue; } |
| 234 | const r = data[i] / 255; |
| 235 | const g = data[i + 1] / 255; |
| 236 | const b = data[i + 2] / 255; |
| 237 | const next = magentaTintedRgb(r, g, b, tint); |
| 238 | if (!next) |
| 239 | { continue; } |
| 240 | data[i] = Math.round(next.r * 255); |
| 241 | data[i + 1] = Math.round(next.g * 255); |
| 242 | data[i + 2] = Math.round(next.b * 255); |
| 243 | } |
| 244 | return imageData; |
| 245 | } |
| 246 | |
| 247 | export function listTierTintPreview () |
| 248 | { |
| 249 | return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) })); |
| 250 | } |
| 251 | |
| 252 | export { GatherMaterialId }; |
| 253 | |
| 254 | function clamp01 (n) |
| 255 | { |
| 256 | return Math.min(1, Math.max(0, Number(n) || 0)); |
| 257 | } |
| 258 | |
| 259 | function mix (a, b, t) |
| 260 | { |
| 261 | return a + (b - a) * t; |
| 262 | } |
| 263 | |
| 264 | /** Rec. 709 luminance for desaturating toward the tint's own gray. */ |
| 265 | function luminance (r, g, b) |
| 266 | { |
| 267 | return 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 268 | } |