fx: comment style
This commit is contained in:
22
src/cli.rs
22
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
|
/// For each provided colour (either explicitly or in the second image), finds the closest colour
|
||||||
/// from the first image, using redmean distance.
|
/// from the first image, using redmean distance.
|
||||||
pub struct Cli {
|
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)]
|
#[arg(value_parser = valid_image_file)]
|
||||||
pub image: DynamicImage,
|
pub image: DynamicImage,
|
||||||
|
|
||||||
// Optional second image; if used, all colours from this image will be matched to their closest
|
/// Optional second image; if used, all colours from this image will be matched to their closest
|
||||||
// equivalent in the first image.
|
/// equivalent in the first image.
|
||||||
#[arg(short = 'i', long = "image", value_parser = valid_image_file)]
|
#[arg(short = 'i', long = "image", value_parser = valid_image_file)]
|
||||||
pub second_image: Option<DynamicImage>,
|
pub second_image: Option<DynamicImage>,
|
||||||
|
|
||||||
// Optional list of hex values; if used, will match each value to their closest equivalent in
|
/// Optional list of hex values; if used, will match each value to their closest equivalent in
|
||||||
// the first image
|
/// the first image
|
||||||
//
|
///
|
||||||
// Should provide valid hex codes only: '24274a', and '#cad3f5' are both valid.
|
/// Should provide valid hex codes only: '24274a', and '#cad3f5' are both valid.
|
||||||
#[arg(short = 'c', long = "colours", value_parser = valid_hex_code)]
|
#[arg(short = 'c', long = "colours", value_parser = valid_hex_code)]
|
||||||
pub colours: Option<Vec<Colour>>,
|
pub colours: Option<Vec<Colour>>,
|
||||||
}
|
}
|
||||||
@@ -42,9 +42,9 @@ impl Debug for Cli {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validates that a given string represents a hex code for a RGB colour.
|
/// Validates that a given string represents a hex code for a RGB colour.
|
||||||
//
|
///
|
||||||
// Optionally accepts a leading '#'.
|
/// Optionally accepts a leading '#'.
|
||||||
fn valid_hex_code(s: &str) -> Result<Colour, String> {
|
fn valid_hex_code(s: &str) -> Result<Colour, String> {
|
||||||
// accept leading '#' for hex values
|
// accept leading '#' for hex values
|
||||||
let start = match s.chars().nth(0) == Some('#') {
|
let start = match s.chars().nth(0) == Some('#') {
|
||||||
@@ -63,7 +63,7 @@ fn valid_hex_code(s: &str) -> Result<Colour, String> {
|
|||||||
Ok(Rgb(rgb))
|
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<DynamicImage, String> {
|
fn valid_image_file(s: &str) -> Result<DynamicImage, String> {
|
||||||
ImageReader::open(s)
|
ImageReader::open(s)
|
||||||
.map_err(|e| format!("{}", e))?
|
.map_err(|e| format!("{}", e))?
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ use std::fmt::Debug;
|
|||||||
|
|
||||||
use crate::encode_hex;
|
use crate::encode_hex;
|
||||||
|
|
||||||
// This type is used to efficiently store all colour values from an image.
|
/// 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.
|
/// 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).
|
/// Additionally stores a Vec of points to remember which colour maps to which pixel(s).
|
||||||
pub type ColourMap = HashMap<u8, HashMap<u8, HashMap<u8, Vec<Point>>>>;
|
pub type ColourMap = HashMap<u8, HashMap<u8, HashMap<u8, Vec<Point>>>>;
|
||||||
|
|
||||||
// We use one colour type.
|
/// We use one colour type.
|
||||||
pub type Colour = Rgb<u8>;
|
pub type Colour = Rgb<u8>;
|
||||||
|
|
||||||
// Struct to represent a point in an image.
|
/// Struct to represent a point in an image.
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Point {
|
pub struct Point {
|
||||||
x: u32,
|
x: u32,
|
||||||
@@ -27,16 +27,16 @@ impl Debug for Point {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Struct to save found matches for colours.
|
/// Struct to save found matches for colours.
|
||||||
// includes the matched colour, and a list of pixels with that colour.
|
/// includes the matched colour, and a list of pixels with that colour.
|
||||||
pub struct Match {
|
pub struct Match {
|
||||||
// matched colour
|
/// matched colour
|
||||||
pub colour: Colour,
|
pub colour: Colour,
|
||||||
// list of pixels with the matching colour
|
/// list of pixels with the matching colour
|
||||||
pub positions: Vec<Point>,
|
pub positions: Vec<Point>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
pub fn extract_colours(img: DynamicImage) -> ColourMap {
|
||||||
let mut out: ColourMap = HashMap::new();
|
let mut out: ColourMap = HashMap::new();
|
||||||
for (x, y, p) in img.pixels() {
|
for (x, y, p) in img.pixels() {
|
||||||
@@ -51,7 +51,7 @@ pub fn extract_colours(img: DynamicImage) -> ColourMap {
|
|||||||
out
|
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<Rgb<u8>> {
|
fn extract_colours_set(img: DynamicImage) -> HashSet<Rgb<u8>> {
|
||||||
let mut out = HashSet::new();
|
let mut out = HashSet::new();
|
||||||
img.pixels().for_each(|(_x, _y, p)| {
|
img.pixels().for_each(|(_x, _y, p)| {
|
||||||
@@ -60,11 +60,11 @@ fn extract_colours_set(img: DynamicImage) -> HashSet<Rgb<u8>> {
|
|||||||
out.into_iter().map(|c| Rgb([c[0], c[1], c[2]])).collect()
|
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.
|
/// Finds the "closest" match to a colour in the provided map. Uses redmean distance.
|
||||||
//
|
///
|
||||||
// arguments:
|
/// arguments:
|
||||||
// colour: Colour which will be matched against all.
|
/// colour: Colour which will be matched against all.
|
||||||
// all: Map of Colours to match against.
|
/// all: Map of Colours to match against.
|
||||||
fn closest_colour(colour: &Colour, all: &ColourMap) -> Option<Match> {
|
fn closest_colour(colour: &Colour, all: &ColourMap) -> Option<Match> {
|
||||||
// use redmean to calculate the distance between colours
|
// use redmean to calculate the distance between colours
|
||||||
let mut closest: Option<Match> = None;
|
let mut closest: Option<Match> = None;
|
||||||
@@ -100,13 +100,13 @@ fn closest_colour(colour: &Colour, all: &ColourMap) -> Option<Match> {
|
|||||||
return closest;
|
return closest;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Maps a list of colours to the closest equivalents in the provided map.
|
/// Maps a list of colours to the closest equivalents in the provided map.
|
||||||
//
|
///
|
||||||
// arguments:
|
/// arguments:
|
||||||
// all_cols: Map of colours match against.
|
/// all_cols: Map of colours match against.
|
||||||
// cols: Vec of Colours which will be matched against all_cols.
|
/// cols: Vec of Colours which will be matched against all_cols.
|
||||||
//
|
///
|
||||||
// returns: Hashmap of colour, match pairs.
|
/// returns: Hashmap of colour, match pairs.
|
||||||
pub fn map_image_list(all_cols: &ColourMap, cols: Vec<Colour>) -> HashMap<Colour, Match> {
|
pub fn map_image_list(all_cols: &ColourMap, cols: Vec<Colour>) -> HashMap<Colour, Match> {
|
||||||
let mut output_cols: HashMap<Colour, Match> = HashMap::new();
|
let mut output_cols: HashMap<Colour, Match> = HashMap::new();
|
||||||
|
|
||||||
@@ -119,13 +119,13 @@ pub fn map_image_list(all_cols: &ColourMap, cols: Vec<Colour>) -> HashMap<Colour
|
|||||||
output_cols
|
output_cols
|
||||||
}
|
}
|
||||||
|
|
||||||
// Maps the colours of one image to the closest equivalents in the provided map.
|
/// Maps the colours of one image to the closest equivalents in the provided map.
|
||||||
//
|
///
|
||||||
// arguments:
|
/// arguments:
|
||||||
// img_1_cols: Map of colours to match against.
|
/// img_1_cols: Map of colours to match against.
|
||||||
// img_2: DynamicImage for which each colour will be matched against img_1_cols.
|
/// img_2: DynamicImage for which each colour will be matched against img_1_cols.
|
||||||
//
|
///
|
||||||
// returns: Hashmap of colour, match pairs.
|
/// returns: Hashmap of colour, match pairs.
|
||||||
pub fn map_image_image(img_1_cols: &ColourMap, img_2: DynamicImage) -> HashMap<Colour, Match> {
|
pub fn map_image_image(img_1_cols: &ColourMap, img_2: DynamicImage) -> HashMap<Colour, Match> {
|
||||||
let img_2_cols = extract_colours_set(img_2);
|
let img_2_cols = extract_colours_set(img_2);
|
||||||
|
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ use dotenv::dotenv;
|
|||||||
use crate::cli::Cli;
|
use crate::cli::Cli;
|
||||||
use crate::colours::{Colour, Match, extract_colours, map_image_image, map_image_list};
|
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 {
|
fn encode_hex(c: &Colour) -> String {
|
||||||
format!("#{:02x}{:02x}{:02x}", c[0], c[1], c[2])
|
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<Colour, Match>) {
|
fn pretty_print(h: HashMap<Colour, Match>) {
|
||||||
h.iter().for_each(|(k, v)| {
|
h.iter().for_each(|(k, v)| {
|
||||||
println!(
|
println!(
|
||||||
@@ -26,7 +26,7 @@ fn pretty_print(h: HashMap<Colour, Match>) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver code.
|
/// Driver code.
|
||||||
fn main() {
|
fn main() {
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|||||||
Reference in New Issue
Block a user