diff --git a/src/ascii_manipulation.rs b/src/ascii_manipulation.rs index d6bd24d..e228ca4 100644 --- a/src/ascii_manipulation.rs +++ b/src/ascii_manipulation.rs @@ -116,7 +116,7 @@ pub fn to_braille_ascii(image: DynamicImage, threshold: u8) -> Vec> { } } } - str.push(Ascii { char: char::from_u32(braille_value).unwrap(), rgb: [(r/8) as u8, (g/8) as u8, (b/8) as u8] }); + str.push(Ascii::new_with_char(char::from_u32(braille_value).unwrap(), (r/8) as u8, (g/8) as u8, (b/8) as u8)); } out.push(str); str = Vec::new(); diff --git a/src/cli.rs b/src/cli.rs index 31b51d0..49802ee 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,8 +18,13 @@ pub struct Cli { #[arg(short = 'c', long)] pub complex: bool, /// Display ASCII art in full colour + /// + /// Takes priority over '--grayscale' #[arg(short = 'C', long)] pub colour: bool, + /// Display ASCII art in grayscale + #[arg(short= 'g', long)] + pub grayscale: bool, /// Use braille characters instead of ASCII /// /// Takes priority over '--complex' and '--map' @@ -49,7 +54,7 @@ pub struct Cli { pub width: Option, /// Save the output to a file, instead of printing to terminal /// - /// Incompatible with '--colour' + /// Incompatible with '--colour' and '--grayscale' #[arg(short = 'o', long = "output")] pub output: Option, } @@ -62,6 +67,7 @@ impl Cli { pub fn debug_print(&self) { debug!("complex: {}", self.complex); debug!("colour: {}", self.colour); + debug!("grayscale: {}", self.grayscale); debug!("braille: {}", self.braille); debug!("debug: {}", self.debug); debug!("full: {}", self.full); diff --git a/src/main.rs b/src/main.rs index 2117d0a..9515a0b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,6 +14,12 @@ mod ascii_manipulation; mod output; mod model_rgb_ascii; +//todo: +/* custom background color, print font in white + * --formats flag + * --version flag + */ + /// This is the main function. /// /// It drives the CLI module, calls the appropriate convert functions and directs output. @@ -45,6 +51,6 @@ fn main() { if let Some(output) = cli.output { print_file(out, output); } else { - print_terminal(out, cli.colour); + print_terminal(out, cli.colour, cli.grayscale); } } diff --git a/src/model_rgb_ascii.rs b/src/model_rgb_ascii.rs index 147e494..6f39f71 100644 --- a/src/model_rgb_ascii.rs +++ b/src/model_rgb_ascii.rs @@ -1,9 +1,15 @@ //! This module contains the Ascii struct. /// This struct represents one character + rgb colour. +/// +/// attributes: +/// char: char - the character associated with this ASCII value +/// rgb: [u8; 3] - vec of 3 u8 values representing red, green, and blue respectively +/// col_depthL u8 - the depth of the rgb values, calculated through the luminosity method pub struct Ascii { pub char: char, pub rgb: [u8; 3], + pub col_depth: u8, } impl Ascii { @@ -21,6 +27,25 @@ impl Ascii { Ascii { char: char::from(ch), rgb: [r, g, b], + col_depth: (r as f32 * 0.3 + g as f32 * 0.59 + b as f32 * 0.11) as u8, + } + } + + /// This function creates a new Ascii instance. + /// + /// Arguments: + /// ch: char - the character + /// r: u8 - Red colour value + /// g: u8 - Green colour value + /// b: u8 - Blue colour value + /// + /// returns: + /// self + pub fn new_with_char(ch: char, r: u8, g: u8, b: u8) -> Self { + Ascii { + char: ch, + rgb: [r, g, b], + col_depth: (r as f32 * 0.3 + g as f32 * 0.59 + b as f32 * 0.11) as u8, } } } \ No newline at end of file diff --git a/src/output.rs b/src/output.rs index 1e14975..fd21317 100644 --- a/src/output.rs +++ b/src/output.rs @@ -13,7 +13,7 @@ use crate::model_rgb_ascii::Ascii; /// arguments: /// art: Vec> - vector of vectors representing the art /// in_colour: bool - whether to print in colour or as hard white -pub fn print_terminal(art: Vec>, in_colour: bool) { +pub fn print_terminal(art: Vec>, in_colour: bool, grayscale: bool) { for line in art { for ascii in line { if in_colour { @@ -21,6 +21,11 @@ pub fn print_terminal(art: Vec>, in_colour: bool) { SetForegroundColor(Color::Rgb {r: ascii.rgb[0], g: ascii.rgb[1], b: ascii.rgb[2]}), Print(ascii.char), ResetColor); + } else if grayscale { + let _ = execute!(stdout(), + SetForegroundColor(Color::Rgb {r: ascii.col_depth, g: ascii.col_depth, b: ascii.col_depth}), + Print(ascii.char), + ResetColor); } else { print!("{}", ascii.char); }