Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: rgb_cmyk_conversion #831

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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;
56 changes: 56 additions & 0 deletions src/conversions/rgb_cmyk_conversion.rs
Original file line number Diff line number Diff line change
@@ -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));
}
}