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 c12e0da
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 28 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 @@ -47,24 +47,24 @@ internal class ColorUtilsTest {
}

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

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

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

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

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

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

0 comments on commit c12e0da

Please sign in to comment.