added grayscale support

This commit is contained in:
Djairo Hougee 2023-02-09 13:46:12 +01:00
parent 21d740cf70
commit 8de99aefac
5 changed files with 46 additions and 4 deletions

View File

@ -116,7 +116,7 @@ pub fn to_braille_ascii(image: DynamicImage, threshold: u8) -> Vec<Vec<Ascii>> {
}
}
}
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();

View File

@ -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<u32>,
/// 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<PathBuf>,
}
@ -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);

View File

@ -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);
}
}

View File

@ -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,
}
}
}

View File

@ -13,7 +13,7 @@ use crate::model_rgb_ascii::Ascii;
/// arguments:
/// art: Vec<Vec<Ascii>> - vector of vectors representing the art
/// in_colour: bool - whether to print in colour or as hard white
pub fn print_terminal(art: Vec<Vec<Ascii>>, in_colour: bool) {
pub fn print_terminal(art: Vec<Vec<Ascii>>, 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<Vec<Ascii>>, 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);
}