diff options
| author | 2020-02-21 15:22:54 -0600 | |
|---|---|---|
| committer | 2020-04-24 15:13:22 -0500 | |
| commit | 62fddce2e676123b1325a16d144a1d0674dcd1ff (patch) | |
| tree | 1bd3680b00e19b0bd2caa4c222c5c882296f3001 /core | |
| parent | 70081c96496ec58563150fe32ee2c4e2a0d46e6a (diff) | |
| download | iced-62fddce2e676123b1325a16d144a1d0674dcd1ff.tar.gz iced-62fddce2e676123b1325a16d144a1d0674dcd1ff.tar.bz2 iced-62fddce2e676123b1325a16d144a1d0674dcd1ff.zip  | |
Add check_rgba fn to clamp float values to [0,1]
Diffstat (limited to '')
| -rw-r--r-- | core/src/color.rs | 26 | 
1 files changed, 23 insertions, 3 deletions
diff --git a/core/src/color.rs b/core/src/color.rs index db509b88..799d85c9 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -2,12 +2,17 @@  #[derive(Debug, Clone, Copy, PartialEq)]  #[allow(missing_docs)]  pub struct Color { +    /// Red component, 0.0 - 1.0      pub r: f32, +    /// Green component, 0.0 - 1.0      pub g: f32, +    /// Blue component, 0.0 - 1.0      pub b: f32, +    /// Transparency, 0.0 - 1.0      pub a: f32,  } +  impl Color {      /// The black color.      pub const BLACK: Color = Color { @@ -33,11 +38,26 @@ impl Color {          a: 0.0,      }; +    /// Calmps a float value to the range [0.0, 1.0] +    pub fn clamp(v: f32) -> f32 { +        v.max(0.0f32).min(1.0f32) +    } + +    /// Ensures RGBA values on the range [0.0, 1.0] +    pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color { +        Color { +            r: Color::clamp(r), +            g: Color::clamp(g), +            b: Color::clamp(b), +            a: Color::clamp(a), +        } +    } +      /// Creates a [`Color`] from its RGB components.      ///      /// [`Color`]: struct.Color.html -    pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color { -        Color { r, g, b, a: 1.0 } +    pub fn from_rgb(r: f32, g: f32, b: f32) -> Color { +        Color::check_rgba(r, g, b, 1.0)      }      /// Creates a [`Color`] from its RGB8 components. @@ -55,7 +75,7 @@ impl Color {              r: f32::from(r) / 255.0,              g: f32::from(g) / 255.0,              b: f32::from(b) / 255.0, -            a, +            a: Color::clamp(a),          }      }  | 
