From d2d3a6995ee2a3b1d6406b2ab84abdab135eff52 Mon Sep 17 00:00:00 2001 From: Jan Seeger Date: Wed, 20 Dec 2023 16:15:32 +0100 Subject: [PATCH] Add color blending func --- .../sipgate/dachlatten/compose/color/blend.kt | 18 ++++++++++++++++++ .../dachlatten/compose/color/ColorTest.kt | 16 ++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 dachlatten-compose/src/main/kotlin/de/sipgate/dachlatten/compose/color/blend.kt create mode 100644 dachlatten-compose/src/test/kotlin/de/sipgate/dachlatten/compose/color/ColorTest.kt 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) + } +}