-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Fabric dynamic color support (#42117)
Summary: Fixes #42006 . ## Changelog: [IOS] [ADDED] - Add Fabric dynamic color support Pull Request resolved: #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
- Loading branch information
1 parent
d00d35e
commit 4c108aa
Showing
15 changed files
with
308 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
...tCommon/react/renderer/graphics/platform/ios/react/renderer/graphics/HostPlatformColor.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
#import <react/utils/ManagedObjectWrapper.h> | ||
#import <string> | ||
|
||
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<void> &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<void> 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 |
Oops, something went wrong.