| 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 | } from "../../shared/render/tint.js"; |
| 12 | import { MaterialTier, GatherMaterialId, hexToRgb01, MATERIAL_TINT_HEX, rgb } from "../../shared/definitions/materialTiers.js"; |
| 13 | import { ItemId } from "../../shared/definitions/items.js"; |
| 14 | import { RangedItemId } from "../../shared/definitions/rangedItems.js"; |
| 15 | |
| 16 | test("magenta tintable when R==B and G < R", () => |
| 17 | { |
| 18 | assert.equal(isMagentaTintable(1, 0, 1), true); |
| 19 | assert.equal(isMagentaTintable(50 / 255, 0, 50 / 255), true); |
| 20 | assert.equal(isMagentaTintable(120 / 255, 50 / 255, 120 / 255), true); |
| 21 | // Near-magenta drift (R = B ± 2) from AA / export — still tintable. |
| 22 | assert.equal(isMagentaTintable(173 / 255, 67 / 255, 171 / 255), true); |
| 23 | assert.equal(isMagentaTintable(87 / 255, 60 / 255, 85 / 255), true); |
| 24 | assert.equal(isMagentaTintable(200 / 255, 100 / 255, 50 / 255), false); |
| 25 | assert.equal(isMagentaTintable(0.5, 0.5, 0.5), false); |
| 26 | }); |
| 27 | |
| 28 | test("magenta tint preserves lightness and desaturates with G", () => |
| 29 | { |
| 30 | const tint = { r: 0, g: 1, b: 0 }; |
| 31 | const dark = magentaTintedRgb(50 / 255, 0, 50 / 255, tint); |
| 32 | const light = magentaTintedRgb(1, 0, 1, tint); |
| 33 | assert.ok(dark.g < light.g); |
| 34 | const desat = magentaTintedRgb(120 / 255, 50 / 255, 120 / 255, tint); |
| 35 | const pure = magentaTintedRgb(120 / 255, 0, 120 / 255, tint); |
| 36 | assert.ok(desat.r > pure.r); |
| 37 | }); |
| 38 | |
| 39 | test("dark target tints stay dark even on high-G magenta highlights", () => |
| 40 | { |
| 41 | const coal = { r: 23 / 255, g: 23 / 255, b: 24 / 255 }; |
| 42 | const pure = magentaTintedRgb(1, 0, 1, coal); |
| 43 | const highlight = magentaTintedRgb(1, 179 / 255, 1, coal); |
| 44 | assert.ok(pure.r < 0.12); |
| 45 | assert.ok(highlight.r < 0.15, `highlight washed out: ${highlight.r}`); |
| 46 | assert.ok(Math.abs(highlight.r - pure.r) < 0.05); |
| 47 | }); |
| 48 | |
| 49 | test("resolveItemTint uses potion instance color and tier tints", () => |
| 50 | { |
| 51 | const potionTint = resolveItemTint({ |
| 52 | itemId: ItemId.potion, |
| 53 | instance: { kind: "potion", color: { r: 0.25, g: 0.72, b: 0.35 } } |
| 54 | }); |
| 55 | assert.equal(potionTint.g, 0.72); |
| 56 | const bronze = tierTintColor(MaterialTier.bronze); |
| 57 | assert.ok(bronze.r > bronze.b); |
| 58 | const barTint = resolveItemTint({ itemId: ItemId.bronzeBar }); |
| 59 | assert.deepEqual(barTint, bronze); |
| 60 | const copper = tierTintColor(GatherMaterialId.copper); |
| 61 | const copperOre = tierTintColor(GatherMaterialId.copper, { ore: true }); |
| 62 | assert.notDeepEqual(copperOre, copper); |
| 63 | assert.deepEqual(resolveItemTint({ itemId: ItemId.copperOre }), copperOre); |
| 64 | const iron = tierTintColor(MaterialTier.iron); |
| 65 | const ironOre = tierTintColor(MaterialTier.iron, { ore: true }); |
| 66 | assert.notDeepEqual(ironOre, iron); |
| 67 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironOre }), ironOre); |
| 68 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironBar }), iron); |
| 69 | }); |
| 70 | |
| 71 | test("dual-channel arrow tints shaft red and tip blue, leaves white fletching", () => |
| 72 | { |
| 73 | assert.ok(channelDominanceMask(1, 0, 0, "shaft") > 0.9); |
| 74 | assert.ok(channelDominanceMask(0, 0, 1, "tip") > 0.9); |
| 75 | assert.equal(channelDominanceMask(1, 1, 1, "shaft"), 0); |
| 76 | assert.equal(channelDominanceMask(1, 1, 1, "tip"), 0); |
| 77 | const bronze = tierTintColor(MaterialTier.bronze); |
| 78 | const pine = tierTintColor("pine"); |
| 79 | const shaftOnly = dualChannelTintedRgb(0.8, 0, 0, pine, bronze); |
| 80 | assert.ok(shaftOnly.r > 0.1); |
| 81 | assert.ok(shaftOnly.r > shaftOnly.b); |
| 82 | const tipOnly = dualChannelTintedRgb(0, 0, 0.8, pine, bronze); |
| 83 | assert.ok(tipOnly.b > tipOnly.r || tipOnly.b > 0.05); |
| 84 | const white = dualChannelTintedRgb(1, 1, 1, pine, bronze); |
| 85 | assert.equal(white.r, 1); |
| 86 | assert.equal(white.g, 1); |
| 87 | assert.equal(white.b, 1); |
| 88 | const dual = resolveItemDualTint({ itemId: RangedItemId.bronzeArrow }); |
| 89 | assert.ok(dual?.shaft); |
| 90 | assert.ok(dual?.tip); |
| 91 | assert.equal(resolveItemTint({ itemId: RangedItemId.bronzeArrow }), null); |
| 92 | }); |
| 93 | |
| 94 | test("hafted tools dual-tint wood handle and metal head", () => |
| 95 | { |
| 96 | const pickaxe = resolveItemDualTint({ itemId: ItemId.bronzePickaxe }); |
| 97 | assert.ok(pickaxe?.shaft); |
| 98 | assert.ok(pickaxe?.tip); |
| 99 | assert.deepEqual(pickaxe.shaft, tierTintColor("pine")); |
| 100 | assert.deepEqual(pickaxe.tip, tierTintColor(MaterialTier.bronze)); |
| 101 | assert.equal(resolveItemTint({ itemId: ItemId.bronzePickaxe }), null); |
| 102 | |
| 103 | const ironAxe = resolveItemDualTint({ itemId: ItemId.ironAxe }); |
| 104 | assert.deepEqual(ironAxe.shaft, tierTintColor("birch")); |
| 105 | assert.deepEqual(ironAxe.tip, tierTintColor(MaterialTier.iron)); |
| 106 | }); |
| 107 | |
| 108 | test("bows dual-tint wood frame and cloth string; unstrung wood only", () => |
| 109 | { |
| 110 | const bow = resolveItemDualTint({ itemId: RangedItemId.pineBow }); |
| 111 | assert.deepEqual(bow.shaft, tierTintColor("pine")); |
| 112 | assert.deepEqual(bow.tip, tierTintColor("cotton")); |
| 113 | assert.equal(resolveItemTint({ itemId: RangedItemId.pineBow }), null); |
| 114 | |
| 115 | const unstrung = resolveItemDualTint({ itemId: RangedItemId.pineUnstrungBow }); |
| 116 | assert.deepEqual(unstrung.shaft, tierTintColor("pine")); |
| 117 | assert.deepEqual(unstrung.tip, tierTintColor("pine")); |
| 118 | assert.equal(resolveItemTint({ itemId: RangedItemId.pineUnstrungBow }), null); |
| 119 | }); |
| 120 | |
| 121 | test("material tint hex converts to 0-1 rgb at load", () => |
| 122 | { |
| 123 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.bronze], rgb(184, 122, 71)); |
| 124 | assert.deepEqual(hexToRgb01(0xB87A47), { |
| 125 | r: 184 / 255, |
| 126 | g: 122 / 255, |
| 127 | b: 71 / 255 |
| 128 | }); |
| 129 | assert.deepEqual(hexToRgb01("#B87A47"), hexToRgb01(0xB87A47)); |
| 130 | assert.deepEqual(hexToRgb01("#abc"), hexToRgb01("#AABBCC")); |
| 131 | assert.deepEqual(tierTintColor(MaterialTier.bronze), rgb(184, 122, 71)); |
| 132 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.mithril], rgb(3, 120, 255)); |
| 133 | assert.deepEqual(hexToRgb01("rgb(3, 120, 255)"), rgb(3, 120, 255)); |
| 134 | }); |