From 06b6ae85e8a141ccee67f3597ccf37ca61f8dcc3 Mon Sep 17 00:00:00 2001 From: "Agatha V. Lovelace" Date: Sun, 9 Apr 2023 18:43:40 +0200 Subject: [PATCH] Support customizing colors --- README.md | 12 ++++++++---- src/main.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9a24be1..7b18f42 100644 --- a/README.md +++ b/README.md @@ -25,8 +25,12 @@ Arguments: Path to image to pick colors from Options: - -c, --colors Number of colors to generate (excluding bold colors) [default:8] - -b, --no-bold Skip generating bold color variants - -h, --help Print help - -V, --version Print version + -c, --colors Number of colors to generate (excluding bold colors) [default: 8] + -b, --no-bold Skip generating bold color variants + --bold-delta How much lightness should be added to bold colors [default: 0.2] + --rotate-hue Rotate colors along the hue axis [default: 0] + --lighten Lighten/darken colors [default: 0] + --saturate Saturate/desaturate colors [default: 0] + -h, --help Print help + -V, --version Print version ``` \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index e452fa7..28bb0dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,18 @@ struct Args { /// Skip generating bold color variants #[arg(short = 'b', long)] no_bold: bool, + /// How much lightness should be added to bold colors + #[arg(long, default_value_t = 0.2, value_parser = validate_color_delta, allow_hyphen_values = true)] + bold_delta: f64, + /// Rotate colors along the hue axis + #[arg(long, default_value_t = 0.0, allow_hyphen_values = true)] + rotate_hue: f64, + /// Lighten/darken colors + #[arg(long, default_value_t = 0.0, value_parser = validate_color_delta, allow_hyphen_values = true)] + lighten: f64, + /// Saturate/desaturate colors + #[arg(long, default_value_t = 0.0, value_parser = validate_color_delta, allow_hyphen_values = true)] + saturate: f64, } fn main() -> Result<()> { @@ -41,10 +53,18 @@ fn main() -> Result<()> { if !args.no_bold { // Create second pairs of lighter colors let bold_colors = colors.clone(); - let bold_colors = bold_colors.iter().map(|c| c.lighten(0.2)); + let bold_colors = bold_colors.iter().map(|c| c.lighten(args.bold_delta)); colors.extend(bold_colors); } + // Apply color transformations, if any + let colors: Vec<_> = colors + .into_iter() + .map(|c| c.rotate_hue(args.rotate_hue)) + .map(|c| c.lighten(args.lighten)) + .map(|c| c.saturate(args.saturate)) + .collect(); + let brush = Brush::from_mode(Some(ansi::Mode::TrueColor)); // Print colors with formatting turned off for pipes @@ -61,3 +81,15 @@ fn main() -> Result<()> { Ok(()) } + +/// Make sure that input value is between -1.0 and 1.0 +fn validate_color_delta(s: &str) -> Result { + let num = s + .parse() + .map_err(|_| format!("`{s}` is not a valid floating point number"))?; + if num >= -1.0 && num <= 1.0 { + Ok(num) + } else { + Err(format!("{num} is not in range [-1.0,1.0]")) + } +}