Skip to content

Commit

Permalink
stream_colors [nfc]: Make StreamColorSwatches.lerp static
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbobbe committed Jun 26, 2024
1 parent 60a1774 commit 0399af6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
12 changes: 6 additions & 6 deletions lib/widgets/stream_colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ abstract class StreamColorSwatches {

StreamColorSwatch _computeForBaseColor(int base);

/// Gives a [StreamColorSwatches], lerped to [other] at [t].
/// Gives a [StreamColorSwatches], lerped between [a] and [b] at [t].
///
/// If [this] and [other] are [identical], returns [this].
/// If [a] and [b] are [identical], returns [this].
///
/// Else returns an instance whose [forBaseColor] will call
/// [this.forBaseColor] and [other.forBaseColor]
/// [a.forBaseColor] and [b.forBaseColor]
/// and return [StreamColorSwatch.lerp]'s result on those.
/// This computation is cached on the instance
/// in order to save work building [t]'s animation frame when there are
/// multiple UI elements using the same [subscription.color].
StreamColorSwatches lerp(StreamColorSwatches other, double t) {
static StreamColorSwatches lerp(StreamColorSwatches a, StreamColorSwatches b, double t) {
// This short-circuit helps when [a] and [b]
// are both [StreamColorSwatches.light]
// or both [StreamColorSwatches.dark].
// Empirically, [lerp] is called even when the theme hasn't changed,
// so this is an important optimization.
if (identical(this, other)) return this;
if (identical(a, b)) return a;

return _StreamColorSwatchesLerped(this, other, t);
return _StreamColorSwatchesLerped(a, b, t);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class DesignVariables extends ThemeExtension<DesignVariables> {
borderBar: Color.lerp(borderBar, other.borderBar, t)!,
icon: Color.lerp(icon, other.icon, t)!,
title: Color.lerp(title, other.title, t)!,
streamColorSwatches: streamColorSwatches.lerp(other.streamColorSwatches, t),
streamColorSwatches: StreamColorSwatches.lerp(streamColorSwatches, other.streamColorSwatches, t),
);
}
}
Expand Down
13 changes: 6 additions & 7 deletions test/widgets/stream_colors_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ void main() {

group('lerp', () {
test('on identical instances', () {
check(
StreamColorSwatches.light.lerp(StreamColorSwatches.light, 0.5)
).identicalTo(StreamColorSwatches.light);
final light = StreamColorSwatches.light;
check(StreamColorSwatches.lerp(light, light, 0.5)).identicalTo(light);

check(
StreamColorSwatches.dark.lerp(StreamColorSwatches.dark, 0.5)
).identicalTo(StreamColorSwatches.dark);
final dark = StreamColorSwatches.dark;
check(StreamColorSwatches.lerp(dark, dark, 0.5)).identicalTo(dark);
});

test('from light to dark', () {
final instance = StreamColorSwatches.light.lerp(StreamColorSwatches.dark, 0.4);
final instance = StreamColorSwatches
.lerp(StreamColorSwatches.light, StreamColorSwatches.dark, 0.4);

const base1 = 0xff76ce90;
final swatch1 = instance.forBaseColor(base1);
Expand Down

0 comments on commit 0399af6

Please sign in to comment.