colors.rs

  1// todo!()
  2use alacritty_terminal::term::color::Rgb as AlacRgb;
  3// use gpui::color::Color;
  4// use theme2::TerminalStyle;
  5
  6///Converts a 2, 8, or 24 bit color ANSI color to the GPUI equivalent
  7// pub fn convert_color(alac_color: &AnsiColor, style: &TerminalStyle) -> Color {
  8//     match alac_color {
  9//         //Named and theme defined colors
 10//         alacritty_terminal::ansi::Color::Named(n) => match n {
 11//             alacritty_terminal::ansi::NamedColor::Black => style.black,
 12//             alacritty_terminal::ansi::NamedColor::Red => style.red,
 13//             alacritty_terminal::ansi::NamedColor::Green => style.green,
 14//             alacritty_terminal::ansi::NamedColor::Yellow => style.yellow,
 15//             alacritty_terminal::ansi::NamedColor::Blue => style.blue,
 16//             alacritty_terminal::ansi::NamedColor::Magenta => style.magenta,
 17//             alacritty_terminal::ansi::NamedColor::Cyan => style.cyan,
 18//             alacritty_terminal::ansi::NamedColor::White => style.white,
 19//             alacritty_terminal::ansi::NamedColor::BrightBlack => style.bright_black,
 20//             alacritty_terminal::ansi::NamedColor::BrightRed => style.bright_red,
 21//             alacritty_terminal::ansi::NamedColor::BrightGreen => style.bright_green,
 22//             alacritty_terminal::ansi::NamedColor::BrightYellow => style.bright_yellow,
 23//             alacritty_terminal::ansi::NamedColor::BrightBlue => style.bright_blue,
 24//             alacritty_terminal::ansi::NamedColor::BrightMagenta => style.bright_magenta,
 25//             alacritty_terminal::ansi::NamedColor::BrightCyan => style.bright_cyan,
 26//             alacritty_terminal::ansi::NamedColor::BrightWhite => style.bright_white,
 27//             alacritty_terminal::ansi::NamedColor::Foreground => style.foreground,
 28//             alacritty_terminal::ansi::NamedColor::Background => style.background,
 29//             alacritty_terminal::ansi::NamedColor::Cursor => style.cursor,
 30//             alacritty_terminal::ansi::NamedColor::DimBlack => style.dim_black,
 31//             alacritty_terminal::ansi::NamedColor::DimRed => style.dim_red,
 32//             alacritty_terminal::ansi::NamedColor::DimGreen => style.dim_green,
 33//             alacritty_terminal::ansi::NamedColor::DimYellow => style.dim_yellow,
 34//             alacritty_terminal::ansi::NamedColor::DimBlue => style.dim_blue,
 35//             alacritty_terminal::ansi::NamedColor::DimMagenta => style.dim_magenta,
 36//             alacritty_terminal::ansi::NamedColor::DimCyan => style.dim_cyan,
 37//             alacritty_terminal::ansi::NamedColor::DimWhite => style.dim_white,
 38//             alacritty_terminal::ansi::NamedColor::BrightForeground => style.bright_foreground,
 39//             alacritty_terminal::ansi::NamedColor::DimForeground => style.dim_foreground,
 40//         },
 41//         //'True' colors
 42//         alacritty_terminal::ansi::Color::Spec(rgb) => Color::new(rgb.r, rgb.g, rgb.b, u8::MAX),
 43//         //8 bit, indexed colors
 44//         alacritty_terminal::ansi::Color::Indexed(i) => get_color_at_index(&(*i as usize), style),
 45//     }
 46// }
 47
 48/// TODO: Move this
 49///Converts an 8 bit ANSI color to it's GPUI equivalent.
 50///Accepts usize for compatibility with the alacritty::Colors interface,
 51///Other than that use case, should only be called with values in the [0,255] range
 52// pub fn get_color_at_index(index: &usize, style: &TerminalStyle) -> Color {
 53//     match index {
 54//         //0-15 are the same as the named colors above
 55//         0 => style.black,
 56//         1 => style.red,
 57//         2 => style.green,
 58//         3 => style.yellow,
 59//         4 => style.blue,
 60//         5 => style.magenta,
 61//         6 => style.cyan,
 62//         7 => style.white,
 63//         8 => style.bright_black,
 64//         9 => style.bright_red,
 65//         10 => style.bright_green,
 66//         11 => style.bright_yellow,
 67//         12 => style.bright_blue,
 68//         13 => style.bright_magenta,
 69//         14 => style.bright_cyan,
 70//         15 => style.bright_white,
 71//         //16-231 are mapped to their RGB colors on a 0-5 range per channel
 72//         16..=231 => {
 73//             let (r, g, b) = rgb_for_index(&(*index as u8)); //Split the index into it's ANSI-RGB components
 74//             let step = (u8::MAX as f32 / 5.).floor() as u8; //Split the RGB range into 5 chunks, with floor so no overflow
 75//             Color::new(r * step, g * step, b * step, u8::MAX) //Map the ANSI-RGB components to an RGB color
 76//         }
 77//         //232-255 are a 24 step grayscale from black to white
 78//         232..=255 => {
 79//             let i = *index as u8 - 232; //Align index to 0..24
 80//             let step = (u8::MAX as f32 / 24.).floor() as u8; //Split the RGB grayscale values into 24 chunks
 81//             Color::new(i * step, i * step, i * step, u8::MAX) //Map the ANSI-grayscale components to the RGB-grayscale
 82//         }
 83//         //For compatibility with the alacritty::Colors interface
 84//         256 => style.foreground,
 85//         257 => style.background,
 86//         258 => style.cursor,
 87//         259 => style.dim_black,
 88//         260 => style.dim_red,
 89//         261 => style.dim_green,
 90//         262 => style.dim_yellow,
 91//         263 => style.dim_blue,
 92//         264 => style.dim_magenta,
 93//         265 => style.dim_cyan,
 94//         266 => style.dim_white,
 95//         267 => style.bright_foreground,
 96//         268 => style.black, //'Dim Background', non-standard color
 97//         _ => Color::new(0, 0, 0, 255),
 98//     }
 99// }
100///Generates the rgb channels in [0, 5] for a given index into the 6x6x6 ANSI color cube
101///See: [8 bit ansi color](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit).
102///
103///Wikipedia gives a formula for calculating the index for a given color:
104///
105///index = 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
106///
107///This function does the reverse, calculating the r, g, and b components from a given index.
108// fn rgb_for_index(i: &u8) -> (u8, u8, u8) {
109//     debug_assert!((&16..=&231).contains(&i));
110//     let i = i - 16;
111//     let r = (i - (i % 36)) / 36;
112//     let g = ((i % 36) - (i % 6)) / 6;
113//     let b = (i % 36) % 6;
114//     (r, g, b)
115// }
116use gpui::Rgba;
117
118//Convenience method to convert from a GPUI color to an alacritty Rgb
119pub fn to_alac_rgb(color: impl Into<Rgba>) -> AlacRgb {
120    let color = color.into();
121    let r = ((color.r * color.a) * 255.) as u8;
122    let g = ((color.g * color.a) * 255.) as u8;
123    let b = ((color.b * color.a) * 255.) as u8;
124    AlacRgb::new(r, g, b)
125}
126
127// #[cfg(test)]
128// mod tests {
129//     #[test]
130//     fn test_rgb_for_index() {
131//         //Test every possible value in the color cube
132//         for i in 16..=231 {
133//             let (r, g, b) = crate::mappings::colors::rgb_for_index(&(i as u8));
134//             assert_eq!(i, 16 + 36 * r + 6 * g + b);
135//         }
136//     }
137// }