Advertisement
cwchen

[Rust] The CYMK model constructor of Color class

Aug 28th, 2017
3,531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.77 KB | None | 0 0
  1. impl Color {
  2.     // Constructor for CMYK model
  3.     pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Color {
  4.         if !Self::is_valid_ratio(c) {
  5.             panic!("Invalid Cyan value {} in CMYK model", c);
  6.         }
  7.  
  8.         if !Self::is_valid_ratio(m) {
  9.             panic!("Invalid Magenta value {} in CMYK model", m);
  10.         }
  11.  
  12.         if !Self::is_valid_ratio(y) {
  13.             panic!("Invalid Yellow value {} in CMYK model", y);
  14.         }
  15.  
  16.         if !Self::is_valid_ratio(k) {
  17.             panic!("Invalid Black value {} in CMYK model", k);
  18.         }
  19.  
  20.         Color::CMYK{ c: c, m: m, y: y, k: k }
  21.     }
  22. }
  23.  
  24. impl Color {
  25.     // Private class method used to validate ratio value
  26.     fn is_valid_ratio(n: f64) -> bool {
  27.         0.0 <= n && n <= 1.0
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement