Skip to content

Commit

Permalink
update Android HostPlatformColor to support color longs
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlntn committed Mar 15, 2024
1 parent b3a8eba commit 8a65961
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1029,11 +1029,11 @@ inline MapBuffer toMapBuffer(const FontVariant& fontVariant) {
inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
auto builder = MapBufferBuilder();
if (textAttributes.foregroundColor) {
builder.putInt(
builder.putLong(
TA_KEY_FOREGROUND_COLOR, toAndroidRepr(textAttributes.foregroundColor));
}
if (textAttributes.backgroundColor) {
builder.putInt(
builder.putLong(
TA_KEY_BACKGROUND_COLOR, toAndroidRepr(textAttributes.backgroundColor));
}
if (!std::isnan(textAttributes.opacity)) {
Expand Down Expand Up @@ -1089,7 +1089,7 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {

// Decoration
if (textAttributes.textDecorationColor) {
builder.putInt(
builder.putLong(
TA_KEY_TEXT_DECORATION_COLOR,
toAndroidRepr(textAttributes.textDecorationColor));
}
Expand All @@ -1110,7 +1110,7 @@ inline MapBuffer toMapBuffer(const TextAttributes& textAttributes) {
TA_KEY_TEXT_SHADOW_RADIUS, textAttributes.textShadowRadius);
}
if (textAttributes.textShadowColor) {
builder.putInt(
builder.putLong(
TA_KEY_TEXT_SHADOW_COLOR,
toAndroidRepr(textAttributes.textShadowColor));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ inline void fromRawValue(
}

#ifdef ANDROID
inline int toAndroidRepr(const SharedColor& color) {
inline int64_t toAndroidRepr(const SharedColor& color) {
return *color;
}
inline folly::dynamic toDynamic(const SharedColor& color) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,59 @@

namespace facebook::react {

using Color = int32_t;
using Color = int64_t;

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

inline Color hostPlatformColorFromComponents(ColorComponents components) {
float ratio = 255;
return ((int)round(components.alpha * ratio) & 0xff) << 24 |
((int)round(components.red * ratio) & 0xff) << 16 |
((int)round(components.green * ratio) & 0xff) << 8 |
((int)round(components.blue * ratio) & 0xff);
if (components.colorSpace == ColorSpace::DisplayP3) {
int ratio = 15360;
int red = static_cast<int>(round(components.red * ratio)) & 0xffff;
int green = static_cast<int>(round(components.green * ratio)) & 0xffff;
int blue = static_cast<int>(round(components.blue * ratio)) & 0xffff;
int alpha = static_cast<int>(round(components.alpha * 0x3ff)) & 0x3ff;
int colorSpace = 7;
int64_t androidColor = (static_cast<int64_t>(red) << 48) |
(static_cast<int64_t>(green) << 32) |
(static_cast<int64_t>(blue) << 16) |
(static_cast<int64_t>(alpha) << 6) |
static_cast<int64_t>(colorSpace);
return androidColor;
} else {
int ratio = 255;
int alpha = static_cast<int>(round(components.alpha * ratio)) & 0xff;
int red = static_cast<int>(round(components.red * ratio)) & 0xff;
int green = static_cast<int>(round(components.green * ratio)) & 0xff;
int blue = static_cast<int>(round(components.blue * ratio)) & 0xff;
int64_t androidColor = (static_cast<int64_t>(alpha) << 56) |
(static_cast<int64_t>(red) << 48) |
(static_cast<int64_t>(green) << 40) |
(static_cast<int64_t>(blue) << 32);
return androidColor;
}
}

inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
float ratio = 255;
return ColorComponents{
(float)((color >> 16) & 0xff) / ratio,
(float)((color >> 8) & 0xff) / ratio,
(float)((color >> 0) & 0xff) / ratio,
(float)((color >> 24) & 0xff) / ratio};
if ((color & 0x3f) == 7) {
int ratio = 15360;
return ColorComponents{
(float)((color >> 48) & 0xffff) / ratio,
(float)((color >> 32) & 0xffff) / ratio,
(float)((color >> 16) & 0xffff) / ratio,
(float)((color >> 6) & 0x3ff) / ratio,
ColorSpace::DisplayP3};
} else {
int ratio = 255;
return ColorComponents{
(float)((color >> 48) & 0xff) / ratio,
(float)((color >> 40) & 0xff) / ratio,
(float)((color >> 32) & 0xff) / ratio,
(float)((color >> 56) & 0xff) / ratio,
ColorSpace::sRGB};
}
}

} // namespace facebook::react

0 comments on commit 8a65961

Please sign in to comment.