Advertisement
cwchen

[Rust] The HSL model constructor of Color class

Aug 28th, 2017
3,520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.57 KB | None | 0 0
  1. impl Color {
  2.     // Constructor for HSL model
  3.     pub fn from_hsl(h: u16, s: f64, l: f64) -> Color {
  4.         if !Self::is_valid_hue(h) {
  5.             panic!("Invalid Hue value {} in HSL model", h);
  6.         }
  7.  
  8.         if !Self::is_valid_ratio(s) {
  9.             panic!("Invalid Saturation value {} in HSL model", s);
  10.         }
  11.  
  12.         if !Self::is_valid_ratio(l) {
  13.             panic!("Invalid Lightness value {} in HSL model", l);
  14.         }
  15.  
  16.         Color::HSL{ h: h, s: s, l: l }
  17.     }
  18. }
  19.  
  20. impl Color {
  21.     fn is_valid_hue(n: u16) -> bool {
  22.         n < 360
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement