invert color

This commit is contained in:
2024-07-07 13:51:40 +02:00
parent 05316561f2
commit 0d7e950503

View File

@@ -1,99 +1,119 @@
use image;
use image::GenericImageView;
use crate::libs::args; use crate::libs::args;
use colored::Colorize; use colored::Colorize;
use image;
use image::GenericImageView;
fn grayscale_ascii(img: &image::DynamicImage, width: u32, height: u32, invert: bool, pixel_size: usize) -> String { fn grayscale_ascii(
let mut ascii_img = String::new(); img: &image::DynamicImage,
width: u32,
height: u32,
invert: bool,
pixel_size: usize,
) -> String {
let mut ascii_img = String::new();
for y in 0..height { for y in 0..height {
for x in 0..width { for x in 0..width {
let pixel = img.get_pixel(x, y); let pixel = img.get_pixel(x, y);
let red = pixel[0]; let red = pixel[0];
let green = pixel[1]; let green = pixel[1];
let blue = pixel[2]; let blue = pixel[2];
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()]; let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap(); let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap(); let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
let mut brightness = ((darkest_color + brightest_color) / 2) as u8; let mut brightness = ((darkest_color + brightest_color) / 2) as u8;
if invert { if invert {
brightness = 255 - brightness; brightness = 255 - brightness;
} }
let char_pixel = select_char(&brightness).repeat(pixel_size); let char_pixel = select_char(&brightness).repeat(pixel_size);
ascii_img.push_str(&char_pixel); ascii_img.push_str(&char_pixel);
}
ascii_img.push('\n');
} }
ascii_img.push('\n');
}
ascii_img ascii_img
} }
fn colorful_ascii(img: &image::DynamicImage, width: u32, height: u32, pixel_size: usize) -> String { fn colorful_ascii(
let mut ascii_img = String::new(); img: &image::DynamicImage,
width: u32,
height: u32,
invert: bool,
pixel_size: usize,
) -> String {
let mut ascii_img = String::new();
for y in 0..height { for y in 0..height {
for x in 0..width { for x in 0..width {
let pixel = img.get_pixel(x, y); let pixel = img.get_pixel(x, y);
let red = pixel[0]; let mut red = pixel[0];
let green = pixel[1]; let mut green = pixel[1];
let blue = pixel[2]; let mut blue = pixel[2];
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()]; let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap(); let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap(); let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
let brightness = ((darkest_color + brightest_color) / 2) as u8; let mut brightness = ((darkest_color + brightest_color) / 2) as u8;
let mut char_pixel = select_char(&brightness).repeat(pixel_size); if invert {
red = 255 - red;
green = 255 - green;
blue = 255 - blue;
char_pixel = select_dominant_color((red, green, blue), char_pixel); brightness = 255 - brightness;
}
ascii_img.push_str(&char_pixel); let mut char_pixel = select_char(&brightness).repeat(pixel_size);
char_pixel = select_dominant_color((red, green, blue), char_pixel);
ascii_img.push_str(&char_pixel);
}
ascii_img.push('\n');
} }
ascii_img.push('\n');
}
ascii_img ascii_img
} }
fn select_char(brightness: &u8) -> String { fn select_char(brightness: &u8) -> String {
let char = match brightness { let char = match brightness {
0..=25 => " ", 0..=25 => " ",
26..=50 => ".", 26..=50 => ".",
51..=75 => ":", 51..=75 => ":",
76..=100 => "-", 76..=100 => "-",
101..=125 => "=", 101..=125 => "=",
126..=150 => "+", 126..=150 => "+",
151..=175 => "*", 151..=175 => "*",
176..=200 => "#", 176..=200 => "#",
201..=225 => "%", 201..=225 => "%",
226..=255 => "@", 226..=255 => "@",
}; };
char.into() char.into()
} }
fn select_dominant_color(pixel : (u8, u8, u8), char_pixel: String) -> String { fn select_dominant_color(pixel: (u8, u8, u8), char_pixel: String) -> String {
let (red, green, blue) = pixel; let (red, green, blue) = pixel;
let char_pixel = char_pixel.truecolor(red, green, blue).to_string(); let char_pixel = char_pixel.truecolor(red, green, blue).to_string();
char_pixel char_pixel
} }
pub fn to_ascii(img: &image::DynamicImage, args: &args::Args) -> String { pub fn to_ascii(img: &image::DynamicImage, args: &args::Args) -> String {
let (width, height) = img.dimensions(); let (width, height) = img.dimensions();
if args.colorful { if args.colorful {
return colorful_ascii(img, width, height, args.pixel); return colorful_ascii(img, width, height, args.invert, args.pixel);
} }
grayscale_ascii(img, width, height, args.invert, args.pixel) grayscale_ascii(img, width, height, args.invert, args.pixel)
} }