diff --git a/README.md b/README.md index 4cafa33..d9e8dab 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #51 | [Asteroid Collision](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-51_Asteroid-Collision) | :white_check_mark: | | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_check_mark: | | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_check_mark: | -| Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_large_square: | +| Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_check_mark: | | Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: | | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_large_square: | | Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_large_square: | diff --git a/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/Cargo.toml b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/Cargo.toml new file mode 100644 index 0000000..1ed7c4c --- /dev/null +++ b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day54" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/lib.rs b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/lib.rs new file mode 100644 index 0000000..c823be8 --- /dev/null +++ b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/lib.rs @@ -0,0 +1,3 @@ +pub fn rgb_to_hex(r: u8, g: u8, b: u8) -> String { + format!("#{:02x}{:02x}{:02x}", r, g, b) +} diff --git a/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/main.rs b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/main.rs new file mode 100644 index 0000000..cb82780 --- /dev/null +++ b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/src/main.rs @@ -0,0 +1,25 @@ +use std::io::{self, Write}; +use std::process::exit; + +use day54::rgb_to_hex; + +fn read_u8(request: &str) -> Result> { + print!("{}", request); + io::stdout().flush()?; + let mut input = String::new(); + io::stdin().read_line(&mut input)?; + input.trim().parse::().map_err(|e| e.into()) +} + +fn show_error(e: Box) -> u8 { + eprintln!("Error: {}", e); + exit(1); +} + +fn main() { + let r = read_u8("Enter the red value: ").unwrap_or_else(show_error); + let g = read_u8("Enter the green value: ").unwrap_or_else(show_error); + let b = read_u8("Enter the blue value: ").unwrap_or_else(show_error); + let hex = rgb_to_hex(r, g, b); + println!("rgb({}, {}, {}) = {}", r, g, b, hex); +} diff --git a/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/tests/examples.rs b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/tests/examples.rs new file mode 100644 index 0000000..b2795f3 --- /dev/null +++ b/Week-08/Day-54_RGB-To-Hex-Color-Converter/day54/tests/examples.rs @@ -0,0 +1,19 @@ +#[cfg(test)] +mod examples { + use day54::rgb_to_hex; + + #[test] + fn example1() { + assert_eq!(rgb_to_hex(0, 128, 192), "#0080c0"); + } + + #[test] + fn example2() { + assert_eq!(rgb_to_hex(45, 255, 192), "#2dffc0"); + } + + #[test] + fn example3() { + assert_eq!(rgb_to_hex(0, 0, 0), "#000000"); + } +}