pub struct ChartRange {
pub min: f32,
pub max: f32,
}Expand description
A numeric domain used to map chart values.
Self::new and Self::sanitized replace non-finite input bounds and
order them. They attempt to widen a degenerate domain by adding 1.0 to
its maximum, but f32 rounding can leave very large equal bounds unchanged.
A span between finite bounds can also overflow to infinity.
§Examples
use ailloli_ui_core::ChartRange;
let range = ChartRange::new(10.0, 30.0);
assert_eq!(range.fraction_for_value(20.0), 0.5);
assert_eq!(range.value_for_fraction(0.25), 15.0);Fields§
§min: f32Inclusive lower domain bound.
max: f32Inclusive upper domain bound.
Implementations§
Source§impl ChartRange
impl ChartRange
Sourcepub fn new(min: f32, max: f32) -> Self
pub fn new(min: f32, max: f32) -> Self
Creates a sanitized range from two candidate bounds.
Non-finite min and max become 0.0 and 1.0, respectively;
reversed bounds are swapped; and a range no wider than
f32::EPSILON is expanded upward by adding 1.0 to its maximum.
At magnitudes where that increment is not representable, the bounds can
remain equal.
§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(4.0, 2.0), ChartRange { min: 2.0, max: 4.0 });Sourcepub fn fraction_for_value(self, value: f32) -> f32
pub fn fraction_for_value(self, value: f32) -> f32
Maps value into the inclusive normalized interval 0.0..=1.0.
Values outside the range are clamped. A non-finite value maps to 0.0.
Extreme finite bounds can produce an infinite or zero intermediate span;
in that case IEEE-754 arithmetic can produce NaN instead of a fraction.
§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(10.0, 30.0).fraction_for_value(20.0), 0.5);Sourcepub fn value_for_fraction(self, fraction: f32) -> f32
pub fn value_for_fraction(self, fraction: f32) -> f32
Maps a normalized fraction back into this range.
Fractions are clamped to 0.0..=1.0; a non-finite fraction selects
Self::min after sanitization. Extreme finite bounds can overflow the
intermediate span, so the returned value can be non-finite.
§Examples
use ailloli_ui_core::ChartRange;
assert_eq!(ChartRange::new(10.0, 30.0).value_for_fraction(0.25), 15.0);Trait Implementations§
Source§impl Clone for ChartRange
impl Clone for ChartRange
Source§fn clone(&self) -> ChartRange
fn clone(&self) -> ChartRange
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more