diff --git a/ReactCommon/fabric/attributedstring/TextAttributes.h b/ReactCommon/fabric/attributedstring/TextAttributes.h index 584387bf5..2b8c04982 100644 --- a/ReactCommon/fabric/attributedstring/TextAttributes.h +++ b/ReactCommon/fabric/attributedstring/TextAttributes.h @@ -31,8 +31,8 @@ public: #pragma mark - Fields // Color - SharedColor foregroundColor {nullptr}; - SharedColor backgroundColor {nullptr}; + SharedColor foregroundColor {}; + SharedColor backgroundColor {}; Float opacity {std::numeric_limits::quiet_NaN()}; // Font @@ -51,7 +51,7 @@ public: folly::Optional baseWritingDirection {}; // Decoration - SharedColor textDecorationColor {nullptr}; + SharedColor textDecorationColor {}; folly::Optional textDecorationLineType {}; folly::Optional textDecorationLineStyle {}; folly::Optional textDecorationLinePattern {}; @@ -60,7 +60,7 @@ public: // TODO: Use `Point` type instead of `Size` for `textShadowOffset` attribute. folly::Optional textShadowOffset {}; Float textShadowRadius {std::numeric_limits::quiet_NaN()}; - SharedColor textShadowColor {nullptr}; + SharedColor textShadowColor {}; // Special folly::Optional isHighlighted {}; diff --git a/ReactCommon/fabric/graphics/platform/android/Color.cpp b/ReactCommon/fabric/graphics/platform/android/Color.cpp index 3511eeb3b..9a7e88ac2 100644 --- a/ReactCommon/fabric/graphics/platform/android/Color.cpp +++ b/ReactCommon/fabric/graphics/platform/android/Color.cpp @@ -11,13 +11,22 @@ namespace facebook { namespace react { SharedColor colorFromComponents(ColorComponents components) { - // Not implemented. - return {}; + return SharedColor( + ((int)components.alpha & 0xff) << 24 | + ((int)components.red & 0xff) << 16 | + ((int)components.green & 0xff) << 8 | + ((int)components.blue & 0xff) + ); } -ColorComponents colorComponentsFromColor(SharedColor color) { - // Not implemented. - return {}; +ColorComponents colorComponentsFromColor(SharedColor sharedColor) { + Color color = *sharedColor; + return ColorComponents { + (float)((color >> 16) & 0xff), + (float)((color >> 8) & 0xff), + (float)((color ) & 0xff), + (float)((color >> 24) & 0xff) + }; } } // namespace react diff --git a/ReactCommon/fabric/graphics/platform/android/Color.h b/ReactCommon/fabric/graphics/platform/android/Color.h index 761af1d2d..ff6ed8ef4 100644 --- a/ReactCommon/fabric/graphics/platform/android/Color.h +++ b/ReactCommon/fabric/graphics/platform/android/Color.h @@ -7,15 +7,51 @@ #pragma once -#include +#include #include namespace facebook { namespace react { -using Color = float; -using SharedColor = std::shared_ptr; +using Color = int; + +/* + * On Android, a color can be represented as 32 bits integer, so there is no need + * to instantiate complex color objects and then pass them as shared pointers. + * Hense instead of using shared_ptr, we use a simple wrapper class + * which provides a pointer-like interface. + */ +class SharedColor { + +public: + static const Color UndefinedColor = std::numeric_limits::max(); + + SharedColor(): + color_(UndefinedColor) {} + + SharedColor(const SharedColor &sharedColor) : + color_(sharedColor.color_) {} + + SharedColor(Color color): + color_(color) {} + + SharedColor &operator=(const SharedColor &sharedColor) { + color_ = sharedColor.color_; + return *this; + } + + Color operator*() const { + return color_; + } + + operator bool() const { + return color_ != UndefinedColor; + } + +private: + Color color_; +}; SharedColor colorFromComponents(ColorComponents components); ColorComponents colorComponentsFromColor(SharedColor color);