|
| 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 <React/RCTAnimationUtils.h> |
| 10 | +#import <React/RCTConversions.h> |
| 11 | +#import <react/utils/FloatComparison.h> |
| 12 | +#import <vector> |
| 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 | +// Spec: https://drafts.csswg.org/css-images-4/#coloring-gradient-line (Refer transition hint section) |
| 30 | +// Browsers add 9 intermediate color stops when a transition hint is present |
| 31 | +// Algorithm is referred from Blink engine |
| 32 | +// [source](https://github.com/chromium/chromium/blob/a296b1bad6dc1ed9d751b7528f7ca2134227b828/third_party/blink/renderer/core/css/css_gradient_value.cc#L240). |
| 33 | +static std::vector<ProcessedColorStop> processColorTransitionHints(const std::vector<ProcessedColorStop> &originalStops) |
| 34 | +{ |
| 35 | + auto colorStops = std::vector<ProcessedColorStop>(originalStops); |
| 36 | + int indexOffset = 0; |
| 37 | + |
| 38 | + for (size_t i = 1; i < originalStops.size() - 1; ++i) { |
| 39 | + // Skip if not a color hint |
| 40 | + if (originalStops[i].color) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + size_t x = i + indexOffset; |
| 45 | + if (x < 1) { |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + auto offsetLeft = colorStops[x - 1].position.value(); |
| 50 | + auto offsetRight = colorStops[x + 1].position.value(); |
| 51 | + auto offset = colorStops[x].position.value(); |
| 52 | + auto leftDist = offset - offsetLeft; |
| 53 | + auto rightDist = offsetRight - offset; |
| 54 | + auto totalDist = offsetRight - offsetLeft; |
| 55 | + SharedColor leftSharedColor = colorStops[x - 1].color; |
| 56 | + SharedColor rightSharedColor = colorStops[x + 1].color; |
| 57 | + |
| 58 | + if (facebook::react::floatEquality(leftDist, rightDist)) { |
| 59 | + colorStops.erase(colorStops.begin() + x); |
| 60 | + --indexOffset; |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + if (facebook::react::floatEquality(leftDist, .0f)) { |
| 65 | + colorStops[x].color = rightSharedColor; |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (facebook::react::floatEquality(rightDist, .0f)) { |
| 70 | + colorStops[x].color = leftSharedColor; |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + std::vector<ProcessedColorStop> newStops; |
| 75 | + newStops.reserve(9); |
| 76 | + |
| 77 | + // Position the new color stops |
| 78 | + if (leftDist > rightDist) { |
| 79 | + for (int y = 0; y < 7; ++y) { |
| 80 | + ProcessedColorStop newStop{SharedColor(), offsetLeft + leftDist * ((7.0f + y) / 13.0f)}; |
| 81 | + newStops.push_back(newStop); |
| 82 | + } |
| 83 | + ProcessedColorStop stop1{SharedColor(), offset + rightDist * (1.0f / 3.0f)}; |
| 84 | + ProcessedColorStop stop2{SharedColor(), offset + rightDist * (2.0f / 3.0f)}; |
| 85 | + newStops.push_back(stop1); |
| 86 | + newStops.push_back(stop2); |
| 87 | + } else { |
| 88 | + ProcessedColorStop stop1{SharedColor(), offsetLeft + leftDist * (1.0f / 3.0f)}; |
| 89 | + ProcessedColorStop stop2{SharedColor(), offsetLeft + leftDist * (2.0f / 3.0f)}; |
| 90 | + newStops.push_back(stop1); |
| 91 | + newStops.push_back(stop2); |
| 92 | + for (int y = 0; y < 7; ++y) { |
| 93 | + ProcessedColorStop newStop{SharedColor(), offset + rightDist * (y / 13.0f)}; |
| 94 | + newStops.push_back(newStop); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // calculate colors for the new color hints. |
| 99 | + // The color weighting for the new color stops will be |
| 100 | + // pointRelativeOffset^(ln(0.5)/ln(hintRelativeOffset)). |
| 101 | + auto hintRelativeOffset = leftDist / totalDist; |
| 102 | + const auto logRatio = log(0.5) / log(hintRelativeOffset); |
| 103 | + auto leftColor = RCTUIColorFromSharedColor(leftSharedColor); |
| 104 | + auto rightColor = RCTUIColorFromSharedColor(rightSharedColor); |
| 105 | + NSArray<NSNumber *> *inputRange = @[ @0.0, @1.0 ]; |
| 106 | + NSArray<UIColor *> *outputRange = @[ leftColor, rightColor ]; |
| 107 | + |
| 108 | + for (auto &newStop : newStops) { |
| 109 | + auto pointRelativeOffset = (newStop.position.value() - offsetLeft) / totalDist; |
| 110 | + auto weighting = pow(pointRelativeOffset, logRatio); |
| 111 | + |
| 112 | + if (!std::isfinite(weighting) || std::isnan(weighting)) { |
| 113 | + continue; |
| 114 | + } |
| 115 | + |
| 116 | + auto interpolatedColor = RCTInterpolateColorInRange(weighting, inputRange, outputRange); |
| 117 | + |
| 118 | + auto alpha = (interpolatedColor >> 24) & 0xFF; |
| 119 | + auto red = (interpolatedColor >> 16) & 0xFF; |
| 120 | + auto green = (interpolatedColor >> 8) & 0xFF; |
| 121 | + auto blue = interpolatedColor & 0xFF; |
| 122 | + |
| 123 | + newStop.color = facebook::react::colorFromRGBA(red, green, blue, alpha); |
| 124 | + } |
| 125 | + |
| 126 | + // Replace the color hint with new color stops |
| 127 | + colorStops.erase(colorStops.begin() + x); |
| 128 | + colorStops.insert(colorStops.begin() + x, newStops.begin(), newStops.end()); |
| 129 | + indexOffset += 8; |
| 130 | + } |
| 131 | + |
| 132 | + return colorStops; |
| 133 | +} |
| 134 | + |
| 135 | +@implementation RCTGradientUtils |
| 136 | +// https://drafts.csswg.org/css-images-4/#color-stop-fixup |
| 137 | ++ (std::vector<ProcessedColorStop>)getFixedColorStops:(const std::vector<ColorStop> &)colorStops |
| 138 | + gradientLineLength:(CGFloat)gradientLineLength |
| 139 | +{ |
| 140 | + std::vector<ProcessedColorStop> fixedColorStops(colorStops.size()); |
| 141 | + bool hasNullPositions = false; |
| 142 | + auto maxPositionSoFar = resolveColorStopPosition(colorStops[0].position, gradientLineLength); |
| 143 | + if (!maxPositionSoFar.has_value()) { |
| 144 | + maxPositionSoFar = 0.0f; |
| 145 | + } |
| 146 | + |
| 147 | + for (size_t i = 0; i < colorStops.size(); i++) { |
| 148 | + const auto &colorStop = colorStops[i]; |
| 149 | + auto newPosition = resolveColorStopPosition(colorStop.position, gradientLineLength); |
| 150 | + |
| 151 | + if (!newPosition.has_value()) { |
| 152 | + // Step 1: |
| 153 | + // If the first color stop does not have a position, |
| 154 | + // set its position to 0%. If the last color stop does not have a position, |
| 155 | + // set its position to 100%. |
| 156 | + if (i == 0) { |
| 157 | + newPosition = 0.0f; |
| 158 | + } else if (i == colorStops.size() - 1) { |
| 159 | + newPosition = 1.0f; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Step 2: |
| 164 | + // If a color stop or transition hint has a position |
| 165 | + // that is less than the specified position of any color stop or transition hint |
| 166 | + // before it in the list, set its position to be equal to the |
| 167 | + // largest specified position of any color stop or transition hint before it. |
| 168 | + if (newPosition.has_value()) { |
| 169 | + newPosition = std::max(newPosition.value(), maxPositionSoFar.value()); |
| 170 | + fixedColorStops[i] = ProcessedColorStop{colorStop.color, newPosition}; |
| 171 | + maxPositionSoFar = newPosition; |
| 172 | + } else { |
| 173 | + hasNullPositions = true; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Step 3: |
| 178 | + // If any color stop still does not have a position, |
| 179 | + // then, for each run of adjacent color stops without positions, |
| 180 | + // set their positions so that they are evenly spaced between the preceding and |
| 181 | + // following color stops with positions. |
| 182 | + if (hasNullPositions) { |
| 183 | + size_t lastDefinedIndex = 0; |
| 184 | + for (size_t i = 1; i < fixedColorStops.size(); i++) { |
| 185 | + auto endPosition = fixedColorStops[i].position; |
| 186 | + if (endPosition.has_value()) { |
| 187 | + size_t unpositionedStops = i - lastDefinedIndex - 1; |
| 188 | + if (unpositionedStops > 0) { |
| 189 | + auto startPosition = fixedColorStops[lastDefinedIndex].position; |
| 190 | + if (startPosition.has_value()) { |
| 191 | + auto increment = (endPosition.value() - startPosition.value()) / (unpositionedStops + 1); |
| 192 | + for (size_t j = 1; j <= unpositionedStops; j++) { |
| 193 | + fixedColorStops[lastDefinedIndex + j] = |
| 194 | + ProcessedColorStop{colorStops[lastDefinedIndex + j].color, startPosition.value() + increment * j}; |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + lastDefinedIndex = i; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + return processColorTransitionHints(fixedColorStops); |
| 203 | +} |
| 204 | +@end |
0 commit comments