|
| 1 | +import { FastColor } from '../src'; |
| 2 | + |
| 3 | +describe('hsl', () => { |
| 4 | + // Unified hex<->hsl fixtures |
| 5 | + const hexHslFixtures: Array<{ hex: string; hsl: { h: number; s: number; l: number } }> = [ |
| 6 | + // Boundaries |
| 7 | + { hex: '#000000', hsl: { h: 0, s: 0, l: 0 } }, |
| 8 | + { hex: '#ffffff', hsl: { h: 0, s: 0, l: 1 } }, |
| 9 | + |
| 10 | + // Primaries |
| 11 | + { hex: '#ff0000', hsl: { h: 0, s: 1, l: 0.5 } }, |
| 12 | + { hex: '#00ff00', hsl: { h: 120, s: 1, l: 0.5 } }, |
| 13 | + { hex: '#0000ff', hsl: { h: 240, s: 1, l: 0.5 } }, |
| 14 | + |
| 15 | + // Secondaries |
| 16 | + { hex: '#ffff00', hsl: { h: 60, s: 1, l: 0.5 } }, |
| 17 | + { hex: '#00ffff', hsl: { h: 180, s: 1, l: 0.5 } }, |
| 18 | + { hex: '#ff00ff', hsl: { h: 300, s: 1, l: 0.5 } }, |
| 19 | + |
| 20 | + // Specific samples |
| 21 | + { hex: '#2400c2', hsl: { h: 251, s: 1, l: 0.3804 } }, |
| 22 | + { hex: '#3d5dff', hsl: { h: 230, s: 1, l: 0.6196 } }, |
| 23 | + ]; |
| 24 | + |
| 25 | + // hex -> hsl object values and roundtrip |
| 26 | + hexHslFixtures.forEach(({ hex, hsl: expected }) => { |
| 27 | + it(`hex to hsl object values: ${hex}`, () => { |
| 28 | + const hsl = new FastColor(hex).toHsl(); |
| 29 | + expect(hsl.h).toBe(expected.h); |
| 30 | + expect(hsl.s).toBe(expected.s); |
| 31 | + expect(hsl.l).toBeCloseTo(expected.l, 4); |
| 32 | + expect(hsl.a).toBe(1); |
| 33 | + |
| 34 | + const back = new FastColor(hsl).toHexString(); |
| 35 | + expect(back).toBe(hex); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + it('setHue should not change lightness', () => { |
| 40 | + const base = new FastColor('#1677ff'); |
| 41 | + expect(base.getLightness()).toBeCloseTo(new FastColor('#1677ff').getLightness(), 4); |
| 42 | + |
| 43 | + const turn = base.setHue(233); |
| 44 | + expect(turn.getLightness()).toBeCloseTo(base.getLightness(), 4); |
| 45 | + }); |
| 46 | + |
| 47 | + const hslaAlphaCases: Array<[string, string, number]> = [ |
| 48 | + ['hsla(251, 100%, 38%, 0.5)', 'hsla(251,100%,38%,0.5)', 0.5], |
| 49 | + ['hsla(120, 25%, 33%, 0.7)', 'hsla(120,25%,33%,0.7)', 0.7], |
| 50 | + ]; |
| 51 | + |
| 52 | + hslaAlphaCases.forEach(([str, normalized, alpha]) => { |
| 53 | + it(`supports hsla alpha: ${str}`, () => { |
| 54 | + expect(new FastColor(str).toHslString()).toBe(normalized); |
| 55 | + expect(new FastColor(str).toRgb().a).toBe(alpha); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('hue 0 and 360 are equivalent', () => { |
| 60 | + const c0 = new FastColor('hsl(0, 100%, 50%)'); |
| 61 | + const c360 = new FastColor('hsl(360, 100%, 50%)'); |
| 62 | + expect(c0.toHexString()).toBe(c360.toHexString()); |
| 63 | + }); |
| 64 | + |
| 65 | + it('s=0 yields grayscale regardless of hue', () => { |
| 66 | + const a = new FastColor('hsl(0, 0%, 40%)'); |
| 67 | + const b = new FastColor('hsl(200, 0%, 40%)'); |
| 68 | + expect(a.toHexString()).toBe(b.toHexString()); |
| 69 | + }); |
| 70 | + |
| 71 | + it('roundtrip toHslString keeps values', () => { |
| 72 | + const c = new FastColor('hsla(120, 25%, 33%, 0.7)'); |
| 73 | + expect(c.toHslString()).toBe('hsla(120,25%,33%,0.7)'); |
| 74 | + const parsed = new FastColor(c.toHslString()); |
| 75 | + expect(parsed.toHslString()).toBe('hsla(120,25%,33%,0.7)'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('darken and lighten adjust lightness bounds', () => { |
| 79 | + const c = new FastColor('hsl(200, 50%, 50%)'); |
| 80 | + const darker = c.darken(20); |
| 81 | + const lighter = c.lighten(20); |
| 82 | + expect(darker.getLightness()).toBeCloseTo(c.getLightness() - 0.2, 4); |
| 83 | + expect(lighter.getLightness()).toBeCloseTo(c.getLightness() + 0.2, 4); |
| 84 | + |
| 85 | + const minCap = c.darken(100); |
| 86 | + const maxCap = c.lighten(100); |
| 87 | + expect(minCap.getLightness()).toBe(0); |
| 88 | + expect(maxCap.getLightness()).toBe(1); |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
0 commit comments