From 4c108aaae4902f47f18dbeb63821618f2a43e895 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 30 Jan 2024 05:29:56 -0800 Subject: [PATCH] Add Fabric dynamic color support (#42117) Summary: Fixes https://github.com/facebook/react-native/issues/42006 . ## Changelog: [IOS] [ADDED] - Add Fabric dynamic color support Pull Request resolved: https://github.com/facebook/react-native/pull/42117 Test Plan: Dynamic color worked. ![image](https://github.com/facebook/react-native/assets/5061845/c6e988c1-9d6b-4c09-a5e9-2f1960bbfcc5) ![image](https://github.com/facebook/react-native/assets/5061845/dcccdef7-990b-4367-b21f-38cabc7040ae) Reviewed By: javache, sammy-SC Differential Revision: D52512672 Pulled By: cipolleschi fbshipit-source-id: f91da08e0bdd07a987289bd82585722496bdc280 --- .../View/RCTViewComponentView.mm | 15 +- .../React/Fabric/RCTConversions.h | 20 +-- .../popupmenu/PopupMenuSelectionEvent.kt | 2 +- .../popupmenu/ReactPopupMenuContainer.kt | 3 +- .../react/renderer/core/ComponentDescriptor.h | 1 - .../react/renderer/core/propsConversions.h | 1 - .../react/renderer/graphics/Color.h | 10 +- .../renderer/graphics/React-graphics.podspec | 4 + .../renderer/graphics/HostPlatformColor.h | 70 ++++++++-- .../renderer/graphics/HostPlatformColor.mm | 132 ++++++++++++++++++ .../renderer/graphics/PlatformColorParser.h | 22 +-- .../renderer/graphics/PlatformColorParser.mm | 72 ++++++++++ .../renderer/graphics/RCTPlatformColorUtils.h | 9 +- .../graphics/RCTPlatformColorUtils.mm | 6 + .../RCTTextPrimitivesConversions.h | 23 +-- 15 files changed, 308 insertions(+), 82 deletions(-) create mode 100644 packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm create mode 100644 packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.mm diff --git a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm index 93482868a5887a..c9e551b96b6ef2 100644 --- a/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm +++ b/packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm @@ -91,6 +91,15 @@ - (void)setBackgroundColor:(UIColor *)backgroundColor _backgroundColor = backgroundColor; } +- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection +{ + [super traitCollectionDidChange:previousTraitCollection]; + + if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) { + [self invalidateLayer]; + } +} + #pragma mark - RCTComponentViewProtocol + (ComponentDescriptorProvider)componentDescriptorProvider @@ -597,6 +606,8 @@ - (void)invalidateLayer borderMetrics.borderWidths.left == 0 || colorComponentsFromColor(borderMetrics.borderColors.left).alpha == 0 || self.clipsToBounds); + CGColorRef backgroundColor = [_backgroundColor resolvedColorWithTraitCollection:self.traitCollection].CGColor; + if (useCoreAnimationBorderRendering) { layer.mask = nil; if (_borderLayer) { @@ -612,7 +623,7 @@ - (void)invalidateLayer layer.cornerCurve = CornerCurveFromBorderCurve(borderMetrics.borderCurves.topLeft); - layer.backgroundColor = _backgroundColor.CGColor; + layer.backgroundColor = backgroundColor; } else { if (!_borderLayer) { _borderLayer = [CALayer new]; @@ -635,7 +646,7 @@ - (void)invalidateLayer RCTCornerRadiiFromBorderRadii(borderMetrics.borderRadii), RCTUIEdgeInsetsFromEdgeInsets(borderMetrics.borderWidths), borderColors, - _backgroundColor.CGColor, + backgroundColor, self.clipsToBounds); RCTReleaseRCTBorderColors(borderColors); diff --git a/packages/react-native/React/Fabric/RCTConversions.h b/packages/react-native/React/Fabric/RCTConversions.h index d8ed955da44bc6..204c54686322ba 100644 --- a/packages/react-native/React/Fabric/RCTConversions.h +++ b/packages/react-native/React/Fabric/RCTConversions.h @@ -11,6 +11,7 @@ #import #import #import +#import #import NS_ASSUME_NONNULL_BEGIN @@ -36,24 +37,7 @@ inline std::string RCTStringFromNSString(NSString *string) inline UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor) { - if (!sharedColor) { - return nil; - } - - if (*facebook::react::clearColor() == *sharedColor) { - return [UIColor clearColor]; - } - - if (*facebook::react::blackColor() == *sharedColor) { - return [UIColor blackColor]; - } - - if (*facebook::react::whiteColor() == *sharedColor) { - return [UIColor whiteColor]; - } - - auto components = facebook::react::colorComponentsFromColor(sharedColor); - return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha]; + return RCTPlatformColorFromColor(*sharedColor); } inline CF_RETURNS_RETAINED CGColorRef _Nullable RCTCreateCGColorRefFromSharedColor( diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/PopupMenuSelectionEvent.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/PopupMenuSelectionEvent.kt index d639e55122a9ff..a418053e7c61b2 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/PopupMenuSelectionEvent.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/PopupMenuSelectionEvent.kt @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ - +@file:Suppress("DEPRECATION") // We want to use RCTEventEmitter for interop purposes package com.facebook.react.views.popupmenu import com.facebook.react.bridge.Arguments diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/ReactPopupMenuContainer.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/ReactPopupMenuContainer.kt index f3a8fa52457b3c..a3f007324e581c 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/ReactPopupMenuContainer.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/popupmenu/ReactPopupMenuContainer.kt @@ -27,8 +27,7 @@ class ReactPopupMenuContainer(context: Context) : FrameLayout(context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { val view = getChildAt(0) val popupMenu = PopupMenu(context, view) - var menu: Menu? = null - menu = popupMenu.menu + var menu = popupMenu.menu val items = menuItems if (items != null) { for (i in 0 until items.size()) { diff --git a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h index fe4be3a27ba551..fd281de68d7032 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h +++ b/packages/react-native/ReactCommon/react/renderer/core/ComponentDescriptor.h @@ -15,7 +15,6 @@ #include #include #include -#include #include namespace facebook::react { diff --git a/packages/react-native/ReactCommon/react/renderer/core/propsConversions.h b/packages/react-native/ReactCommon/react/renderer/core/propsConversions.h index 59e24d40150111..920621c7519229 100644 --- a/packages/react-native/ReactCommon/react/renderer/core/propsConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/core/propsConversions.h @@ -13,7 +13,6 @@ #include #include #include -#include namespace facebook::react { diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/Color.h b/packages/react-native/ReactCommon/react/renderer/graphics/Color.h index c3ae69eaa28ed5..0197dd127ea087 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/Color.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/Color.h @@ -29,7 +29,11 @@ class SharedColor { SharedColor(Color color) : color_(color) {} - Color operator*() const { + Color& operator*() { + return color_; + } + + const Color& operator*() const { return color_; } @@ -61,7 +65,7 @@ SharedColor whiteColor(); template <> struct std::hash { - size_t operator()(facebook::react::SharedColor color) const { - return std::hash{}(*color); + size_t operator()(const facebook::react::SharedColor& color) const { + return std::hash{}(*color); } }; diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec b/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec index 236d1ba277a9d8..2a34496eb1c7df 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec +++ b/packages/react-native/ReactCommon/react/renderer/graphics/React-graphics.podspec @@ -28,6 +28,8 @@ Pod::Spec.new do |s| "\"$(PODS_ROOT)/boost\"", "\"$(PODS_TARGET_SRCROOT)/../../../\"", "\"$(PODS_ROOT)/RCT-Folly\"", + "\"$(PODS_ROOT)/DoubleConversion\"", + "\"$(PODS_ROOT)/fmt/include\"" ] s.name = "React-graphics" @@ -60,4 +62,6 @@ Pod::Spec.new do |s| s.dependency "RCT-Folly/Fabric", folly_version s.dependency "React-Core/Default", version s.dependency "React-utils" + s.dependency "DoubleConversion" + s.dependency "fmt", "9.1.0" end 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 0dd7abe08f05d0..64c26bc7461019 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 @@ -8,32 +8,72 @@ #pragma once #include +#include #include namespace facebook::react { -using Color = int32_t; +struct DynamicColor { + int32_t lightColor = 0; + int32_t darkColor = 0; + int32_t highContrastLightColor = 0; + int32_t highContrastDarkColor = 0; +}; + +struct Color { + Color(int32_t color); + Color(const DynamicColor& dynamicColor); + Color(const ColorComponents& components); + Color(std::shared_ptr uiColor); + int32_t getColor() const; + std::shared_ptr getUIColor() const { + return uiColor_; + } + ColorComponents getColorComponents() const { + float ratio = 255; + int32_t primitiveColor = getColor(); + return ColorComponents{ + (float)((primitiveColor >> 16) & 0xff) / ratio, + (float)((primitiveColor >> 8) & 0xff) / ratio, + (float)((primitiveColor >> 0) & 0xff) / ratio, + (float)((primitiveColor >> 24) & 0xff) / ratio}; + } + bool operator==(const Color& other) const; + bool operator!=(const Color& other) const; + operator int32_t() const { + return getColor(); + } + + private: + std::shared_ptr uiColor_; +}; namespace HostPlatformColor { -static const facebook::react::Color UndefinedColor = - std::numeric_limits::max(); -} + +#if defined(__clang__) +#define NO_DESTROY [[clang::no_destroy]] +#else +#define NO_DESTROY +#endif + +NO_DESTROY static const facebook::react::Color UndefinedColor = Color(nullptr); +} // namespace HostPlatformColor inline Color hostPlatformColorFromComponents(ColorComponents components) { - float ratio = 255; - return ((int)round(components.alpha * ratio) & 0xff) << 24 | - ((int)round(components.red * ratio) & 0xff) << 16 | - ((int)round(components.green * ratio) & 0xff) << 8 | - ((int)round(components.blue * ratio) & 0xff); + return Color(components); } inline ColorComponents colorComponentsFromHostPlatformColor(Color color) { - float ratio = 255; - return ColorComponents{ - (float)((color >> 16) & 0xff) / ratio, - (float)((color >> 8) & 0xff) / ratio, - (float)((color >> 0) & 0xff) / ratio, - (float)((color >> 24) & 0xff) / ratio}; + return color.getColorComponents(); } } // namespace facebook::react + +template <> +struct std::hash { + size_t operator()(const facebook::react::Color& color) const { + auto seed = size_t{0}; + facebook::react::hash_combine(seed, color.getColor()); + return seed; + } +}; 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 new file mode 100644 index 00000000000000..36cd4ac08f7b75 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm @@ -0,0 +1,132 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "HostPlatformColor.h" + +#import +#import +#import +#import + +using namespace facebook::react; + +NS_ASSUME_NONNULL_BEGIN + +namespace facebook::react { + +namespace { +UIColor *_Nullable UIColorFromInt32(int32_t intColor) +{ + CGFloat a = CGFloat((intColor >> 24) & 0xFF) / 255.0; + CGFloat r = CGFloat((intColor >> 16) & 0xFF) / 255.0; + CGFloat g = CGFloat((intColor >> 8) & 0xFF) / 255.0; + CGFloat b = CGFloat(intColor & 0xFF) / 255.0; + return [UIColor colorWithRed:r green:g blue:b alpha:a]; +} + +UIColor *_Nullable UIColorFromDynamicColor(const facebook::react::DynamicColor &dynamicColor) +{ + int32_t light = dynamicColor.lightColor; + int32_t dark = dynamicColor.darkColor; + int32_t highContrastLight = dynamicColor.highContrastLightColor; + int32_t highContrastDark = dynamicColor.highContrastDarkColor; + + UIColor *lightColor = UIColorFromInt32(light); + UIColor *darkColor = UIColorFromInt32(dark); + UIColor *highContrastLightColor = UIColorFromInt32(highContrastLight); + UIColor *highContrastDarkColor = UIColorFromInt32(highContrastDark); + + if (lightColor != nil && darkColor != nil) { + UIColor *color = [UIColor colorWithDynamicProvider:^UIColor *_Nonnull(UITraitCollection *_Nonnull collection) { + if (collection.userInterfaceStyle == UIUserInterfaceStyleDark) { + if (collection.accessibilityContrast == UIAccessibilityContrastHigh && highContrastDarkColor != nil) { + return highContrastDarkColor; + } else { + return darkColor; + } + } else { + if (collection.accessibilityContrast == UIAccessibilityContrastHigh && highContrastLightColor != nil) { + return highContrastLightColor; + } else { + return lightColor; + } + } + }]; + return color; + } else { + return nil; + } + + return nil; +} + +int32_t ColorFromUIColor(UIColor *color) +{ + float ratio = 255; + CGFloat rgba[4]; + [color getRed:&rgba[0] green:&rgba[1] blue:&rgba[2] alpha:&rgba[3]]; + return ((int32_t)round((float)rgba[3] * ratio) & 0xff) << 24 | ((int)round((float)rgba[0] * ratio) & 0xff) << 16 | + ((int)round((float)rgba[1] * ratio) & 0xff) << 8 | ((int)round((float)rgba[2] * ratio) & 0xff); +} + +int32_t ColorFromUIColor(const std::shared_ptr &uiColor) +{ + UIColor *color = (UIColor *)unwrapManagedObject(uiColor); + if (color) { + UITraitCollection *currentTraitCollection = [UITraitCollection currentTraitCollection]; + color = [color resolvedColorWithTraitCollection:currentTraitCollection]; + return ColorFromUIColor(color); + } + + return 0; +} + +UIColor *_Nullable UIColorFromComponentsColor(const facebook::react::ColorComponents &components) +{ + return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha]; +} +} // anonymous namespace + +Color::Color(int32_t color) +{ + uiColor_ = wrapManagedObject(UIColorFromInt32(color)); +} + +Color::Color(const DynamicColor &dynamicColor) +{ + uiColor_ = wrapManagedObject(UIColorFromDynamicColor(dynamicColor)); +} + +Color::Color(const ColorComponents &components) +{ + uiColor_ = wrapManagedObject(UIColorFromComponentsColor(components)); +} + +Color::Color(std::shared_ptr uiColor) +{ + uiColor_ = std::move(uiColor); +} + +bool Color::operator==(const Color &other) const +{ + return (!uiColor_ && !other.uiColor_) || + (uiColor_ && other.uiColor_ && + [unwrapManagedObject(getUIColor()) isEqual:unwrapManagedObject(other.getUIColor())]); +} + +bool Color::operator!=(const Color &other) const +{ + return !(*this == other); +} + +int32_t Color::getColor() const +{ + return ColorFromUIColor(uiColor_); +} +} // namespace facebook::react + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h index d18798e7e600b6..5ed11e598a4eea 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.h @@ -10,29 +10,15 @@ #include #include #include -#include #include #include -#include namespace facebook::react { -inline SharedColor parsePlatformColor( - const ContextContainer& /*contextContainer*/, - int32_t /*surfaceId*/, - const RawValue& value) { - if (value.hasType>()) { - auto items = (std::unordered_map)value; - if (items.find("semantic") != items.end() && - items.at("semantic").hasType>()) { - auto semanticItems = (std::vector)items.at("semantic"); - return {colorFromComponents( - RCTPlatformColorComponentsFromSemanticItems(semanticItems))}; - } - } - - return clearColor(); -} +SharedColor parsePlatformColor( + const ContextContainer& contextContainer, + int32_t surfaceId, + const RawValue& value); inline void fromRawValue( const ContextContainer& contextContainer, diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.mm b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.mm new file mode 100644 index 00000000000000..7c06f8eddf4218 --- /dev/null +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/PlatformColorParser.mm @@ -0,0 +1,72 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "PlatformColorParser.h" + +#import +#import +#import +#import +#import + +using namespace facebook::react; + +NS_ASSUME_NONNULL_BEGIN + +namespace facebook::react { + +inline facebook::react::SharedColor RCTPlatformColorComponentsFromDynamicItems( + const facebook::react::ContextContainer &contextContainer, + int32_t surfaceId, + std::unordered_map &dynamicItems) +{ + SharedColor lightSharedColor{}; + SharedColor darkSharedColor{}; + SharedColor highContrastLightSharedColor{}; + SharedColor highContrastDarkSharedColor{}; + if (dynamicItems.count("light")) { + fromRawValue(contextContainer, surfaceId, dynamicItems.at("light"), lightSharedColor); + } + if (dynamicItems.count("dark")) { + fromRawValue(contextContainer, surfaceId, dynamicItems.at("dark"), darkSharedColor); + } + if (dynamicItems.count("highContrastLight")) { + fromRawValue(contextContainer, surfaceId, dynamicItems.at("highContrastLight"), highContrastLightSharedColor); + } + if (dynamicItems.count("highContrastDark")) { + fromRawValue(contextContainer, surfaceId, dynamicItems.at("highContrastDark"), highContrastDarkSharedColor); + } + + Color color = Color(DynamicColor{ + (*lightSharedColor).getColor(), + (*darkSharedColor).getColor(), + (*highContrastLightSharedColor).getColor(), + (*highContrastDarkSharedColor).getColor()}); + return SharedColor(color); +} + +SharedColor parsePlatformColor(const ContextContainer &contextContainer, int32_t surfaceId, const RawValue &value) +{ + if (value.hasType>()) { + auto items = (std::unordered_map)value; + if (items.find("semantic") != items.end() && items.at("semantic").hasType>()) { + auto semanticItems = (std::vector)items.at("semantic"); + return {colorFromComponents(RCTPlatformColorComponentsFromSemanticItems(semanticItems))}; + } else if ( + items.find("dynamic") != items.end() && + items.at("dynamic").hasType>()) { + auto dynamicItems = (std::unordered_map)items.at("dynamic"); + return RCTPlatformColorComponentsFromDynamicItems(contextContainer, surfaceId, dynamicItems); + } + } + + return clearColor(); +} + +} // namespace facebook::react + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.h b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.h index 397fcde6b59954..8d380fcf714d45 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.h +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.h @@ -5,8 +5,13 @@ * LICENSE file in the root directory of this source tree. */ -#include -#include +#pragma once + +#import +#import +#import facebook::react::ColorComponents RCTPlatformColorComponentsFromSemanticItems( std::vector& semanticItems); + +UIColor* RCTPlatformColorFromColor(const facebook::react::Color& color); diff --git a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.mm b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.mm index 1d566ac8d6c440..118ca4b665e4ab 100644 --- a/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.mm +++ b/packages/react-native/ReactCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/RCTPlatformColorUtils.mm @@ -9,6 +9,7 @@ #import #import +#import #include @@ -200,4 +201,9 @@ return {0, 0, 0, 0}; } +UIColor *RCTPlatformColorFromColor(const facebook::react::Color &color) +{ + return (UIColor *)facebook::react::unwrapManagedObject(color.getUIColor()); +} + NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h index 1921d1ca1cffed..c3f3ac20bd8597 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextPrimitivesConversions.h @@ -7,6 +7,7 @@ #import +#include #include #include @@ -94,24 +95,8 @@ inline static NSUnderlineStyle RCTNSUnderlineStyleFromTextDecorationStyle( } } -inline static UIColor *RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor) +// TODO: this file has some duplicates method, we can remove it +inline static UIColor *_Nullable RCTUIColorFromSharedColor(const facebook::react::SharedColor &sharedColor) { - if (!sharedColor) { - return nil; - } - - if (*facebook::react::clearColor() == *sharedColor) { - return [UIColor clearColor]; - } - - if (*facebook::react::blackColor() == *sharedColor) { - return [UIColor blackColor]; - } - - if (*facebook::react::whiteColor() == *sharedColor) { - return [UIColor whiteColor]; - } - - auto components = facebook::react::colorComponentsFromColor(sharedColor); - return [UIColor colorWithRed:components.red green:components.green blue:components.blue alpha:components.alpha]; + return RCTPlatformColorFromColor(*sharedColor); }