diff --git a/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/color/blend.kt b/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/color/blend.kt new file mode 100644 index 0000000..ef6ff86 --- /dev/null +++ b/dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/color/blend.kt @@ -0,0 +1,18 @@ +package de.sipgate.dachlatten.compose.color + +import androidx.compose.ui.graphics.Color + +fun blend( + color1: Color, + color2: Color, + ratio: Float, +): Color { + val inverseRatio = 1f - ratio + + val red = color1.red * ratio + color2.red * inverseRatio + val green = color1.green * ratio + color2.green * inverseRatio + val blue = color1.blue * ratio + color2.blue * inverseRatio + val alpha = color1.alpha * ratio + color2.alpha * inverseRatio + + return Color(red, green, blue, alpha) +} diff --git a/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/color/ColorTest.kt b/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/color/ColorTest.kt new file mode 100644 index 0000000..9c9ac50 --- /dev/null +++ b/dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/color/ColorTest.kt @@ -0,0 +1,16 @@ +package de.sipgate.dachlatten.compose.color + +import androidx.compose.ui.graphics.Color +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test + +class ColorTest { + @Test + fun equalPartsOfRedAndBlueResultInPurple() { + val red = Color.Red + val blue = Color.Blue + + val result = blend(red, blue, 0.5f) + assertEquals(Color(128, 0, 128), result) + } +}