Fix crash that happens with shadow color with 0 alpha (#46551)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/46551

We crash if we pass a shadow color with 0 alpha due to some divide by 0 logic. This fixes that for inset and outset shadows and adds a test for that case.

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D62899047

fbshipit-source-id: 2aff1d016dd97bed024df1c3f89bcc62e49f0306
This commit is contained in:
Joe Vilches
2024-09-18 16:44:45 -07:00
committed by Facebook GitHub Bot
parent 40638b8c26
commit 7e38cf32eb
3 changed files with 19 additions and 4 deletions
@@ -14,6 +14,7 @@ import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PixelFormat
import android.graphics.RectF
import android.graphics.drawable.Drawable
import androidx.annotation.RequiresApi
@@ -84,8 +85,11 @@ internal class InsetBoxShadowDrawable(
invalidateSelf()
}
override fun getOpacity(): Int =
((shadowPaint.alpha / 255f) / (Color.alpha(shadowColor) / 255f) * 255f).roundToInt()
override fun getOpacity(): Int {
val alpha = Color.alpha(shadowColor)
return if (alpha == 0) PixelFormat.TRANSPARENT
else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
}
override fun draw(canvas: Canvas) {
val computedBorderRadii = computeBorderRadii()
@@ -14,6 +14,7 @@ import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PixelFormat
import android.graphics.RectF
import android.graphics.drawable.Drawable
import androidx.annotation.RequiresApi
@@ -72,8 +73,11 @@ internal class OutsetBoxShadowDrawable(
invalidateSelf()
}
override fun getOpacity(): Int =
((shadowPaint.alpha / 255f) / (Color.alpha(shadowColor) / 255f) * 255f).roundToInt()
override fun getOpacity(): Int {
val alpha = Color.alpha(shadowColor)
return if (alpha == 0) PixelFormat.TRANSPARENT
else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
}
override fun draw(canvas: Canvas) {
val resolutionWidth = bounds.width().toFloat().pxToDp()
@@ -528,6 +528,13 @@ function BoxShadowExample(): React.Node {
boxShadow: '0px 10px',
}}
/>
<View
style={{
...defaultStyleSize,
backgroundColor: 'orange',
boxShadow: '5px 5px 5px 0px rgba(0, 0, 0, 0)',
}}
/>
</View>
</View>
);