start_here/
start_here.rs

1use lineic::{
2    interpolators::{F32InterpolationBucket, F32LinearInterpolator},
3    LinearInterpolator,
4};
5
6fn main() {
7    // The simplest possible use of the library is mapping one range to another
8    // Here we can map values in the range 0.0..=10.0 to the range 30.0..=35.0
9    let interpolator = F32InterpolationBucket::new(0.0..=10.0, [30.0], [35.0]);
10    assert_eq!(interpolator.interpolate(5.0), [32.5]);
11
12    // The target does not have to be a single value - here we interpolate across a pair of RGB values
13    // The result is a smooth gradient from red to green for values in the range 0.0..=10.0
14    let interpolator =
15        F32InterpolationBucket::new(0.0..=10.0, [255.0, 0.0, 0.0], [0.0, 255.0, 0.0]);
16    assert_eq!(interpolator.interpolate(5.0), [127.5, 127.5, 0.0]);
17
18    // The library can also interpolate smoothly across multiple buckets
19    // This example forms a sort of traffic light sequence, interpolating between red, yellow, and green
20    // The range is reversed here to demonstrate that the library can handle that
21    let interpolator = F32LinearInterpolator::new(
22        10.0..=0.0,
23        &[[0.0, 255.0, 0.0], [255.0, 255.0, 0.0], [255.0, 0.0, 0.0]],
24    );
25    assert_eq!(interpolator.interpolate(5.0), [255.0, 255.0, 0.0]);
26    assert_eq!(interpolator.interpolate(0.0), [255.0, 0.0, 0.0]);
27
28    // The types for the range and values do not need to the same
29    // Here a f64 range is used to interpolate between u8 values
30    let interpolator: LinearInterpolator<'_, 3, f64, u8> =
31        LinearInterpolator::new(0.0..=10.0, &[[0, 255, 0], [255, 255, 0], [255, 0, 0]]);
32    assert_eq!(interpolator.interpolate(5.0), [127, 127, 0]);
33    assert_eq!(interpolator.interpolate(0.0), [0, 255, 0]);
34}