Skip to content

Commit e57fde2

Browse files
radial gradient iOS changes
1 parent cbd2ee5 commit e57fde2

File tree

12 files changed

+706
-240
lines changed

12 files changed

+706
-240
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/View/RCTViewComponentView.mm

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import <React/RCTBoxShadow.h>
1818
#import <React/RCTConversions.h>
1919
#import <React/RCTLinearGradient.h>
20+
#import <React/RCTRadialGradient.h>
2021
#import <React/RCTLocalizedString.h>
2122
#import <React/RCTViewFinder.h>
2223
#import <react/featureflags/ReactNativeFeatureFlags.h>
@@ -1026,6 +1027,15 @@ - (void)invalidateLayer
10261027
backgroundImageLayer.zPosition = BACKGROUND_COLOR_ZPOSITION;
10271028
[self.layer addSublayer:backgroundImageLayer];
10281029
[_backgroundImageLayers addObject:backgroundImageLayer];
1030+
} else if (std::holds_alternative<RadialGradient>(backgroundImage)) {
1031+
const auto &radialGradient = std::get<RadialGradient>(backgroundImage);
1032+
CALayer *backgroundImageLayer = [RCTRadialGradient gradientLayerWithSize:self.layer.bounds.size
1033+
gradient:radialGradient];
1034+
[self shapeLayerToMatchView:backgroundImageLayer borderMetrics:borderMetrics];
1035+
backgroundImageLayer.masksToBounds = YES;
1036+
backgroundImageLayer.zPosition = BACKGROUND_COLOR_ZPOSITION;
1037+
[self.layer addSublayer:backgroundImageLayer];
1038+
[_backgroundImageLayers addObject:backgroundImageLayer];
10291039
}
10301040
}
10311041
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include <react/renderer/graphics/ColorStop.h>
9+
#import <vector>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface RCTGradientUtils : NSObject
14+
15+
+ (std::vector<facebook::react::ProcessedColorStop>)getFixedColorStops:(const std::vector<facebook::react::ColorStop> &)colorStops gradientLineLength:(CGFloat)gradientLineLength;
16+
@end
17+
18+
NS_ASSUME_NONNULL_END
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTGradientUtils.h"
9+
#import <vector>
10+
#import <React/RCTConversions.h>
11+
#import <react/utils/FloatComparison.h>
12+
#import <React/RCTAnimationUtils.h>
13+
14+
using namespace facebook::react;
15+
16+
static std::optional<Float> resolveColorStopPosition(ValueUnit position, CGFloat gradientLineLength)
17+
{
18+
if (position.unit == UnitType::Point) {
19+
return position.resolve(0.0f) / gradientLineLength;
20+
}
21+
22+
if (position.unit == UnitType::Percent) {
23+
return position.resolve(1.0f);
24+
}
25+
26+
return std::nullopt;
27+
}
28+
29+
30+
// Spec: https://drafts.csswg.org/css-images-4/#coloring-gradient-line (Refer transition hint section)
31+
// Browsers add 9 intermediate color stops when a transition hint is present
32+
// Algorithm is referred from Blink engine
33+
// [source](https://github.com/chromium/chromium/blob/a296b1bad6dc1ed9d751b7528f7ca2134227b828/third_party/blink/renderer/core/css/css_gradient_value.cc#L240).
34+
static std::vector<ProcessedColorStop> processColorTransitionHints(const std::vector<ProcessedColorStop>& originalStops)
35+
{
36+
auto colorStops = std::vector<ProcessedColorStop>(originalStops);
37+
int indexOffset = 0;
38+
39+
for (size_t i = 1; i < originalStops.size() - 1; ++i) {
40+
// Skip if not a color hint
41+
if (originalStops[i].color) {
42+
continue;
43+
}
44+
45+
size_t x = i + indexOffset;
46+
if (x < 1) {
47+
continue;
48+
}
49+
50+
auto offsetLeft = colorStops[x - 1].position.value();
51+
auto offsetRight = colorStops[x + 1].position.value();
52+
auto offset = colorStops[x].position.value();
53+
auto leftDist = offset - offsetLeft;
54+
auto rightDist = offsetRight - offset;
55+
auto totalDist = offsetRight - offsetLeft;
56+
SharedColor leftSharedColor = colorStops[x - 1].color;
57+
SharedColor rightSharedColor = colorStops[x + 1].color;
58+
59+
if (facebook::react::floatEquality(leftDist, rightDist)) {
60+
colorStops.erase(colorStops.begin() + x);
61+
--indexOffset;
62+
continue;
63+
}
64+
65+
if (facebook::react::floatEquality(leftDist, .0f)) {
66+
colorStops[x].color = rightSharedColor;
67+
continue;
68+
}
69+
70+
if (facebook::react::floatEquality(rightDist, .0f)) {
71+
colorStops[x].color = leftSharedColor;
72+
continue;
73+
}
74+
75+
std::vector<ProcessedColorStop> newStops;
76+
newStops.reserve(9);
77+
78+
// Position the new color stops
79+
if (leftDist > rightDist) {
80+
for (int y = 0; y < 7; ++y) {
81+
ProcessedColorStop newStop{SharedColor(), offsetLeft + leftDist * ((7.0f + y) / 13.0f)};
82+
newStops.push_back(newStop);
83+
}
84+
ProcessedColorStop stop1{SharedColor(), offset + rightDist * (1.0f / 3.0f)};
85+
ProcessedColorStop stop2{SharedColor(), offset + rightDist * (2.0f / 3.0f)};
86+
newStops.push_back(stop1);
87+
newStops.push_back(stop2);
88+
} else {
89+
ProcessedColorStop stop1{SharedColor(), offsetLeft + leftDist * (1.0f / 3.0f)};
90+
ProcessedColorStop stop2{SharedColor(), offsetLeft + leftDist * (2.0f / 3.0f)};
91+
newStops.push_back(stop1);
92+
newStops.push_back(stop2);
93+
for (int y = 0; y < 7; ++y) {
94+
ProcessedColorStop newStop{SharedColor(), offset + rightDist * (y / 13.0f)};
95+
newStops.push_back(newStop);
96+
}
97+
}
98+
99+
// calculate colors for the new color hints.
100+
// The color weighting for the new color stops will be
101+
// pointRelativeOffset^(ln(0.5)/ln(hintRelativeOffset)).
102+
auto hintRelativeOffset = leftDist / totalDist;
103+
const auto logRatio = log(0.5) / log(hintRelativeOffset);
104+
auto leftColor = RCTUIColorFromSharedColor(leftSharedColor);
105+
auto rightColor = RCTUIColorFromSharedColor(rightSharedColor);
106+
NSArray<NSNumber *> *inputRange = @[ @0.0, @1.0 ];
107+
NSArray<UIColor *> *outputRange = @[ leftColor, rightColor ];
108+
109+
for (auto &newStop : newStops) {
110+
auto pointRelativeOffset = (newStop.position.value() - offsetLeft) / totalDist;
111+
auto weighting = pow(pointRelativeOffset, logRatio);
112+
113+
if (!std::isfinite(weighting) || std::isnan(weighting)) {
114+
continue;
115+
}
116+
117+
auto interpolatedColor = RCTInterpolateColorInRange(weighting, inputRange, outputRange);
118+
119+
auto alpha = (interpolatedColor >> 24) & 0xFF;
120+
auto red = (interpolatedColor >> 16) & 0xFF;
121+
auto green = (interpolatedColor >> 8) & 0xFF;
122+
auto blue = interpolatedColor & 0xFF;
123+
124+
newStop.color = facebook::react::colorFromRGBA(red, green, blue, alpha);
125+
}
126+
127+
// Replace the color hint with new color stops
128+
colorStops.erase(colorStops.begin() + x);
129+
colorStops.insert(colorStops.begin() + x, newStops.begin(), newStops.end());
130+
indexOffset += 8;
131+
}
132+
133+
return colorStops;
134+
}
135+
136+
137+
@implementation RCTGradientUtils
138+
// https://drafts.csswg.org/css-images-4/#color-stop-fixup
139+
+ (std::vector<ProcessedColorStop>)getFixedColorStops:(const std::vector<ColorStop> &)colorStops gradientLineLength:(CGFloat)gradientLineLength
140+
{
141+
std::vector<ProcessedColorStop> fixedColorStops(colorStops.size());
142+
bool hasNullPositions = false;
143+
auto maxPositionSoFar = resolveColorStopPosition(colorStops[0].position, gradientLineLength);
144+
if (!maxPositionSoFar.has_value()) {
145+
maxPositionSoFar = 0.0f;
146+
}
147+
148+
for (size_t i = 0; i < colorStops.size(); i++) {
149+
const auto &colorStop = colorStops[i];
150+
auto newPosition = resolveColorStopPosition(colorStop.position, gradientLineLength);
151+
152+
if (!newPosition.has_value()) {
153+
// Step 1:
154+
// If the first color stop does not have a position,
155+
// set its position to 0%. If the last color stop does not have a position,
156+
// set its position to 100%.
157+
if (i == 0) {
158+
newPosition = 0.0f;
159+
} else if (i == colorStops.size() - 1) {
160+
newPosition = 1.0f;
161+
}
162+
}
163+
164+
// Step 2:
165+
// If a color stop or transition hint has a position
166+
// that is less than the specified position of any color stop or transition hint
167+
// before it in the list, set its position to be equal to the
168+
// largest specified position of any color stop or transition hint before it.
169+
if (newPosition.has_value()) {
170+
newPosition = std::max(newPosition.value(), maxPositionSoFar.value());
171+
fixedColorStops[i] = ProcessedColorStop{colorStop.color, newPosition};
172+
maxPositionSoFar = newPosition;
173+
} else {
174+
hasNullPositions = true;
175+
}
176+
}
177+
178+
// Step 3:
179+
// If any color stop still does not have a position,
180+
// then, for each run of adjacent color stops without positions,
181+
// set their positions so that they are evenly spaced between the preceding and
182+
// following color stops with positions.
183+
if (hasNullPositions) {
184+
size_t lastDefinedIndex = 0;
185+
for (size_t i = 1; i < fixedColorStops.size(); i++) {
186+
auto endPosition = fixedColorStops[i].position;
187+
if (endPosition.has_value()) {
188+
size_t unpositionedStops = i - lastDefinedIndex - 1;
189+
if (unpositionedStops > 0) {
190+
auto startPosition = fixedColorStops[lastDefinedIndex].position;
191+
if (startPosition.has_value()) {
192+
auto increment = (endPosition.value() - startPosition.value()) / (unpositionedStops + 1);
193+
for (size_t j = 1; j <= unpositionedStops; j++) {
194+
fixedColorStops[lastDefinedIndex + j] =
195+
ProcessedColorStop{colorStops[lastDefinedIndex + j].color, startPosition.value() + increment * j};
196+
}
197+
}
198+
}
199+
lastDefinedIndex = i;
200+
}
201+
}
202+
}
203+
return processColorTransitionHints(fixedColorStops);
204+
}
205+
@end
206+

0 commit comments

Comments
 (0)