Wrote program for Day 54

This commit is contained in:
Mariano Riefolo 2024-09-17 08:21:21 +02:00
parent 2e22b2400b
commit e1c0448375
5 changed files with 54 additions and 1 deletions

View File

@ -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 #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 #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 #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 #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 #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: | | 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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day54"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -0,0 +1,3 @@
pub fn rgb_to_hex(r: u8, g: u8, b: u8) -> String {
format!("#{:02x}{:02x}{:02x}", r, g, b)
}

View File

@ -0,0 +1,25 @@
use std::io::{self, Write};
use std::process::exit;
use day54::rgb_to_hex;
fn read_u8(request: &str) -> Result<u8, Box<dyn std::error::Error>> {
print!("{}", request);
io::stdout().flush()?;
let mut input = String::new();
io::stdin().read_line(&mut input)?;
input.trim().parse::<u8>().map_err(|e| e.into())
}
fn show_error(e: Box<dyn std::error::Error>) -> 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);
}

View File

@ -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");
}
}