Skip to content

Commit

Permalink
Remove recursive calculation of alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Nov 30, 2023
1 parent 65b3d02 commit b3ff8d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

package com.datadog.reactnative.sessionreplay

import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting

private const val HEX_COLOR_INCLUDING_ALPHA_LENGTH: Int = 8
Expand All @@ -16,25 +14,11 @@ internal fun resolveBackgroundColorAsHex(backgroundColor: Int): String {
val colorHexString = Integer.toHexString(backgroundColor)

// reorder the hex string from argb to rgba
return "#${reorderRGBAtoARGB(colorHexString)}"
}

internal fun resolveOpacity(view: View, currentOpacity: Float): Float {
return if (view.alpha == 0f) {
0f
} else {
val combinedOpacity = view.alpha * currentOpacity

if (view.parent != null && view.parent is ViewGroup) {
resolveOpacity(view.parent as ViewGroup, combinedOpacity)
} else {
combinedOpacity
}
}
return "#${reorderARGBtoRGBA(colorHexString)}"
}

@VisibleForTesting
internal fun reorderRGBAtoARGB(hexString: String): String {
internal fun reorderARGBtoRGBA(hexString: String): String {
return if (hexString.length == HEX_COLOR_INCLUDING_ALPHA_LENGTH) {
hexString.substring(2, 8) + hexString.substring(0, 2)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ internal class ReactViewGroupMapper :
val backgroundDrawable = view.background

// view.alpha is the value of the opacity prop on the js side
val opacity = resolveOpacity(view, 1f)

if (opacity == 0f) {
return emptyList()
}
val opacity = view.alpha

val (shapeStyle, border) =
if (backgroundDrawable is ReactViewBackgroundDrawable) {
Expand All @@ -52,7 +48,7 @@ internal class ReactViewGroupMapper :
} else {
backgroundDrawable?.resolveShapeStyleAndBorder(opacity) ?: (null to null)
}

return listOf(
MobileSegment.Wireframe.ShapeWireframe(
resolveViewId(view),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,33 @@ internal class ColorUtilsTest {
val hexColor = resolveBackgroundColorAsHex(16711680)

// Then
assertThat(hexColor)
.isEqualTo("#ff0000")
assertThat(hexColor).isEqualTo("#ff0000")
}

@Test
fun `M resolve with alpha W resolveBackgroundColorAsHex { color with alpha }`() {
// Given
val color = 1717960806

// When
val alphaValue = resolveBackgroundColorAsHex(color)
val hexColor = resolveBackgroundColorAsHex(1717960806)

// decimal color 1717960806 translates to hex 66660066
// 66 being the hex alpha, which is 40% opacity -> 40% of 255 is 102
// Then
assertThat(alphaValue).isEqualTo("#66006666")
assertThat(hexColor).isEqualTo("#66006666")
}

@Test
fun `M reverse alpha to end W reverseRGBAtoARGB { hex color with alpha }`() {
// Given
val hexString = "FF006666"

fun `M reverse alpha to end W reorderARGBtoRGBA { hex color with alpha }`() {
// When
val reversedString = reorderRGBAtoARGB(hexString)
val result = reorderARGBtoRGBA("FF006666")

// Then
assertThat(reversedString).isEqualTo("006666FF")
assertThat(result).isEqualTo("006666FF")
}

@Test
fun `M return original string W reverseRGBAtoARGB { hex color without alpha }`() {
// Given
val hexString = "006666"

fun `M return original string W reorderARGBtoRGBA { hex color without alpha }`() {
// When
val reversedString = reorderRGBAtoARGB(hexString)
val result = reorderARGBtoRGBA("006666")

// Then
assertThat(reversedString).isEqualTo("006666")
assertThat(result).isEqualTo("006666")
}
}

0 comments on commit b3ff8d9

Please sign in to comment.