Skip to content

Commit b5683a7

Browse files
zeyapfacebook-github-bot
authored andcommitted
Create util functions to convert Color <-> uint8_t RGBA values (#45139)
Summary: Pull Request resolved: #45139 ## Changelog Sometimes we pass down int RGBA values in [0, 255] to represent color components in ReactNative; and other times we use float. Since we introduce display P3, we probably want to use color depth more than 8bit In the first case, when RGBA are passed down as uint8_t, we can directly calculate Color/SharedColor without normalizing them to [0,1] and scale back to [0, 255] as `colorFromComponents` does; otherwise there could be some precision loss.. So here I'm adding some new utils for that purpose: see `colorFromRGBA`, `redFromColor`, `greenFromColor`, etc [Internal] Examples in codebase: * rgba in [0, 255]: https://www.internalfb.com/code/fbsource/[67148a47147b0e15f0f0748003394040611c2bc2]/xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h?lines=28-35 * rgba in [0, 1]: https://www.internalfb.com/code/fbsource/[67148a47147b0e15f0f0748003394040611c2bc2]/xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h?lines=37-45 https://www.internalfb.com/code/fbsource/[67148a47147b0e15f0f0748003394040611c2bc2]/xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/graphics/fromRawValueShared.h?lines=47-64 Reviewed By: rshest Differential Revision: D58872165 fbshipit-source-id: 748a12a99591c895fed65cda6deeaa2a3e0c3cf5
1 parent 08b05f3 commit b5683a7

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ bool isColorMeaningful(const SharedColor& color) noexcept {
1717
return colorComponentsFromColor(color).alpha > 0;
1818
}
1919

20+
// Create Color from float RGBA values in [0, 1] range
2021
SharedColor colorFromComponents(ColorComponents components) {
2122
return {hostPlatformColorFromComponents(components)};
2223
}
2324

25+
// Read Color components in [0, 1] range
2426
ColorComponents colorComponentsFromColor(SharedColor sharedColor) {
2527
return colorComponentsFromHostPlatformColor(*sharedColor);
2628
}

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

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <react/renderer/graphics/ColorComponents.h>
1111
#include <cmath>
12+
#include <cstdint>
1213

1314
namespace facebook::react {
1415

@@ -19,21 +20,43 @@ static const facebook::react::Color UndefinedColor =
1920
std::numeric_limits<facebook::react::Color>::max();
2021
}
2122

23+
inline Color
24+
hostPlatformColorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
25+
return (a & 0xff) << 24 | (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff);
26+
}
27+
2228
inline Color hostPlatformColorFromComponents(ColorComponents components) {
2329
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);
30+
return hostPlatformColorFromRGBA(
31+
static_cast<uint8_t>(std::round(components.red * ratio)),
32+
static_cast<uint8_t>(std::round(components.green * ratio)),
33+
static_cast<uint8_t>(std::round(components.blue * ratio)),
34+
static_cast<uint8_t>(std::round(components.alpha * ratio)));
35+
}
36+
37+
inline uint8_t alphaFromHostPlatformColor(Color color) {
38+
return static_cast<uint8_t>((color >> 24) & 0xff);
39+
}
40+
41+
inline uint8_t redFromHostPlatformColor(Color color) {
42+
return static_cast<uint8_t>((color >> 16) & 0xff);
43+
}
44+
45+
inline uint8_t greenFromHostPlatformColor(Color color) {
46+
return static_cast<uint8_t>((color >> 8) & 0xff);
47+
}
48+
49+
inline uint8_t blueFromHostPlatformColor(Color color) {
50+
return static_cast<uint8_t>((color >> 0) & 0xff);
2851
}
2952

3053
inline ColorComponents colorComponentsFromHostPlatformColor(Color color) {
3154
float ratio = 255;
3255
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};
56+
static_cast<float>(redFromHostPlatformColor(color)) / ratio,
57+
static_cast<float>(greenFromHostPlatformColor(color)) / ratio,
58+
static_cast<float>(blueFromHostPlatformColor(color)) / ratio,
59+
static_cast<float>(alphaFromHostPlatformColor(color)) / ratio};
3760
}
3861

3962
} // namespace facebook::react

0 commit comments

Comments
 (0)