|
| 1 | +import type { ThemeRegistrationResolved } from '@shikijs/types' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { tokenizeAnsiWithTheme } from '../src/highlight/code-to-tokens-ansi' |
| 4 | + |
| 5 | +describe('aNSI color dimming', () => { |
| 6 | + const mockTheme: ThemeRegistrationResolved = { |
| 7 | + name: 'test-theme', |
| 8 | + type: 'dark', |
| 9 | + fg: '#ffffff', |
| 10 | + bg: '#000000', |
| 11 | + colors: {}, |
| 12 | + settings: [], |
| 13 | + } |
| 14 | + |
| 15 | + it('should dim 3-digit hex colors by adding 80 alpha', () => { |
| 16 | + // ANSI code: dim (2) + RGB color (38;2;r;g;b) + text + reset (0) |
| 17 | + const code = '\x1B[2;38;2;17;34;51mtest\x1B[0m' |
| 18 | + const tokens = tokenizeAnsiWithTheme(mockTheme, code) |
| 19 | + |
| 20 | + expect(tokens).toBeDefined() |
| 21 | + expect(tokens[0]).toBeDefined() |
| 22 | + expect(tokens[0][0]).toBeDefined() |
| 23 | + // #112233 should become #11223380 |
| 24 | + expect(tokens[0][0].color).toBe('#11223380') |
| 25 | + }) |
| 26 | + |
| 27 | + it('should dim 4-digit hex colors by halving the alpha', () => { |
| 28 | + // Using a color that when converted to 4-digit hex would be #1234 |
| 29 | + // RGB(17,34,51) = #112233, with alpha 68 (0x44) = #11223344 |
| 30 | + const code = '\x1B[2;38;2;17;34;51mtest\x1B[0m' |
| 31 | + const tokens = tokenizeAnsiWithTheme(mockTheme, code) |
| 32 | + |
| 33 | + expect(tokens).toBeDefined() |
| 34 | + expect(tokens[0][0]).toBeDefined() |
| 35 | + // Should be dimmed with 80 alpha (since we're passing RGB, not RGBA) |
| 36 | + expect(tokens[0][0].color).toBe('#11223380') |
| 37 | + }) |
| 38 | + |
| 39 | + it('should dim 6-digit hex colors by adding 80 alpha', () => { |
| 40 | + // RGB(18,52,86) = #123456 |
| 41 | + const code = '\x1B[2;38;2;18;52;86mtest\x1B[0m' |
| 42 | + const tokens = tokenizeAnsiWithTheme(mockTheme, code) |
| 43 | + |
| 44 | + expect(tokens).toBeDefined() |
| 45 | + expect(tokens[0][0]).toBeDefined() |
| 46 | + // #123456 should become #12345680 |
| 47 | + expect(tokens[0][0].color).toBe('#12345680') |
| 48 | + }) |
| 49 | + |
| 50 | + it('should dim 8-digit hex colors by halving the alpha', () => { |
| 51 | + // RGB(18,52,86) = #123456, with alpha 120 (0x78) = #12345678 |
| 52 | + // When dimmed, alpha should be 60 (0x3c) |
| 53 | + const code = '\x1B[2;38;2;18;52;86mtest\x1B[0m' |
| 54 | + const tokens = tokenizeAnsiWithTheme(mockTheme, code) |
| 55 | + |
| 56 | + expect(tokens).toBeDefined() |
| 57 | + expect(tokens[0][0]).toBeDefined() |
| 58 | + // Should be dimmed with 80 alpha |
| 59 | + expect(tokens[0][0].color).toBe('#12345680') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should handle CSS variables for ANSI colors', () => { |
| 63 | + // Test with a theme that uses CSS variables |
| 64 | + const themeWithCssVars: ThemeRegistrationResolved = { |
| 65 | + ...mockTheme, |
| 66 | + colors: { |
| 67 | + 'terminal.ansiRed': 'var(--my-ansi-red)', |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + // ANSI red color code with dim |
| 72 | + const code = '\x1B[2;31mtest\x1B[0m' |
| 73 | + const tokens = tokenizeAnsiWithTheme(themeWithCssVars, code) |
| 74 | + |
| 75 | + expect(tokens).toBeDefined() |
| 76 | + expect(tokens[0][0]).toBeDefined() |
| 77 | + // CSS variable should get -dim suffix |
| 78 | + expect(tokens[0][0].color).toBe('var(--my-ansi-red-dim)') |
| 79 | + }) |
| 80 | + |
| 81 | + it('should not modify colors without dim decoration', () => { |
| 82 | + // No dim decoration, just color |
| 83 | + const code = '\x1B[38;2;18;52;86mtest\x1B[0m' |
| 84 | + const tokens = tokenizeAnsiWithTheme(mockTheme, code) |
| 85 | + |
| 86 | + expect(tokens).toBeDefined() |
| 87 | + expect(tokens[0][0]).toBeDefined() |
| 88 | + // Should not be dimmed |
| 89 | + expect(tokens[0][0].color).toBe('#123456') |
| 90 | + }) |
| 91 | +}) |
0 commit comments