Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved docs for gradient parser #2065

Merged
merged 1 commit into from Apr 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -123,6 +123,9 @@ private GradientColor addOpacityStopsToGradientIfNeeded(GradientColor gradientCo
return gradientColor;
}

// When there are opacity stops, we create a merged list of color stops and opacity stops.
// For a given color stop, we linearly interpolate the opacity for the two opacity stops around it.
// For a given opacity stop, we linearly interpolate the color for the two color stops around it.
float[] colorStopPositions = gradientColor.getPositions();
int[] colorStopColors = gradientColor.getColors();

Expand Down Expand Up @@ -154,14 +157,13 @@ private GradientColor addOpacityStopsToGradientIfNeeded(GradientColor gradientCo
if (colorStopIndex < 0 || opacityIndex > 0) {
// This is a stop derived from an opacity stop.
if (opacityIndex < 0) {
throw new IllegalArgumentException("Unable to find opacity stop position for " + position);
// TODO: use this backup instead…
// opacityIndex = -(opacityIndex + 1);
// The formula here is derived from the return value for binarySearch. When an item isn't found, it returns -insertionPoint - 1.
opacityIndex = -(opacityIndex + 1);
}
newColors[i] = getColorInBetweenColorStops(position, opacityStopOpacities[opacityIndex], colorStopPositions, colorStopColors);
} else {
// This os a step derived from a color stop.
newColors[i] = getColorWithOpacityStops(colorStopColors[colorStopIndex], position, opacityStopPositions, opacityStopOpacities);
newColors[i] = getColorInBetweenOpacityStops(position, colorStopColors[colorStopIndex], opacityStopPositions, opacityStopOpacities);
}
}
return new GradientColor(newPositions, newColors);
Expand Down Expand Up @@ -191,7 +193,7 @@ private int getColorInBetweenColorStops(float position, float opacity, float[] c
throw new IllegalArgumentException("Unreachable code.");
}

private int getColorWithOpacityStops(int color, float position, float[] opacityStopPositions, float[] opacityStopOpacities) {
private int getColorInBetweenOpacityStops(float position, int color, float[] opacityStopPositions, float[] opacityStopOpacities) {
if (opacityStopOpacities.length < 2 || position <= opacityStopPositions[0]) {
int a = (int) (opacityStopOpacities[0] * 255);
int r = Color.red(color);
Expand Down