Remove lerp functions, add FloatRange annotations

This commit is contained in:
Kyant0 committed 2025-10-07 22:04:29 +08:00
1 parent 995a246a2f
commit 15732854e5
10 files changed
+18 -67

No files matched your search

@@ -1,15 +1,12 @@
package com.kyant.backdrop.effects
import android.graphics.RenderEffect
import android.graphics.Shader
import android.os.Build
import androidx.compose.ui.graphics.TileMode
import androidx.compose.ui.graphics.toAndroidTileMode
import androidx.annotation.FloatRange
import com.kyant.backdrop.BackdropEffectScope
fun BackdropEffectScope.blur(
blurRadius: Float,
tileMode: TileMode = TileMode.Clamp
) {
fun BackdropEffectScope.blur(@FloatRange(from = 0.0) blurRadius: Float) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) return
if (blurRadius <= 0f) return
@@ -20,13 +17,13 @@ fun BackdropEffectScope.blur(
blurRadius,
blurRadius,
currentEffect,
tileMode.toAndroidTileMode()
Shader.TileMode.CLAMP
)
} else {
RenderEffect.createBlurEffect(
blurRadius,
blurRadius,
tileMode.toAndroidTileMode()
Shader.TileMode.CLAMP
)
}
}
@@ -5,6 +5,7 @@ import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.RenderEffect
import android.os.Build
import androidx.annotation.FloatRange
import androidx.compose.ui.graphics.asAndroidColorFilter
import com.kyant.backdrop.BackdropEffectScope
import com.kyant.backdrop.GammaAdjustmentShaderString
@@ -26,7 +27,7 @@ fun BackdropEffectScope.colorFilter(colorFilter: androidx.compose.ui.graphics.Co
colorFilter(colorFilter.asAndroidColorFilter())
}
fun BackdropEffectScope.opacity(alpha: Float) {
fun BackdropEffectScope.opacity(@FloatRange(from = 0.0, to = 1.0) alpha: Float) {
val colorMatrix = ColorMatrix(
floatArrayOf(
1f, 0f, 0f, 0f, 0f,
@@ -2,6 +2,7 @@ package com.kyant.backdrop.effects
import android.graphics.RenderEffect
import android.os.Build
import androidx.annotation.FloatRange
import androidx.compose.foundation.shape.CornerBasedShape
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.unit.LayoutDirection
@@ -11,8 +12,8 @@ import com.kyant.backdrop.RoundedRectRefractionShaderString
import com.kyant.backdrop.RoundedRectRefractionWithDispersionShaderString
fun BackdropEffectScope.lens(
refractionHeight: Float,
refractionAmount: Float,
@FloatRange(from = 0.0) refractionHeight: Float,
@FloatRange(from = 0.0) refractionAmount: Float,
chromaticAberration: Offset = Offset.Zero,
depthEffect: Boolean = false
) {
@@ -1,17 +1,16 @@
package com.kyant.backdrop.highlight
import androidx.annotation.FloatRange
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.lerp
import androidx.compose.ui.util.lerp
@Immutable
data class Highlight(
val width: Dp = 0.5f.dp,
val blurRadius: Dp = width / 2f,
val alpha: Float = 1f,
@param:FloatRange(from = 0.0, to = 1.0) val alpha: Float = 1f,
val style: HighlightStyle = HighlightStyle.Default
) {
@@ -27,13 +26,3 @@ data class Highlight(
val Plain: Highlight = Highlight(style = HighlightStyle.Plain)
}
}
@Stable
fun lerp(start: Highlight, stop: Highlight, fraction: Float): Highlight {
return Highlight(
width = lerp(start.width, stop.width, fraction),
alpha = lerp(start.alpha, stop.alpha, fraction),
blurRadius = lerp(start.blurRadius, stop.blurRadius, fraction),
style = lerp(start.style, stop.style, fraction)
)
}
@@ -12,10 +12,8 @@ import androidx.compose.ui.graphics.RenderEffect
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.asComposeRenderEffect
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.util.fastCoerceAtMost
import androidx.compose.ui.util.lerp
import com.kyant.backdrop.AmbientHighlightShaderString
import com.kyant.backdrop.DefaultHighlightShaderString
import com.kyant.backdrop.RuntimeShaderCache
@@ -49,7 +47,7 @@ interface HighlightStyle {
@Immutable
data class Default(
val intensity: Float = 0.5f,
@param:FloatRange(from = 0.0, to = 1.0) val intensity: Float = 0.5f,
val angle: Float = 45f,
@param:FloatRange(from = 0.0) val falloff: Float = 1f
) : HighlightStyle {
@@ -81,7 +79,7 @@ interface HighlightStyle {
@Immutable
data class Ambient(
val intensity: Float = 0.5f
@param:FloatRange(from = 0.0, to = 1.0) val intensity: Float = 0.38f
) : HighlightStyle {
override val color: Color = Color.White.copy(alpha = intensity)
@@ -122,29 +120,6 @@ interface HighlightStyle {
}
}
@Stable
fun lerp(start: HighlightStyle, stop: HighlightStyle, fraction: Float): HighlightStyle {
if (start is HighlightStyle.Plain && stop is HighlightStyle.Plain) {
return HighlightStyle.Plain(
color = lerp(start.color, stop.color, fraction),
blendMode = if (fraction < 0.5f) start.blendMode else stop.blendMode
)
}
if (start is HighlightStyle.Default && stop is HighlightStyle.Default) {
return HighlightStyle.Default(
intensity = lerp(start.intensity, stop.intensity, fraction),
angle = lerp(start.angle, stop.angle, fraction),
falloff = lerp(start.falloff, stop.falloff, fraction)
)
}
if (start is HighlightStyle.Ambient && stop is HighlightStyle.Ambient) {
return HighlightStyle.Ambient(
intensity = lerp(start.intensity, stop.intensity, fraction)
)
}
return if (fraction < 0.5f) start else stop
}
private fun DrawScope.getCornerRadii(shape: Shape): FloatArray {
val shape = shape as? CornerBasedShape ?: return FloatArray(4) { size.minDimension / 2f }
val size = size
@@ -1,5 +1,6 @@
package com.kyant.backdrop.shadow
import androidx.annotation.FloatRange
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.BlendMode
@@ -17,7 +18,7 @@ data class InnerShadow(
val radius: Dp = 24f.dp,
val offset: DpOffset = DpOffset(0f.dp, radius),
val color: Color = Color.Black.copy(alpha = 0.15f),
val alpha: Float = 1f,
@param:FloatRange(from = 0.0, to = 1.0) val alpha: Float = 1f,
val blendMode: BlendMode = DrawScope.DefaultBlendMode
) {
@@ -1,23 +1,21 @@
package com.kyant.backdrop.shadow
import androidx.annotation.FloatRange
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.lerp
import androidx.compose.ui.util.lerp
@Immutable
data class Shadow(
val radius: Dp = 24f.dp,
val offset: DpOffset = DpOffset(0f.dp, radius / 6f),
val color: Color = Color.Black.copy(alpha = 0.1f),
val alpha: Float = 1f,
@param:FloatRange(from = 0.0, to = 1.0) val alpha: Float = 1f,
val blendMode: BlendMode = DrawScope.DefaultBlendMode
) {
@@ -27,14 +25,3 @@ data class Shadow(
val Default: Shadow = Shadow()
}
}
@Stable
fun lerp(start: Shadow, stop: Shadow, fraction: Float): Shadow {
return Shadow(
radius = lerp(start.radius, stop.radius, fraction),
offset = lerp(start.offset, stop.offset, fraction),
color = lerp(start.color, stop.color, fraction),
alpha = lerp(start.alpha, stop.alpha, fraction),
blendMode = if (fraction < 0.5f) start.blendMode else stop.blendMode
)
}