| 1 | import { MATERIAL_TIER_ORDER, materialTintColor, 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 | * - |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 | |
| 14 | /** Max |R − B| in 0–255 channel units for near-magenta pixels. */ |
| 15 | export const MAGENTA_CHANNEL_TOLERANCE = 2; |
| 16 | |
| 17 | /** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */ |
| 18 | export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255; |
| 19 | |
| 20 | /** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */ |
| 21 | export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js"; |
| 22 | |
| 23 | export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 }) |
| 24 | { |
| 25 | if (!color) |
| 26 | { return { r: fallback.r, g: fallback.g, b: fallback.b }; } |
| 27 | return { |
| 28 | r: clamp01(color.r ?? fallback.r), |
| 29 | g: clamp01(color.g ?? fallback.g), |
| 30 | b: clamp01(color.b ?? fallback.b) |
| 31 | }; |
| 32 | } |
| 33 | |
| 34 | export function tierTintColor (tier, { ore = false } = {}) |
| 35 | { |
| 36 | return normalizeRgb(materialTintColor(tier, { ore })); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Resolve tint for an inventory/ground stack or item def. |
| 41 | * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides). |
| 42 | */ |
| 43 | export function resolveItemTint (stackOrDef, def = null) |
| 44 | { |
| 45 | const stack = stackOrDef?.itemId != null ? stackOrDef : null; |
| 46 | const definition = def |
| 47 | ?? (stack ? itemDefinition(stack.itemId) : stackOrDef) |
| 48 | ?? null; |
| 49 | if (stack?.instance?.kind === "potion" && stack.instance.color) |
| 50 | { return normalizeRgb(stack.instance.color); } |
| 51 | if (stack?.instance?.kind === "dye" && stack.instance.color) |
| 52 | { return normalizeRgb(stack.instance.color); } |
| 53 | if (stack?.instance?.kind === "flower" && stack.instance.color) |
| 54 | { return normalizeRgb(stack.instance.color); } |
| 55 | if (stack?.instance?.dyeColor) |
| 56 | { return normalizeRgb(stack.instance.dyeColor); } |
| 57 | if (definition?.tint) |
| 58 | { return normalizeRgb(definition.tint); } |
| 59 | const tier = resolveTintTier(definition); |
| 60 | if (tier) |
| 61 | { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | export function resolveEquipTint (itemIdOrDef, instance = null) |
| 66 | { |
| 67 | const definition = typeof itemIdOrDef === "number" |
| 68 | ? itemDefinition(itemIdOrDef) |
| 69 | : itemIdOrDef; |
| 70 | if (!definition) |
| 71 | { return null; } |
| 72 | if (instance?.dyeColor) |
| 73 | { return normalizeRgb(instance.dyeColor); } |
| 74 | if (definition.tint) |
| 75 | { return normalizeRgb(definition.tint); } |
| 76 | const tier = resolveTintTier(definition); |
| 77 | if (tier) |
| 78 | { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } |
| 79 | return null; |
| 80 | } |
| 81 | |
| 82 | function isOreItemDefinition (definition) |
| 83 | { |
| 84 | return definition?.defaultState === "ore"; |
| 85 | } |
| 86 | |
| 87 | function resolveTintTier (definition) |
| 88 | { |
| 89 | if (!definition) |
| 90 | { return null; } |
| 91 | if (typeof definition.tintFromTier === "string") |
| 92 | { return definition.tintFromTier; } |
| 93 | if (definition.tintFromTier === true) |
| 94 | { return definition.equipment?.tier ?? definition.tier ?? null; } |
| 95 | if (definition.equipment?.tintFromTier) |
| 96 | { return definition.equipment.tier ?? null; } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */ |
| 101 | export function magentaTintedRgb (r, g, b, tintRgb) |
| 102 | { |
| 103 | if (!isMagentaTintable(r, g, b)) |
| 104 | { return null; } |
| 105 | const L = r; |
| 106 | const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0; |
| 107 | const tint = normalizeRgb(tintRgb); |
| 108 | const tintLuma = luminance(tint.r, tint.g, tint.b); |
| 109 | return { |
| 110 | r: clamp01(mix(tint.r, tintLuma, gBlend) * L), |
| 111 | g: clamp01(mix(tint.g, tintLuma, gBlend) * L), |
| 112 | b: clamp01(mix(tint.b, tintLuma, gBlend) * L) |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE) |
| 117 | { |
| 118 | // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs. |
| 119 | const R = Math.round(r * 255); |
| 120 | const G = Math.round(g * 255); |
| 121 | const B = Math.round(b * 255); |
| 122 | return Math.abs(R - B) <= tolerance && G < R; |
| 123 | } |
| 124 | |
| 125 | /** Mutate ImageData in place with magenta→tint (alpha unchanged). */ |
| 126 | export function applyMagentaTintToImageData (imageData, tintRgb) |
| 127 | { |
| 128 | const data = imageData.data; |
| 129 | const tint = normalizeRgb(tintRgb); |
| 130 | for (let i = 0; i < data.length; i += 4) |
| 131 | { |
| 132 | if (data[i + 3] < 8) |
| 133 | { continue; } |
| 134 | const r = data[i] / 255; |
| 135 | const g = data[i + 1] / 255; |
| 136 | const b = data[i + 2] / 255; |
| 137 | const next = magentaTintedRgb(r, g, b, tint); |
| 138 | if (!next) |
| 139 | { continue; } |
| 140 | data[i] = Math.round(next.r * 255); |
| 141 | data[i + 1] = Math.round(next.g * 255); |
| 142 | data[i + 2] = Math.round(next.b * 255); |
| 143 | } |
| 144 | return imageData; |
| 145 | } |
| 146 | |
| 147 | export function listTierTintPreview () |
| 148 | { |
| 149 | return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) })); |
| 150 | } |
| 151 | |
| 152 | export { GatherMaterialId }; |
| 153 | |
| 154 | function clamp01 (n) |
| 155 | { |
| 156 | return Math.min(1, Math.max(0, Number(n) || 0)); |
| 157 | } |
| 158 | |
| 159 | function mix (a, b, t) |
| 160 | { |
| 161 | return a + (b - a) * t; |
| 162 | } |
| 163 | |
| 164 | /** Rec. 709 luminance for desaturating toward the tint's own gray. */ |
| 165 | function luminance (r, g, b) |
| 166 | { |
| 167 | return 0.2126 * r + 0.7152 * g + 0.0722 * b; |
| 168 | } |