From 7aeac4236fc5437c1bb3d26cb1cd4435b13797a4 Mon Sep 17 00:00:00 2001 From: Andrew Coates <30809111+acoates-ms@users.noreply.github.com> Date: Sat, 22 May 2021 23:22:57 -0700 Subject: [PATCH] Move isColorMeaningful to platform specific code (#31557) Summary: `isColorMeaningful` is the only place in xplat code that currently uses `colorComponentsFromColor`, which assumes that a color is an RGBA value. When implementing `PlatformColor` for windows, where colors might be complex patterns or effects, I'd like to keep the details of `SharedColor` isolated within `SharedColor`. This change moves `isColorMeaningful` into `color.cpp`, where each platform can provide an implementation that takes into account its platform specific color capabilities. See https://github.com/microsoft/react-native-windows/pull/7801 for an example of window's SharedColor which can be either an RGBA value, or a name of a native color/brush. ## Changelog [Internal] [Changed] - Move isColorMeaningful to platform specific code Pull Request resolved: https://github.com/facebook/react-native/pull/31557 Test Plan: This shouldn't change any of the code, its just moving the existing function - normal CI/automation should be plenty of validation. Reviewed By: JoshuaGross, sammy-SC Differential Revision: D28557698 Pulled By: mdvacca fbshipit-source-id: 2a94850fe9c5037598107e1307f4153cee6491fb --- .../react/renderer/components/view/ViewShadowNode.cpp | 8 -------- .../platform/cxx/react/renderer/graphics/Color.cpp | 8 ++++++++ .../graphics/platform/cxx/react/renderer/graphics/Color.h | 1 + .../react/renderer/graphics/platform/ios/Color.cpp | 8 ++++++++ ReactCommon/react/renderer/graphics/platform/ios/Color.h | 1 + 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp b/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp index 1dcbb084b3a4e2..029c3eaf8fc0c9 100644 --- a/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp +++ b/ReactCommon/react/renderer/components/view/ViewShadowNode.cpp @@ -28,14 +28,6 @@ ViewShadowNode::ViewShadowNode( initialize(); } -static bool isColorMeaningful(SharedColor const &color) noexcept { - if (!color) { - return false; - } - - return colorComponentsFromColor(color).alpha > 0; -} - void ViewShadowNode::initialize() noexcept { auto &viewProps = static_cast(*props_); diff --git a/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.cpp b/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.cpp index 92b738e5b33f9c..e33c3ef32707da 100644 --- a/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.cpp +++ b/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.cpp @@ -10,6 +10,14 @@ namespace facebook { namespace react { +bool isColorMeaningful(SharedColor const &color) noexcept { + if (!color) { + return false; + } + + return colorComponentsFromColor(color).alpha > 0; +} + SharedColor colorFromComponents(ColorComponents components) { float ratio = 255; return SharedColor( diff --git a/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h b/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h index 5ab4dabd56874d..34c7f6f53f3eb8 100644 --- a/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h +++ b/ReactCommon/react/renderer/graphics/platform/cxx/react/renderer/graphics/Color.h @@ -59,6 +59,7 @@ class SharedColor { Color color_; }; +bool isColorMeaningful(SharedColor const &color) noexcept; SharedColor colorFromComponents(ColorComponents components); ColorComponents colorComponentsFromColor(SharedColor color); diff --git a/ReactCommon/react/renderer/graphics/platform/ios/Color.cpp b/ReactCommon/react/renderer/graphics/platform/ios/Color.cpp index 0b9bf45c56d4cd..4617555cdb2ce2 100644 --- a/ReactCommon/react/renderer/graphics/platform/ios/Color.cpp +++ b/ReactCommon/react/renderer/graphics/platform/ios/Color.cpp @@ -11,6 +11,14 @@ namespace facebook { namespace react { +bool isColorMeaningful(SharedColor const &color) noexcept { + if (!color) { + return false; + } + + return colorComponentsFromColor(color).alpha > 0; +} + SharedColor colorFromComponents(ColorComponents components) { float ratio = 255; return SharedColor( diff --git a/ReactCommon/react/renderer/graphics/platform/ios/Color.h b/ReactCommon/react/renderer/graphics/platform/ios/Color.h index e85a92fffac11e..bd57a2f9ba6cea 100644 --- a/ReactCommon/react/renderer/graphics/platform/ios/Color.h +++ b/ReactCommon/react/renderer/graphics/platform/ios/Color.h @@ -20,6 +20,7 @@ using Color = int32_t; using SharedColor = better::optional; +bool isColorMeaningful(SharedColor const &color) noexcept; SharedColor colorFromComponents(ColorComponents components); ColorComponents colorComponentsFromColor(SharedColor color);