From 3c67962512331cb4eb9cb9708b8415f6b6a1c1be Mon Sep 17 00:00:00 2001 From: Jason Morse Date: Thu, 12 Nov 2026 21:58:17 +0000 Subject: [PATCH] fix(graphics): make default text colors portable Define the shared text-color factory through the selected graphics backend so Android and generic C++ link without the Apple implementation. Preserve Apple's existing semantic label color and cover the factory in ColorTest. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 33ea5c72-8bbd-448d-aa21-c9b99dd03c96 --- .../ReactCommon/react/renderer/graphics/Color.cpp | 7 +++++++ .../react/renderer/graphics/HostPlatformColor.h | 6 ++++++ .../cxx/react/renderer/graphics/HostPlatformColor.h | 6 ++++++ .../ios/react/renderer/graphics/HostPlatformColor.h | 2 ++ .../ios/react/renderer/graphics/HostPlatformColor.mm | 8 ++++---- .../react/renderer/graphics/tests/ColorTest.cpp | 10 ++++++++++ 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp index 22361dd9d025..a6f0c0ca2567 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp @@ -86,4 +86,11 @@ SharedColor whiteColor() { return color; } +// [macOS +// Select by graphics backend, not host OS: C++ also builds on Apple hosts. +SharedColor defaultForegroundTextColor() { + return {hostPlatformDefaultForegroundTextColor()}; +} +// macOS] + } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/HostPlatformColor.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/HostPlatformColor.h index bfbefc3832a3..5994da8c63b7 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/HostPlatformColor.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/android/react/renderer/graphics/HostPlatformColor.h @@ -23,6 +23,12 @@ inline Color hostPlatformColorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t return (a & 0xff) << 24 | (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff); } +// [macOS +inline Color hostPlatformDefaultForegroundTextColor() { + return hostPlatformColorFromRGBA(0, 0, 0, 255); +} +// macOS] + inline Color hostPlatformColorFromComponents(ColorComponents components) { float ratio = 255; diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/HostPlatformColor.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/HostPlatformColor.h index a13540363420..3ca4cbe18669 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/HostPlatformColor.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/HostPlatformColor.h @@ -24,6 +24,12 @@ inline Color hostPlatformColorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t return (a & 0xff) << 24 | (r & 0xff) << 16 | (g & 0xff) << 8 | (b & 0xff); } +// [macOS +inline Color hostPlatformDefaultForegroundTextColor() { + return hostPlatformColorFromRGBA(0, 0, 0, 255); +} +// macOS] + inline Color hostPlatformColorFromComponents(ColorComponents components) { float ratio = 255; diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.h index 983deccc82e9..c3524fde1412 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.h @@ -83,6 +83,8 @@ namespace HostPlatformColor { NO_DESTROY static const facebook::react::Color UndefinedColor = Color(); } // namespace HostPlatformColor +Color hostPlatformDefaultForegroundTextColor(); // [macOS] + inline Color hostPlatformColorFromRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { float ratio = 255; diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm index a01024a20646..2a74e6eb5410 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm @@ -10,7 +10,6 @@ #import #import // [macOS] #import -#import // [macOS] #import #import #import @@ -345,10 +344,11 @@ int32_t ColorFromUIColor(const std::shared_ptr &uiColor) } // [macOS -SharedColor defaultForegroundTextColor() { - static SharedColor color = [] { +Color hostPlatformDefaultForegroundTextColor() +{ + static Color color = [] { std::vector items = {"labelColor"}; - return SharedColor(Color::createSemanticColor(items)); + return Color::createSemanticColor(items); }(); return color; } diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/tests/ColorTest.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/tests/ColorTest.cpp index 1e29701ec3f4..683e060d92ad 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/tests/ColorTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/tests/ColorTest.cpp @@ -9,6 +9,16 @@ #include +// [macOS +TEST(ColorTest, testDefaultForegroundTextColorIsDefined) { + using namespace facebook::react; + auto color = defaultForegroundTextColor(); + + EXPECT_TRUE(color); + EXPECT_NE(color, clearColor()); +} +// macOS] + TEST(ColorTest, testColorConversion) { using namespace facebook::react; {