| 1 | import { ItemId, itemDefinition, itemAssetPathForStack } from "/shared/definitions/items.js"; |
| 2 | import { SPRITE_SIZE_PX } from "/shared/protocol/constants.js"; |
| 3 | import { |
| 4 | applyMagentaTintToImageData, |
| 5 | normalizeRgb, |
| 6 | resolveItemTint |
| 7 | } from "/shared/render/tint.js"; |
| 8 | |
| 9 | const bottleCache = new Map(); |
| 10 | const fillCache = new Map(); |
| 11 | const urlCache = new Map(); |
| 12 | const imageCache = new Map(); |
| 13 | |
| 14 | function loadImage (src) |
| 15 | { |
| 16 | if (imageCache.has(src)) |
| 17 | { return imageCache.get(src); } |
| 18 | const promise = new Promise((resolve, reject) => |
| 19 | { |
| 20 | const img = new Image(); |
| 21 | img.onload = () => resolve(img); |
| 22 | img.onerror = () => reject(new Error(`Failed to load ${src}`)); |
| 23 | img.src = src; |
| 24 | }); |
| 25 | imageCache.set(src, promise); |
| 26 | return promise; |
| 27 | } |
| 28 | |
| 29 | async function getBottle () |
| 30 | { |
| 31 | if (!bottleCache.has("bottle")) |
| 32 | { bottleCache.set("bottle", loadImage("/assets/items/glass-bottle/glass-bottle.png")); } |
| 33 | return bottleCache.get("bottle"); |
| 34 | } |
| 35 | |
| 36 | async function getFill (level) |
| 37 | { |
| 38 | const key = Math.max(1, Math.min(5, level | 0 || 4)); |
| 39 | if (!fillCache.has(key)) |
| 40 | { fillCache.set(key, loadImage(`/assets/items/potion/fill-${key}.png`)); } |
| 41 | return fillCache.get(key); |
| 42 | } |
| 43 | |
| 44 | function colorKey (color) |
| 45 | { |
| 46 | const c = normalizeRgb(color); |
| 47 | return `${Math.round(c.r * 255)},${Math.round(c.g * 255)},${Math.round(c.b * 255)}`; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Bottle + magenta fill mask → tinted potion icon (UI). |
| 52 | * Fill sprites use magenta lightness; bottle stays non-magenta. |
| 53 | */ |
| 54 | export async function potionIconDataUrl (instance) |
| 55 | { |
| 56 | const fillLevel = Math.max(1, Math.min(5, instance?.fillLevel | 0 || 4)); |
| 57 | const tint = normalizeRgb(instance?.color); |
| 58 | const key = `potion|${fillLevel}|${colorKey(tint)}`; |
| 59 | if (urlCache.has(key)) |
| 60 | { return urlCache.get(key); } |
| 61 | |
| 62 | const [bottle, fill] = await Promise.all([getBottle(), getFill(fillLevel)]); |
| 63 | const canvas = document.createElement("canvas"); |
| 64 | canvas.width = SPRITE_SIZE_PX; |
| 65 | canvas.height = SPRITE_SIZE_PX; |
| 66 | const ctx = canvas.getContext("2d"); |
| 67 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 68 | ctx.drawImage(fill, 0, 0); |
| 69 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 70 | applyMagentaTintToImageData(image, tint); |
| 71 | ctx.putImageData(image, 0, 0); |
| 72 | ctx.drawImage(bottle, 0, 0); |
| 73 | |
| 74 | const url = canvas.toDataURL("image/png"); |
| 75 | urlCache.set(key, url); |
| 76 | return url; |
| 77 | } |
| 78 | |
| 79 | /** Tint magenta pixels in an arbitrary item PNG for UI icons. */ |
| 80 | export async function tintedItemIconDataUrl (assetPath, tint) |
| 81 | { |
| 82 | const key = `item|${assetPath}|${colorKey(tint)}`; |
| 83 | if (urlCache.has(key)) |
| 84 | { return urlCache.get(key); } |
| 85 | const img = await loadImage(assetPath); |
| 86 | const canvas = document.createElement("canvas"); |
| 87 | canvas.width = img.width || SPRITE_SIZE_PX; |
| 88 | canvas.height = img.height || SPRITE_SIZE_PX; |
| 89 | const ctx = canvas.getContext("2d"); |
| 90 | ctx.clearRect(0, 0, canvas.width, canvas.height); |
| 91 | ctx.drawImage(img, 0, 0); |
| 92 | const image = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 93 | applyMagentaTintToImageData(image, tint); |
| 94 | ctx.putImageData(image, 0, 0); |
| 95 | const url = canvas.toDataURL("image/png"); |
| 96 | urlCache.set(key, url); |
| 97 | return url; |
| 98 | } |
| 99 | |
| 100 | export function isPotionStack (stack) |
| 101 | { |
| 102 | return stack?.itemId === ItemId.potion && stack.instance?.kind === "potion"; |
| 103 | } |
| 104 | |
| 105 | /** Set img.src to tinted icon when the stack/def has a tint; else fallback path. */ |
| 106 | export function applyItemIcon (img, stack, fallbackPath) |
| 107 | { |
| 108 | if (!img) |
| 109 | { return; } |
| 110 | img.src = fallbackPath; |
| 111 | const tint = resolveItemTint(stack, itemDefinition(stack?.itemId)); |
| 112 | if (!tint) |
| 113 | { return; } |
| 114 | const work = isPotionStack(stack) |
| 115 | ? potionIconDataUrl(stack.instance) |
| 116 | : tintedItemIconDataUrl(fallbackPath || itemAssetPathForStack(stack), tint); |
| 117 | work.then((url) => |
| 118 | { |
| 119 | img.src = url; |
| 120 | }).catch(() => |
| 121 | { |
| 122 | img.src = fallbackPath; |
| 123 | }); |
| 124 | } |