iced_rizzen 0.14.0

Extra widgets for official releases of iced GUI library.
Documentation
// This file is part of `iced_rizzen` project. For the terms of use, please see the file
// called `LICENSE-BSD-3-Clause` at the top level of the `iced_rizzen` project's Git repository.

//! Provides a simple struct for holding the bounds of an [`f32`] range.

use crate::core::Pixels;

/// Specifies a `f32` range.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Range {
    min: f32,
    max: f32,
}

impl Range {
    /// Create a range from 0.0 to infinity.
    pub const ZERO_INFINITY: Range = Range {
        min: 0.0,
        max: f32::INFINITY,
    };

    /// Create a range with a minimum and maximum values.
    pub fn new(min: impl Into<Pixels>, max: impl Into<Pixels>) -> Self {
        let min = min.into();
        let max = max.into();
        if min.0 > max.0 {
            Self {
                min: max.0,
                max: min.0,
            }
        } else {
            Self {
                min: min.0,
                max: max.0,
            }
        }
    }

    /// Get the minimum value of the range.
    pub fn min(self) -> f32 {
        self.min
    }

    /// Get the maximum value of the range.
    pub fn max(self) -> f32 {
        self.max
    }

    /// Check if provided value is within the range.
    pub fn is_within(self, value: f32) -> bool {
        value >= self.min && value <= self.max
    }

    /// Ensure the value is within the range.
    pub fn clamp(self, value: f32) -> f32 {
        value.min(self.max).max(self.min)
    }
}