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,9 +1,15 @@
|
|||||||
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(
|
||||||
|
img: &image::DynamicImage,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
invert: bool,
|
||||||
|
pixel_size: usize,
|
||||||
|
) -> String {
|
||||||
let mut ascii_img = String::new();
|
let mut ascii_img = String::new();
|
||||||
|
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
@@ -34,22 +40,36 @@ fn grayscale_ascii(img: &image::DynamicImage, width: u32, height: u32, invert: b
|
|||||||
ascii_img
|
ascii_img
|
||||||
}
|
}
|
||||||
|
|
||||||
fn colorful_ascii(img: &image::DynamicImage, width: u32, height: u32, pixel_size: usize) -> String {
|
fn colorful_ascii(
|
||||||
|
img: &image::DynamicImage,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
invert: bool,
|
||||||
|
pixel_size: usize,
|
||||||
|
) -> String {
|
||||||
let mut ascii_img = String::new();
|
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;
|
||||||
|
|
||||||
|
if invert {
|
||||||
|
red = 255 - red;
|
||||||
|
green = 255 - green;
|
||||||
|
blue = 255 - blue;
|
||||||
|
|
||||||
|
brightness = 255 - brightness;
|
||||||
|
}
|
||||||
|
|
||||||
let mut char_pixel = select_char(&brightness).repeat(pixel_size);
|
let mut char_pixel = select_char(&brightness).repeat(pixel_size);
|
||||||
|
|
||||||
@@ -80,7 +100,7 @@ fn select_char(brightness: &u8) -> String {
|
|||||||
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();
|
||||||
@@ -92,7 +112,7 @@ 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)
|
||||||
|
|||||||
Reference in New Issue
Block a user