From b3ff8d90287ebe2fd18ebe7f491d6fe4818cc7f7 Mon Sep 17 00:00:00 2001 From: Jonathan Moskovich <48201295+jonathanmos@users.noreply.github.com> Date: Thu, 30 Nov 2023 12:57:00 +0200 Subject: [PATCH] Remove recursive calculation of alpha --- .../reactnative/sessionreplay/ColorUtils.kt | 20 ++----------- .../sessionreplay/ReactViewGroupMapper.kt | 8 ++--- .../sessionreplay/ColorUtilsTest.kt | 30 ++++++------------- 3 files changed, 13 insertions(+), 45 deletions(-) diff --git a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt index 768768ebf..d07b9ea37 100644 --- a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt +++ b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ColorUtils.kt @@ -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 @@ -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 { diff --git a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt index 6e5acc402..fbef968ff 100644 --- a/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt +++ b/packages/react-native-session-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactViewGroupMapper.kt @@ -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) { @@ -52,7 +48,7 @@ internal class ReactViewGroupMapper : } else { backgroundDrawable?.resolveShapeStyleAndBorder(opacity) ?: (null to null) } - + return listOf( MobileSegment.Wireframe.ShapeWireframe( resolveViewId(view), diff --git a/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt b/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt index 18ad97bbf..aa7ca52f5 100644 --- a/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt +++ b/packages/react-native-session-replay/android/src/test/kotlin/com/datadog/reactnative/sessionreplay/ColorUtilsTest.kt @@ -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") } }