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) 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 / triple channel ({@link applyDualChannelTintToImageData}): * - Dominant red (R > G,B): shaft / wood / metal blade; lightness = R. * - Dominant blue (B > R,G): tip / leather / string; lightness = B. * - Dominant green (G > R,B): coating (tipped arrows); lightness = G. * - When one channel is highest and the other two are equal and non-zero, * that is a lighter tint of the assigned colour (blend toward white) — * not a mix of multiple assigned colours. * - Neutral / white (fletching): neither channel dominates — RGB unchanged. */ const SECONDARY_EQUAL_TOL = 0.04; /** 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); } 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, preserving channel lightness. * Equal non-dominant channels → blend assigned tint toward white. * Unequal → keep legacy soft desat via the lesser secondary (compat with dual art). */ 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, o2] = channelSecondaries(r, g, b, channel); const equalSeconds = Math.abs(o1 - o2) <= SECONDARY_EQUAL_TOL; const whiteBlend = L > 0 && equalSeconds ? Math.min(1, Math.max(0, o1 / L)) : (L > 0 ? Math.min(1, Math.max(0, Math.min(o1, o2) / L)) : 0); const tint = normalizeRgb(tintRgb); const toward = equalSeconds ? { r: 1, g: 1, b: 1 } : (() => { const tintLuma = luminance(tint.r, tint.g, tint.b); return { r: tintLuma, g: tintLuma, b: tintLuma }; })(); return { r: clamp01(mix(tint.r, toward.r, whiteBlend) * L), g: clamp01(mix(tint.g, toward.g, whiteBlend) * L), b: clamp01(mix(tint.b, toward.b, 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; } // 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) { 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) { return normalizeRgb(instance.dyeColor); } if (definition?.tint) { return normalizeRgb(definition.tint); } if (definition?.dualTintFromTier) { 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; } 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); 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; }