| 1 | import { MaterialTier, MATERIAL_TIER_ORDER, MATERIAL_TINT_COLOR, GatherMaterialId } 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 | * - Lightness L = R (= B): (50,0,50) dark … (255,0,255) max light. |
| 8 | * - G > 0 (still G < R) desaturates toward white/gray: mix(tint, white, G/R) * L. |
| 9 | * - Otherwise leave RGB as-is. World shaders apply darkness after this pass. |
| 10 | */ |
| 11 | |
| 12 | export const MAGENTA_CHANNEL_EPS = 1 / 255; |
| 13 | |
| 14 | /** @deprecated Prefer MATERIAL_TINT_COLOR — kept as alias for call sites. */ |
| 15 | export const TIER_TINT_COLOR = MATERIAL_TINT_COLOR; |
| 16 | |
| 17 | export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 }) |
| 18 | { |
| 19 | if (!color) |
| 20 | { return { r: fallback.r, g: fallback.g, b: fallback.b }; } |
| 21 | return { |
| 22 | r: clamp01(color.r ?? fallback.r), |
| 23 | g: clamp01(color.g ?? fallback.g), |
| 24 | b: clamp01(color.b ?? fallback.b) |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | export function tierTintColor (tier) |
| 29 | { |
| 30 | return normalizeRgb(MATERIAL_TINT_COLOR[tier] ?? MATERIAL_TINT_COLOR[MaterialTier.bronze]); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Resolve tint for an inventory/ground stack or item def. |
| 35 | * Priority: potion instance.color → def.tint → tintFromTier + tier. |
| 36 | */ |
| 37 | export function resolveItemTint (stackOrDef, def = null) |
| 38 | { |
| 39 | const stack = stackOrDef?.itemId != null ? stackOrDef : null; |
| 40 | const definition = def |
| 41 | ?? (stack ? itemDefinition(stack.itemId) : stackOrDef) |
| 42 | ?? null; |
| 43 | if (stack?.instance?.kind === "potion" && stack.instance.color) |
| 44 | { return normalizeRgb(stack.instance.color); } |
| 45 | if (definition?.tint) |
| 46 | { return normalizeRgb(definition.tint); } |
| 47 | const tier = resolveTintTier(definition); |
| 48 | if (tier) |
| 49 | { return tierTintColor(tier); } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | export function resolveEquipTint (itemIdOrDef) |
| 54 | { |
| 55 | const definition = typeof itemIdOrDef === "number" |
| 56 | ? itemDefinition(itemIdOrDef) |
| 57 | : itemIdOrDef; |
| 58 | if (!definition) |
| 59 | { return null; } |
| 60 | if (definition.tint) |
| 61 | { return normalizeRgb(definition.tint); } |
| 62 | const tier = resolveTintTier(definition); |
| 63 | if (tier) |
| 64 | { return tierTintColor(tier); } |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | function resolveTintTier (definition) |
| 69 | { |
| 70 | if (!definition) |
| 71 | { return null; } |
| 72 | if (typeof definition.tintFromTier === "string") |
| 73 | { return definition.tintFromTier; } |
| 74 | if (definition.tintFromTier === true) |
| 75 | { return definition.equipment?.tier ?? definition.tier ?? null; } |
| 76 | if (definition.equipment?.tintFromTier) |
| 77 | { return definition.equipment.tier ?? null; } |
| 78 | return null; |
| 79 | } |
| 80 | |
| 81 | /** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */ |
| 82 | export function magentaTintedRgb (r, g, b, tintRgb) |
| 83 | { |
| 84 | if (!isMagentaTintable(r, g, b)) |
| 85 | { return null; } |
| 86 | const L = r; |
| 87 | const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0; |
| 88 | const tint = normalizeRgb(tintRgb); |
| 89 | return { |
| 90 | r: clamp01(mix(tint.r, 1, gBlend) * L), |
| 91 | g: clamp01(mix(tint.g, 1, gBlend) * L), |
| 92 | b: clamp01(mix(tint.b, 1, gBlend) * L) |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | export function isMagentaTintable (r, g, b, eps = MAGENTA_CHANNEL_EPS) |
| 97 | { |
| 98 | return Math.abs(r - b) <= eps && g < r - eps; |
| 99 | } |
| 100 | |
| 101 | /** Mutate ImageData in place with magenta→tint (alpha unchanged). */ |
| 102 | export function applyMagentaTintToImageData (imageData, tintRgb) |
| 103 | { |
| 104 | const data = imageData.data; |
| 105 | const tint = normalizeRgb(tintRgb); |
| 106 | for (let i = 0; i < data.length; i += 4) |
| 107 | { |
| 108 | if (data[i + 3] < 8) |
| 109 | { continue; } |
| 110 | const r = data[i] / 255; |
| 111 | const g = data[i + 1] / 255; |
| 112 | const b = data[i + 2] / 255; |
| 113 | const next = magentaTintedRgb(r, g, b, tint); |
| 114 | if (!next) |
| 115 | { continue; } |
| 116 | data[i] = Math.round(next.r * 255); |
| 117 | data[i + 1] = Math.round(next.g * 255); |
| 118 | data[i + 2] = Math.round(next.b * 255); |
| 119 | } |
| 120 | return imageData; |
| 121 | } |
| 122 | |
| 123 | export function listTierTintPreview () |
| 124 | { |
| 125 | return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) })); |
| 126 | } |
| 127 | |
| 128 | export { GatherMaterialId }; |
| 129 | |
| 130 | function clamp01 (n) |
| 131 | { |
| 132 | return Math.min(1, Math.max(0, Number(n) || 0)); |
| 133 | } |
| 134 | |
| 135 | function mix (a, b, t) |
| 136 | { |
| 137 | return a + (b - a) * t; |
| 138 | } |