Skip to content

Commit

Permalink
convert ColorUtil to Kotlin (facebook#43878)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#43878

convert Java to Kotlin: `/react/views/view/ColorUtil.java`

Changelog:
[Internal] internal

Differential Revision: D55768201
  • Loading branch information
alanleedev authored and facebook-github-bot committed Apr 4, 2024
1 parent fd3ef7b commit eaa30aa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
10 changes: 5 additions & 5 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -7589,11 +7589,11 @@ public class com/facebook/react/views/unimplementedview/ReactUnimplementedViewMa
public fun setProperty (Lcom/facebook/react/views/unimplementedview/ReactUnimplementedViewManager;Lcom/facebook/react/views/unimplementedview/ReactUnimplementedView;Ljava/lang/String;Ljava/lang/Object;)V
}

public class com/facebook/react/views/view/ColorUtil {
public fun <init> ()V
public static fun getOpacityFromColor (I)I
public static fun multiplyColorAlpha (II)I
public static fun normalize (DDDD)I
public final class com/facebook/react/views/view/ColorUtil {
public static final field INSTANCE Lcom/facebook/react/views/view/ColorUtil;
public static final fun getOpacityFromColor (I)I
public static final fun multiplyColorAlpha (II)I
public static final fun normalize (DDDD)I
}

public final class com/facebook/react/views/view/MeasureUtil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.view;
package com.facebook.react.views.view

import android.graphics.PixelFormat;
import com.facebook.infer.annotation.Nullsafe;
import android.graphics.PixelFormat
import kotlin.math.roundToInt

/**
* Simple utility class for manipulating colors, based on Fresco's DrawableUtils
* (https://github.com/facebook/fresco). For a small helper like this, copying is simpler than
* adding a dependency on com.facebook.fresco.drawee.
*/
@Nullsafe(Nullsafe.Mode.LOCAL)
public class ColorUtil {
public object ColorUtil {

/**
* Multiplies the color with the given alpha.
Expand All @@ -25,17 +24,18 @@ public class ColorUtil {
* @param alpha value between 0 and 255
* @return multiplied color
*/
public static int multiplyColorAlpha(int color, int alpha) {
@JvmStatic
public fun multiplyColorAlpha(color: Int, alpha: Int): Int {
if (alpha == 255) {
return color;
return color
} else if (alpha == 0) {
return color and 0x00FFFFFF
}
if (alpha == 0) {
return color & 0x00FFFFFF;
}
alpha = alpha + (alpha >> 7); // make it 0..256
int colorAlpha = color >>> 24;
int multipliedAlpha = colorAlpha * alpha >> 8;
return (multipliedAlpha << 24) | (color & 0x00FFFFFF);

val scaledAlpha = alpha + (alpha shr 7) // make it 0..256
val colorAlpha = color ushr 24
val multipliedAlpha = (colorAlpha * scaledAlpha) shr 8
return (multipliedAlpha shl 24) or (color and 0x00FFFFFF)
}

/**
Expand All @@ -44,14 +44,13 @@ public static int multiplyColorAlpha(int color, int alpha) {
* @param color color to get opacity from
* @return opacity expressed by one of PixelFormat constants
*/
public static int getOpacityFromColor(int color) {
int colorAlpha = color >>> 24;
if (colorAlpha == 255) {
return PixelFormat.OPAQUE;
} else if (colorAlpha == 0) {
return PixelFormat.TRANSPARENT;
} else {
return PixelFormat.TRANSLUCENT;
@JvmStatic
public fun getOpacityFromColor(color: Int): Int {
val colorAlpha = color ushr 24
return when (colorAlpha) {
255 -> PixelFormat.OPAQUE
0 -> PixelFormat.TRANSPARENT
else -> PixelFormat.TRANSLUCENT
}
}

Expand All @@ -65,11 +64,10 @@ public static int getOpacityFromColor(int color) {
* @param a alpha channel value, [0, 1]
* @return integer representation of the color as 0xAARRGGBB
*/
public static int normalize(double r, double g, double b, double a) {
return (clamp255(a * 255) << 24) | (clamp255(r) << 16) | (clamp255(g) << 8) | clamp255(b);
@JvmStatic
public fun normalize(r: Double, g: Double, b: Double, a: Double): Int {
return (clamp255(a * 255) shl 24) or (clamp255(r) shl 16) or (clamp255(g) shl 8) or clamp255(b)
}

private static int clamp255(double value) {
return Math.max(0, Math.min(255, (int) Math.round(value)));
}
private fun clamp255(value: Double): Int = maxOf(0, minOf(255, value.roundToInt()))
}

0 comments on commit eaa30aa

Please sign in to comment.