Skip to content

Commit 6d83619

Browse files
committed
feat: rgb_cmyk_conversion
1 parent 274ca13 commit 6d83619

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/hexadecimal_to_decimal.rs)
5656
* [Octal To Binary](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_binary.rs)
5757
* [Octal To Decimal](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/octal_to_decimal.rs)
58+
* [RGB to CMYK](https://github.com/TheAlgorithms/Rust/blob/master/src/conversions/rgb_cmyk_conversion.rs)
5859
* Data Structures
5960
* [Avl Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/avl_tree.rs)
6061
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)

src/conversions/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod hexadecimal_to_binary;
66
mod hexadecimal_to_decimal;
77
mod octal_to_binary;
88
mod octal_to_decimal;
9+
mod rgb_cmyk_conversion;
910
pub use self::binary_to_decimal::binary_to_decimal;
1011
pub use self::binary_to_hexadecimal::binary_to_hexadecimal;
1112
pub use self::decimal_to_binary::decimal_to_binary;
@@ -14,3 +15,4 @@ pub use self::hexadecimal_to_binary::hexadecimal_to_binary;
1415
pub use self::hexadecimal_to_decimal::hexadecimal_to_decimal;
1516
pub use self::octal_to_binary::octal_to_binary;
1617
pub use self::octal_to_decimal::octal_to_decimal;
18+
pub use self::rgb_cmyk_conversion::rgb_to_cmyk;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// Author : https://github.com/ali77gh\
2+
/// References:\
3+
/// RGB: https://en.wikipedia.org/wiki/RGB_color_model\
4+
/// CMYK: https://en.wikipedia.org/wiki/CMYK_color_model\
5+
6+
/// This function Converts RGB to CMYK format
7+
///
8+
/// ### Params
9+
/// * `r` - red
10+
/// * `g` - green
11+
/// * `b` - blue
12+
///
13+
/// ### Returns
14+
/// (C, M, Y, K)
15+
pub fn rgb_to_cmyk(r: u8, g: u8, b: u8) -> (u8, u8, u8, u8) {
16+
// Safety: no need to check if input is positive and less than 255 because it's u8
17+
18+
// change scale from [0,255] to [0,1]
19+
let (r, g, b) = (r as f64 / 255f64, g as f64 / 255f64, b as f64 / 255f64);
20+
21+
match 1f64 - r.max(g).max(b) {
22+
1f64 => (0, 0, 0, 100), // pure black
23+
k => (
24+
(100f64 * (1f64 - r - k) / (1f64 - k)) as u8, // c
25+
(100f64 * (1f64 - g - k) / (1f64 - k)) as u8, // m
26+
(100f64 * (1f64 - b - k) / (1f64 - k)) as u8, // y
27+
(100f64 * k) as u8, // k
28+
),
29+
}
30+
}
31+
32+
#[cfg(test)]
33+
mod tests {
34+
use super::*;
35+
36+
#[test]
37+
fn rgb_to_cmyk_test() {
38+
// white
39+
assert_eq!(rgb_to_cmyk(255, 255, 255), (0, 0, 0, 0));
40+
41+
// gray
42+
assert_eq!(rgb_to_cmyk(128, 128, 128), (0, 0, 0, 49));
43+
44+
// black
45+
assert_eq!(rgb_to_cmyk(0, 0, 0), (0, 0, 0, 100));
46+
47+
// red
48+
assert_eq!(rgb_to_cmyk(255, 0, 0), (0, 100, 100, 0));
49+
50+
// green
51+
assert_eq!(rgb_to_cmyk(0, 255, 0), (100, 0, 100, 0));
52+
53+
// blue
54+
assert_eq!(rgb_to_cmyk(0, 0, 255), (100, 100, 0, 0));
55+
}
56+
}

0 commit comments

Comments
 (0)