Skip to content

Commit c046c38

Browse files
committed
add valueToFraction and fractionToValue functions
1 parent 14cf18a commit c046c38

File tree

1 file changed

+12
-0
lines changed
  • slider/src/main/java/io/monstarlab/mosaic/slider

1 file changed

+12
-0
lines changed

slider/src/main/java/io/monstarlab/mosaic/slider/MathUtils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ internal fun scale(a1: Float, b1: Float, x1: Float, a2: Float, b2: Float) =
77

88
internal fun calcFraction(a: Float, b: Float, pos: Float) =
99
(if (b - a == 0f) 0f else (pos - a) / (b - a)).coerceIn(0f, 1f)
10+
11+
internal fun Float.valueToFraction(rangeStart: Float, rangeEnd: Float) =
12+
calcFraction(rangeStart, rangeEnd, this)
13+
14+
internal fun Float.valueToFraction(range: ClosedFloatingPointRange<Float>) =
15+
valueToFraction(range.start, range.endInclusive)
16+
17+
internal fun Float.fractionToValue(rangeStart: Float, rangeEnd: Float): Float =
18+
scale(0f, 1f, this.coerceIn(0f,1f), rangeStart, rangeEnd)
19+
20+
internal fun Float.fractionToValue(range: ClosedFloatingPointRange<Float>): Float =
21+
scale(0f, 1f, this, range.start, range.endInclusive)

0 commit comments

Comments
 (0)