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 ff6e7e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 26 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 @@ -52,7 +52,7 @@ internal class ColorUtilsTest {
val hexString = "FF006666"

// When
val reversedString = reorderRGBAtoARGB(hexString)
val reversedString = reorderARGBtoRGBA(hexString)

// Then
assertThat(reversedString).isEqualTo("006666FF")
Expand All @@ -64,7 +64,7 @@ internal class ColorUtilsTest {
val hexString = "006666"

// When
val reversedString = reorderRGBAtoARGB(hexString)
val reversedString = reorderARGBtoRGBA(hexString)

// Then
assertThat(reversedString).isEqualTo("006666")
Expand Down

0 comments on commit ff6e7e9

Please sign in to comment.