Skip to content

Commit

Permalink
feat: rgb_cmyk_conversion (#831)
Browse files Browse the repository at this point in the history
* feat: rgb_cmyk_conversion

* Update src/conversions/rgb_cmyk_conversion.rs

Co-authored-by: Piotr Idzik <[email protected]>

* Update src/conversions/rgb_cmyk_conversion.rs

Co-authored-by: Piotr Idzik <[email protected]>

* fix: compile error (switching macro and distruction to tuple)

* style: use `rgb` in tests

---------

Co-authored-by: Piotr Idzik <[email protected]>
  • Loading branch information
ali77gh and vil02 authored Oct 30, 2024
1 parent c906255 commit 3422002
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
* [Length Conversion](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/length_conversion.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)
Expand Down
2 changes: 2 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod hexadecimal_to_decimal;
mod length_conversion;
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;
Expand All @@ -16,3 +17,4 @@ pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
pub use self::length_conversion::length_conversion;
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;
60 changes: 60 additions & 0 deletions src/conversions/rgb_cmyk_conversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// 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(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) = (
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
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::*;

macro_rules! test_rgb_to_cmyk {
($($name:ident: $tc:expr,)*) => {
$(
#[test]
fn $name() {
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)),
}
}

0 comments on commit 3422002

Please sign in to comment.