-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
feat: rgb_cmyk_conversion #831
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6d83619
feat: rgb_cmyk_conversion
ali77gh 5483952
Update src/conversions/rgb_cmyk_conversion.rs
ali77gh 3e25101
Update src/conversions/rgb_cmyk_conversion.rs
ali77gh e72459f
fix: compile error (switching macro and distruction to tuple)
ali77gh 126f893
style: use `rgb` in tests
vil02 efbe4af
Merge branch 'master' into rgb_cmyk_conversion
vil02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } | ||
ali77gh marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.