Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color mismatch issue #12

Open
d47081 opened this issue Dec 5, 2024 · 0 comments
Open

Color mismatch issue #12

d47081 opened this issue Dec 5, 2024 · 0 comments

Comments

@d47081
Copy link

d47081 commented Dec 5, 2024

Could somebody please try to parse this code:

        �[38;5;77m_�[0m   �[38;5;77m/n\�[0m          
       �[38;5;77m//.\�[0m �[38;5;77m||�[0m �[38;5;77m\|�[0m        
      �[38;5;77m//�[0m  �[38;5;77m\\|`//\�[0m        
      �[38;5;77m|�[0m  �[38;5;77m||\|//�[0m  �[38;5;77m\�[0m       
          �[38;5;77m/\V/|�[0m   �[38;5;77m|�[0m      
         �[38;5;77m|�[0m  �[38;5;77m||�[0m           
            �[38;5;77m||/\�[0m         
�[38;5;95m.�[0m  �[38;5;95m,�[0m �[38;5;95m_�[0m �[38;5;95m.�[0m �[38;5;95m.,�[0m �[38;5;77m||�[0m �[38;5;95m_�[0m �[38;5;95m.,�[0m �[38;5;95m_�[0m �[38;5;95m.�[0m  
  �[38;5;95m^�[0m      �[38;5;95m'�[0m        �[38;5;95m`�[0m    �[38;5;95m'�[0m 

name  : "Christopher"
stage : common luminous violet seed-bearing brugmansia
age   : 878 days
rate  : 1st generation (x1.0)
score : 63166430
coord : 23 South, 9 East
water : |�[38;5;27m███       �[0m| 30%

For some reasons, it return Color::BrightMagenta instead of some green:

Pasted image (2)

But should be like on this picture:

Pasted image

Not sure it's bug in my integration or this format not supported yet. I'm using following code to integrate cansi with GTK TextTag by extracting with categorise_text function:

mod tag;
use tag::Tag;

use cansi::{v3::categorise_text, Color};
use gtk::{
    gdk::RGBA,
    pango::{Style, Underline},
    prelude::TextTagExt,
    TextTag,
};

/// Apply `ANSI`/`SGR` format to new buffer
pub fn format(source_code: &str) -> Vec<(TextTag, String)> {
    // Init new line buffer
    let mut buffer = Vec::new();

    // Begin entities parse
    for entity in categorise_text(source_code) {
        // Create new tag from default preset
        let tag = Tag::new();

        // Apply supported decorations
        if let Some(color) = entity.fg {
            tag.text_tag
                .set_foreground_rgba(Some(&color_to_rgba(color)));
        }

        if let Some(color) = entity.bg {
            tag.text_tag
                .set_background_rgba(Some(&color_to_rgba(color)));
        }

        if let Some(is_italic) = entity.italic {
            if is_italic {
                tag.text_tag.set_style(Style::Italic);
            }
        }

        if let Some(is_underline) = entity.underline {
            if is_underline {
                tag.text_tag.set_underline(Underline::Single);
            }
        }

        if let Some(is_strikethrough) = entity.strikethrough {
            tag.text_tag.set_strikethrough(is_strikethrough);
        }

        // Append
        buffer.push((tag.text_tag, entity.text.to_string()));
    }

    buffer
}

fn color_to_rgba(value: Color) -> RGBA {
    match value {
        Color::Black => RGBA::new(0.0, 0.0, 0.0, 1.0),
        Color::Red => RGBA::new(0.8, 0.0, 0.0, 1.0),
        Color::Green => RGBA::new(0.0, 0.8, 0.0, 1.0),
        Color::Yellow => RGBA::new(0.8, 0.8, 0.0, 1.0),
        Color::Blue => RGBA::new(0.0, 0.0, 0.9, 1.0),
        Color::Magenta => RGBA::new(0.8, 0.0, 0.8, 1.0),
        Color::Cyan => RGBA::new(0.0, 0.8, 0.8, 1.0),
        Color::White => RGBA::new(0.9, 0.9, 0.9, 1.0),
        Color::BrightBlack => RGBA::new(0.5, 0.5, 0.5, 1.0),
        Color::BrightRed => RGBA::new(1.0, 0.0, 0.0, 1.0),
        Color::BrightGreen => RGBA::new(0.0, 1.0, 0.0, 1.0),
        Color::BrightYellow => RGBA::new(1.0, 1.0, 0.0, 1.0),
        Color::BrightBlue => RGBA::new(0.4, 0.4, 1.0, 1.0),
        Color::BrightMagenta => RGBA::new(1.0, 0.0, 1.0, 1.0),
        Color::BrightCyan => RGBA::new(0.0, 1.0, 1.0, 1.0),
        Color::BrightWhite => RGBA::new(1.0, 1.0, 1.0, 1.0),
    }
}
  • for a note: I'm parsing line-by line, maybe reason in that, even all lines could be parsed separately on subject example
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant