daily pastebin goal
70%
SHARE
TWEET

[Rust] The HSL model constructor of Color class

cwchen Aug 28th, 2017 324 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top