Skip to content

Commit

Permalink
Add color blending func
Browse files Browse the repository at this point in the history
  • Loading branch information
janseeger committed Dec 20, 2023
1 parent b293621 commit d2d3a69
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d2d3a69

Please sign in to comment.