Skip to content

Commit 2b688f6

Browse files
rozelefacebook-github-bot
authored andcommitted
Abstract underlying Color implementation (#38668)
Summary: Pull Request resolved: #38668 On other platforms (e.g., react-native-windows), it's possible that platform colors may not be efficiently represented as int32_t values. In the case of react-native-windows, Color can either be an ARGB value or a list of fallback strings for platform colors. This change should decouple how platforms represent color values, allowing them to manage how they are used at the point they are used. ## Changelog: [General] [Added] - Support customization of underlying Color representation in out-of-tree platforms Reviewed By: NickGerleman Differential Revision: D47873465 fbshipit-source-id: 1dbb36be409c04ce87b356d75503ec0cf88f1c5b
1 parent 6e3e6b5 commit 2b688f6

File tree

9 files changed

+137
-29
lines changed

9 files changed

+137
-29
lines changed

packages/react-native/ReactCommon/react/renderer/core/graphicsConversions.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ inline void fromRawValue(
4646
colorComponents.blue = items.at(2);
4747
colorComponents.alpha = length == 4 ? items.at(3) : 1.0f;
4848
} else {
49-
colorComponents = parsePlatformColor(context, value);
49+
result = parsePlatformColor(context, value);
50+
return;
5051
}
5152

5253
result = colorFromComponents(colorComponents);

packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,11 @@ bool isColorMeaningful(SharedColor const &color) noexcept {
1818
}
1919

2020
SharedColor colorFromComponents(ColorComponents components) {
21-
float ratio = 255;
22-
return {
23-
((int)round(components.alpha * ratio) & 0xff) << 24 |
24-
((int)round(components.red * ratio) & 0xff) << 16 |
25-
((int)round(components.green * ratio) & 0xff) << 8 |
26-
((int)round(components.blue * ratio) & 0xff)};
21+
return {hostPlatformColorFromComponents(components)};
2722
}
2823

2924
ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
30-
float ratio = 255;
31-
Color color = *sharedColor;
32-
return ColorComponents{
33-
(float)((color >> 16) & 0xff) / ratio,
34-
(float)((color >> 8) & 0xff) / ratio,
35-
(float)((color >> 0) & 0xff) / ratio,
36-
(float)((color >> 24) & 0xff) / ratio};
25+
return colorComponentsFromHostPlatformColor(*sharedColor);
3726
}
3827

