servo: Merge #14747 - Interpolate colors with premultiplied alpha (from hiikezoe:color-interpolation2); r=emilio
authorHiroyuki Ikezoe <hiikezoe@mozilla-japan.org>
Wed, 28 Dec 2016 02:06:38 -0800
changeset 340434 77f675794490dd0d93991975510b385fab061b93
parent 340433 46534af8c2a99c22b42221e4f133dbace63ba3e2
child 340435 83bb24b47d8a4ba9951601c2c0be17d0f229ed67
push id31307
push usergszorc@mozilla.com
push dateSat, 04 Feb 2017 00:59:06 +0000
treeherdermozilla-central@94079d43835f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersemilio
servo: Merge #14747 - Interpolate colors with premultiplied alpha (from hiikezoe:color-interpolation2); r=emilio We need to clamp each interpolated values because some cubic-bezier functions produce values that are out of range [0, 1]. r? @emilio <!-- Please describe your changes on the following line: --> --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors <!-- Either: --> - [X] There are tests for these changes <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: f9d971771c62f244c338be1cb34e0aa191d64206
servo/components/style/properties/helpers/animated_properties.mako.rs
servo/tests/unit/style/animated_properties.rs
servo/tests/unit/style/lib.rs
--- a/servo/components/style/properties/helpers/animated_properties.mako.rs
+++ b/servo/components/style/properties/helpers/animated_properties.mako.rs
@@ -385,22 +385,33 @@ impl Interpolate for BackgroundSize {
     }
 }
 
 
 /// https://drafts.csswg.org/css-transitions/#animtype-color
 impl Interpolate for RGBA {
     #[inline]
     fn interpolate(&self, other: &RGBA, progress: f64) -> Result<Self, ()> {
-        Ok(RGBA {
-            red: try!(self.red.interpolate(&other.red, progress)),
-            green: try!(self.green.interpolate(&other.green, progress)),
-            blue: try!(self.blue.interpolate(&other.blue, progress)),
-            alpha: try!(self.alpha.interpolate(&other.alpha, progress)),
-        })
+        fn clamp(val: f32) -> f32 {
+            val.max(0.).min(1.)
+        }
+
+        let alpha = clamp(try!(self.alpha.interpolate(&other.alpha, progress)));
+        if alpha == 0. {
+            Ok(RGBA { red: 0., green: 0., blue: 0., alpha: 0. })
+        } else {
+            Ok(RGBA { red: clamp(try!((self.red * self.alpha).interpolate(&(other.red * other.alpha), progress))
+                                 * 1. / alpha),
+                      green: clamp(try!((self.green * self.alpha).interpolate(&(other.green * other.alpha), progress))
+                                   * 1. / alpha),
+                      blue: clamp(try!((self.blue * self.alpha).interpolate(&(other.blue * other.alpha), progress))
+                                  * 1. / alpha),
+                      alpha: alpha
+            })
+        }
     }
 }
 
 /// https://drafts.csswg.org/css-transitions/#animtype-color
 impl Interpolate for CSSParserColor {
     #[inline]
     fn interpolate(&self, other: &Self, progress: f64) -> Result<Self, ()> {
         match (*self, *other) {
new file mode 100644
--- /dev/null
+++ b/servo/tests/unit/style/animated_properties.rs
@@ -0,0 +1,36 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+use cssparser::{Color, RGBA};
+use style::properties::animated_properties::Interpolate;
+
+#[test]
+fn test_rgba_color_interepolation() {
+    assert_eq!(Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }), 1.0).unwrap(),
+               Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }));
+
+    assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.6 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.4 }), 0.5).unwrap(),
+               Color::RGBA(RGBA { red: 0.6, green: 0.4, blue: 0.0, alpha: 0.5 }));
+
+    // Some cubic-bezier functions produce values that are out of range [0, 1].
+    // Unclamped cases.
+    assert_eq!(Color::RGBA(RGBA { red: 0.3, green: 0.0, blue: 0.0, alpha: 0.4 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.6 }), -0.5).unwrap(),
+               Color::RGBA(RGBA { red: 0.6, green: 0.0, blue: 0.0, alpha: 0.3 }));
+
+    assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.6 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 0.3, blue: 0.0, alpha: 0.4 }), 1.5).unwrap(),
+               Color::RGBA(RGBA { red: 0.0, green: 0.6, blue: 0.0, alpha: 0.3 }));
+
+    // Clamped cases.
+    assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.8 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.2 }), -0.5).unwrap(),
+               Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0 }));
+
+    assert_eq!(Color::RGBA(RGBA { red: 1.0, green: 0.0, blue: 0.0, alpha: 0.8 }).interpolate(
+              &Color::RGBA(RGBA { red: 0.0, green: 1.0, blue: 0.0, alpha: 0.2 }), 1.5).unwrap(),
+               Color::RGBA(RGBA { red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0 }));
+}
--- a/servo/tests/unit/style/lib.rs
+++ b/servo/tests/unit/style/lib.rs
@@ -17,16 +17,17 @@ extern crate rustc_serialize;
 extern crate selectors;
 #[macro_use] extern crate servo_atoms;
 extern crate servo_config;
 extern crate servo_url;
 extern crate style;
 extern crate style_traits;
 extern crate test;
 
+mod animated_properties;
 mod atomic_refcell;
 mod attr;
 mod cache;
 mod logical_geometry;
 mod media_queries;
 mod owning_handle;
 mod parsing;
 mod properties;