Skip to main content

alpha2_model/
domain.rs

1//! The values a numeric place accepts, shared by validation and by operations that write values.
2
3/// An inclusive range of accepted values; an unbounded end is infinite.
4#[derive(Copy, Clone, Debug, PartialEq)]
5pub struct Domain {
6    /// The least value.
7    pub min: f32,
8    /// The greatest value.
9    pub max: f32,
10}
11
12impl Domain {
13    /// Every finite value: positions, scales, rotations.
14    pub const ANY: Self = Self::new(f32::NEG_INFINITY, f32::INFINITY);
15    /// `0..=1`: opacities, roundness, transition progress.
16    pub const UNIT: Self = Self::new(0.0, 1.0);
17    /// `0..`: lengths, sizes, feathers.
18    pub const NON_NEGATIVE: Self = Self::new(0.0, f32::INFINITY);
19    /// `-1..=1`: pans.
20    pub const PAN: Self = Self::new(-1.0, 1.0);
21    /// `0..=MAX_GAIN`: clip and bus gains.
22    pub const GAIN: Self = Self::new(0.0, crate::MAX_GAIN);
23    /// The points of a polystar.
24    pub const POLYSTAR_POINTS: Self =
25        Self::new(crate::MIN_POLYSTAR_POINTS, crate::MAX_POLYSTAR_POINTS);
26
27    /// The range `min..=max`.
28    #[must_use]
29    pub const fn new(min: f32, max: f32) -> Self {
30        Self { min, max }
31    }
32
33    /// Whether `value` is finite and inside.
34    #[must_use]
35    pub fn contains(self, value: f32) -> bool {
36        value.is_finite() && self.min <= value && value <= self.max
37    }
38
39    /// `value` moved to the nearest end when outside.
40    #[must_use]
41    pub const fn clamp(self, value: f32) -> f32 {
42        value.clamp(self.min, self.max)
43    }
44}