import { describe, it, expect } from '@jest/globals'; import { Color } from '../src/common/Color'; describe('Color', () => { describe('fromHex', () => { it('parses a hex string with # prefix correctly', () => { const color = Color.fromHex('#FF8040FF'); expect(color.red).toBe(0xff); expect(color.green).toBe(0x80); expect(color.blue).toBe(0x40); expect(color.alpha).toBe(0xff); }); it('parses a hex string without # prefix correctly', () => { const color = Color.fromHex('FF8040FF'); expect(color.red).toBe(0xff); expect(color.green).toBe(0x80); expect(color.blue).toBe(0x40); expect(color.alpha).toBe(0xff); }); it('adds default full alpha when alpha is omitted', () => { const color = Color.fromHex('#FF8040'); expect(color.red).toBe(0xff); expect(color.green).toBe(0x80); expect(color.blue).toBe(0x40); expect(color.alpha).toBe(0xff); }); it('expands shorthand 3-digit hex', () => { const color = Color.fromHex('#F80'); expect(color.red).toBe(0xff); expect(color.green).toBe(0x88); expect(color.blue).toBe(0x00); expect(color.alpha).toBe(0xff); }); }); describe('fromRGBA', () => { it('creates color from integer RGBA components', () => { const color = Color.fromRGBA(255, 128, 64, 255); expect(color.red).toBe(255); expect(color.green).toBe(128); expect(color.blue).toBe(64); expect(color.alpha).toBe(255); }); it('normalizes fractional alpha in range (0, 1] to 0-255', () => { const color = Color.fromRGBA(0, 0, 0, 1.0); expect(color.alpha).toBe(255); }); it('uses full alpha by default', () => { const color = Color.fromRGBA(10, 20, 30); expect(color.alpha).toBe(255); }); }); describe('component accessors return valid numbers', () => { it('red is not NaN', () => { expect(Color.fromHex('#AABBCCDD').red).not.toBeNaN(); }); it('green is not NaN', () => { expect(Color.fromHex('#AABBCCDD').green).not.toBeNaN(); }); it('blue is not NaN', () => { expect(Color.fromHex('#AABBCCDD').blue).not.toBeNaN(); }); it('alpha is not NaN', () => { expect(Color.fromHex('#AABBCCDD').alpha).not.toBeNaN(); }); }); describe('withAlpha', () => { it('replaces alpha while keeping RGB intact', () => { const color = Color.fromHex('#FF8040FF').withAlpha(0.5); expect(color.red).toBe(0xff); expect(color.green).toBe(0x80); expect(color.blue).toBe(0x40); expect(color.alpha).toBe(Math.round(255 * 0.5)); }); }); describe('toJSON', () => { it('serializes with a leading # character', () => { const color = Color.fromHex('#FF8040FF'); expect(color.toJSON()).toMatch(/^#/); }); it('round-trips through fromHex and toJSON', () => { const hex = '#FF8040FF'; expect(Color.fromHex(hex).toJSON()).toBe(hex); }); }); });