Fabric: SharedColor for Android

Summary:
@public
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.

Reviewed By: mdvacca

Differential Revision: D8742014

fbshipit-source-id: 14109b61fd84a34989538a15bc6fe4e2a8ce83a6
This commit is contained in:
Valentin Shergin 2018-07-17 18:05:40 -07:00 committed by Facebook Github Bot
parent 50a481d23a
commit 2d1fabbab2
3 changed files with 57 additions and 12 deletions

View File

@ -31,8 +31,8 @@ public:
#pragma mark - Fields
// Color
SharedColor foregroundColor {nullptr};
SharedColor backgroundColor {nullptr};
SharedColor foregroundColor {};
SharedColor backgroundColor {};
Float opacity {std::numeric_limits<Float>::quiet_NaN()};
// Font
@ -51,7 +51,7 @@ public:
folly::Optional<WritingDirection> baseWritingDirection {};
// Decoration
SharedColor textDecorationColor {nullptr};
SharedColor textDecorationColor {};
folly::Optional<TextDecorationLineType> textDecorationLineType {};
folly::Optional<TextDecorationLineStyle> textDecorationLineStyle {};
folly::Optional<TextDecorationLinePattern> textDecorationLinePattern {};
@ -60,7 +60,7 @@ public:
// TODO: Use `Point` type instead of `Size` for `textShadowOffset` attribute.
folly::Optional<Size> textShadowOffset {};
Float textShadowRadius {std::numeric_limits<Float>::quiet_NaN()};
SharedColor textShadowColor {nullptr};
SharedColor textShadowColor {};
// Special
folly::Optional<bool> isHighlighted {};

View File

@ -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

View File

@ -7,15 +7,51 @@
#pragma once
#include <memory>
#include <limits>
#include <fabric/graphics/ColorComponents.h>
namespace facebook {
namespace react {
using Color = float;
using SharedColor = std::shared_ptr<Color>;
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<Color>::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);