Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit de6e17da authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Convert interpolated colors to original color space" into main

parents c523e690 c352ec75
Loading
Loading
Loading
Loading
+4 −4
Original line number Original line Diff line number Diff line
@@ -225,7 +225,7 @@ fun ElementScope<*>.animateElementColorAsState(value: Color, key: ValueKey): Ani
    return animateElementValueAsState(value, key, SharedColorType, canOverflow = false)
    return animateElementValueAsState(value, key, SharedColorType, canOverflow = false)
}
}


private object SharedColorType : SharedValueType<Color, ColorDelta> {
internal object SharedColorType : SharedValueType<Color, ColorDelta> {
    override val unspecifiedValue: Color = Color.Unspecified
    override val unspecifiedValue: Color = Color.Unspecified
    override val zeroDeltaValue: ColorDelta = ColorDelta(0f, 0f, 0f, 0f)
    override val zeroDeltaValue: ColorDelta = ColorDelta(0f, 0f, 0f, 0f)


@@ -255,17 +255,17 @@ private object SharedColorType : SharedValueType<Color, ColorDelta> {
                alpha = aOklab.alpha + b.alpha * bWeight,
                alpha = aOklab.alpha + b.alpha * bWeight,
                colorSpace = ColorSpaces.Oklab,
                colorSpace = ColorSpaces.Oklab,
            )
            )
            .convert(aOklab.colorSpace)
            .convert(a.colorSpace)
    }
    }
}
}


/**
/**
 * Represents the diff between two colors in the same color space.
 * Represents the diff between two colors in the Oklab color space.
 *
 *
 * Note: This class is necessary because Color() checks the bounds of its values and UncheckedColor
 * Note: This class is necessary because Color() checks the bounds of its values and UncheckedColor
 * is internal.
 * is internal.
 */
 */
private class ColorDelta(val red: Float, val green: Float, val blue: Float, val alpha: Float)
internal class ColorDelta(val red: Float, val green: Float, val blue: Float, val alpha: Float)


@Composable
@Composable
internal fun <T> animateSharedValueAsState(
internal fun <T> animateSharedValueAsState(
+12 −0
Original line number Original line Diff line number Diff line
@@ -18,7 +18,10 @@ package com.android.compose.animation.scene


import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.SideEffect
@@ -495,4 +498,13 @@ class AnimatedSharedAsStateTest {
        assertThat(lastValues[SceneA]).isWithin(0.001f).of(100f)
        assertThat(lastValues[SceneA]).isWithin(0.001f).of(100f)
        assertThat(lastValues[SceneB]).isWithin(0.001f).of(100f)
        assertThat(lastValues[SceneB]).isWithin(0.001f).of(100f)
    }
    }

    @Test
    fun interpolatedColor() {
        val a = Color.Red
        val b = Color.Green
        val delta = SharedColorType.diff(b, a) // b - a
        val interpolated = SharedColorType.addWeighted(a, delta, 0.5f) // a + (b - a) * 0.5f
        rule.setContent { Box(Modifier.fillMaxSize().background(interpolated)) }
    }
}
}