Transparent images have now a background with the common white-gray pattern

This commit is contained in:
azy 2025-02-07 10:00:54 +08:00
parent 80e70bcc67
commit c92c05f40f
2 changed files with 49 additions and 3 deletions

View File

@ -229,13 +229,15 @@ fn build_image_paragraph(
let lines = image
.pixel_grid
.into_iter()
.map(|double_pixel_line| {
Line::from_iter(double_pixel_line.iter().map(
|(color_up, color_down)| {
.enumerate()
.map(|(y, double_pixel_line)| {
Line::from_iter(double_pixel_line.iter().enumerate().map(
|(x, (color_up, color_down))| {
convert_pixel_to_span(
*color_up,
*color_down,
Some(colorscheme.highlight_bg),
(x, y),
)
},
))
@ -545,11 +547,13 @@ pub fn convert_pixel_to_span<'a>(
color_up: ImageColor,
color_down: ImageColor,
background: Option<Color>,
position: (usize, usize),
) -> Span<'a> {
let bg_color = match background {
Some(Color::Rgb(r, g, b)) => Some((r, g, b)),
_ => None,
};
// mean of background and picture
let (color_up, color_down) = if let Some(bg_color) = bg_color {
let color_up_with_alpha = Color::Rgb(
(color_up.r * color_up.a + bg_color.0 * 255 - color_up.a) / 255,
@ -567,6 +571,25 @@ pub fn convert_pixel_to_span<'a>(
(color_up_with_alpha, color_down_with_alpha)
} else {
let alpha_threshold = 30;
let color_up = if color_up.a <= alpha_threshold {
if (position.0 + position.1 * 2) % 2 == 0 {
ImageColor::WHITE
} else {
ImageColor::GRAY
}
} else {
color_up
};
let color_down = if color_down.a <= alpha_threshold {
if (position.0 + position.1 * 2 + 1) % 2 == 0 {
ImageColor::WHITE
} else {
ImageColor::GRAY
}
} else {
color_down
};
(
convert_image_color_to_ratatui_color(color_up),
convert_image_color_to_ratatui_color(color_down),

View File

@ -62,3 +62,26 @@ pub struct ImageColor {
pub b: u8,
pub a: u8,
}
impl ImageColor {
pub fn new(r: u8, g: u8, b: u8, a: u8) -> Self {
ImageColor { r, g, b, a }
}
pub const BLACK: ImageColor = ImageColor {
r: 0,
g: 0,
b: 0,
a: 255,
};
pub const WHITE: ImageColor = ImageColor {
r: 255,
g: 255,
b: 255,
a: 255,
};
pub const GRAY: ImageColor = ImageColor {
r: 242,
g: 242,
b: 242,
a: 255,
};
}