ferroui_base/media/immutable/
immutable_gradient_stop.rs1use crate::media::{Color, IGradientStop};
2use crate::utilities::MathUtilities;
3
4#[derive(Clone, Copy, Debug, PartialEq)]
6pub struct ImmutableGradientStop {
7 offset: f64,
8 color: Color,
9}
10
11impl ImmutableGradientStop {
12 pub fn new(offset: f64, color: Color) -> Self {
14 let mut offset = offset;
15 if MathUtilities::is_zero(offset) {
16 offset = 0.0;
17 }
18
19 Self { offset: offset.clamp(0.0, 1.0), color }
20 }
21
22 #[inline]
24 pub fn offset(&self) -> f64 {
25 self.offset
26 }
27
28 #[inline]
30 pub fn color(&self) -> Color {
31 self.color
32 }
33}
34
35impl IGradientStop for ImmutableGradientStop {
36 #[inline]
37 fn color(&self) -> Color {
38 self.color
39 }
40
41 #[inline]
42 fn offset(&self) -> f64 {
43 self.offset
44 }
45
46 fn is_immutable_gradient_stop(&self) -> bool {
47 true
48 }
49}
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54 use crate::media::Colors;
55
56 #[test]
57 fn offset_is_clamped() {
58 assert_eq!(0.0, ImmutableGradientStop::new(-1.0, Colors::RED).offset());
59 assert_eq!(1.0, ImmutableGradientStop::new(2.0, Colors::RED).offset());
60 assert_eq!(0.5, ImmutableGradientStop::new(0.5, Colors::RED).offset());
61 assert!(ImmutableGradientStop::new(-0.0, Colors::RED).offset().is_sign_positive());
62 assert!(ImmutableGradientStop::new(f64::NAN, Colors::RED).offset().is_nan());
63 }
64}