Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions problem-solving/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ name = "matrix_multiplication"
path = "src/basic/matrix_multiplication.rs"

[[bin]]
name = "color_converter"
path = "src/basic/color_converter.rs"
name = "color-converter"
path = "src/basic/color-converter.rs"

# ============================[ Dynamic Programmign]============================

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
/// To run/test the binary, pease use the following commands:
/// * cargo run --bin color_converter
/// * cargo test --bin color_converter
use std::cmp::{max, min};
/// * cargo run --bin color-converter
/// * cargo test --bin color-converter
use std::{
cmp::{max, min},
process::exit,
};

use common::input;

/// The enum `Color` can store one of the following values:
/// * RGB
Expand Down Expand Up @@ -155,7 +160,11 @@ impl Color {
Color::RGB(r, g, b)
}
Color::HEX(hex) => {
let (_, rgb) = hex.split_at(1);
let (_, rgb) = if hex.starts_with("#") {
hex.split_at(1)
} else {
("", hex.as_str())
};
println!("{:?}", rgb);
let rgb = u32::from_str_radix(rgb, 16).unwrap();
Color::RGB(
Expand Down Expand Up @@ -194,6 +203,36 @@ fn main() {
println!("blue HEX: {:?}", blue.to_hex());
println!("blue RGB: {:?}", blue.to_rgb());
println!("blue HSL: {:?}", blue.to_hsl());

println!("{}", "=".repeat(80));
match input("Enter Source Format:\n1. RGB\n2. HSL\n3.HEX\n\nANY OTHER KEY TO EXIT\n").as_str() {
"1" => {
let data = input("Enter color in RGB separated by space(example: 255 0 0): ");
let rgb: Vec<&str> = data.split(" ").collect();
let rgb: Vec<u8> = rgb.iter().map(|v| v.parse().unwrap()).collect();
let rgb = Color::RGB(rgb[0], rgb[1], rgb[2]);
println!("RGB to HEX: {:?}", rgb.to_hex());
println!("RGB to HSL: {:?}", rgb.to_hsl());
}
"2" => {
let data = input("Enter color in HSL separated by space(example: 60 50 75): ");
let hsl: Vec<&str> = data.split(" ").collect();
let hsl: Vec<u16> = hsl.iter().map(|v| v.parse().unwrap()).collect();
let hsl = Color::HSL(hsl[0], hsl[1] as u8, hsl[2] as u8);
println!("HSL to HEX: {:?}", hsl.to_hex());
println!("HSL to RGB: {:?}", hsl.to_rgb());
}
"3" => {
let data = input("Enter HEX color code(example: 0066aa or #0066aa): ");
let hex = Color::HEX(data);
println!("HEX to RGB: {:?}", hex.to_rgb());
println!("HEX to HSL: {:?}", hex.to_hsl());
}
_ => {
println!("Exiting now");
exit(0);
}
}
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions problem-solving/src/basic/comprehension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
* To run the code, run the following:
* =============================================================================
*
* cargo run --bin m3
* cargo test --bin m3
* cargo run --bin comprehension
* cargo test --bin comprehension
*
* =============================================================================
**/
macro_rules! comprehension {
(foreach $iterable:expr_2021; apply $function:expr_2021) => {
(foreach $iterable:expr; apply $function:expr) => {
$iterable.iter().map($function).collect::<Vec<_>>()
};
(foreach $iterable:expr_2021; apply $function:expr_2021; where $condition:expr_2021) => {
(foreach $iterable:expr; apply $function:expr; where $condition:expr) => {
$iterable
.iter()
.filter($condition)
Expand Down