| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | isMagentaTintable, |
| 5 | magentaTintedRgb, |
| 6 | tierTintColor, |
| 7 | resolveItemTint |
| 8 | } from "../../shared/render/tint.js"; |
| 9 | import { MaterialTier, GatherMaterialId, hexToRgb01, MATERIAL_TINT_HEX, rgb } from "../../shared/definitions/materialTiers.js"; |
| 10 | import { ItemId } from "../../shared/definitions/items.js"; |
| 11 | |
| 12 | test("magenta tintable when R==B and G < R", () => |
| 13 | { |
| 14 | assert.equal(isMagentaTintable(1, 0, 1), true); |
| 15 | assert.equal(isMagentaTintable(50 / 255, 0, 50 / 255), true); |
| 16 | assert.equal(isMagentaTintable(120 / 255, 50 / 255, 120 / 255), true); |
| 17 | // Near-magenta drift (R = B ± 2) from AA / export — still tintable. |
| 18 | assert.equal(isMagentaTintable(173 / 255, 67 / 255, 171 / 255), true); |
| 19 | assert.equal(isMagentaTintable(87 / 255, 60 / 255, 85 / 255), true); |
| 20 | assert.equal(isMagentaTintable(200 / 255, 100 / 255, 50 / 255), false); |
| 21 | assert.equal(isMagentaTintable(0.5, 0.5, 0.5), false); |
| 22 | }); |
| 23 | |
| 24 | test("magenta tint preserves lightness and desaturates with G", () => |
| 25 | { |
| 26 | const tint = { r: 0, g: 1, b: 0 }; |
| 27 | const dark = magentaTintedRgb(50 / 255, 0, 50 / 255, tint); |
| 28 | const light = magentaTintedRgb(1, 0, 1, tint); |
| 29 | assert.ok(dark.g < light.g); |
| 30 | const desat = magentaTintedRgb(120 / 255, 50 / 255, 120 / 255, tint); |
| 31 | const pure = magentaTintedRgb(120 / 255, 0, 120 / 255, tint); |
| 32 | assert.ok(desat.r > pure.r); |
| 33 | }); |
| 34 | |
| 35 | test("dark target tints stay dark even on high-G magenta highlights", () => |
| 36 | { |
| 37 | const coal = { r: 23 / 255, g: 23 / 255, b: 24 / 255 }; |
| 38 | const pure = magentaTintedRgb(1, 0, 1, coal); |
| 39 | const highlight = magentaTintedRgb(1, 179 / 255, 1, coal); |
| 40 | assert.ok(pure.r < 0.12); |
| 41 | assert.ok(highlight.r < 0.15, `highlight washed out: ${highlight.r}`); |
| 42 | assert.ok(Math.abs(highlight.r - pure.r) < 0.05); |
| 43 | }); |
| 44 | |
| 45 | test("resolveItemTint uses potion instance color and tier tints", () => |
| 46 | { |
| 47 | const potionTint = resolveItemTint({ |
| 48 | itemId: ItemId.potion, |
| 49 | instance: { kind: "potion", color: { r: 0.25, g: 0.72, b: 0.35 } } |
| 50 | }); |
| 51 | assert.equal(potionTint.g, 0.72); |
| 52 | const bronze = tierTintColor(MaterialTier.bronze); |
| 53 | assert.ok(bronze.r > bronze.b); |
| 54 | const barTint = resolveItemTint({ itemId: ItemId.bronzeBar }); |
| 55 | assert.deepEqual(barTint, bronze); |
| 56 | const copper = tierTintColor(GatherMaterialId.copper); |
| 57 | const copperOre = tierTintColor(GatherMaterialId.copper, { ore: true }); |
| 58 | assert.notDeepEqual(copperOre, copper); |
| 59 | assert.deepEqual(resolveItemTint({ itemId: ItemId.copperOre }), copperOre); |
| 60 | const iron = tierTintColor(MaterialTier.iron); |
| 61 | const ironOre = tierTintColor(MaterialTier.iron, { ore: true }); |
| 62 | assert.notDeepEqual(ironOre, iron); |
| 63 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironOre }), ironOre); |
| 64 | assert.deepEqual(resolveItemTint({ itemId: ItemId.ironBar }), iron); |
| 65 | }); |
| 66 | |
| 67 | test("material tint hex converts to 0-1 rgb at load", () => |
| 68 | { |
| 69 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.bronze], rgb(184, 122, 71)); |
| 70 | assert.deepEqual(hexToRgb01(0xB87A47), { |
| 71 | r: 184 / 255, |
| 72 | g: 122 / 255, |
| 73 | b: 71 / 255 |
| 74 | }); |
| 75 | assert.deepEqual(hexToRgb01("#B87A47"), hexToRgb01(0xB87A47)); |
| 76 | assert.deepEqual(hexToRgb01("#abc"), hexToRgb01("#AABBCC")); |
| 77 | assert.deepEqual(tierTintColor(MaterialTier.bronze), rgb(184, 122, 71)); |
| 78 | assert.deepEqual(MATERIAL_TINT_HEX[MaterialTier.mithril], rgb(3, 120, 255)); |
| 79 | assert.deepEqual(hexToRgb01("rgb(3, 120, 255)"), rgb(3, 120, 255)); |
| 80 | }); |