From 6d83619c53bc904b7f38644b4ee01615720a3940 Mon Sep 17 00:00:00 2001 From: Ali Ghahremani Date: Wed, 23 Oct 2024 22:43:49 +0330 Subject: [PATCH 1/5] feat: rgb_cmyk_conversion --- DIRECTORY.md | 1 + src/conversions/mod.rs | 2 + src/conversions/rgb_cmyk_conversion.rs | 56 ++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 src/conversions/rgb_cmyk_conversion.rs diff --git a/DIRECTORY.md b/DIRECTORY.md index f4e1fa0e58c..128e6b69337 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -55,6 +55,7 @@ * [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs) * [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs) * [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs) + * [RGB to CMYK](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs) * Data Structures * [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs) * [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs) diff --git a/src/conversions/mod.rs b/src/conversions/mod.rs index af02e16a631..fb15cab237e 100644 --- a/src/conversions/mod.rs +++ b/src/conversions/mod.rs @@ -6,6 +6,7 @@ mod hexadecimal_to_binary; mod hexadecimal_to_decimal; mod octal_to_binary; mod octal_to_decimal; +mod rgb_cmyk_conversion; pub use self::binary_to_decimal::binary_to_decimal; pub use self::binary_to_hexadecimal::binary_to_hexadecimal; pub use self::decimal_to_binary::decimal_to_binary; @@ -14,3 +15,4 @@ pub use self::hexadecimal_to_binary::hexadecimal_to_binary; pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal; pub use self::octal_to_binary::octal_to_binary; pub use self::octal_to_decimal::octal_to_decimal; +pub use self::rgb_cmyk_conversion::rgb_to_cmyk; diff --git a/src/conversions/rgb_cmyk_conversion.rs b/src/conversions/rgb_cmyk_conversion.rs new file mode 100644 index 00000000000..c94987ca788 --- /dev/null +++ b/src/conversions/rgb_cmyk_conversion.rs @@ -0,0 +1,56 @@ +/// Author : https://github.com/ali77gh\ +/// References:\ +/// RGB: https://en.wikipedia.org/wiki/RGB_color_model\ +/// CMYK: https://en.wikipedia.org/wiki/CMYK_color_model\ + +/// This function Converts RGB to CMYK format +/// +/// ### Params +/// * `r` - red +/// * `g` - green +/// * `b` - blue +/// +/// ### Returns +/// (C, M, Y, K) +pub fn rgb_to_cmyk(r: u8, g: u8, b: u8) -> (u8, u8, u8, u8) { + // Safety: no need to check if input is positive and less than 255 because it's u8 + + // change scale from [0,255] to [0,1] + let (r, g, b) = (r as f64 / 255f64, g as f64 / 255f64, b as f64 / 255f64); + + match 1f64 - r.max(g).max(b) { + 1f64 => (0, 0, 0, 100), // pure black + k => ( + (100f64 * (1f64 - r - k) / (1f64 - k)) as u8, // c + (100f64 * (1f64 - g - k) / (1f64 - k)) as u8, // m + (100f64 * (1f64 - b - k) / (1f64 - k)) as u8, // y + (100f64 * k) as u8, // k + ), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rgb_to_cmyk_test() { + // white + assert_eq!(rgb_to_cmyk(255, 255, 255), (0, 0, 0, 0)); + + // gray + assert_eq!(rgb_to_cmyk(128, 128, 128), (0, 0, 0, 49)); + + // black + assert_eq!(rgb_to_cmyk(0, 0, 0), (0, 0, 0, 100)); + + // red + assert_eq!(rgb_to_cmyk(255, 0, 0), (0, 100, 100, 0)); + + // green + assert_eq!(rgb_to_cmyk(0, 255, 0), (100, 0, 100, 0)); + + // blue + assert_eq!(rgb_to_cmyk(0, 0, 255), (100, 100, 0, 0)); + } +} From 548395218fe32422f64aafd755ebdb871cc3be66 Mon Sep 17 00:00:00 2001 From: Ali Ghahremani Date: Tue, 29 Oct 2024 23:23:48 +0330 Subject: [PATCH 2/5] Update src/conversions/rgb_cmyk_conversion.rs Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- src/conversions/rgb_cmyk_conversion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conversions/rgb_cmyk_conversion.rs b/src/conversions/rgb_cmyk_conversion.rs index c94987ca788..a2ce909dea9 100644 --- a/src/conversions/rgb_cmyk_conversion.rs +++ b/src/conversions/rgb_cmyk_conversion.rs @@ -12,7 +12,7 @@ /// /// ### Returns /// (C, M, Y, K) -pub fn rgb_to_cmyk(r: u8, g: u8, b: u8) -> (u8, u8, u8, u8) { +pub fn rgb_to_cmyk(rgb: (u8, u8, u8)) -> (u8, u8, u8, u8) { // Safety: no need to check if input is positive and less than 255 because it's u8 // change scale from [0,255] to [0,1] From 3e2510174db7a57567ea24cc37f24664c9947e9b Mon Sep 17 00:00:00 2001 From: Ali Ghahremani Date: Tue, 29 Oct 2024 23:24:31 +0330 Subject: [PATCH 3/5] Update src/conversions/rgb_cmyk_conversion.rs Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- src/conversions/rgb_cmyk_conversion.rs | 36 +++++++++++++------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/conversions/rgb_cmyk_conversion.rs b/src/conversions/rgb_cmyk_conversion.rs index a2ce909dea9..706f2b40c0b 100644 --- a/src/conversions/rgb_cmyk_conversion.rs +++ b/src/conversions/rgb_cmyk_conversion.rs @@ -33,24 +33,24 @@ pub fn rgb_to_cmyk(rgb: (u8, u8, u8)) -> (u8, u8, u8, u8) { mod tests { use super::*; - #[test] - fn rgb_to_cmyk_test() { - // white - assert_eq!(rgb_to_cmyk(255, 255, 255), (0, 0, 0, 0)); - - // gray - assert_eq!(rgb_to_cmyk(128, 128, 128), (0, 0, 0, 49)); - - // black - assert_eq!(rgb_to_cmyk(0, 0, 0), (0, 0, 0, 100)); - - // red - assert_eq!(rgb_to_cmyk(255, 0, 0), (0, 100, 100, 0)); - - // green - assert_eq!(rgb_to_cmyk(0, 255, 0), (100, 0, 100, 0)); + macro_rules! test_rgb_to_cmyk { + ($($name:ident: $tc:expr,)*) => { + $( + #[test] + fn $name() { + let (r, g, b, cmyk) = $tc; + assert_eq!(rgb_to_cmyk(r, g, b), cmyk); + } + )* + } + } - // blue - assert_eq!(rgb_to_cmyk(0, 0, 255), (100, 100, 0, 0)); + test_rgb_to_cmyk! { + white: (255, 255, 255, (0, 0, 0, 0)), + gray: (128, 128, 128, (0, 0, 0, 49)), + black: (0, 0, 0, (0, 0, 0, 100)), + red: (255, 0, 0, (0, 100, 100, 0)), + green: (0, 255, 0, (100, 0, 100, 0)), + blue: (0, 0, 255, (100, 100, 0, 0)), } } From e72459f4de58c74a5c94e08c1196e37eabf319eb Mon Sep 17 00:00:00 2001 From: Ali Ghahremani Date: Tue, 29 Oct 2024 23:31:04 +0330 Subject: [PATCH 4/5] fix: compile error (switching macro and distruction to tuple) --- src/conversions/rgb_cmyk_conversion.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/conversions/rgb_cmyk_conversion.rs b/src/conversions/rgb_cmyk_conversion.rs index 706f2b40c0b..4c714e38e18 100644 --- a/src/conversions/rgb_cmyk_conversion.rs +++ b/src/conversions/rgb_cmyk_conversion.rs @@ -16,7 +16,11 @@ pub fn rgb_to_cmyk(rgb: (u8, u8, u8)) -> (u8, u8, u8, u8) { // Safety: no need to check if input is positive and less than 255 because it's u8 // change scale from [0,255] to [0,1] - let (r, g, b) = (r as f64 / 255f64, g as f64 / 255f64, b as f64 / 255f64); + let (r, g, b) = ( + rgb.0 as f64 / 255f64, + rgb.1 as f64 / 255f64, + rgb.2 as f64 / 255f64, + ); match 1f64 - r.max(g).max(b) { 1f64 => (0, 0, 0, 100), // pure black @@ -39,7 +43,7 @@ mod tests { #[test] fn $name() { let (r, g, b, cmyk) = $tc; - assert_eq!(rgb_to_cmyk(r, g, b), cmyk); + assert_eq!(rgb_to_cmyk((r, g, b)), cmyk); } )* } From 126f8936d8d7270e159e9bf9f4126ba25cc22e75 Mon Sep 17 00:00:00 2001 From: Piotr Idzik <65706193+vil02@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:05:57 +0100 Subject: [PATCH 5/5] style: use `rgb` in tests --- src/conversions/rgb_cmyk_conversion.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/conversions/rgb_cmyk_conversion.rs b/src/conversions/rgb_cmyk_conversion.rs index 4c714e38e18..30a8bc9bd84 100644 --- a/src/conversions/rgb_cmyk_conversion.rs +++ b/src/conversions/rgb_cmyk_conversion.rs @@ -42,19 +42,19 @@ mod tests { $( #[test] fn $name() { - let (r, g, b, cmyk) = $tc; - assert_eq!(rgb_to_cmyk((r, g, b)), cmyk); + let (rgb, cmyk) = $tc; + assert_eq!(rgb_to_cmyk(rgb), cmyk); } )* } } test_rgb_to_cmyk! { - white: (255, 255, 255, (0, 0, 0, 0)), - gray: (128, 128, 128, (0, 0, 0, 49)), - black: (0, 0, 0, (0, 0, 0, 100)), - red: (255, 0, 0, (0, 100, 100, 0)), - green: (0, 255, 0, (100, 0, 100, 0)), - blue: (0, 0, 255, (100, 100, 0, 0)), + white: ((255, 255, 255), (0, 0, 0, 0)), + gray: ((128, 128, 128), (0, 0, 0, 49)), + black: ((0, 0, 0), (0, 0, 0, 100)), + red: ((255, 0, 0), (0, 100, 100, 0)), + green: ((0, 255, 0), (100, 0, 100, 0)), + blue: ((0, 0, 255), (100, 100, 0, 0)), } }