From d42d46306a10c4098ffae6295652af43f8b37b1f Mon Sep 17 00:00:00 2001 From: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Date: Thu, 22 May 2025 07:20:32 -0700 Subject: [PATCH] Allow platforms to provide custom IsColorMeaningful implementation (#51478) Summary: On windows, not all PlatformColors are convertible directly into colorComponents. For PlatformColors, the Color.isColorMeaningful may need to do something other than check the alpha component of the color. Here I'm moving the implementation of isColorMeaningful into the HostPlatformColor implemantion to allow the host platform to customize the implemenation of isColorMeaningful. ## Changelog: [INTERNAL] [ADDED] - Allow platforms to override isColorMeaningful Pull Request resolved: https://github.com/facebook/react-native/pull/51478 Test Plan: No behavior change in core. -- Will be used in react-native-windows to fix https://github.com/microsoft/react-native-windows/issues/14006 Reviewed By: NickGerleman Differential Revision: D75088378 Pulled By: javache fbshipit-source-id: 0a456bfe6be93098e3d8fa22390a971e14a4312b --- .../ReactCommon/react/renderer/graphics/Color.cpp | 2 +- .../android/react/renderer/graphics/HostPlatformColor.h | 4 ++++ .../platform/cxx/react/renderer/graphics/HostPlatformColor.h | 4 ++++ .../platform/ios/react/renderer/graphics/HostPlatformColor.h | 4 ++++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp index 94c9ad2c9b2..08f43575864 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Color.cpp @@ -14,7 +14,7 @@ bool isColorMeaningful(const SharedColor& color) noexcept { return false; } - return colorComponentsFromColor(color).alpha > 0; + return hostPlatformColorIsColorMeaningful(*color); } // Create Color from float RGBA values in [0, 1] range 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 0703aa558a5..a09eb626d2e 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 @@ -56,4 +56,8 @@ inline float blueFromHostPlatformColor(Color color) { return static_cast((color >> 0) & 0xff); } +inline bool hostPlatformColorIsColorMeaningful(Color color) noexcept { + return alphaFromHostPlatformColor(color) > 0; +} + } // namespace facebook::react 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 0003bbd6b9f..3bbcb1e11ea 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 @@ -49,6 +49,10 @@ inline float blueFromHostPlatformColor(Color color) { return static_cast((color >> 0) & 0xff); } +inline bool hostPlatformColorIsColorMeaningful(Color color) noexcept { + return alphaFromHostPlatformColor(color) > 0; +} + inline ColorComponents colorComponentsFromHostPlatformColor(Color color) { float ratio = 255; return ColorComponents{ 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 5dd1ac810e5..8deffedcb1d 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 @@ -104,6 +104,10 @@ inline float blueFromHostPlatformColor(Color color) { return color.getChannel(2) * 255; } +inline bool hostPlatformColorIsColorMeaningful(Color color) noexcept { + return alphaFromHostPlatformColor(color) > 0; +} + } // namespace facebook::react template <>