Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Update HostPlatformColor to support color longs #43163

Closed
Closed
Show file tree
Hide file tree
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 @@ -1035,11 +1035,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 @@ -1095,7 +1095,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 @@ -1116,7 +1116,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,61 @@

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();
}

// As defined by Android's ColorSpace.Named
constexpr int kDisplayP3ColorSpace = 7;
// DisplayP3 component range per Android's Color longs
constexpr int kDisplayP3Ratio = 15360;
// sRGB component range
constexpr int kSRGBRatio = 255;

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 red = static_cast<int>(round(components.red * kDisplayP3Ratio)) & 0xffff;
int green = static_cast<int>(round(components.green * kDisplayP3Ratio)) & 0xffff;
int blue = static_cast<int>(round(components.blue * kDisplayP3Ratio)) & 0xffff;
int alpha = static_cast<int>(round(components.alpha * 0x3ff)) & 0x3ff;
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>(kDisplayP3ColorSpace);
return androidColor;
} else {
int alpha = static_cast<int>(round(components.alpha * kSRGBRatio)) & 0xff;
int red = static_cast<int>(round(components.red * kSRGBRatio)) & 0xff;
int green = static_cast<int>(round(components.green * kSRGBRatio)) & 0xff;
int blue = static_cast<int>(round(components.blue * kSRGBRatio)) & 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) == kDisplayP3ColorSpace) {
return ColorComponents{
(float)((color >> 48) & 0xffff) / kDisplayP3Ratio,
(float)((color >> 32) & 0xffff) / kDisplayP3Ratio,
(float)((color >> 16) & 0xffff) / kDisplayP3Ratio,
(float)((color >> 6) & 0x3ff) / kDisplayP3Ratio,
ColorSpace::DisplayP3};
} else {
return ColorComponents{
(float)((color >> 48) & 0xff) / kSRGBRatio,
(float)((color >> 40) & 0xff) / kSRGBRatio,
(float)((color >> 32) & 0xff) / kSRGBRatio,
(float)((color >> 56) & 0xff) / kSRGBRatio,
ColorSpace::sRGB};
}
}

} // namespace facebook::react
Loading