import { describe, it, expect } from 'vitest' import { apcaContrast, colorToGray, hexToRgb, reversePalette, rgbToHex } from './colors' describe('hexToRgb', () => { it('should convert 6-digit hex to RGB', () => { expect(hexToRgb('#ffffff')).toEqual({ r: 255, g: 255, b: 255 }) expect(hexToRgb('#000000')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('#ff0000')).toEqual({ r: 255, g: 0, b: 0 }) }) it('should convert 3-digit hex to RGB', () => { expect(hexToRgb('#fff')).toEqual({ r: 255, g: 255, b: 255 }) expect(hexToRgb('#000')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('#f00')).toEqual({ r: 255, g: 0, b: 0 }) }) it('should handle hex without hash symbol', () => { expect(hexToRgb('ffffff')).toEqual({ r: 255, g: 255, b: 255 }) expect(hexToRgb('000000')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('ff0000')).toEqual({ r: 255, g: 0, b: 0 }) }) it('should return { r: 0, g: 0, b: 0 } for invalid hex', () => { expect(hexToRgb('zzz')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('#12345')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('#1234567')).toEqual({ r: 0, g: 0, b: 0 }) expect(hexToRgb('')).toEqual({ r: 0, g: 0, b: 0 }) }) }) describe('rgbToHex', () => { it('should convert RGB to hex', () => { expect(rgbToHex({ r: 255, g: 255, b: 255 })).toBe('#ffffff') expect(rgbToHex({ r: 0, g: 0, b: 0 })).toBe('#000000') expect(rgbToHex({ r: 255, g: 0, b: 0 })).toBe('#ff0000') expect(rgbToHex({ r: 0, g: 255, b: 0 })).toBe('#00ff00') expect(rgbToHex({ r: 0, g: 0, b: 255 })).toBe('#0000ff') }) it('should handle mixed RGB values', () => { expect(rgbToHex({ r: 123, g: 45, b: 67 })).toBe('#7b2d43') expect(rgbToHex({ r: 12, g: 34, b: 56 })).toBe('#0c2238') expect(rgbToHex({ r: 78, g: 90, b: 12 })).toBe('#4e5a0c') }) it('should handle edge cases', () => { expect(rgbToHex({ r: 0, g: 0, b: 0 })).toBe('#000000') expect(rgbToHex({ r: 255, g: 255, b: 255 })).toBe('#ffffff') }) }) describe('colorToGray', () => { it('should return an object with r, g, and b properties', () => { const result = colorToGray({ r: 255, g: 255, b: 255 }) expect(result).toHaveProperty('r') expect(result).toHaveProperty('g') expect(result).toHaveProperty('b') }) }) describe('reversePalette', () => { it('should reverse the palette keys correctly', () => { const input = { '50': '#f0f0f0', '100': '#e0e0e0', '200': '#c0c0c0', '300': '#a0a0a0', '400': '#808080', '500': '#606060', '600': '#404040', '700': '#202020', '800': '#101010', '900': '#080808', '950': '#040404', } const expectedOutput = { '950': '#f0f0f0', '900': '#e0e0e0', '800': '#c0c0c0', '700': '#a0a0a0', '600': '#808080', '500': '#606060', '400': '#404040', '300': '#202020', '200': '#101010', '100': '#080808', '50': '#040404', } expect(reversePalette(input)).toEqual(expectedOutput) }) it('should handle empty input', () => { const input = {} const expectedOutput = {} expect(reversePalette(input)).toEqual(expectedOutput) }) }) describe('apcaContrast', () => { it('should return 0 for undefined foreground or background', () => { expect(apcaContrast(undefined, '#ffffff')).toBe(0) expect(apcaContrast('#000000', undefined)).toBe(0) expect(apcaContrast(undefined, undefined)).toBe(0) }) it('should return a number for valid hex color inputs', () => { expect(typeof apcaContrast('#000000', '#ffffff')).toBe('number') expect(typeof apcaContrast('#ffffff', '#000000')).toBe('number') expect(typeof apcaContrast('#ff0000', '#00ff00')).toBe('number') expect(typeof apcaContrast('#00ff00', '#0000ff')).toBe('number') expect(typeof apcaContrast('#0000ff', '#ff0000')).toBe('number') }) it('should handle edge cases', () => { expect(apcaContrast('#000000', '#000000')).toBe(0) expect(apcaContrast('#ffffff', '#ffffff')).toBe(0) }) })