From 4edaa2ba03363fafc4fea4d2cd0be7c6bce30b46 Mon Sep 17 00:00:00 2001 From: djairoh Date: Thu, 25 Jun 2026 16:24:10 +0200 Subject: [PATCH] fx: comment style --- src/cli.rs | 22 +++++++++--------- src/colours.rs | 60 +++++++++++++++++++++++++------------------------- src/main.rs | 6 ++--- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 4aa4315..ade1fe6 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -15,19 +15,19 @@ use crate::Colour; /// For each provided colour (either explicitly or in the second image), finds the closest colour /// from the first image, using redmean distance. pub struct Cli { - // The source image from which colours should be picked. + /// The source image from which colours should be picked. #[arg(value_parser = valid_image_file)] pub image: DynamicImage, - // Optional second image; if used, all colours from this image will be matched to their closest - // equivalent in the first image. + /// Optional second image; if used, all colours from this image will be matched to their closest + /// equivalent in the first image. #[arg(short = 'i', long = "image", value_parser = valid_image_file)] pub second_image: Option, - // Optional list of hex values; if used, will match each value to their closest equivalent in - // the first image - // - // Should provide valid hex codes only: '24274a', and '#cad3f5' are both valid. + /// Optional list of hex values; if used, will match each value to their closest equivalent in + /// the first image + /// + /// Should provide valid hex codes only: '24274a', and '#cad3f5' are both valid. #[arg(short = 'c', long = "colours", value_parser = valid_hex_code)] pub colours: Option>, } @@ -42,9 +42,9 @@ impl Debug for Cli { } } -// Validates that a given string represents a hex code for a RGB colour. -// -// Optionally accepts a leading '#'. +/// Validates that a given string represents a hex code for a RGB colour. +/// +/// Optionally accepts a leading '#'. fn valid_hex_code(s: &str) -> Result { // accept leading '#' for hex values let start = match s.chars().nth(0) == Some('#') { @@ -63,7 +63,7 @@ fn valid_hex_code(s: &str) -> Result { Ok(Rgb(rgb)) } -// Validates that agiven string points to a valid image file. +/// Validates that agiven string points to a valid image file. fn valid_image_file(s: &str) -> Result { ImageReader::open(s) .map_err(|e| format!("{}", e))? diff --git a/src/colours.rs b/src/colours.rs index aef122d..9f323a8 100644 --- a/src/colours.rs +++ b/src/colours.rs @@ -6,15 +6,15 @@ use std::fmt::Debug; use crate::encode_hex; -// This type is used to efficiently store all colour values from an image. -// By using nested hashmaps we can save on space for colours with identical r and g values. -// Additionally stores a Vec of points to remember which colour maps to which pixel(s). +/// This type is used to efficiently store all colour values from an image. +/// By using nested hashmaps we can save on space for colours with identical r and g values. +/// Additionally stores a Vec of points to remember which colour maps to which pixel(s). pub type ColourMap = HashMap>>>; -// We use one colour type. +/// We use one colour type. pub type Colour = Rgb; -// Struct to represent a point in an image. +/// Struct to represent a point in an image. #[derive(Clone)] pub struct Point { x: u32, @@ -27,16 +27,16 @@ impl Debug for Point { } } -// Struct to save found matches for colours. -// includes the matched colour, and a list of pixels with that colour. +/// Struct to save found matches for colours. +/// includes the matched colour, and a list of pixels with that colour. pub struct Match { - // matched colour + /// matched colour pub colour: Colour, - // list of pixels with the matching colour + /// list of pixels with the matching colour pub positions: Vec, } -// Extract all colours from a provided image into a nested ColourMap +/// Extract all colours from a provided image into a nested ColourMap pub fn extract_colours(img: DynamicImage) -> ColourMap { let mut out: ColourMap = HashMap::new(); for (x, y, p) in img.pixels() { @@ -51,7 +51,7 @@ pub fn extract_colours(img: DynamicImage) -> ColourMap { out } -// Extract all colours in a provided image into a flat hashmap. +/// Extract all colours in a provided image into a flat hashmap. fn extract_colours_set(img: DynamicImage) -> HashSet> { let mut out = HashSet::new(); img.pixels().for_each(|(_x, _y, p)| { @@ -60,11 +60,11 @@ fn extract_colours_set(img: DynamicImage) -> HashSet> { out.into_iter().map(|c| Rgb([c[0], c[1], c[2]])).collect() } -// Finds the "closest" match to a colour in the provided map. Uses redmean distance. -// -// arguments: -// colour: Colour which will be matched against all. -// all: Map of Colours to match against. +/// Finds the "closest" match to a colour in the provided map. Uses redmean distance. +/// +/// arguments: +/// colour: Colour which will be matched against all. +/// all: Map of Colours to match against. fn closest_colour(colour: &Colour, all: &ColourMap) -> Option { // use redmean to calculate the distance between colours let mut closest: Option = None; @@ -100,13 +100,13 @@ fn closest_colour(colour: &Colour, all: &ColourMap) -> Option { return closest; } -// Maps a list of colours to the closest equivalents in the provided map. -// -// arguments: -// all_cols: Map of colours match against. -// cols: Vec of Colours which will be matched against all_cols. -// -// returns: Hashmap of colour, match pairs. +/// Maps a list of colours to the closest equivalents in the provided map. +/// +/// arguments: +/// all_cols: Map of colours match against. +/// cols: Vec of Colours which will be matched against all_cols. +/// +/// returns: Hashmap of colour, match pairs. pub fn map_image_list(all_cols: &ColourMap, cols: Vec) -> HashMap { let mut output_cols: HashMap = HashMap::new(); @@ -119,13 +119,13 @@ pub fn map_image_list(all_cols: &ColourMap, cols: Vec) -> HashMap HashMap { let img_2_cols = extract_colours_set(img_2); diff --git a/src/main.rs b/src/main.rs index 7037b44..31149fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,12 +9,12 @@ use dotenv::dotenv; use crate::cli::Cli; use crate::colours::{Colour, Match, extract_colours, map_image_image, map_image_list}; -// Encodes a RGB colour as a hex code. +/// Encodes a RGB colour as a hex code. fn encode_hex(c: &Colour) -> String { format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2]) } -// Formats found matches for CLI otuput. +/// Formats found matches for CLI otuput. fn pretty_print(h: HashMap) { h.iter().for_each(|(k, v)| { println!( @@ -26,7 +26,7 @@ fn pretty_print(h: HashMap) { }) } -// Driver code. +/// Driver code. fn main() { dotenv().ok(); pretty_env_logger::init();