Skip to content

Commit

Permalink
nit perf
Browse files Browse the repository at this point in the history
  • Loading branch information
intergalacticspacehighway committed Jan 2, 2025
1 parent f65c873 commit 8bbcc0a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
13 changes: 7 additions & 6 deletions packages/react-native/React/Fabric/Utils/RCTLinearGradient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -224,22 +224,23 @@ static CGFloat getAngleForKeyword(GradientKeyword keyword, CGSize size)
// The color weighting for the new color stops will be
// pointRelativeOffset^(ln(0.5)/ln(hintRelativeOffset)).
Float hintRelativeOffset = leftDist / totalDist;
const Float logRatio = log(0.5) / log(hintRelativeOffset);
auto leftColor = RCTUIColorFromSharedColor(leftSharedColor);
auto rightColor = RCTUIColorFromSharedColor(rightSharedColor);
NSArray<NSNumber *> *inputRange = @[@0.0, @1.0];
NSArray<UIColor *> *outputRange = @[leftColor, rightColor];

for (auto &newStop : newStops) {
Float pointRelativeOffset = (newStop.position - offsetLeft) / totalDist;
Float weighting = pow(
pointRelativeOffset,
log(0.5) / log(hintRelativeOffset)
logRatio
);

if (!std::isfinite(weighting) || std::isnan(weighting)) {
continue;
}

NSArray<NSNumber *> *inputRange = @[@0.0, @1.0];
auto leftColor = RCTUIColorFromSharedColor(leftSharedColor);
auto rightColor = RCTUIColorFromSharedColor(rightSharedColor);
NSArray<UIColor *> *outputRange = @[leftColor, rightColor];

auto interpolatedColor = RCTInterpolateColorInRange(weighting, inputRange, outputRange);

auto alpha = (interpolatedColor >> 24) & 0xFF;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class Gradient(gradient: ReadableMap?, context: Context) {
// Browsers add 9 intermediate color stops when a transition hint is present
// Algorithm is referred from Blink engine [source](https://github.com/chromium/chromium/blob/a296b1bad6dc1ed9d751b7528f7ca2134227b828/third_party/blink/renderer/core/css/css_gradient_value.cc#L240).
private fun processColorTransitionHints(originalStopsArray: ReadableArray, context: Context): List<ColorStop> {
val colorStops = ArrayList<ColorStop>(originalStopsArray.size())
val colorStops = ArrayList<ColorStop>(originalStopsArray.size() + 9)
for (i in 0 until originalStopsArray.size()) {
val colorStop = originalStopsArray.getMap(i) ?: continue
val position = colorStop.getDouble("position").toFloat()
Expand Down Expand Up @@ -161,11 +161,12 @@ internal class Gradient(gradient: ReadableMap?, context: Context) {
// The color weighting for the new color stops will be
// pointRelativeOffset^(ln(0.5)/ln(hintRelativeOffset)).
val hintRelativeOffset = leftDist / totalDist
val logRatio = ln(0.5) / ln(hintRelativeOffset)
for (newStop in newStops) {
val pointRelativeOffset = (newStop.position - offsetLeft) / totalDist
val weighting = Math.pow(
pointRelativeOffset.toDouble(),
ln(0.5) / ln(hintRelativeOffset.toDouble())
logRatio
).toFloat()

if (weighting.isInfinite() || weighting.isNaN()) {
Expand Down

0 comments on commit 8bbcc0a

Please sign in to comment.