Value arithmetic for Drafter.Widget.Slider.
Every function takes the slider's bounds as min_value, max_value and a step
that may be nil, and returns a value inside those bounds. A range whose bounds
and step are all integers keeps integer values; any other range works in floats,
rounded to the decimals the step implies so that repeated stepping does not
accumulate float error.
A nil step means "continuous", which is still quantised: step_size/3 supplies
a hundredth of the range as the working step, whole numbers for an integer range.
Summary
Functions
value held inside min_value..max_value.
How many decimals a step needs to be written exactly, up to 6.
Where value sits in the range, as 0.0 at min_value through 1.0 at
max_value.
Whether the range works in integers.
value moved count steps, stopping at the ends of the range.
value moved onto the nearest step from min_value and clamped into the range.
The working step for a range.
The value at fraction of the way through the range, snapped to the working step.
Functions
value held inside min_value..max_value.
iex> Drafter.Widget.Slider.Scale.clamp(1.5, 0.0, 1.0)
1.0
@spec decimals(number()) :: non_neg_integer()
How many decimals a step needs to be written exactly, up to 6.
iex> Drafter.Widget.Slider.Scale.decimals(0.25)
2
iex> Drafter.Widget.Slider.Scale.decimals(5)
0
Where value sits in the range, as 0.0 at min_value through 1.0 at
max_value.
Values outside the range come back clamped, and a range with no span reads as
0.0.
iex> Drafter.Widget.Slider.Scale.fraction(50, 0, 100)
0.5
iex> Drafter.Widget.Slider.Scale.fraction(5, 5, 5)
0.0
Whether the range works in integers.
True when min_value, max_value and step are all integers; a nil step counts
as integral when both bounds are.
iex> Drafter.Widget.Slider.Scale.integral?(0, 10, 1)
true
iex> Drafter.Widget.Slider.Scale.integral?(0.0, 1.0, nil)
false
value moved count steps, stopping at the ends of the range.
count is negative to move down.
iex> Drafter.Widget.Slider.Scale.nudge(0.5, 0.0, 1.0, 0.1, 1)
0.6
iex> Drafter.Widget.Slider.Scale.nudge(0.0, 0.0, 1.0, 0.1, -1)
0.0
value moved onto the nearest step from min_value and clamped into the range.
A nil or non-positive step clamps only.
iex> Drafter.Widget.Slider.Scale.snap(0.37, 0.0, 1.0, 0.25)
0.25
iex> Drafter.Widget.Slider.Scale.snap(2.6, 0, 10, 1)
3
The working step for a range.
An explicit step is returned as given. A nil step becomes a hundredth of the
range, rounded up to at least 1 when the range is integral.
iex> Drafter.Widget.Slider.Scale.step_size(0.0, 1.0, nil)
0.01
iex> Drafter.Widget.Slider.Scale.step_size(0, 3, nil)
1
iex> Drafter.Widget.Slider.Scale.step_size(0.0, 1.0, 0.25)
0.25
The value at fraction of the way through the range, snapped to the working step.
iex> Drafter.Widget.Slider.Scale.value_at(0.5, 0, 100, nil)
50
iex> Drafter.Widget.Slider.Scale.value_at(0.42, 0.0, 1.0, 0.25)
0.5