|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import ColorUtils from '../utils/ColorUtils'; |
| 3 | + |
| 4 | +describe('ColorUtils', () => { |
| 5 | + describe('convertHexColorToBytes', () => { |
| 6 | + it('should throw if hex color has wrong value', () => { |
| 7 | + // @ts-expect-error testing invalid param type |
| 8 | + expect(() => ColorUtils.convertHexColorToBytes()).toThrowError( |
| 9 | + 'Expected hexColor param to be a string instead got undefined', |
| 10 | + ); |
| 11 | + expect(() => ColorUtils.convertHexColorToBytes('A')).toThrowError( |
| 12 | + 'Expected hexColor to be of length 3, 4, 6 or 8 with 0-9 A-F characters, instead got A with length 1', |
| 13 | + ); |
| 14 | + expect(() => ColorUtils.convertHexColorToBytes('red')).toThrowError( |
| 15 | + 'Expected hexColor to be of length 3, 4, 6 or 8 with 0-9 A-F characters, instead got red with length 3', |
| 16 | + ); |
| 17 | + expect(() => ColorUtils.convertHexColorToBytes('##FFF')).toThrowError( |
| 18 | + 'Expected hexColor to be of length 3, 4, 6 or 8 with 0-9 A-F characters, instead got #FFF with length 4', |
| 19 | + ); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should return bytes for hex color with alpha', () => { |
| 23 | + expect(ColorUtils.convertHexColorToBytes('#FFFFFFFF')).toEqual([ |
| 24 | + 255, 255, 255, 255, |
| 25 | + ]); |
| 26 | + expect(ColorUtils.convertHexColorToBytes('#00000000')).toEqual([ |
| 27 | + 0, 0, 0, 0, |
| 28 | + ]); |
| 29 | + expect(ColorUtils.convertHexColorToBytes('#AABBCCDD')).toEqual([ |
| 30 | + 170, 187, 204, 221, |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return bytes with default alpha for hex color without alpha', () => { |
| 35 | + expect(ColorUtils.convertHexColorToBytes('#FFFFFF')).toEqual([ |
| 36 | + 255, 255, 255, 255, |
| 37 | + ]); |
| 38 | + expect(ColorUtils.convertHexColorToBytes('#000000')).toEqual([ |
| 39 | + 0, 0, 0, 255, |
| 40 | + ]); |
| 41 | + expect(ColorUtils.convertHexColorToBytes('#AABBCC')).toEqual([ |
| 42 | + 170, 187, 204, 255, |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should return bytes for short hex color with alpha', () => { |
| 47 | + expect(ColorUtils.convertHexColorToBytes('#FFFF')).toEqual([ |
| 48 | + 255, 255, 255, 255, |
| 49 | + ]); |
| 50 | + expect(ColorUtils.convertHexColorToBytes('#0000')).toEqual([0, 0, 0, 0]); |
| 51 | + expect(ColorUtils.convertHexColorToBytes('#ABCD')).toEqual([ |
| 52 | + 170, 187, 204, 221, |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return bytes with default alpha for short hex color without alpha', () => { |
| 57 | + expect(ColorUtils.convertHexColorToBytes('#FFF')).toEqual([ |
| 58 | + 255, 255, 255, 255, |
| 59 | + ]); |
| 60 | + expect(ColorUtils.convertHexColorToBytes('#000')).toEqual([0, 0, 0, 255]); |
| 61 | + expect(ColorUtils.convertHexColorToBytes('#ABC')).toEqual([ |
| 62 | + 170, 187, 204, 255, |
| 63 | + ]); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should allow to use hex color without #', () => { |
| 67 | + expect(ColorUtils.convertHexColorToBytes('FFF')).toEqual([ |
| 68 | + 255, 255, 255, 255, |
| 69 | + ]); |
| 70 | + expect(ColorUtils.convertHexColorToBytes('000')).toEqual([0, 0, 0, 255]); |
| 71 | + expect(ColorUtils.convertHexColorToBytes('ABC')).toEqual([ |
| 72 | + 170, 187, 204, 255, |
| 73 | + ]); |
| 74 | + |
| 75 | + expect(ColorUtils.convertHexColorToBytes('FFFA')).toEqual([ |
| 76 | + 255, 255, 255, 170, |
| 77 | + ]); |
| 78 | + expect(ColorUtils.convertHexColorToBytes('000A')).toEqual([0, 0, 0, 170]); |
| 79 | + expect(ColorUtils.convertHexColorToBytes('ABCA')).toEqual([ |
| 80 | + 170, 187, 204, 170, |
| 81 | + ]); |
| 82 | + |
| 83 | + expect(ColorUtils.convertHexColorToBytes('FFFFFF')).toEqual([ |
| 84 | + 255, 255, 255, 255, |
| 85 | + ]); |
| 86 | + expect(ColorUtils.convertHexColorToBytes('0A0A0A')).toEqual([ |
| 87 | + 10, 10, 10, 255, |
| 88 | + ]); |
| 89 | + expect(ColorUtils.convertHexColorToBytes('0A0B0C')).toEqual([ |
| 90 | + 10, 11, 12, 255, |
| 91 | + ]); |
| 92 | + |
| 93 | + expect(ColorUtils.convertHexColorToBytes('FFFFFF0F')).toEqual([ |
| 94 | + 255, 255, 255, 15, |
| 95 | + ]); |
| 96 | + expect(ColorUtils.convertHexColorToBytes('0000000A')).toEqual([ |
| 97 | + 0, 0, 0, 10, |
| 98 | + ]); |
| 99 | + expect(ColorUtils.convertHexColorToBytes('42424242')).toEqual([ |
| 100 | + 66, 66, 66, 66, |
| 101 | + ]); |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments