|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + getColorFromNodeNum, |
| 4 | + hexToRgb, |
| 5 | + isLightColor, |
| 6 | + type RGBColor, |
| 7 | + rgbToHex, |
| 8 | +} from "./color.ts"; |
| 9 | + |
| 10 | +describe("hexToRgb", () => { |
| 11 | + it.each([ |
| 12 | + [0x000000, { r: 0, g: 0, b: 0 }], |
| 13 | + [0xffffff, { r: 255, g: 255, b: 255 }], |
| 14 | + [0x123456, { r: 0x12, g: 0x34, b: 0x56 }], |
| 15 | + [0xff8000, { r: 255, g: 128, b: 0 }], |
| 16 | + ])("parses 0x%s correctly", (hex, expected) => { |
| 17 | + expect(hexToRgb(hex)).toEqual(expected); |
| 18 | + }); |
| 19 | +}); |
| 20 | + |
| 21 | +describe("rgbToHex", () => { |
| 22 | + it.each<[RGBColor, number]>([ |
| 23 | + [{ r: 0, g: 0, b: 0 }, 0x000000], |
| 24 | + [{ r: 255, g: 255, b: 255 }, 0xffffff], |
| 25 | + [{ r: 0x12, g: 0x34, b: 0x56 }, 0x123456], |
| 26 | + [{ r: 255, g: 128, b: 0 }, 0xff8000], |
| 27 | + ])("packs %j into 0x%s", (rgb, expected) => { |
| 28 | + expect(rgbToHex(rgb)).toBe(expected); |
| 29 | + }); |
| 30 | + |
| 31 | + it("rounds component values before packing", () => { |
| 32 | + expect(rgbToHex({ r: 12.2, g: 12.8, b: 99.5 })).toBe( |
| 33 | + (12 << 16) | (13 << 8) | 100, |
| 34 | + ); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("hexToRgb ⟷ rgbToHex round-trip", () => { |
| 39 | + it("is identity for representative values (masked to 24-bit)", () => { |
| 40 | + const samples = [0, 1, 0x7fffff, 0x800000, 0xffffff, 0x123456, 0x00ff00]; |
| 41 | + for (const hex of samples) { |
| 42 | + const rgb = hexToRgb(hex); |
| 43 | + expect(rgbToHex(rgb)).toBe(hex & 0xffffff); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + it("holds for random 24-bit values", () => { |
| 48 | + for (let i = 0; i < 100; i++) { |
| 49 | + const hex = Math.floor(Math.random() * 0x1000000); // 0..0xFFFFFF |
| 50 | + expect(rgbToHex(hexToRgb(hex))).toBe(hex); |
| 51 | + } |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe("isLightColor", () => { |
| 56 | + it("detects obvious extremes", () => { |
| 57 | + expect(isLightColor({ r: 255, g: 255, b: 255 })).toBe(true); // white |
| 58 | + expect(isLightColor({ r: 0, g: 0, b: 0 })).toBe(false); // black |
| 59 | + }); |
| 60 | + |
| 61 | + it("respects the 127.5 threshold at boundary", () => { |
| 62 | + // mid-gray 127 → false, 128 → true (given the formula and 127.5 threshold) |
| 63 | + expect(isLightColor({ r: 127, g: 127, b: 127 })).toBe(false); |
| 64 | + expect(isLightColor({ r: 128, g: 128, b: 128 })).toBe(true); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("getColorFromNodeNum", () => { |
| 69 | + it.each([ |
| 70 | + [0x000000, { r: 0, g: 0, b: 0 }], |
| 71 | + [0xffffff, { r: 255, g: 255, b: 255 }], |
| 72 | + [0x123456, { r: 0x12, g: 0x34, b: 0x56 }], |
| 73 | + ])("extracts RGB from lower 24 bits of %s", (nodeNum, expected) => { |
| 74 | + expect(getColorFromNodeNum(nodeNum)).toEqual(expected); |
| 75 | + }); |
| 76 | + |
| 77 | + it("matches hexToRgb when masking to 24 bits", () => { |
| 78 | + const nodeNums = [1127947528, 42, 999999, 0xfeef12, 0xfeedface, -123456]; |
| 79 | + for (const n of nodeNums) { |
| 80 | + // JS bitwise ops use signed 32-bit, so mask the lower 24 bits for comparison. |
| 81 | + const masked = n & 0xffffff; |
| 82 | + expect(getColorFromNodeNum(n)).toEqual(hexToRgb(masked)); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it("always yields components within 0..255", () => { |
| 87 | + const color = getColorFromNodeNum(Math.floor(Math.random() * 2 ** 31)); |
| 88 | + for (const v of Object.values(color)) { |
| 89 | + expect(v).toBeGreaterThanOrEqual(0); |
| 90 | + expect(v).toBeLessThanOrEqual(255); |
| 91 | + } |
| 92 | + }); |
| 93 | +}); |
0 commit comments