mirror of
https://github.com/eRgo35/ascii.git
synced 2026-02-04 04:46:09 +01:00
invert color
This commit is contained in:
@@ -1,99 +1,119 @@
|
||||
use image;
|
||||
use image::GenericImageView;
|
||||
use crate::libs::args;
|
||||
use colored::Colorize;
|
||||
use image;
|
||||
use image::GenericImageView;
|
||||
|
||||
fn grayscale_ascii(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 x in 0..width {
|
||||
let pixel = img.get_pixel(x, y);
|
||||
fn grayscale_ascii(
|
||||
img: &image::DynamicImage,
|
||||
width: u32,
|
||||
height: u32,
|
||||
invert: bool,
|
||||
pixel_size: usize,
|
||||
) -> String {
|
||||
let mut ascii_img = String::new();
|
||||
|
||||
let red = pixel[0];
|
||||
let green = pixel[1];
|
||||
let blue = pixel[2];
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let pixel = img.get_pixel(x, y);
|
||||
|
||||
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
|
||||
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
|
||||
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
|
||||
let red = pixel[0];
|
||||
let green = pixel[1];
|
||||
let blue = pixel[2];
|
||||
|
||||
let mut brightness = ((darkest_color + brightest_color) / 2) as u8;
|
||||
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
|
||||
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
|
||||
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
|
||||
|
||||
if invert {
|
||||
brightness = 255 - brightness;
|
||||
}
|
||||
let mut brightness = ((darkest_color + brightest_color) / 2) as u8;
|
||||
|
||||
let char_pixel = select_char(&brightness).repeat(pixel_size);
|
||||
if invert {
|
||||
brightness = 255 - brightness;
|
||||
}
|
||||
|
||||
ascii_img.push_str(&char_pixel);
|
||||
let char_pixel = select_char(&brightness).repeat(pixel_size);
|
||||
|
||||
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 {
|
||||
let mut ascii_img = String::new();
|
||||
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let pixel = img.get_pixel(x, y);
|
||||
fn colorful_ascii(
|
||||
img: &image::DynamicImage,
|
||||
width: u32,
|
||||
height: u32,
|
||||
invert: bool,
|
||||
pixel_size: usize,
|
||||
) -> String {
|
||||
let mut ascii_img = String::new();
|
||||
|
||||
let red = pixel[0];
|
||||
let green = pixel[1];
|
||||
let blue = pixel[2];
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let pixel = img.get_pixel(x, y);
|
||||
|
||||
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
|
||||
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
|
||||
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
|
||||
let mut red = pixel[0];
|
||||
let mut green = pixel[1];
|
||||
let mut blue = pixel[2];
|
||||
|
||||
let brightness = ((darkest_color + brightest_color) / 2) as u8;
|
||||
let pixel_iterator: Vec<u16> = vec![red.into(), green.into(), blue.into()];
|
||||
let darkest_color: &u16 = pixel_iterator.iter().min().unwrap();
|
||||
let brightest_color: &u16 = pixel_iterator.iter().max().unwrap();
|
||||
|
||||
let mut char_pixel = select_char(&brightness).repeat(pixel_size);
|
||||
let mut brightness = ((darkest_color + brightest_color) / 2) as u8;
|
||||
|
||||
char_pixel = select_dominant_color((red, green, blue), char_pixel);
|
||||
if invert {
|
||||
red = 255 - red;
|
||||
green = 255 - green;
|
||||
blue = 255 - blue;
|
||||
|
||||
ascii_img.push_str(&char_pixel);
|
||||
brightness = 255 - brightness;
|
||||
}
|
||||
|
||||
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 {
|
||||
let char = match brightness {
|
||||
0..=25 => " ",
|
||||
26..=50 => ".",
|
||||
51..=75 => ":",
|
||||
76..=100 => "-",
|
||||
101..=125 => "=",
|
||||
126..=150 => "+",
|
||||
151..=175 => "*",
|
||||
176..=200 => "#",
|
||||
201..=225 => "%",
|
||||
226..=255 => "@",
|
||||
};
|
||||
|
||||
char.into()
|
||||
let char = match brightness {
|
||||
0..=25 => " ",
|
||||
26..=50 => ".",
|
||||
51..=75 => ":",
|
||||
76..=100 => "-",
|
||||
101..=125 => "=",
|
||||
126..=150 => "+",
|
||||
151..=175 => "*",
|
||||
176..=200 => "#",
|
||||
201..=225 => "%",
|
||||
226..=255 => "@",
|
||||
};
|
||||
|
||||
char.into()
|
||||
}
|
||||
|
||||
fn select_dominant_color(pixel : (u8, u8, u8), char_pixel: String) -> String {
|
||||
let (red, green, blue) = pixel;
|
||||
fn select_dominant_color(pixel: (u8, u8, u8), char_pixel: String) -> String {
|
||||
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 {
|
||||
let (width, height) = img.dimensions();
|
||||
let (width, height) = img.dimensions();
|
||||
|
||||
if args.colorful {
|
||||
return colorful_ascii(img, width, height, args.pixel);
|
||||
}
|
||||
if args.colorful {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user