Define ColorSwatch.lerp()
function (#103701)
This commit is contained in:
parent
440e0e2c4e
commit
b1f10cebbe
@ -457,6 +457,44 @@ class ColorSwatch<T> extends Color {
|
||||
|
||||
@override
|
||||
String toString() => '${objectRuntimeType(this, 'ColorSwatch')}(primary value: ${super.toString()})';
|
||||
|
||||
/// Linearly interpolate between two [ColorSwatch]es.
|
||||
///
|
||||
/// It delegates to [Color.lerp] to interpolate the different colors of the
|
||||
/// swatch.
|
||||
///
|
||||
/// If either color is null, this function linearly interpolates from a
|
||||
/// transparent instance of the other color.
|
||||
///
|
||||
/// The `t` argument represents position on the timeline, with 0.0 meaning
|
||||
/// that the interpolation has not started, returning `a` (or something
|
||||
/// equivalent to `a`), 1.0 meaning that the interpolation has finished,
|
||||
/// returning `b` (or something equivalent to `b`), and values in between
|
||||
/// meaning that the interpolation is at the relevant point on the timeline
|
||||
/// between `a` and `b`. The interpolation can be extrapolated beyond 0.0 and
|
||||
/// 1.0, so negative values and values greater than 1.0 are valid (and can
|
||||
/// easily be generated by curves such as [Curves.elasticInOut]). Each channel
|
||||
/// will be clamped to the range 0 to 255.
|
||||
///
|
||||
/// Values for `t` are usually obtained from an [Animation<double>], such as
|
||||
/// an [AnimationController].
|
||||
static ColorSwatch<T>? lerp<T>(ColorSwatch<T>? a, ColorSwatch<T>? b, double t) {
|
||||
final Map<T, Color> swatch;
|
||||
if (b == null) {
|
||||
if (a == null) {
|
||||
return null;
|
||||
} else {
|
||||
swatch = a._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(color, null, t)!));
|
||||
}
|
||||
} else {
|
||||
if (a == null) {
|
||||
swatch = b._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(null, color, t)!));
|
||||
} else {
|
||||
swatch = a._swatch.map((T key, Color color) => MapEntry<T, Color>(key, Color.lerp(color, b[key], t)!));
|
||||
}
|
||||
}
|
||||
return ColorSwatch<T>(Color.lerp(a, b, t)!.value, swatch);
|
||||
}
|
||||
}
|
||||
|
||||
/// [DiagnosticsProperty] that has an [Color] as value.
|
||||
|
@ -426,6 +426,31 @@ void main() {
|
||||
expect(greens1.value, 0xFF027223);
|
||||
});
|
||||
|
||||
test('ColorSwatch.lerp', () {
|
||||
const ColorSwatch<int> swatchA = ColorSwatch<int>(0x00000000, <int, Color>{1: Color(0x00000000)});
|
||||
const ColorSwatch<int> swatchB = ColorSwatch<int>(0xFFFFFFFF, <int, Color>{1: Color(0xFFFFFFFF)});
|
||||
expect(
|
||||
ColorSwatch.lerp(swatchA, swatchB, 0.0),
|
||||
const ColorSwatch<int>(0x00000000, <int, Color>{1: Color(0x00000000)}),
|
||||
);
|
||||
expect(
|
||||
ColorSwatch.lerp(swatchA, swatchB, 0.5),
|
||||
const ColorSwatch<int>(0x7F7F7F7F, <int, Color>{1: Color(0x7F7F7F7F)}),
|
||||
);
|
||||
expect(
|
||||
ColorSwatch.lerp(swatchA, swatchB, 1.0),
|
||||
const ColorSwatch<int>(0xFFFFFFFF, <int, Color>{1: Color(0xFFFFFFFF)}),
|
||||
);
|
||||
expect(
|
||||
ColorSwatch.lerp(swatchA, swatchB, -0.1),
|
||||
const ColorSwatch<int>(0x00000000, <int, Color>{1: Color(0x00000000)}),
|
||||
);
|
||||
expect(
|
||||
ColorSwatch.lerp(swatchA, swatchB, 1.1),
|
||||
const ColorSwatch<int>(0xFFFFFFFF, <int, Color>{1: Color(0xFFFFFFFF)}),
|
||||
);
|
||||
});
|
||||
|
||||
test('ColorDiagnosticsProperty includes valueProperties in JSON', () {
|
||||
ColorProperty property = ColorProperty('foo', const Color.fromARGB(10, 20, 30, 40));
|
||||
final Map<String, Object> valueProperties = property.toJsonMap(const DiagnosticsSerializationDelegate())['valueProperties']! as Map<String, Object>;
|
||||
|
Loading…
x
Reference in New Issue
Block a user