Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/conversions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
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 (r, g, b, cmyk) = $tc;
assert_eq!(rgb_to_cmyk((r, g, b)), 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)),
}
}