Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement fallback for HazeProgressive #400

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion haze/src/commonMain/kotlin/dev/chrisbanes/haze/Gradient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal fun HazeProgressive.LinearGradient.asBrush(numStops: Int = 20): Brush {
return Brush.linearGradient(
colors = List(numStops) { i ->
val x = i * 1f / (numStops - 1)
Color.Black.copy(alpha = lerp(startIntensity, endIntensity, easing.transform(x)))
Color.Magenta.copy(alpha = lerp(startIntensity, endIntensity, easing.transform(x)))
},
start = start,
end = end,
Expand Down
29 changes: 15 additions & 14 deletions haze/src/commonMain/kotlin/dev/chrisbanes/haze/HazeChildNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -331,25 +331,28 @@ class HazeChildNode(
private fun DrawScope.drawEffectWithScrim() {
val scrimTint = resolveFallbackTint().takeIf { it.isSpecified }
?: resolveTints().firstOrNull()?.boostForFallback(resolveBlurRadius().takeOrElse { 0.dp })

fun scrim() {
if (scrimTint != null) {
val m = mask
if (m != null) {
drawRect(brush = m, colorFilter = ColorFilter.tint(scrimTint.color))
} else {
drawRect(color = scrimTint.color, blendMode = scrimTint.blendMode)
}
?: return

fun scrim(tint: HazeTint) {
val m = mask
val p = progressive

if (m != null) {
drawRect(brush = m, colorFilter = ColorFilter.tint(tint.color))
} else if (p is HazeProgressive.LinearGradient) {
drawRect(brush = p.asBrush(), colorFilter = ColorFilter.tint(tint.color))
} else {
drawRect(color = tint.color, blendMode = tint.blendMode)
}
}

if (alpha != 1f) {
paint.alpha = alpha
drawContext.canvas.withSaveLayer(size.toRect(), paint) {
scrim()
scrim(scrimTint)
}
} else {
scrim()
scrim(scrimTint)
}
}

Expand Down Expand Up @@ -393,13 +396,13 @@ class HazeChildNode(
* Parameters for applying a progressive blur effect.
*/
sealed interface HazeProgressive {

/**
* A linear gradient effect.
*
* You may wish to use the convenience builder functions provided in [horizontalGradient] and
* [verticalGradient] for more common use cases.
*
* @param steps - The number of steps in the effect. See [HazeProgressive.steps] for information.
* @param easing - The easing function to use when applying the effect. Defaults to a
* linear easing effect.
* @param start - Starting position of the gradient. Defaults to [Offset.Zero] which
Expand All @@ -421,7 +424,6 @@ sealed interface HazeProgressive {
/**
* A vertical gradient effect.
*
* @param steps - The number of steps in the effect. See [HazeProgressive.steps] for information.
* @param easing - The easing function to use when applying the effect. Defaults to a
* linear easing effect.
* @param startY - Starting x position of the horizontal gradient. Defaults to 0 which
Expand All @@ -448,7 +450,6 @@ sealed interface HazeProgressive {
/**
* A horizontal gradient effect.
*
* @param steps - The number of steps in the effect. See [HazeProgressive.steps] for information.
* @param easing - The easing function to use when applying the effect. Defaults to a
* linear easing effect.
* @param startX - Starting x position of the horizontal gradient. Defaults to 0 which
Expand Down