Commit d57cc44f authored by Victor Choueiri's avatar Victor Choueiri Committed by Greg Spencer

Add SweepGradient (#17368)

This PR adds a SweepGradient class, extending Gradient to expose the engine's ui.Gradient.sweep shader.

Similar to LinearGradient and RadialGradient - SweepGradients can be used in a BoxDecoration or passed to a Paint's shader.
parent 6fc7199d
......@@ -23,3 +23,4 @@ Ali Bitek <alibitek@protonmail.ch>
Tetsuhiro Ueda <najeira@gmail.com>
Dan Field <dfield@gmail.com>
Noah Groß <gross@ngsger.de>
Victor Choueiri <victor@ctrlanddev.com>
......@@ -233,9 +233,9 @@ class ShaderMask extends SingleChildRenderObjectWidget {
/// The shader callback is called with the current size of the child so that
/// it can customize the shader to the size and location of the child.
///
/// Typically this will use a [LinearGradient] or [RadialGradient] to create
/// the [dart:ui.Shader], though the [dart:ui.ImageShader] class could also be
/// used.
/// Typically this will use a [LinearGradient], [RadialGradient], or
/// [SweepGradient] to create the [dart:ui.Shader], though the
/// [dart:ui.ImageShader] class could also be used.
final ShaderCallback shaderCallback;
/// The [BlendMode] to use when applying the shader to the child.
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' as math;
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/painting.dart';
......@@ -193,6 +194,45 @@ void main() {
);
});
test('SweepGradient with AlignmentDirectional', () {
expect(
() {
return const SweepGradient(
center: AlignmentDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
throwsAssertionError,
);
expect(
() {
return const SweepGradient(
center: AlignmentDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.rtl);
},
returnsNormally,
);
expect(
() {
return const SweepGradient(
center: AlignmentDirectional.topStart,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0), textDirection: TextDirection.ltr);
},
returnsNormally,
);
expect(
() {
return const SweepGradient(
center: Alignment.topLeft,
colors: const <Color>[ const Color(0xFFFFFFFF), const Color(0xFFFFFFFF) ]
).createShader(new Rect.fromLTWH(0.0, 0.0, 100.0, 100.0));
},
returnsNormally,
);
});
test('RadialGradient lerp test', () {
const RadialGradient testGradient1 = const RadialGradient(
center: Alignment.topLeft,
......@@ -263,6 +303,106 @@ void main() {
));
});
test('SweepGradient lerp test', () {
const SweepGradient testGradient1 = const SweepGradient(
center: Alignment.topLeft,
startAngle: 0.0,
endAngle: math.pi / 2,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
);
const SweepGradient testGradient2 = const SweepGradient(
center: Alignment.topRight,
startAngle: math.pi / 2,
endAngle: math.pi,
colors: const <Color>[
const Color(0x44444444),
const Color(0x88888888),
],
);
final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
expect(actual, const SweepGradient(
center: const Alignment(0.0, -1.0),
startAngle: math.pi / 4,
endAngle: math.pi * 3/4,
colors: const <Color>[
const Color(0x3B3B3B3B),
const Color(0x77777777),
],
));
});
test('SweepGradient lerp test with stops', () {
const SweepGradient testGradient1 = const SweepGradient(
center: Alignment.topLeft,
startAngle: 0.0,
endAngle: math.pi / 2,
colors: const <Color>[
const Color(0x33333333),
const Color(0x66666666),
],
stops: const <double>[
0.0,
0.5,
],
);
const SweepGradient testGradient2 = const SweepGradient(
center: Alignment.topRight,
startAngle: math.pi / 2,
endAngle: math.pi,
colors: const <Color>[
const Color(0x44444444),
const Color(0x88888888),
],
stops: const <double>[
0.5,
1.0,
],
);
final SweepGradient actual = SweepGradient.lerp(testGradient1, testGradient2, 0.5);
expect(actual, const SweepGradient(
center: const Alignment(0.0, -1.0),
startAngle: math.pi / 4,
endAngle: math.pi * 3/4,
colors: const <Color>[
const Color(0x3B3B3B3B),
const Color(0x77777777),
],
stops: const <double>[
0.25,
0.75,
],
));
});
test('SweepGradient scale test)', () {
const SweepGradient testGradient = const SweepGradient(
center: Alignment.topLeft,
startAngle: 0.0,
endAngle: math.pi / 2,
colors: const <Color>[
const Color(0xff333333),
const Color(0xff666666),
],
);
final SweepGradient actual = testGradient.scale(0.5);
expect(actual, const SweepGradient(
center: Alignment.topLeft,
startAngle: 0.0,
endAngle: math.pi / 2,
colors: const <Color>[
const Color(0x80333333),
const Color(0x80666666),
],
));
});
test('Gradient lerp test (with RadialGradient)', () {
const RadialGradient testGradient1 = const RadialGradient(
center: Alignment.topLeft,
......@@ -357,4 +497,4 @@ void main() {
expect(() { test2a.createShader(rect); }, throwsArgumentError);
expect(() { test2b.createShader(rect); }, throwsArgumentError);
});
}
\ No newline at end of file
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment