freya_terminal/
colors.rs

1use freya_core::prelude::Color;
2
3/// ANSI 16-color palette (matches WezTerm defaults)
4const ANSI_COLORS: [(u8, u8, u8); 16] = [
5    (0, 0, 0),       // Black
6    (204, 85, 85),   // Red
7    (85, 204, 85),   // Green
8    (215, 215, 0),   // Yellow
9    (84, 85, 203),   // Blue
10    (204, 85, 204),  // Magenta
11    (122, 202, 202), // Cyan
12    (204, 204, 204), // White
13    (85, 85, 85),    // Bright Black
14    (255, 85, 85),   // Bright Red
15    (85, 255, 85),   // Bright Green
16    (255, 255, 0),   // Bright Yellow
17    (85, 85, 255),   // Bright Blue
18    (255, 85, 255),  // Bright Magenta
19    (85, 255, 255),  // Bright Cyan
20    (255, 255, 255), // Bright White
21];
22
23/// 6x6x6 RGB cube levels for 256-color palette
24const RGB_LEVELS: [u8; 6] = [0u8, 95u8, 135u8, 175u8, 215u8, 255u8];
25
26/// Map VT100 color to Skia Color
27///
28/// If `is_bg` is true, Default maps to background color instead of foreground
29pub fn map_vt100_color(c: vt100::Color, default: Color) -> Color {
30    match c {
31        vt100::Color::Default => default,
32        vt100::Color::Rgb(r, g, b) => Color::from_rgb(r, g, b),
33        vt100::Color::Idx(idx) => {
34            let i = idx as usize;
35
36            // ANSI 16 colors
37            if i < 16 {
38                let (r, g, b) = ANSI_COLORS[i];
39                return Color::from_rgb(r, g, b);
40            }
41
42            // 6x6x6 RGB cube (216 colors, indices 16-231)
43            if (16..=231).contains(&i) {
44                let v = i - 16;
45                let r = v / 36;
46                let g = (v / 6) % 6;
47                let b = v % 6;
48                return Color::from_rgb(RGB_LEVELS[r], RGB_LEVELS[g], RGB_LEVELS[b]);
49            }
50
51            // Grayscale (24 colors, indices 232-255)
52            if (232..=255).contains(&i) {
53                let shade = 8 + ((i - 232) * 10) as u8;
54                return Color::from_rgb(shade, shade, shade);
55            }
56
57            // Fallback
58            default
59        }
60    }
61}