mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Update ColorPropConverterto support color function values (#43031)
Summary: This adds support for color function values to ColorPropConverter per the wide gamut color [RFC](https://github.com/react-native-community/discussions-and-proposals/pull/738). It updates the color conversion code so that it returns a Color instance before ultimately being converted to an Integer in preparation for returning long values as needed. bypass-github-export-checks ## Changelog: [ANDROID] [ADDED] - Update ColorPropConverter to support color function values Pull Request resolved: https://github.com/facebook/react-native/pull/43031 Test Plan: Colors should work exactly the same as before. Follow test steps from https://github.com/facebook/react-native/pull/42831 to test support for color() function syntax. While colors specified with color() function syntax will not yet render in DisplayP3 color space they will not be misrecognized as resource path colors but will instead fallback to their sRGB color space values. Reviewed By: cortinico Differential Revision: D55749058 Pulled By: cipolleschi fbshipit-source-id: 37659d22c1db4b1a27a9a4f88c9beb703517b01f
This commit is contained in:
committed by
Facebook GitHub Bot
parent
098454d425
commit
fa7dbd578d
@@ -648,6 +648,7 @@ public class com/facebook/react/bridge/ColorPropConverter {
|
||||
public fun <init> ()V
|
||||
public static fun getColor (Ljava/lang/Object;Landroid/content/Context;)Ljava/lang/Integer;
|
||||
public static fun getColor (Ljava/lang/Object;Landroid/content/Context;I)Ljava/lang/Integer;
|
||||
public static fun getColorInstance (Ljava/lang/Object;Landroid/content/Context;)Landroid/graphics/Color;
|
||||
public static fun resolveResourcePath (Landroid/content/Context;Ljava/lang/String;)Ljava/lang/Integer;
|
||||
}
|
||||
|
||||
|
||||
+43
-7
@@ -9,7 +9,11 @@ package com.facebook.react.bridge;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorSpace;
|
||||
import android.os.Build;
|
||||
import android.util.TypedValue;
|
||||
import androidx.annotation.ColorLong;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.res.ResourcesCompat;
|
||||
import com.facebook.common.logging.FLog;
|
||||
@@ -24,13 +28,17 @@ public class ColorPropConverter {
|
||||
private static final String ATTR = "attr";
|
||||
private static final String ATTR_SEGMENT = "attr/";
|
||||
|
||||
public static Integer getColor(Object value, Context context) {
|
||||
private static Boolean apiSupportWideGamut() {
|
||||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
|
||||
}
|
||||
|
||||
public static Color getColorInstance(Object value, Context context) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (value instanceof Double) {
|
||||
return ((Double) value).intValue();
|
||||
if (apiSupportWideGamut() && value instanceof Double) {
|
||||
return Color.valueOf(((Double) value).intValue());
|
||||
}
|
||||
|
||||
if (context == null) {
|
||||
@@ -39,6 +47,22 @@ public class ColorPropConverter {
|
||||
|
||||
if (value instanceof ReadableMap) {
|
||||
ReadableMap map = (ReadableMap) value;
|
||||
|
||||
// handle color(space r g b a) value
|
||||
if (apiSupportWideGamut() && map.hasKey("space")) {
|
||||
String rawColorSpace = map.getString("space");
|
||||
boolean isDisplayP3 = rawColorSpace.equals("display-p3");
|
||||
ColorSpace space =
|
||||
ColorSpace.get(isDisplayP3 ? ColorSpace.Named.DISPLAY_P3 : ColorSpace.Named.SRGB);
|
||||
float r = (float) map.getDouble("r");
|
||||
float g = (float) map.getDouble("g");
|
||||
float b = (float) map.getDouble("b");
|
||||
float a = (float) map.getDouble("a");
|
||||
|
||||
@ColorLong long color = Color.pack(r, g, b, a, space);
|
||||
return Color.valueOf(color);
|
||||
}
|
||||
|
||||
ReadableArray resourcePaths = map.getArray(JSON_KEY);
|
||||
|
||||
if (resourcePaths == null) {
|
||||
@@ -48,8 +72,8 @@ public class ColorPropConverter {
|
||||
|
||||
for (int i = 0; i < resourcePaths.size(); i++) {
|
||||
Integer result = resolveResourcePath(context, resourcePaths.getString(i));
|
||||
if (result != null) {
|
||||
return result;
|
||||
if (apiSupportWideGamut() && result != null) {
|
||||
return Color.valueOf(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +87,17 @@ public class ColorPropConverter {
|
||||
"ColorValue: the value must be a number or Object.");
|
||||
}
|
||||
|
||||
public static Integer getColor(Object value, Context context) {
|
||||
Color color = getColorInstance(value, context);
|
||||
if (color == null) {
|
||||
return null;
|
||||
}
|
||||
if (apiSupportWideGamut()) {
|
||||
return color.toArgb();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer getColor(Object value, Context context, int defaultInt) {
|
||||
try {
|
||||
return getColor(value, context);
|
||||
@@ -89,8 +124,9 @@ public class ColorPropConverter {
|
||||
return resolveThemeAttribute(context, resourcePath);
|
||||
}
|
||||
} catch (Resources.NotFoundException exception) {
|
||||
// The resource could not be found so do nothing to allow the for loop to continue and
|
||||
// try the next fallback resource in the array. If none of the fallbacks are
|
||||
// The resource could not be found so do nothing to allow the for loop to
|
||||
// continue and
|
||||
// try the next fallback resource in the array. If none of the fallbacks are
|
||||
// found then the exception immediately after the for loop will be thrown.
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user