Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.util.TypedValue;
import androidx.annotation.ColorLong;
import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;
import com.facebook.common.logging.FLog;
Expand All @@ -24,13 +27,13 @@ public class ColorPropConverter {
private static final String ATTR = "attr";
private static final String ATTR_SEGMENT = "attr/";

public static Integer getColor(Object value, Context context) {
public static Color getColorInstance(Object value, Context context) {
if (value == null) {
return null;
}

if (value instanceof Double) {
return ((Double) value).intValue();
return Color.valueOf(((Double) value).intValue());
}

if (context == null) {
Expand All @@ -39,6 +42,22 @@ public static Integer getColor(Object value, Context context) {

if (value instanceof ReadableMap) {
ReadableMap map = (ReadableMap) value;

// handle color(space r g b a) value
if (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) {
Expand All @@ -49,7 +68,7 @@ public static Integer getColor(Object value, Context context) {
for (int i = 0; i < resourcePaths.size(); i++) {
Integer result = resolveResourcePath(context, resourcePaths.getString(i));
if (result != null) {
return result;
return Color.valueOf(result);
}
}

Expand All @@ -63,6 +82,14 @@ public static Integer getColor(Object value, Context context) {
"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;
}
return color.toArgb();
}

public static Integer getColor(Object value, Context context, int defaultInt) {
try {
return getColor(value, context);
Expand All @@ -89,8 +116,9 @@ public static Integer resolveResourcePath(Context context, @Nullable String reso
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;
Expand Down Expand Up @@ -139,4 +167,4 @@ private static int resolveThemeAttribute(Context context, String resourcePath) {

throw new Resources.NotFoundException();
}
}
}