Skip to content

Commit 2d1fabb

Browse files
sherginfacebook-github-bot
authored andcommitted
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
1 parent 50a481d commit 2d1fabb

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

Diff for: ReactCommon/fabric/attributedstring/TextAttributes.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class TextAttributes:
3131
#pragma mark - Fields
3232

3333
// Color
34-
SharedColor foregroundColor {nullptr};
35-
SharedColor backgroundColor {nullptr};
34+
SharedColor foregroundColor {};
35+
SharedColor backgroundColor {};
3636
Float opacity {std::numeric_limits<Float>::quiet_NaN()};
3737

3838
// Font
@@ -51,7 +51,7 @@ class TextAttributes:
5151
folly::Optional<WritingDirection> baseWritingDirection {};
5252

5353
// Decoration
54-
SharedColor textDecorationColor {nullptr};
54+
SharedColor textDecorationColor {};
5555
folly::Optional<TextDecorationLineType> textDecorationLineType {};
5656
folly::Optional<TextDecorationLineStyle> textDecorationLineStyle {};
5757
folly::Optional<TextDecorationLinePattern> textDecorationLinePattern {};
@@ -60,7 +60,7 @@ class TextAttributes:
6060
// TODO: Use `Point` type instead of `Size` for `textShadowOffset` attribute.
6161
folly::Optional<Size> textShadowOffset {};
6262
Float textShadowRadius {std::numeric_limits<Float>::quiet_NaN()};
63-
SharedColor textShadowColor {nullptr};
63+
SharedColor textShadowColor {};
6464

6565
// Special
6666
folly::Optional<bool> isHighlighted {};

Diff for: ReactCommon/fabric/graphics/platform/android/Color.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ namespace facebook {
1111
namespace react {
1212

1313
SharedColor colorFromComponents(ColorComponents components) {
14-
// Not implemented.
15-
return {};
14+
return SharedColor(
15+
((int)components.alpha & 0xff) << 24 |
16+
((int)components.red & 0xff) << 16 |
17+
((int)components.green & 0xff) << 8 |
18+
((int)components.blue & 0xff)
19+
);
1620
}
1721

18-
ColorComponents colorComponentsFromColor(SharedColor color) {
19-
// Not implemented.
20-
return {};
22+
ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
23+
Color color = *sharedColor;
24+
return ColorComponents {
25+
(float)((color >> 16) & 0xff),
26+
(float)((color >> 8) & 0xff),
27+
(float)((color ) & 0xff),
28+
(float)((color >> 24) & 0xff)
29+
};
2130
}
2231

2332
} // namespace react

Diff for: ReactCommon/fabric/graphics/platform/android/Color.h

+39-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,51 @@
77

88
#pragma once
99

10-
#include <memory>
10+
#include <limits>
1111

1212
#include <fabric/graphics/ColorComponents.h>
1313

1414
namespace facebook {
1515
namespace react {
1616

17-
using Color = float;
18-
using SharedColor = std::shared_ptr<Color>;
17+
using Color = int;
18+
19+
/*
20+
* On Android, a color can be represented as 32 bits integer, so there is no need
21+
* to instantiate complex color objects and then pass them as shared pointers.
22+
* Hense instead of using shared_ptr, we use a simple wrapper class
23+
* which provides a pointer-like interface.
24+
*/
25+
class SharedColor {
26+
27+
public:
28+
static const Color UndefinedColor = std::numeric_limits<Color>::max();
29+
30+
SharedColor():
31+
color_(UndefinedColor) {}
32+
33+
SharedColor(const SharedColor &sharedColor) :
34+
color_(sharedColor.color_) {}
35+
36+
SharedColor(Color color):
37+
color_(color) {}
38+
39+
SharedColor &operator=(const SharedColor &sharedColor) {
40+
color_ = sharedColor.color_;
41+
return *this;
42+
}
43+
44+
Color operator*() const {
45+
return color_;
46+
}
47+
48+
operator bool() const {
49+
return color_ != UndefinedColor;
50+
}
51+
52+
private:
53+
Color color_;
54+
};
1955

2056
SharedColor colorFromComponents(ColorComponents components);
2157
ColorComponents colorComponentsFromColor(SharedColor color);

0 commit comments

Comments
 (0)