|
9 | 9 | #import <React/RCTAnimationUtils.h> |
10 | 10 | #import <React/RCTConversions.h> |
11 | 11 | #import <react/utils/FloatComparison.h> |
| 12 | +#include <algorithm> |
| 13 | +#include <iterator> |
| 14 | +#include <limits> |
12 | 15 | #include <optional> |
13 | 16 | #import <vector> |
14 | 17 |
|
@@ -143,19 +146,48 @@ CGSize calculateMultipliers(CGSize bounds) |
143 | 146 |
|
144 | 147 | } // namespace |
145 | 148 |
|
| 149 | +// Like Blink, clamp to finite values so that interpolation never divides infinity by infinity. |
| 150 | +static Float clampToFinite(Float position) |
| 151 | +{ |
| 152 | + return std::clamp<Float>(position, -std::numeric_limits<float>::max(), std::numeric_limits<float>::max()); |
| 153 | +} |
| 154 | + |
146 | 155 | static std::optional<Float> resolveColorStopPosition(ValueUnit position, CGFloat gradientLineLength) |
147 | 156 | { |
148 | 157 | if (position.unit == UnitType::Point) { |
149 | | - return position.resolve(0.0f) / gradientLineLength; |
| 158 | + return clampToFinite(gradientLineLength > 0 ? position.resolve(0.0f) / gradientLineLength : 0); |
150 | 159 | } |
151 | 160 |
|
152 | 161 | if (position.unit == UnitType::Percent) { |
153 | | - return position.resolve(1.0f); |
| 162 | + return clampToFinite(position.resolve(1.0f)); |
154 | 163 | } |
155 | 164 |
|
156 | 165 | return std::nullopt; |
157 | 166 | } |
158 | 167 |
|
| 168 | +// Interpolates with premultiplied alpha, as CSS does. |
| 169 | +// Spec: https://drafts.csswg.org/css-color-4/#interpolation-alpha |
| 170 | +static SharedColor interpolateColor(const SharedColor &from, const SharedColor &to, float weight) |
| 171 | +{ |
| 172 | + if (!from || !to) { |
| 173 | + return from ? from : to; |
| 174 | + } |
| 175 | + auto start = colorComponentsFromColor(from); |
| 176 | + auto end = colorComponentsFromColor(to); |
| 177 | + auto alpha = start.alpha + (end.alpha - start.alpha) * weight; |
| 178 | + if (alpha <= 0) { |
| 179 | + return clearColor(); |
| 180 | + } |
| 181 | + auto channel = [&](float startChannel, float endChannel) { |
| 182 | + return (startChannel * start.alpha * (1 - weight) + endChannel * end.alpha * weight) / alpha; |
| 183 | + }; |
| 184 | + return colorFromComponents( |
| 185 | + {.red = channel(start.red, end.red), |
| 186 | + .green = channel(start.green, end.green), |
| 187 | + .blue = channel(start.blue, end.blue), |
| 188 | + .alpha = alpha}); |
| 189 | +} |
| 190 | + |
159 | 191 | // Spec: https://drafts.csswg.org/css-images-4/#coloring-gradient-line (Refer transition hint section) |
160 | 192 | // Browsers add 9 intermediate color stops when a transition hint is present |
161 | 193 | // Algorithm is referred from Blink engine |
@@ -336,6 +368,50 @@ @implementation RCTGradientUtils |
336 | 368 | return processColorTransitionHints(fixedColorStops); |
337 | 369 | } |
338 | 370 |
|
| 371 | +// Spec: https://drafts.csswg.org/css-images-4/#conic-color-stops |
| 372 | +// Color stops outside [0, end] are "never directly consulted for rendering", but "can affect the color of |
| 373 | +// color-stops within the rendered region through interpolation". So they are replaced by stops at the range |
| 374 | +// boundaries, colored as the gradient would be at that point. |
| 375 | ++ (std::vector<ProcessedColorStop>)clipColorStops:(const std::vector<ProcessedColorStop> &)colorStops end:(Float)end |
| 376 | +{ |
| 377 | + if (colorStops.empty() || |
| 378 | + (colorStops.front().position.value_or(0) >= 0 && colorStops.back().position.value_or(0) <= end)) { |
| 379 | + return colorStops; |
| 380 | + } |
| 381 | + |
| 382 | + auto firstInside = std::find_if( |
| 383 | + colorStops.begin(), colorStops.end(), [](const auto &stop) { return stop.position.value_or(0) >= 0; }); |
| 384 | + auto lastInside = std::find_if( |
| 385 | + colorStops.rbegin(), colorStops.rend(), [end](const auto &stop) { return stop.position.value_or(0) <= end; }); |
| 386 | + if (firstInside == colorStops.end()) { |
| 387 | + return {{.color = colorStops.back().color, .position = 0}, {.color = colorStops.back().color, .position = end}}; |
| 388 | + } |
| 389 | + if (lastInside == colorStops.rend()) { |
| 390 | + return {{.color = colorStops.front().color, .position = 0}, {.color = colorStops.front().color, .position = end}}; |
| 391 | + } |
| 392 | + |
| 393 | + auto colorAt = [](const ProcessedColorStop &before, const ProcessedColorStop &after, Float position) { |
| 394 | + auto weight = (position - before.position.value()) / (after.position.value() - before.position.value()); |
| 395 | + return interpolateColor(before.color, after.color, static_cast<float>(weight)); |
| 396 | + }; |
| 397 | + |
| 398 | + std::vector<ProcessedColorStop> clippedColorStops; |
| 399 | + if (firstInside != colorStops.begin() && firstInside->position.value() > 0) { |
| 400 | + clippedColorStops.push_back({.color = colorAt(*std::prev(firstInside), *firstInside, 0), .position = 0}); |
| 401 | + } |
| 402 | + auto insideEnd = lastInside.base(); |
| 403 | + if (firstInside < insideEnd) { |
| 404 | + clippedColorStops.insert(clippedColorStops.end(), firstInside, insideEnd); |
| 405 | + } |
| 406 | + if (insideEnd != colorStops.end() && std::prev(insideEnd)->position.value() < end) { |
| 407 | + clippedColorStops.push_back({.color = colorAt(*std::prev(insideEnd), *insideEnd, end), .position = end}); |
| 408 | + } |
| 409 | + if (clippedColorStops.size() == 1) { |
| 410 | + clippedColorStops.push_back({.color = clippedColorStops.front().color, .position = end}); |
| 411 | + } |
| 412 | + return clippedColorStops; |
| 413 | +} |
| 414 | + |
339 | 415 | // CAGradientLayer linear gradient squishes the non-square gradient to square gradient. |
340 | 416 | // This function fixes the "squished" effect. |
341 | 417 | // See https://stackoverflow.com/a/43176174 for more information. |
|
0 commit comments