import test from "node:test"; import assert from "node:assert/strict"; import { isMagentaTintable, magentaTintedRgb, channelDominanceMask, dualChannelTintedRgb, tierTintColor, resolveItemTint, resolveItemDualTint } from "../../shared/render/tint.js"; import { MaterialTier, GatherMaterialId, hexToRgb01, MATERIAL_TINT_HEX, rgb } from "../../shared/definitions/materialTiers.js"; import { ItemId } from "../../shared/definitions/items.js"; import { RangedItemId } from "../../shared/definitions/rangedItems.js"; import { GemItemId } from "../../shared/definitions/gemItems.js"; test("magenta tintable when R==B and G < R", () => { assert.equal(isMagentaTintable(1, 0, 1), true); assert.equal(isMagentaTintable(50 / 255, 0, 50 / 255), true); assert.equal(isMagentaTintable(120 / 255, 50 / 255, 120 / 255), true); // Near-magenta drift (R = B ± 2) from AA / export — still tintable. assert.equal(isMagentaTintable(173 / 255, 67 / 255, 171 / 255), true); assert.equal(isMagentaTintable(87 / 255, 60 / 255, 85 / 255), true); assert.equal(isMagentaTintable(200 / 255, 100 / 255, 50 / 255), false); assert.equal(isMagentaTintable(0.5, 0.5, 0.5), false); }); test("magenta tint preserves lightness and desaturates with G", () => { const tint = { r: 0, g: 1, b: 0 }; const dark = magentaTintedRgb(50 / 255, 0, 50 / 255, tint); const light = magentaTintedRgb(1, 0, 1, tint); assert.ok(dark.g < light.g); const desat = magentaTintedRgb(120 / 255, 50 / 255, 120 / 255, tint); const pure = magentaTintedRgb(120 / 255, 0, 120 / 255, tint); assert.ok(desat.r > pure.r); }); test("dark target tints stay dark even on high-G magenta highlights", () => { const coal = { r: 23 / 255, g: 23 / 255, b: 24 / 255 }; const pure = magentaTintedRgb(1, 0, 1, coal); const highlight = magentaTintedRgb(1, 179 / 255, 1, coal); assert.ok(pure.r < 0.12); assert.ok(highlight.r < 0.15, `highlight washed out: ${highlight.r}`); assert.ok(Math.abs(highlight.r - pure.r) < 0.05); }); test("resolveItemTint uses potion instance color and tier tints", () => { const potionTint = resolveItemTint({ itemId: ItemId.potion, instance: { kind: "potion", color: { r: 0.25, g: 0.72, b: 0.35 } } }); assert.equal(potionTint.g, 0.72); const bronze = tierTintColor(MaterialTier.bronze); assert.ok(bronze.r > bronze.b); const barTint = resolveItemTint({ itemId: ItemId.bronzeBar }); assert.deepEqual(barTint, bronze); const copper = tierTintColor(GatherMaterialId.copper); const copperOre = tierTintColor(GatherMaterialId.copper, { ore: true }); assert.notDeepEqual(copperOre, copper); assert.deepEqual(resolveItemTint({ itemId: ItemId.copperOre }), copperOre); const iron = tierTintColor(MaterialTier.iron); const ironOre = tierTintColor(MaterialTier.iron, { ore: true }); assert.notDeepEqual(ironOre, iron); assert.deepEqual(resolveItemTint({ itemId: ItemId.ironOre }), ironOre); assert.deepEqual(resolveItemTint({ itemId: ItemId.ironBar }), iron); }); test("dual-channel arrow tints shaft red and tip blue, leaves white fletching", () => { assert.ok(channelDominanceMask(1, 0, 0, "shaft") > 0.9); assert.ok(channelDominanceMask(0, 0, 1, "tip") > 0.9); assert.equal(channelDominanceMask(1, 1, 1, "shaft"), 0); assert.equal(channelDominanceMask(1, 1, 1, "tip"), 0); const bronze = tierTintColor(MaterialTier.bronze); const pine = tierTintColor("pine"); const shaftOnly = dualChannelTintedRgb(0.8, 0, 0, pine, bronze); assert.ok(shaftOnly.r > 0.1); assert.ok(shaftOnly.r > shaftOnly.b); const tipOnly = dualChannelTintedRgb(0, 0, 0.8, pine, bronze); assert.ok(tipOnly.b > tipOnly.r || tipOnly.b > 0.05); const white = dualChannelTintedRgb(1, 1, 1, pine, bronze); assert.equal(white.r, 1); assert.equal(white.g, 1); assert.equal(white.b, 1); const dual = resolveItemDualTint({ itemId: RangedItemId.bronzeArrow }); assert.ok(dual?.shaft); assert.ok(dual?.tip); assert.equal(resolveItemTint({ itemId: RangedItemId.bronzeArrow }), null); }); test("hafted tools dual-tint wood handle and metal head", () => { const pickaxe = resolveItemDualTint({ itemId: ItemId.bronzePickaxe }); assert.ok(pickaxe?.shaft); assert.ok(pickaxe?.tip); assert.deepEqual(pickaxe.shaft, tierTintColor("pine")); assert.deepEqual(pickaxe.tip, tierTintColor(MaterialTier.bronze)); assert.equal(resolveItemTint({ itemId: ItemId.bronzePickaxe }), null); const ironAxe = resolveItemDualTint({ itemId: ItemId.ironAxe }); assert.deepEqual(ironAxe.shaft, tierTintColor("birch")); assert.deepEqual(ironAxe.tip, tierTintColor(MaterialTier.iron)); }); test("bows dual-tint wood frame and cloth string; unstrung wood only", () => { const bow = resolveItemDualTint({ itemId: RangedItemId.pineBow }); assert.deepEqual(bow.shaft, tierTintColor("pine")); assert.deepEqual(bow.tip, tierTintColor("cotton")); assert.equal(resolveItemTint({ itemId: RangedItemId.pineBow }), null); const unstrung = resolveItemDualTint({ itemId: RangedItemId.pineUnstrungBow }); assert.deepEqual(unstrung.shaft, tierTintColor("pine")); assert.deepEqual(unstrung.tip, tierTintColor("pine")); assert.equal(resolveItemTint({ itemId: RangedItemId.pineUnstrungBow }), null); }); test("staff dual-tints wood shaft and gem tip", () => { const staff = resolveItemDualTint({ itemId: GemItemId.pineStaff }); assert.deepEqual(staff.shaft, tierTintColor("pine")); assert.deepEqual(staff.tip, tierTintColor("tourmaline")); assert.equal(resolveItemTint({ itemId: GemItemId.pineStaff }), null); }); test("material tint hex converts to 0-1 rgb at load", () => { assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.bronze], rgb(184, 122, 71)); assert.deepEqual(hexToRgb01(0xB87A47), { r: 184 / 255, g: 122 / 255, b: 71 / 255 }); assert.deepEqual(hexToRgb01("#B87A47"), hexToRgb01(0xB87A47)); assert.deepEqual(hexToRgb01("#abc"), hexToRgb01("#AABBCC")); assert.deepEqual(tierTintColor(MaterialTier.bronze), rgb(184, 122, 71)); assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.mithril], rgb(3, 120, 255)); assert.deepEqual(hexToRgb01("rgb(3, 120, 255)"), rgb(3, 120, 255)); });