Skip to content

Commit

Permalink
Fix getOpacity() for drawables (#47103)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47103

`getOpacity()` is supposed to return a PixelFormat enum instead of a value between 0..255.
https://developer.android.com/reference/android/graphics/drawable/Drawable#getOpacity()

Also, I'm taking the value from the paint instead of the color's alpha since the paint might've been modified by setAlpha by this point and otherwise the paint will hold the alpha set by the color anyway since setAlpha() on Paints is just a helper for setColor() (https://developer.android.com/reference/android/graphics/Paint#setAlpha(int))

Changelog: [Internal]

Reviewed By: NickGerleman

Differential Revision: D64273014

fbshipit-source-id: e2c524f1d816ee3705c5118232be759898e666ea
  • Loading branch information
jorge-cab authored and facebook-github-bot committed Oct 18, 2024
1 parent 02e4f3e commit 398512a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PixelFormat;
import android.graphics.PointF;
import android.graphics.PorterDuff;
import android.graphics.Rect;
Expand Down Expand Up @@ -189,7 +190,15 @@ public int getLayoutDirection() {

@Override
public int getOpacity() {
return (Color.alpha(mColor) * mAlpha) >> 8;
int alpha = (Color.alpha(mColor) * mAlpha) >> 8;
switch (alpha) {
case 255:
return PixelFormat.OPAQUE;
case 0:
return PixelFormat.TRANSPARENT;
default:
return PixelFormat.TRANSLUCENT;
}
}

/* Android's elevation implementation requires this to be implemented to know where to draw the shadow. */
Expand Down Expand Up @@ -385,7 +394,7 @@ private void drawRoundedBackgroundWithBorders(Canvas canvas) {
canvas.clipPath(Preconditions.checkNotNull(mOuterClipPathForBorderRadius), Region.Op.INTERSECT);

// Draws the View without its border first (with background color fill)
int useColor = ColorUtils.setAlphaComponent(mColor, getOpacity());
int useColor = ColorUtils.setAlphaComponent(mColor, (Color.alpha(mColor) * mAlpha) >> 8);
if (Color.alpha(useColor) != 0) {
mPaint.setColor(useColor);
mPaint.setStyle(Paint.Style.FILL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ internal class InsetBoxShadowDrawable(

@Deprecated("Deprecated in Java")
override fun getOpacity(): Int {
val alpha = Color.alpha(shadowColor)
return if (alpha == 0) PixelFormat.TRANSPARENT
else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
val alpha = shadowPaint.alpha
return when (alpha) {
255 -> PixelFormat.OPAQUE
in 1..254 -> PixelFormat.TRANSLUCENT
else -> PixelFormat.TRANSPARENT
}
}

override fun draw(canvas: Canvas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PathEffect
import android.graphics.PixelFormat
import android.graphics.RectF
import android.graphics.drawable.Drawable
import com.facebook.react.uimanager.PixelUtil.dpToPx
Expand Down Expand Up @@ -104,8 +105,14 @@ internal class OutlineDrawable(
}

@Deprecated("Deprecated in Java")
override fun getOpacity(): Int =
((outlinePaint.alpha / 255f) / (Color.alpha(outlineColor) / 255f) * 255f).roundToInt()
override fun getOpacity(): Int {
val alpha = outlinePaint.alpha
return when (alpha) {
255 -> PixelFormat.OPAQUE
in 1..254 -> PixelFormat.TRANSLUCENT
else -> PixelFormat.TRANSPARENT
}
}

override fun draw(canvas: Canvas) {
if (outlineWidth == 0f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,12 @@ internal class OutsetBoxShadowDrawable(

@Deprecated("Deprecated in Java")
override fun getOpacity(): Int {
val alpha = Color.alpha(shadowColor)
return if (alpha == 0) PixelFormat.TRANSPARENT
else ((shadowPaint.alpha / 255f) / (alpha / 255f) * 255f).roundToInt()
val alpha = shadowPaint.alpha
return when (alpha) {
255 -> PixelFormat.OPAQUE
in 1..254 -> PixelFormat.TRANSLUCENT
else -> PixelFormat.TRANSPARENT
}
}

override fun draw(canvas: Canvas) {
Expand Down

0 comments on commit 398512a

Please sign in to comment.