3928
SharedColor clearColor() {

packages/react-native/ReactCommon/react/renderer/graphics/Color.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77

88
#pragma once
99

10-
#include <cmath>
1110
#include <functional>
1211
#include <limits>
1312

1413
#include <react/renderer/graphics/ColorComponents.h>
14+
#include <react/renderer/graphics/HostPlatformColor.h>
1515

1616
namespace facebook::react {
1717

18-
using Color = int32_t;
19-
2018
/*
2119
* On Android, a color can be represented as 32 bits integer, so there is no
2220
* need to instantiate complex color objects and then pass them as shared
2321
* pointers. Hense instead of using shared_ptr, we use a simple wrapper class
24-
* which provides a pointer-like interface.
22+
* which provides a pointer-like interface. On other platforms, colors may be
23+
* represented by more complex objects that cannot be represented as 32-bits
24+
* integers, so we hide the implementation detail in HostPlatformColor.h.
2525
*/
2626
class SharedColor {
2727
public:
28-
static const Color UndefinedColor = std::numeric_limits<Color>::max();
28+
static const Color UndefinedColor = HostPlatformColor::UndefinedColor;
2929

3030
SharedColor() : color_(UndefinedColor) {}
3131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/ColorComponents.h>
11+
#include <cmath>
12+
13+
namespace facebook::react {
14+
15+
using Color = int32_t;
16+
17+
namespace HostPlatformColor {
18+
static const facebook::react::Color UndefinedColor =
19+
std::numeric_limits<facebook::react::Color>::max();
20+
}
21+
22+
inline Color hostPlatformColorFromComponents(ColorComponents components) {
23+
float ratio = 255;
24+
return ((int)round(components.alpha * ratio) & 0xff) << 24 |
25+
((int)round(components.red * ratio) & 0xff) << 16 |
26+
((int)round(components.green * ratio) & 0xff) << 8 |
27+
((int)round(components.blue * ratio) & 0xff);
28+
}
29+
30+
inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
31+
float ratio = 255;
32+
return ColorComponents{
33+
(float)((color >> 16) & 0xff) / ratio,
34+
(float)((color >> 8) & 0xff) / ratio,
35+
(float)((color >> 0) & 0xff) / ratio,
36+
(float)((color >> 24) & 0xff) / ratio};
37+
}
38+
39+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/PlatformColorParser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#include <fbjni/fbjni.h>
1111
#include <react/renderer/core/PropsParserContext.h>
1212
#include <react/renderer/core/RawProps.h>
13-
#include <react/renderer/graphics/ColorComponents.h>
13+
#include <react/renderer/graphics/Color.h>
1414

1515
namespace facebook::react {
1616

17-
inline ColorComponents parsePlatformColor(
17+
inline SharedColor parsePlatformColor(
1818
const PropsParserContext &context,
1919
const RawValue &value) {
2020
ColorComponents colorComponents = {0, 0, 0, 0};
@@ -46,7 +46,7 @@ inline ColorComponents parsePlatformColor(
4646
colorComponents.blue = (argb & 0xFF) / ratio;
4747
}
4848

49-
return colorComponents;
49+
return {colorFromComponents(colorComponents)};
5050
}
5151

5252
} // namespace facebook::react
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/ColorComponents.h>
11+
#include <cmath>
12+
13+
namespace facebook::react {
14+
15+
using Color = int32_t;
16+
17+
namespace HostPlatformColor {
18+
static const facebook::react::Color UndefinedColor =
19+
std::numeric_limits<facebook::react::Color>::max();
20+
}
21+
22+
inline Color hostPlatformColorFromComponents(ColorComponents components) {
23+
float ratio = 255;
24+
return ((int)std::round(components.alpha * ratio) & 0xff) << 24 |
25+
((int)std::round(components.red * ratio) & 0xff) << 16 |
26+
((int)std::round(components.green * ratio) & 0xff) << 8 |
27+
((int)std::round(components.blue * ratio) & 0xff);
28+
}
29+
30+
inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
31+
float ratio = 255;
32+
return ColorComponents{
33+
(float)((color >> 16) & 0xff) / ratio,
34+
(float)((color >> 8) & 0xff) / ratio,
35+
(float)((color >> 0) & 0xff) / ratio,
36+
(float)((color >> 24) & 0xff) / ratio};
37+
}
38+
39+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/PlatformColorParser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99

1010
#include <react/renderer/core/PropsParserContext.h>
1111
#include <react/renderer/core/RawProps.h>
12-
#include <react/renderer/graphics/ColorComponents.h>
12+
#include <react/renderer/graphics/Color.h>
1313

1414
namespace facebook::react {
1515

16-
inline ColorComponents parsePlatformColor(
16+
inline SharedColor parsePlatformColor(
1717
const PropsParserContext &context,
1818
const RawValue &value) {
1919
float alpha = 0;
2020
float red = 0;
2121
float green = 0;
2222
float blue = 0;
2323

24-
return {red, green, blue, alpha};
24+
return {colorFromComponents({red, green, blue, alpha})};
2525
}
2626

2727
} // namespace facebook::react
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/ColorComponents.h>
11+
#include <cmath>
12+
13+
namespace facebook::react {
14+
15+
using Color = int32_t;
16+
17+
namespace HostPlatformColor {
18+
static const facebook::react::Color UndefinedColor =
19+
std::numeric_limits<facebook::react::Color>::max();
20+
}
21+
22+
inline Color hostPlatformColorFromComponents(ColorComponents components) {
23+
float ratio = 255;
24+
return ((int)round(components.alpha * ratio) & 0xff) << 24 |
25+
((int)round(components.red * ratio) & 0xff) << 16 |
26+
((int)round(components.green * ratio) & 0xff) << 8 |
27+
((int)round(components.blue * ratio) & 0xff);
28+
}
29+
30+
inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
31+
float ratio = 255;
32+
return ColorComponents{
33+
(float)((color >> 16) & 0xff) / ratio,
34+
(float)((color >> 8) & 0xff) / ratio,
35+
(float)((color >> 0) & 0xff) / ratio,
36+
(float)((color >> 24) & 0xff) / ratio};
37+
}
38+
39+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,25 @@
99

1010
#include <react/renderer/core/PropsParserContext.h>
1111
#include <react/renderer/core/RawProps.h>
12-
#include <react/renderer/graphics/ColorComponents.h>
12+
#include <react/renderer/graphics/Color.h>
1313
#include <react/renderer/graphics/RCTPlatformColorUtils.h>
1414

1515
namespace facebook::react {
1616

17-
inline ColorComponents parsePlatformColor(
17+
inline SharedColor parsePlatformColor(
1818
const PropsParserContext &context,
1919
const RawValue &value) {
2020
if (value.hasType<butter::map<std::string, RawValue>>()) {
2121
auto items = (butter::map<std::string, RawValue>)value;
2222
if (items.find("semantic") != items.end() &&
2323
items.at("semantic").hasType<std::vector<std::string>>()) {
2424
auto semanticItems = (std::vector<std::string>)items.at("semantic");
25-
return RCTPlatformColorComponentsFromSemanticItems(semanticItems);
25+
return {colorFromComponents(
26+
RCTPlatformColorComponentsFromSemanticItems(semanticItems))};
2627
}
2728
}
2829

29-
return {0, 0, 0, 0};
30+
return clearColor();
3031
}
3132

3233
} // namespace facebook::react

0 commit comments

Comments
 (0)