commonMain.LinearInterpolator.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of interpolate-jvm Show documentation
Show all versions of interpolate-jvm Show documentation
A collection of drawing/charting utilities
package com.juul.krayon.interpolate
import com.juul.krayon.color.Color
import com.juul.krayon.color.lerp
import com.juul.krayon.time.minus
import com.juul.krayon.time.plus
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDateTime
internal class LinearIntInterpolator(
private val start: Int,
stop: Int,
) : BidirectionalInterpolator {
private val range = stop - start
override fun interpolate(fraction: Float): Int = start + (range * fraction).toInt()
override fun invert(value: Int): Float = (value - start).toFloat() / range
}
internal class LinearFloatInterpolator(
private val start: Float,
stop: Float,
) : BidirectionalInterpolator {
private val range = stop - start
override fun interpolate(fraction: Float): Float = start + (range * fraction)
override fun invert(value: Float): Float = (value - start) / range
}
internal class LinearDoubleInterpolator(
private val start: Double,
stop: Double,
) : BidirectionalInterpolator {
private val range = stop - start
override fun interpolate(fraction: Float): Double = start + (range * fraction)
override fun invert(value: Double): Float = ((value - start) / range).toFloat()
}
internal class LinearInstantInterpolator(
private val start: Instant,
stop: Instant,
) : BidirectionalInterpolator {
private val range = stop - start
override fun interpolate(fraction: Float): Instant = start + (range * fraction.toDouble())
override fun invert(value: Instant): Float = ((value - start) / range).toFloat()
}
internal class LinearLocalDateTimeInterpolator(
private val start: LocalDateTime,
stop: LocalDateTime,
) : BidirectionalInterpolator {
private val range = stop - start
override fun interpolate(fraction: Float): LocalDateTime = start + (range * fraction.toDouble())
override fun invert(value: LocalDateTime): Float = ((value - start) / range).toFloat()
}
internal class ArgbLinearColorInterpolator(
private val start: Color,
private val stop: Color,
) : Interpolator {
override fun interpolate(fraction: Float): Color = lerp(start, stop, fraction)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy