import { MATERIAL_TIER_ORDER, materialTintColor, GatherMaterialId } from "../definitions/materialTiers.js"; import { itemDefinition } from "../definitions/items.js"; /** * Artist convention for tintable pixels (source sprites): * - Tintable when R ≈ B and G < R (magenta family). * - |R − B| may be up to {@link MAGENTA_CHANNEL_TOLERANCE} (AA / export drift). * - Lightness L = R (= B): (50,0,50) dark … (255,0,255) max light. * - G > 0 (still G < R) desaturates toward the tint's own gray (not white), * so dark target colours stay dark: mix(tint, vec3(luma(tint)), G/R) * L. * - Otherwise leave RGB as-is. World shaders apply darkness after this pass. * * Dual-channel arrows / tools / bows ({@link applyDualChannelTintToImageData}): * - Dominant red (R > G,B): shaft / wood / handle mask; lightness = R; tint with shaft colour. * - Dominant blue (B > R,G): tip / metal / string mask; lightness = B; tint with tip colour. * - Neutral / white (fletching): neither channel dominates — RGB unchanged. */ /** Mask strength when a paint channel dominates (matches world dual-tint shader). */ export function channelDominanceMask (r, g, b, channel) { if (channel === "shaft") { return clamp01((r - Math.max(g, b)) * 1.75); } return clamp01((b - Math.max(r, g)) * 1.75); } /** Tint pixels where `channel` dominates, preserving channel lightness. */ export function channelTintedRgb (r, g, b, channel, tintRgb) { const mask = channelDominanceMask(r, g, b, channel); if (mask <= 0) { return null; } const L = channel === "shaft" ? r : b; const gBlend = L > 0 ? Math.min(1, Math.max(0, g / L)) : 0; const tint = normalizeRgb(tintRgb); const tintLuma = luminance(tint.r, tint.g, tint.b); return { r: clamp01(mix(tint.r, tintLuma, gBlend) * L), g: clamp01(mix(tint.g, tintLuma, gBlend) * L), b: clamp01(mix(tint.b, tintLuma, gBlend) * L), mask }; } /** Shaft + tip channel tints for full-arrow sprites. */ export function dualChannelTintedRgb (r, g, b, shaftTint, tipTint) { let rgb = { r, g, b }; const tip = channelTintedRgb(r, g, b, "tip", tipTint); if (tip) { rgb = { r: mix(r, tip.r, tip.mask), g: mix(g, tip.g, tip.mask), b: mix(b, tip.b, tip.mask) }; } const shaft = channelTintedRgb(r, g, b, "shaft", shaftTint); if (shaft) { rgb = { r: mix(rgb.r, shaft.r, shaft.mask), g: mix(rgb.g, shaft.g, shaft.mask), b: mix(rgb.b, shaft.b, shaft.mask) }; } return rgb; } /** Mutate ImageData: red channel = shaft, blue = tip, white unchanged. */ export function applyDualChannelTintToImageData (imageData, { shaft, tip }) { const data = imageData.data; const shaftTint = normalizeRgb(shaft); const tipTint = normalizeRgb(tip); for (let i = 0; i < data.length; i += 4) { if (data[i + 3] < 8) { continue; } const r = data[i] / 255; const g = data[i + 1] / 255; const b = data[i + 2] / 255; const next = dualChannelTintedRgb(r, g, b, shaftTint, tipTint); data[i] = Math.round(next.r * 255); data[i + 1] = Math.round(next.g * 255); data[i + 2] = Math.round(next.b * 255); } return imageData; } /** Dual shaft/tip tier tints for assembled arrows, hafted tools, and bows (`dualTintFromTier`). * Red channel = shaft (wood); blue channel = tip/string (metal or cloth). */ export function resolveItemDualTint (stackOrDef, def = null) { const stack = stackOrDef?.itemId != null ? stackOrDef : null; const definition = def ?? (stack ? itemDefinition(stack.itemId) : stackOrDef) ?? null; if (!definition?.dualTintFromTier) { return null; } // Tools / arrows: metal primary tier + matching wood shaft. if (definition.woodTier) { const metalTier = definition.tier ?? definition.equipment?.tier ?? null; const woodTier = definition.woodTier; if (!metalTier || !woodTier) { return null; } return { shaft: tierTintColor(woodTier), tip: tierTintColor(metalTier) }; } // Strung bow: wood frame + cloth string. if (definition.clothTier) { const woodTier = definition.tier ?? null; const clothTier = definition.clothTier; if (!woodTier || !clothTier) { return null; } return { shaft: tierTintColor(woodTier), tip: tierTintColor(clothTier) }; } // Unstrung bow (and similar): wood on red only; tip unused in art. const woodTier = definition.tier ?? definition.equipment?.tier ?? null; if (!woodTier) { return null; } const wood = tierTintColor(woodTier); return { shaft: wood, tip: wood }; } /** Max |R − B| in 0–255 channel units for near-magenta pixels. */ export const MAGENTA_CHANNEL_TOLERANCE = 2; /** @deprecated Float form of tolerance; prefer {@link MAGENTA_CHANNEL_TOLERANCE}. */ export const MAGENTA_CHANNEL_EPS = MAGENTA_CHANNEL_TOLERANCE / 255; /** @deprecated Prefer materialTintColor / MATERIAL_TINT_COLOR — kept for call sites. */ export { MATERIAL_TINT_COLOR as TIER_TINT_COLOR } from "../definitions/materialTiers.js"; export function normalizeRgb (color, fallback = { r: 0.7, g: 0.7, b: 0.7 }) { if (!color) { return { r: fallback.r, g: fallback.g, b: fallback.b }; } return { r: clamp01(color.r ?? fallback.r), g: clamp01(color.g ?? fallback.g), b: clamp01(color.b ?? fallback.b) }; } export function tierTintColor (tier, { ore = false } = {}) { return normalizeRgb(materialTintColor(tier, { ore })); } /** * Resolve tint for an inventory/ground stack or item def. * Priority: potion instance.color → def.tint → tintFromTier + tier (ore items use ore overrides). */ export function resolveItemTint (stackOrDef, def = null) { const stack = stackOrDef?.itemId != null ? stackOrDef : null; const definition = def ?? (stack ? itemDefinition(stack.itemId) : stackOrDef) ?? null; if (stack?.instance?.kind === "potion" && stack.instance.color) { return normalizeRgb(stack.instance.color); } if (stack?.instance?.kind === "dye" && stack.instance.color) { return normalizeRgb(stack.instance.color); } if (stack?.instance?.kind === "flower" && stack.instance.color) { return normalizeRgb(stack.instance.color); } if (stack?.instance?.dyeColor) { return normalizeRgb(stack.instance.dyeColor); } if (definition?.tint) { return normalizeRgb(definition.tint); } if (definition?.dualTintFromTier) { return null; } const tier = resolveTintTier(definition); if (tier) { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } return null; } export function resolveEquipTint (itemIdOrDef, instance = null) { const definition = typeof itemIdOrDef === "number" ? itemDefinition(itemIdOrDef) : itemIdOrDef; if (!definition) { return null; } if (instance?.dyeColor) { return normalizeRgb(instance.dyeColor); } if (definition?.tint) { return normalizeRgb(definition.tint); } if (definition?.dualTintFromTier) { return null; } const tier = resolveTintTier(definition); if (tier) { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } return null; } function isOreItemDefinition (definition) { return definition?.defaultState === "ore"; } function resolveTintTier (definition) { if (!definition) { return null; } if (typeof definition.tintFromTier === "string") { return definition.tintFromTier; } if (definition.tintFromTier === true) { return definition.equipment?.tier ?? definition.tier ?? null; } if (definition.equipment?.tintFromTier) { return definition.equipment.tier ?? null; } return null; } /** Pure pixel transform (channels 0–1). Returns null if pixel is not tintable. */ export function magentaTintedRgb (r, g, b, tintRgb) { if (!isMagentaTintable(r, g, b)) { return null; } const L = r; const gBlend = r > 0 ? Math.min(1, Math.max(0, g / r)) : 0; const tint = normalizeRgb(tintRgb); const tintLuma = luminance(tint.r, tint.g, tint.b); return { r: clamp01(mix(tint.r, tintLuma, gBlend) * L), g: clamp01(mix(tint.g, tintLuma, gBlend) * L), b: clamp01(mix(tint.b, tintLuma, gBlend) * L) }; } export function isMagentaTintable (r, g, b, tolerance = MAGENTA_CHANNEL_TOLERANCE) { // Compare in 0–255 ints — float |r-b| vs k/255 fails for some R=B±2 pairs. const R = Math.round(r * 255); const G = Math.round(g * 255); const B = Math.round(b * 255); return Math.abs(R - B) <= tolerance && G < R; } /** Mutate ImageData in place with magenta→tint (alpha unchanged). */ export function applyMagentaTintToImageData (imageData, tintRgb) { const data = imageData.data; const tint = normalizeRgb(tintRgb); for (let i = 0; i < data.length; i += 4) { if (data[i + 3] < 8) { continue; } const r = data[i] / 255; const g = data[i + 1] / 255; const b = data[i + 2] / 255; const next = magentaTintedRgb(r, g, b, tint); if (!next) { continue; } data[i] = Math.round(next.r * 255); data[i + 1] = Math.round(next.g * 255); data[i + 2] = Math.round(next.b * 255); } return imageData; } export function listTierTintPreview () { return MATERIAL_TIER_ORDER.map((tier) => ({ tier, color: tierTintColor(tier) })); } export { GatherMaterialId }; function clamp01 (n) { return Math.min(1, Math.max(0, Number(n) || 0)); } function mix (a, b, t) { return a + (b - a) * t; } /** Rec. 709 luminance for desaturating toward the tint's own gray. */ function luminance (r, g, b) { return 0.2126 * r + 0.7152 * g + 0.0722 * b; }