| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | isMagentaTintable, |
| 5 | magentaTintedRgb, |
| 6 | channelDominanceMask, |
| 7 | dualChannelTintedRgb, |
| 8 | tierTintColor, |
| 9 | resolveItemTint, |
| 10 | resolveItemDualTint, |
| 11 | resolveEquipTint, |
| 12 | resolveEquipDualTint |
| 13 | } from "../../shared/render/tint.js"; |
| 14 | import { MaterialTier, GatherMaterialId, hexToRgb01, MATERIAL_TINT_HEX, rgb } from "../../shared/definitions/materialTiers.js"; |
| 15 | import { ItemId } from "../../shared/definitions/items.js"; |
| 16 | import { RangedItemId } from "../../shared/definitions/rangedItems.js"; |
| 17 | import { GemItemId } from "../../shared/definitions/gemItems.js"; |
| 18 | import { WizardItemId } from "../../shared/definitions/wizardItems.js"; |
| 19 | import { LeatherTier } from "../../shared/definitions/materialTiers.js"; |
| 20 | import { normalizeRgb } from "../../shared/render/tint.js"; |
| 21 | |
| 22 | test("magenta tintable when R==B and G < R", () => |
| 23 | { |
| 24 | assert.equal(isMagentaTintable(1, 0, 1), true); |
| 25 | assert.equal(isMagentaTintable(50 / 255, 0, 50 / 255), true); |
| 26 | assert.equal(isMagentaTintable(120 / 255, 50 / 255, 120 / 255), true); |
| 27 | // Near-magenta drift (R = B ± 2) from AA / export — still tintable. |
| 28 | assert.equal(isMagentaTintable(173 / 255, 67 / 255, 171 / 255), true); |
| 29 | assert.equal(isMagentaTintable(87 / 255, 60 / 255, 85 / 255), true); |
| 30 | assert.equal(isMagentaTintable(200 / 255, 100 / 255, 50 / 255), false); |
| 31 | assert.equal(isMagentaTintable(0.5, 0.5, 0.5), false); |
| 32 | }); |
| 33 | |
| 34 | test("magenta tint preserves lightness and highlights blend toward white", () => |
| 35 | { |
| 36 | const tint = { r: 0.2, g: 0.4, b: 0.8 }; |
| 37 | const dark = magentaTintedRgb(50 / 255, 0, 50 / 255, tint); |
| 38 | const light = magentaTintedRgb(1, 0, 1, tint); |
| 39 | assert.ok(dark.b < light.b); |
| 40 | const highlight = magentaTintedRgb(120 / 255, 50 / 255, 120 / 255, tint); |
| 41 | const pure = magentaTintedRgb(120 / 255, 0, 120 / 255, tint); |
| 42 | assert.ok(highlight.g > pure.g); |
| 43 | assert.ok(highlight.r > pure.r); |
| 44 | }); |
| 45 | |
| 46 | test("magenta G channel adds shiny white on dark tier tints", () => |
| 47 | { |
| 48 | const coal = { r: 23 / 255, g: 23 / 255, b: 24 / 255 }; |
| 49 | const pure = magentaTintedRgb(1, 0, 1, coal); |
| 50 | const highlight = magentaTintedRgb(1, 179 / 255, 1, coal); |
| 51 | assert.ok(pure.r < 0.12); |
| 52 | assert.ok(highlight.r > pure.r, "high-G magenta should shine toward white"); |
| 53 | assert.ok(highlight.g > pure.g); |
| 54 | }); |
| 55 | |
| 56 | test("resolveItemTint uses potion instance color and tier tints", () => |
| 57 | { |
| 58 | const potionTint = resolveItemTint({ |
| 59 | itemId: ItemId.potion, |
| 60 | instance: { kind: "potion", color: { r: 0.25, g: 0.72, b: 0.35 } } |
| 61 | }); |
| 62 | assert.equal(potionTint.g, 0.72); |
| 63 | const bronze = tierTintColor(MaterialTier.bronze); |
| 64 | assert.ok(bronze.r > bronze.b); |
| 65 | const barTint = resolveItemTint({ itemId: ItemId.bronzeBar }); |
| 66 | assert.deepEqual(barTint, bronze); |
| 67 | const copper = tierTintColor(GatherMaterialId.copper); |
| 68 | const copperOre = tierTintColor(GatherMaterialId.copper, { ore: true }); |
| 69 | assert.notDeepEqual(copperOre, copper); |
| 70 | assert.deepEqual(resolveItemTint({ itemId: ItemId.copperOre }), copperOre); |
| 71 | const iron = tierTintColor(MaterialTier.iron); |
| 72 | const ironOre = tierTintColor(MaterialTier.iron, { ore: true }); |
| 73 | assert.notDeepEqual(ironOre, iron); |
| 74 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironOre }), ironOre); |
| 75 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironBar }), iron); |
| 76 | }); |
| 77 | |
| 78 | test("dual-channel arrow tints shaft red and tip blue, leaves white fletching", () => |
| 79 | { |
| 80 | assert.ok(channelDominanceMask(1, 0, 0, "shaft") > 0.9); |
| 81 | assert.ok(channelDominanceMask(0, 0, 1, "tip") > 0.9); |
| 82 | assert.equal(channelDominanceMask(1, 1, 1, "shaft"), 0); |
| 83 | assert.equal(channelDominanceMask(1, 1, 1, "tip"), 0); |
| 84 | const bronze = tierTintColor(MaterialTier.bronze); |
| 85 | const pine = tierTintColor("pine"); |
| 86 | const shaftOnly = dualChannelTintedRgb(0.8, 0, 0, pine, bronze); |
| 87 | assert.ok(shaftOnly.r > 0.1); |
| 88 | assert.ok(shaftOnly.r > shaftOnly.b); |
| 89 | const tipOnly = dualChannelTintedRgb(0, 0, 0.8, pine, bronze); |
| 90 | assert.ok(tipOnly.b > tipOnly.r || tipOnly.b > 0.05); |
| 91 | const white = dualChannelTintedRgb(1, 1, 1, pine, bronze); |
| 92 | assert.equal(white.r, 1); |
| 93 | assert.equal(white.g, 1); |
| 94 | assert.equal(white.b, 1); |
| 95 | const unequal = dualChannelTintedRgb(0.9, 0.4, 0.1, pine, bronze); |
| 96 | assert.equal(unequal.r, 0.9); |
| 97 | assert.equal(unequal.g, 0.4); |
| 98 | assert.equal(unequal.b, 0.1); |
| 99 | assert.equal(channelDominanceMask(0.9, 0.4, 0.1, "shaft"), 0); |
| 100 | const dual = resolveItemDualTint({ itemId: RangedItemId.bronzeArrow }); |
| 101 | assert.ok(dual?.shaft); |
| 102 | assert.ok(dual?.tip); |
| 103 | assert.equal(resolveItemTint({ itemId: RangedItemId.bronzeArrow }), null); |
| 104 | }); |
| 105 | |
| 106 | test("hafted tools dual-tint wood handle and metal head", () => |
| 107 | { |
| 108 | const pickaxe = resolveItemDualTint({ itemId: ItemId.bronzePickaxe }); |
| 109 | assert.ok(pickaxe?.shaft); |
| 110 | assert.ok(pickaxe?.tip); |
| 111 | assert.deepEqual(pickaxe.shaft, tierTintColor("pine")); |
| 112 | assert.deepEqual(pickaxe.tip, tierTintColor(MaterialTier.bronze)); |
| 113 | assert.equal(resolveItemTint({ itemId: ItemId.bronzePickaxe }), null); |
| 114 | |
| 115 | const ironAxe = resolveItemDualTint({ itemId: ItemId.ironAxe }); |
| 116 | assert.deepEqual(ironAxe.shaft, tierTintColor("birch")); |
| 117 | assert.deepEqual(ironAxe.tip, tierTintColor(MaterialTier.iron)); |
| 118 | }); |
| 119 | |
| 120 | test("bows dual-tint wood frame and cloth string; unstrung wood only", () => |
| 121 | { |
| 122 | const bow = resolveItemDualTint({ itemId: RangedItemId.pineBow }); |
| 123 | assert.deepEqual(bow.shaft, tierTintColor("pine")); |
| 124 | assert.deepEqual(bow.tip, tierTintColor("cotton")); |
| 125 | assert.equal(resolveItemTint({ itemId: RangedItemId.pineBow }), null); |
| 126 | |
| 127 | const unstrung = resolveItemDualTint({ itemId: RangedItemId.pineUnstrungBow }); |
| 128 | assert.deepEqual(unstrung.shaft, tierTintColor("pine")); |
| 129 | assert.deepEqual(unstrung.tip, tierTintColor("pine")); |
| 130 | assert.equal(resolveItemTint({ itemId: RangedItemId.pineUnstrungBow }), null); |
| 131 | }); |
| 132 | |
| 133 | test("staff dual-tints wood shaft and gem tip", () => |
| 134 | { |
| 135 | const staff = resolveItemDualTint({ itemId: GemItemId.pineStaff }); |
| 136 | assert.deepEqual(staff.shaft, tierTintColor("pine")); |
| 137 | assert.deepEqual(staff.tip, tierTintColor("tourmaline")); |
| 138 | assert.equal(resolveItemTint({ itemId: GemItemId.pineStaff }), null); |
| 139 | // Staff keeps dual equip art even though equipment.tintFromTier is also set. |
| 140 | const equipDual = resolveEquipDualTint({ itemId: GemItemId.pineStaff }); |
| 141 | assert.deepEqual(equipDual.shaft, tierTintColor("pine")); |
| 142 | assert.deepEqual(equipDual.tip, tierTintColor("tourmaline")); |
| 143 | assert.equal(resolveEquipTint(GemItemId.pineStaff), null); |
| 144 | }); |
| 145 | |
| 146 | test("material tint hex converts to 0-1 rgb at load", () => |
| 147 | { |
| 148 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.bronze], rgb(184, 122, 71)); |
| 149 | assert.deepEqual(hexToRgb01(0xB87A47), { |
| 150 | r: 184 / 255, |
| 151 | g: 122 / 255, |
| 152 | b: 71 / 255 |
| 153 | }); |
| 154 | assert.deepEqual(hexToRgb01("#B87A47"), hexToRgb01(0xB87A47)); |
| 155 | assert.deepEqual(hexToRgb01("#abc"), hexToRgb01("#AABBCC")); |
| 156 | assert.deepEqual(tierTintColor(MaterialTier.bronze), rgb(184, 122, 71)); |
| 157 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.mithril], rgb(3, 120, 255)); |
| 158 | assert.deepEqual(hexToRgb01("rgb(3, 120, 255)"), rgb(3, 120, 255)); |
| 159 | }); |
| 160 | |
| 161 | test("wizard robe dye tints cloth channel only on dual-tint pieces", () => |
| 162 | { |
| 163 | const dye = { r: 0.85, g: 0.15, b: 0.35 }; |
| 164 | const stack = { |
| 165 | itemId: WizardItemId.cottonHat, |
| 166 | instance: { kind: "clothing", dyeColor: dye } |
| 167 | }; |
| 168 | const dual = resolveItemDualTint(stack); |
| 169 | assert.deepEqual(dual.shaft, tierTintColor(LeatherTier.softLeather)); |
| 170 | assert.deepEqual(dual.tip, normalizeRgb(dye)); |
| 171 | assert.equal(resolveEquipTint(WizardItemId.cottonHat, stack.instance), null); |
| 172 | assert.equal(resolveItemTint(stack), null); |
| 173 | |
| 174 | const bottoms = { |
| 175 | itemId: WizardItemId.cottonRobeBottoms, |
| 176 | instance: { kind: "clothing", dyeColor: dye } |
| 177 | }; |
| 178 | assert.deepEqual(resolveEquipTint(WizardItemId.cottonRobeBottoms, bottoms.instance), normalizeRgb(dye)); |
| 179 | assert.equal(resolveEquipDualTint(bottoms), null); |
| 180 | }); |