Skip to content

Commit e0d99c3

Browse files
committed
update Android HostPlatformColor to support color longs
1 parent a9e6759 commit e0d99c3

File tree

3 files changed

+48
-17
lines changed

3 files changed

+48
-17
lines changed

packages/react-native/ReactCommon/react/renderer/attributedstring/conversions.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,11 +1029,11 @@ inline MapBuffer toMapBuffer(const FontVariant& fontVariant) {
10291029
inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
10301030
auto builder = MapBufferBuilder();
10311031
if (textAttributes.foregroundColor) {
1032-
builder.putInt(
1032+
builder.putLong(
10331033
TA_KEY_FOREGROUND_COLOR, toAndroidRepr(textAttributes.foregroundColor));
10341034
}
10351035
if (textAttributes.backgroundColor) {
1036-
builder.putInt(
1036+
builder.putLong(
10371037
TA_KEY_BACKGROUND_COLOR, toAndroidRepr(textAttributes.backgroundColor));
10381038
}
10391039
if (!std::isnan(textAttributes.opacity)) {
@@ -1089,7 +1089,7 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
10891089

10901090
// Decoration
10911091
if (textAttributes.textDecorationColor) {
1092-
builder.putInt(
1092+
builder.putLong(
10931093
TA_KEY_TEXT_DECORATION_COLOR,
10941094
toAndroidRepr(textAttributes.textDecorationColor));
10951095
}
@@ -1110,7 +1110,7 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
11101110
TA_KEY_TEXT_SHADOW_RADIUS, textAttributes.textShadowRadius);
11111111
}
11121112
if (textAttributes.textShadowColor) {
1113-
builder.putInt(
1113+
builder.putLong(
11141114
TA_KEY_TEXT_SHADOW_COLOR,
11151115
toAndroidRepr(textAttributes.textShadowColor));
11161116
}

packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ inline void fromRawValue(
3434
}
3535

3636
#ifdef ANDROID
37-
inline int toAndroidRepr(const SharedColor& color) {
37+
inline int64_t toAndroidRepr(const SharedColor& color) {
3838
return *color;
3939
}
4040
inline folly::dynamic toDynamic(const SharedColor& color) {

packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/HostPlatformColor.h

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,59 @@
1212

1313
namespace facebook::react {
1414

15-
using Color = int32_t;
15+
using Color = int64_t;
1616

1717
namespace HostPlatformColor {
1818
static const facebook::react::Color UndefinedColor =
1919
std::numeric_limits<facebook::react::Color>::max();
2020
}
2121

2222
inline Color hostPlatformColorFromComponents(ColorComponents components) {
23-
float ratio = 255;
24-
return ((int)round(components.alpha * ratio) & 0xff) << 24 |
25-
((int)round(components.red * ratio) & 0xff) << 16 |
26-
((int)round(components.green * ratio) & 0xff) << 8 |
27-
((int)round(components.blue * ratio) & 0xff);
23+
if (components.colorSpace == ColorSpace::DisplayP3) {
24+
int ratio = 15360;
25+
int red = static_cast<int>(round(components.red * ratio)) & 0xffff;
26+
int green = static_cast<int>(round(components.green * ratio)) & 0xffff;
27+
int blue = static_cast<int>(round(components.blue * ratio)) & 0xffff;
28+
int alpha = static_cast<int>(round(components.alpha * 0x3ff)) & 0x3ff;
29+
int colorSpace = 7;
30+
int64_t androidColor = (static_cast<int64_t>(red) << 48) |
31+
(static_cast<int64_t>(green) << 32) |
32+
(static_cast<int64_t>(blue) << 16) |
33+
(static_cast<int64_t>(alpha) << 6) |
34+
static_cast<int64_t>(colorSpace);
35+
return androidColor;
36+
} else {
37+
int ratio = 255;
38+
int alpha = static_cast<int>(round(components.alpha * ratio)) & 0xff;
39+
int red = static_cast<int>(round(components.red * ratio)) & 0xff;
40+
int green = static_cast<int>(round(components.green * ratio)) & 0xff;
41+
int blue = static_cast<int>(round(components.blue * ratio)) & 0xff;
42+
int64_t androidColor = (static_cast<int64_t>(alpha) << 56) |
43+
(static_cast<int64_t>(red) << 48) |
44+
(static_cast<int64_t>(green) << 40) |
45+
(static_cast<int64_t>(blue) << 32);
46+
return androidColor;
47+
}
2848
}
2949

3050
inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
31-
float ratio = 255;
32-
return ColorComponents{
33-
(float)((color >> 16) & 0xff) / ratio,
34-
(float)((color >> 8) & 0xff) / ratio,
35-
(float)((color >> 0) & 0xff) / ratio,
36-
(float)((color >> 24) & 0xff) / ratio};
51+
if ((color & 0x3f) == 7) {
52+
int ratio = 15360;
53+
return ColorComponents{
54+
(float)((color >> 48) & 0xffff) / ratio,
55+
(float)((color >> 32) & 0xffff) / ratio,
56+
(float)((color >> 16) & 0xffff) / ratio,
57+
(float)((color >> 6) & 0x3ff) / ratio,
58+
ColorSpace::DisplayP3};
59+
} else {
60+
int ratio = 255;
61+
return ColorComponents{
62+
(float)((color >> 48) & 0xff) / ratio,
63+
(float)((color >> 40) & 0xff) / ratio,
64+
(float)((color >> 32) & 0xff) / ratio,
65+
(float)((color >> 56) & 0xff) / ratio,
66+
ColorSpace::sRGB};
67+
}
3768
}
3869

3970
} // namespace facebook::react

0 commit comments

Comments
 (0)