fn parse_sgr_color(attrs: &[i64], i: &mut usize) -> Option<Color> {
    if attrs.len() < 2 {
        return None;
    }

    match attrs[*i + 1] {
        2 => {
            // RGB color spec
            if attrs.len() < 5 {
                debug!("Expected RGB color spec; got {:?}", attrs);
                return None;
            }

            let r = attrs[*i + 2];
            let g = attrs[*i + 3];
            let b = attrs[*i + 4];

            *i += 4;

            let range = 0..256;
            if !range.contains(&r) || !range.contains(&g) || !range.contains(&b) {
                debug!("Invalid RGB color spec: ({}, {}, {})", r, g, b);
                return None;
            }

            Some(Color::Spec(Rgb { r: r as u8, g: g as u8, b: b as u8 }))
        },
        5 => {
            if attrs.len() < 3 {
                debug!("Expected color index; got {:?}", attrs);
                None
            } else {
                *i += 2;
                let idx = attrs[*i];
                match idx {
                    0..=255 => Some(Color::Indexed(idx as u8)),
                    _ => {
                        debug!("Invalid color index: {}", idx);
                        None
                    },
                }
            }
        },
        _ => {
            debug!("Unexpected color attr: {}", attrs[*i + 1]);
            None
        },
    }
}
/// Parse a color specifier from list of attributes
Parse a color specifier from list of attributes
Parse a color specifier from list of attributes
fn parse_sgr_color(attrs: &[i64], i: &mut usize) -> Option<Color> {
    if attrs.len() < 2 {
        return None;
    }
    match attrs[*i + 1] {
        2 => {
            // RGB color spec
            if attrs.len() < 5 {
                debug!("Expected RGB color spec; got {:?}", attrs);
                return None;
            }
            let r = attrs[*i + 2];
            let g = attrs[*i + 3];
            let b = attrs[*i + 4];
            *i += 4;
            let range = 0..256;
            if !range.contains(&r) || !range.contains(&g) || !range.contains(&b) {
                debug!("Invalid RGB color spec: ({}, {}, {})", r, g, b);
                return None;
            }
            Some(Color::Spec(Rgb { r: r as u8, g: g as u8, b: b as u8 }))
        },
        5 => {
            if attrs.len() < 3 {
                debug!("Expected color index; got {:?}", attrs);
                None
            } else {
                *i += 2;
                let idx = attrs[*i];
                match idx {
                    0..=255 => Some(Color::Indexed(idx as u8)),
                    _ => {
                        debug!("Invalid color index: {}", idx);
                        None
                    },
                }
            }
        },
        _ => {
            debug!("Unexpected color attr: {}", attrs[*i + 1]);
            None
        },
    }
}
fn parse_sgr_color ( attrs : & [ i64 ] , i : & mut usize ) -> Option < Color > { if attrs . len ( ) < 2 { return None ; } match attrs [ * i + 1 ] { 2 => { // RGB color spec if attrs . len ( ) < 5 { debug ! ( " " attrs ) ; return None ; } let r = attrs [ * i + 2 ] ; let g = attrs [ * i + 3 ] ; let b = attrs [ * i + 4 ] ; * i += 4 ; let range = 0 .. 256 ; if ! range . contains ( & r ) || ! range . contains ( & g ) || ! range . contains ( & b ) { debug ! ( " " r g b ) ; return None ; } Some ( Color :: Spec ( Rgb { r : r as u8 , g : g as u8 , b : b as u8 } ) ) } , 5 => { if attrs . len ( ) < 3 { debug ! ( " " attrs ) ; None } else { * i += 2 ; let idx = attrs [ * i ] ; match idx { 0 ..= 255 => Some ( Color :: Indexed ( idx as u8 ) ) , _ => { debug ! ( " " idx ) ; None } , } } } , _ => { debug ! ( " " attrs [ i 1 ] ) ; None } , } }
Parse a color specifier from list of attributes
Parse a color specifier from list of attributes
attrs
&[i64]
i
&mut usize
attrs
&[i64]
i
&mut usize