import { MATERIAL_TIER_ORDER, materialTintColor, GatherMaterialId } from "../definitions/materialTiers.js"; import { itemDefinition } from "../definitions/items.js"; import { magicElementTint } from "../definitions/attackTypes.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) lightens the assigned tint toward white for shiny highlights: * mix(tint, vec3(1), G/R) * L. * - Otherwise leave RGB as-is. World shaders apply darkness after this pass. * * Dual / triple channel ({@link applyDualChannelTintToImageData}): * - Dominant red (R > G,B) with G ≈ B: shaft; lightness = R. * - Dominant blue (B > R,G) with R ≈ G: tip; lightness = B. * - Dominant green (G > R,B) with R ≈ B: coat; lightness = G. * - Equal non-zero secondaries lighten the assigned tint toward white. * - If the primary wins but the other two channels differ, leave the pixel as-is. * - Neutral / white (fletching): neither channel dominates — RGB unchanged. */ const SECONDARY_EQUAL_TOL = 0.04; /** Mask strength when a paint channel dominates with equal secondaries (matches world dual-tint shader). */ export function channelDominanceMask (r, g, b, channel) { const [o1, o2] = channelSecondaries(r, g, b, channel); if (Math.abs(o1 - o2) > SECONDARY_EQUAL_TOL) { return 0; } if (channel === "shaft") { return clamp01((r - Math.max(g, b)) * 1.75); } if (channel === "coat") { return clamp01((g - Math.max(r, b)) * 1.75); } return clamp01((b - Math.max(r, g)) * 1.75); } function channelLightness (r, g, b, channel) { if (channel === "shaft") { return r; } if (channel === "coat") { return g; } return b; } function channelSecondaries (r, g, b, channel) { if (channel === "shaft") { return [g, b]; } if (channel === "coat") { return [r, b]; } return [r, g]; } /** * Tint pixels where `channel` dominates with equal secondaries, preserving lightness. * Equal non-zero secondaries → blend assigned tint toward white. */ export function channelTintedRgb (r, g, b, channel, tintRgb) { const mask = channelDominanceMask(r, g, b, channel); if (mask <= 0) { return null; } const L = channelLightness(r, g, b, channel); const [o1] = channelSecondaries(r, g, b, channel); const whiteBlend = L > 0 ? Math.min(1, Math.max(0, o1 / L)) : 0; const tint = normalizeRgb(tintRgb); return { r: clamp01(mix(tint.r, 1, whiteBlend) * L), g: clamp01(mix(tint.g, 1, whiteBlend) * L), b: clamp01(mix(tint.b, 1, whiteBlend) * L), mask }; } /** Shaft + tip (+ optional coat) channel tints. */ export function dualChannelTintedRgb (r, g, b, shaftTint, tipTint, coatTint = null) { let rgb = { r, g, b }; if (coatTint) { const coat = channelTintedRgb(r, g, b, "coat", coatTint); if (coat) { rgb = { r: mix(r, coat.r, coat.mask), g: mix(g, coat.g, coat.mask), b: mix(b, coat.b, coat.mask) }; } } const tip = channelTintedRgb(r, g, b, "tip", tipTint); if (tip) { rgb = { r: mix(rgb.r, tip.r, tip.mask), g: mix(rgb.g, tip.g, tip.mask), b: mix(rgb.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 = shaft, blue = tip, optional green = coat. */ export function applyDualChannelTintToImageData (imageData, { shaft, tip, coat = null }) { const data = imageData.data; const shaftTint = normalizeRgb(shaft); const tipTint = normalizeRgb(tip); const coatTint = coat ? normalizeRgb(coat) : null; 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, coatTint); 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; } // Leather + cloth (red = leather, blue = cloth trim). if (definition.dualTintLeatherCloth) { const leatherTier = definition.leatherTier ?? null; const clothTier = definition.clothTier ?? null; if (!leatherTier || !clothTier) { return null; } return { shaft: tierTintColor(leatherTier), tip: clothChannelTint(definition, stack, clothTier) }; } // Tools / arrows: metal primary tier + matching wood shaft (red = wood, blue = metal). if (definition.woodTier) { const metalTier = definition.tier ?? definition.equipment?.tier ?? null; const woodTier = definition.woodTier; if (!metalTier || !woodTier) { return null; } const dual = { shaft: tierTintColor(woodTier), tip: tierTintColor(metalTier) }; if (stack?.instance?.kind === "tipped-arrow" && stack.instance.element) { dual.coat = magicElementTint(stack.instance.element); } return dual; } // Swords: red = metal, blue = leather wrap. if (definition.leatherTier) { const metalTier = definition.tier ?? definition.equipment?.tier ?? null; const leatherTier = definition.leatherTier; if (!metalTier || !leatherTier) { return null; } return { shaft: tierTintColor(metalTier), tip: tierTintColor(leatherTier) }; } // Magic staff: wood shaft (red) + gem tip (blue). if (definition.gemTier) { const woodTier = definition.tier ?? definition.equipment?.tier ?? null; const gemTier = definition.gemTier; if (!woodTier || !gemTier) { return null; } return { shaft: tierTintColor(woodTier), tip: tierTintColor(gemTier) }; } // 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 })); } /** Max towards grey between charcoal and the wood tier colour (shared ash sprite). */ export function ashTintColor (woodTier) { const wood = tierTintColor(woodTier); const dark = { r: 0.22, g: 0.22, b: 0.22 }; return { r: dark.r * 0.65 + wood.r * 0.35, g: dark.g * 0.65 + wood.g * 0.35, b: dark.b * 0.65 + wood.b * 0.35 }; } /** * 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 && !definition?.dualTintLeatherCloth) { return normalizeRgb(stack.instance.dyeColor); } if (definition?.tint) { return normalizeRgb(definition.tint); } if (definition?.dualTintFromTier && !definition?.iconTintFromTier) { return null; } if (definition?.tintFromTier === "ash" && definition.tier) { return ashTintColor(definition.tier); } 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 && !definition?.dualTintLeatherCloth) { return normalizeRgb(instance.dyeColor); } if (definition?.tint) { return normalizeRgb(definition.tint); } // Dual worn art wins; magenta-only equip (e.g. plant clippers) still uses single tint. if (definition?.dualTintFromTier && !equipUsesMagentaOnly(definition)) { return null; } if (definition?.tintFromTier === "ash" && definition.tier) { return ashTintColor(definition.tier); } const tier = resolveTintTier(definition); if (tier) { return tierTintColor(tier, { ore: isOreItemDefinition(definition) }); } return null; } /** Dual tint for worn overlays. Null when equip art is single-channel magenta. */ export function resolveEquipDualTint (stackOrDef, def = null) { const stack = stackOrDef?.itemId != null ? stackOrDef : null; const definition = def ?? (stack ? itemDefinition(stack.itemId) : stackOrDef) ?? null; if (equipUsesMagentaOnly(definition)) { return null; } return resolveItemDualTint(stackOrDef, definition); } /** Magenta equip sprite with dual bag icon (grips hidden in hand). */ function equipUsesMagentaOnly (definition) { return !!definition?.equipment?.tintFromTier && !definition?.equipment?.dualTintFromTier; } function clothChannelTint (definition, stack, clothTier) { if (stack?.instance?.dyeColor) { return normalizeRgb(stack.instance.dyeColor); } return tierTintColor(clothTier); } function isOreItemDefinition (definition) { return definition?.defaultState === "ore"; } function resolveTintTier (definition) { if (!definition) { return null; } if (definition.tintFromTier === "ash") { 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); return { r: clamp01(mix(tint.r, 1, gBlend) * L), g: clamp01(mix(tint.g, 1, gBlend) * L), b: clamp01(mix(tint.b, 1, 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; }