From 736d19ecbf2cba24b686171893ae62470ee480dd Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sat, 24 Oct 2020 09:49:21 -0700 Subject: [PATCH] Fix _lerpInt precision bug (flutter/engine#22089) Fixes a precision bug was introduced in 5f363dc, which improved the precision of lerpDouble when the extrema differed significantly in magnitude. _lerpInt doesn't have this issue since the extrema are passed as 64-bit twos-complement values, therefore the difference will always be accurate so long as it doesn't overflow. This reverts the _lerpInt implementation to the original a + (b - a) * t, but adds a test to avoid a regression if anyone is tempted to make it consistent with the others. --- engine/src/flutter/lib/ui/lerp.dart | 2 +- engine/src/flutter/testing/dart/color_test.dart | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/lib/ui/lerp.dart b/engine/src/flutter/lib/ui/lerp.dart index 9c219dbb4c5..77e4a830b30 100644 --- a/engine/src/flutter/lib/ui/lerp.dart +++ b/engine/src/flutter/lib/ui/lerp.dart @@ -34,7 +34,7 @@ double _lerpDouble(double a, double b, double t) { /// /// Same as [lerpDouble] but specialized for non-null `int` type. double _lerpInt(int a, int b, double t) { - return a * (1.0 - t) + b * t; + return a + (b - a) * t; } /// Same as [num.clamp] but specialized for non-null [int]. diff --git a/engine/src/flutter/testing/dart/color_test.dart b/engine/src/flutter/testing/dart/color_test.dart index 445cae12c19..28649b09e89 100644 --- a/engine/src/flutter/testing/dart/color_test.dart +++ b/engine/src/flutter/testing/dart/color_test.dart @@ -77,6 +77,12 @@ void main() { Color.lerp(const Color(0x00000000), const Color(0xFFFFFFFF), 1.1), const Color(0xFFFFFFFF), ); + + // Prevent regression: https://github.com/flutter/flutter/issues/67423 + expect( + Color.lerp(const Color(0xFFFFFFFF), const Color(0xFFFFFFFF), 0.04), + const Color(0xFFFFFFFF), + ); }); test('Color.alphaBlend', () {