Skip to content

Commit

Permalink
Fix crash that happens with shadow color with 0 alpha (#46551)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #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]

Differential Revision: D62899047
  • Loading branch information
joevilches authored and facebook-github-bot committed Sep 17, 2024
1 parent bca232a commit df76002
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ 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) 0 else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
}

override fun draw(canvas: Canvas) {
val computedBorderRadii = computeBorderRadii()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ 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) 0 else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
}

override fun draw(canvas: Canvas) {
val resolutionWidth = bounds.width().toFloat().pxToDp()
Expand Down
7 changes: 7 additions & 0 deletions packages/rn-tester/js/examples/View/ViewExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>
);
Expand Down

0 comments on commit df76002

Please sign in to comment.