Skip to content

Include transform in static Gradient lerp methods #149624

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

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/flutter/lib/src/painting/gradient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ class LinearGradient extends Gradient {
colors: interpolated.colors,
stops: interpolated.stops,
tileMode: t < 0.5 ? a.tileMode : b.tileMode, // TODO(ianh): interpolate tile mode
transform: t < 0.5 ? a.transform : b.transform,
Comment on lines 509 to +510
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a big deal, but if you feel like it, you could remove the TODO on the line above (or alternatively, add one on the new line, with a link to #443)

(same for all three gradients)

);
}

Expand Down Expand Up @@ -787,6 +788,7 @@ class RadialGradient extends Gradient {
tileMode: t < 0.5 ? a.tileMode : b.tileMode, // TODO(ianh): interpolate tile mode
focal: AlignmentGeometry.lerp(a.focal, b.focal, t),
focalRadius: math.max(0.0, ui.lerpDouble(a.focalRadius, b.focalRadius, t)!),
transform: t < 0.5 ? a.transform : b.transform,
);
}

Expand Down Expand Up @@ -1052,6 +1054,7 @@ class SweepGradient extends Gradient {
colors: interpolated.colors,
stops: interpolated.stops,
tileMode: t < 0.5 ? a.tileMode : b.tileMode, // TODO(ianh): interpolate tile mode
transform: t < 0.5 ? a.transform : b.transform,
);
}

Expand Down
78 changes: 78 additions & 0 deletions packages/flutter/test/painting/gradient_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ void main() {
));
});

test('LinearGradient lerp test with transforms', () {
const LinearGradient testGradient1 = LinearGradient(
transform: GradientRotation(math.pi/4),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);
const LinearGradient testGradient2 = LinearGradient(
transform: GradientRotation(math.pi/2),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);

final LinearGradient? actual0 = LinearGradient.lerp(testGradient1, testGradient2, 0.0);
final LinearGradient? actual1 = LinearGradient.lerp(testGradient1, testGradient2, 1.0);
final LinearGradient? actual2 = LinearGradient.lerp(testGradient1, testGradient2, 0.5);
expect(testGradient1, equals(actual0));
expect(testGradient2, equals(actual1));
expect(testGradient2, equals(actual2));
Comment on lines +215 to +217
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(testGradient1, equals(actual0));
expect(testGradient2, equals(actual1));
expect(testGradient2, equals(actual2));
expect(actual0, equals(testGradient1));
expect(actual1, equals(testGradient2));
expect(actual2, equals(testGradient2));

Tiny nit: "actual" should be passed as the first argument for expect() (here and below). Not a big deal though, since in this case the test works fine either way.

});

test('LinearGradient toString', () {
expect(
const LinearGradient(
Expand Down Expand Up @@ -481,6 +507,32 @@ void main() {
));
});

test('RadialGradient lerp test with transforms', () {
const RadialGradient testGradient1 = RadialGradient(
transform: GradientRotation(math.pi/4),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);
const RadialGradient testGradient2 = RadialGradient(
transform: GradientRotation(math.pi/2),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);

final RadialGradient? actual0 = RadialGradient.lerp(testGradient1, testGradient2, 0.0);
final RadialGradient? actual1 = RadialGradient.lerp(testGradient1, testGradient2, 1.0);
final RadialGradient? actual2 = RadialGradient.lerp(testGradient1, testGradient2, 0.5);
expect(testGradient1, equals(actual0));
expect(testGradient2, equals(actual1));
expect(testGradient2, equals(actual2));
});

test('RadialGradient lerp test with focal', () {
const RadialGradient testGradient1 = RadialGradient(
center: Alignment.topLeft,
Expand Down Expand Up @@ -706,6 +758,32 @@ void main() {
));
});

test('SweepGradient lerp test with transforms', () {
const SweepGradient testGradient1 = SweepGradient(
transform: GradientRotation(math.pi/4),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);
const SweepGradient testGradient2 = SweepGradient(
transform: GradientRotation(math.pi/2),
colors: <Color>[
Color(0x33333333),
Color(0x66666666),
],
stops: <double>[0, 1],
);

final SweepGradient? actual0 = SweepGradient.lerp(testGradient1, testGradient2, 0.0);
final SweepGradient? actual1 = SweepGradient.lerp(testGradient1, testGradient2, 1.0);
final SweepGradient? actual2 = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
expect(testGradient1, equals(actual0));
expect(testGradient2, equals(actual1));
expect(testGradient2, equals(actual2));
});

test('SweepGradient scale test)', () {
const SweepGradient testGradient = SweepGradient(
center: Alignment.topLeft,
Expand Down