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 6ff09d1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
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 6ff09d1

Please sign in to comment.